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

Commit

Permalink
feat: support local single function to set multiple paths (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed May 6, 2020
1 parent 53f40ef commit c21044d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/gateway-common-http/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export async function parseInvokeOptionsByOriginUrl(
}
}
}
if (currentUrl === '/favicon.ico') {
return {};
}
const invokeOptions: Partial<InvokeOptions> = {};
invokeOptions.functionDir = options.functionDir;
invokeOptions.sourceDir = options.sourceDir;
Expand All @@ -46,16 +43,19 @@ export async function parseInvokeOptionsByOriginUrl(
let urlMatchList = [];
Object.keys(functions).forEach((functionName) => {
const functionItem = functions[functionName] || {};
const event = (functionItem.events || []).find((eventItem: any) => {
const httpEvents = (functionItem.events || []).filter((eventItem: any) => {
return eventItem.http || eventItem.apigw;
});
const eventItem = event?.http || event?.apigw;
if (eventItem) {
urlMatchList.push({
functionName,
originRouter: eventItem.path || '/*',
router: eventItem.path?.replace(/\/\*$/, '/**') || '/**',
});

for( const event of httpEvents) {
const eventItem = event?.http || event?.apigw;
if (eventItem) {
urlMatchList.push({
functionName,
originRouter: eventItem.path || '/*',
router: eventItem.path?.replace(/\/\*$/, '/**') || '/**',
});
}
}
});
// 1. 绝对路径规则优先级最高如 /ab/cb/e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ functions:
events:
- http:
path: /server/user/info
- http:
path: /server/user/info2
test2:
handler: test2.handler
events:
Expand Down
32 changes: 32 additions & 0 deletions packages/gateway-common-http/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ describe('/test/index.test.ts', () => {
.expect(200, done);
});

it('test second url /server/user/info2', (done) => {
createExpressSuit({
functionDir: join(__dirname, './fixtures/ice-demo-repo'),
sourceDir: 'src/apis',
})
.post('/server/user/info2')
.query({
action: 'doTest',
})
.send({ name: 'zhangting' })
.expect('Content-type', 'text/html; charset=utf-8')
.expect(/zhangting,hello http world,doTest/)
.expect('x-schema', 'bbb')
.expect(200, done);
});

it('test /* router', (done) => {
createExpressSuit({
functionDir: join(__dirname, './fixtures/ice-demo-repo'),
Expand Down Expand Up @@ -139,6 +155,22 @@ describe('/test/index.test.ts', () => {
.expect(200, done);
});

it('should invoke second url by http api and koa', (done) => {
createKoaSuit({
functionDir: join(__dirname, './fixtures/ice-demo-repo'),
sourceDir: 'src/apis',
})
.post('/server/user/info2')
.query({
action: 'doTest',
})
.send({ name: 'zhangting' })
.expect('Content-type', 'text/html; charset=utf-8')
.expect(/zhangting,hello http world,doTest/)
.expect('x-schema', 'bbb')
.expect(200, done);
});

describe('test koa ignore pattern', () => {
it('should test ignore pattern', (done) => {
createKoaSuit({
Expand Down

0 comments on commit c21044d

Please sign in to comment.