Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: Support @func class method (#100)
Browse files Browse the repository at this point in the history
* feat: Support @func class method

* feat: update test cases

* test: add new case
  • Loading branch information
Lellansin committed Mar 31, 2020
1 parent f3cc94f commit cce042a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/faas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/core": "^2.0.0",
"@midwayjs/decorator": "^2.0.0",
"@midwayjs/decorator": "^2.0.8",
"@midwayjs/simple-lock": "^0.2.59",
"debug": "^4.1.1",
"deep-eql": "^4.0.0",
Expand Down
46 changes: 20 additions & 26 deletions packages/faas/src/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { existsSync } from 'fs';
import {
ContainerLoader,
getClassMetadata,
getPropertyDataFromClass,
IMiddleware,
listModule,
listPreloadModule,
Expand All @@ -13,7 +12,7 @@ import {
MidwayRequestContainer,
REQUEST_OBJ_CTX_KEY,
} from '@midwayjs/core';
import { FUNC_KEY, HANDLER_KEY } from '@midwayjs/decorator';
import { FUNC_KEY } from '@midwayjs/decorator';
import SimpleLock from '@midwayjs/simple-lock';
import * as compose from 'koa-compose';

Expand Down Expand Up @@ -110,6 +109,7 @@ export class FaaSStarter implements IFaaSStarter {
mod: any;
middleware: Array<IMiddleware<FaaSContext>>;
method: string;
descriptor: any;
} = this.funMappingStore.get(handlerMapping);

return async (...args) => {
Expand Down Expand Up @@ -212,37 +212,27 @@ export class FaaSStarter implements IFaaSStarter {
// store all function entry
const funModules = listModule(FUNC_KEY);
for (const funModule of funModules) {
const funOptions: {
const funOptions: Array<{
funHandler;
key;
descriptor;
middleware: string[];
} = getClassMetadata(FUNC_KEY, funModule);
// @fun(key), only if key is set
if (funOptions.funHandler) {
this.funMappingStore.set(funOptions.funHandler, {
middleware: funOptions.middleware,
mod: funModule,
});
}
const methods = Object.getOwnPropertyNames(
(funModule as () => void).prototype
);
for (const method of methods) {
const meta = getPropertyDataFromClass(HANDLER_KEY, funModule, method);
if (!meta) {
continue;
}
}> = getClassMetadata(FUNC_KEY, funModule);
funOptions.map((opts) => {
// { method: 'handler', data: 'index.handler' }
const handlerName = meta.data
? // @handler(key), if key is set
meta.data
const handlerName = opts.funHandler
? // @Func(key), if key is set
// or @Func({ handler })
opts.funHandler
: // else use ClassName.mehtod as handler key
(funModule as () => {}).name + '.' + method;
covertId(funModule.name, opts.key);
this.funMappingStore.set(handlerName, {
middleware: funOptions.middleware,
middleware: opts.middleware || [],
mod: funModule,
method,
method: opts.key,
descriptor: opts.descriptor
});
}
});
}

const modules = listPreloadModule();
Expand Down Expand Up @@ -290,3 +280,7 @@ export class FaaSStarter implements IFaaSStarter {
return mwArr;
}
}

function covertId(cls, method) {
return cls.replace(/^[A-Z]/, (c) => c.toLowerCase()) + '.' + method;
}
7 changes: 3 additions & 4 deletions packages/faas/test/fixtures/base-app-handler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Inject, Provide, Func, Handler } from '@midwayjs/decorator';
import { Inject, Provide, Func } from '@midwayjs/decorator';
import { FunctionHandler } from '../../../../src';

@Provide()
@Func('')
export class IndexService implements FunctionHandler {
@Inject()
ctx; // context

@Handler('index.entry')
@Func('index.entry')
handler(event) {
return event.text + this.ctx.text;
}

@Handler('index.list')
@Func('index.list', { event: 'HTTP', path: '/list' })
getList(event) {
return event.text + this.ctx.text;
}
Expand Down
9 changes: 7 additions & 2 deletions packages/faas/test/fixtures/base-app-handler2/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Provide, Func, Handler } from '@midwayjs/decorator';
import { Inject, Provide, Func } from '@midwayjs/decorator';
import { FunctionHandler } from '../../../../src';

@Provide()
Expand All @@ -12,8 +12,13 @@ export class IndexService implements FunctionHandler {
return 'default' + event.text + this.ctx.text;
}

@Handler('index.list')
@Func('index.list', {})
getList(event) {
return event.text + this.ctx.text;
}

@Func({ path: '/' })
get(event) {
return 'hello';
}
}
1 change: 1 addition & 0 deletions packages/faas/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('test/index.test.ts', () => {
{ text: 'ab' }
)) === 'abhello'
);
assert((await starter.handleInvokeWrapper('indexService.get')({}, {})) === 'hello');
});

it('invoke handler by default name', async () => {
Expand Down

0 comments on commit cce042a

Please sign in to comment.