Skip to content

Commit

Permalink
feat: use @ServerlessTrigger replace functions in f.yml (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Apr 1, 2021
1 parent 3d2388f commit a85af14
Show file tree
Hide file tree
Showing 23 changed files with 839 additions and 148 deletions.
8 changes: 2 additions & 6 deletions packages-serverless/serverless-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
"@midwayjs/decorator": "^2.9.2",
"@midwayjs/faas": "^2.9.3",
"@midwayjs/faas-middleware-upload": "^0.0.5",
"@midwayjs/mock": "^2.9.3",
"@midwayjs/serverless-fc-starter": "^2.9.2",
"@midwayjs/serverless-fc-trigger": "^2.9.2",
"@midwayjs/serverless-scf-starter": "^2.8.13",
"@midwayjs/serverless-scf-trigger": "^2.9.2",
"supertest": "^4.0.2",
"typescript": "^4.1.0"
"@midwayjs/serverless-scf-trigger": "^2.9.2"
},
"engines": {
"node": ">= 10"
Expand All @@ -39,9 +38,6 @@
"type": "git",
"url": "git@github.com:midwayjs/midway.git"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"gitHead": "58706de896b8e3b50605bb29f40ff29abe43924d"
}
6 changes: 3 additions & 3 deletions packages-serverless/serverless-app/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export const output404 = (path, functionsMap) => {
// 分析装饰器上面的函数信息
export const analysisDecorator = async (cwd: string) => {
const midwayCoreMod = findNpmModule(cwd, '@midwayjs/core');
const { WebRouterCollector } = require(midwayCoreMod);
const collector = new WebRouterCollector();
const result = await collector.getFlattenRouterTable();
const { ServerlessTriggerCollector } = require(midwayCoreMod);
const collector = new ServerlessTriggerCollector();
const result = await collector.getFunctionList();
const allFunc = {};
if (Array.isArray(result)) {
result.forEach(func => {
Expand Down
24 changes: 17 additions & 7 deletions packages-serverless/serverless-app/test/faas-v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
import { join } from 'path';
import * as assert from 'assert';
import { createApp, close } from '../../../packages/mock';
import { createApp, close, createHttpRequest } from '@midwayjs/mock';
import { Framework, Application } from '../src';
import { EventService } from './fixtures/faas-v2/src/event';
const request = require('supertest');
const cwd = join(__dirname, 'fixtures/faas-v2');
describe('test/faas-v2.test.ts', () => {

Expand All @@ -21,7 +20,7 @@ describe('test/faas-v2.test.ts', () => {
await close(app);
});
it('http get', async done => {
request(app)
createHttpRequest(app)
.get('/hello?name=test&age=123')
.expect(200)
.then(response => {
Expand All @@ -35,7 +34,7 @@ describe('test/faas-v2.test.ts', () => {
.catch(err => done(err));
});
it('http get controller', async done => {
request(app)
createHttpRequest(app)
.get('/user')
.expect(200)
.then(response => {
Expand All @@ -45,7 +44,7 @@ describe('test/faas-v2.test.ts', () => {
.catch(err => done(err));
});
it('http get controller params', async done => {
request(app)
createHttpRequest(app)
.get('/user/midway')
.expect(200)
.then(response => {
Expand All @@ -55,7 +54,7 @@ describe('test/faas-v2.test.ts', () => {
.catch(err => done(err));
});
it('http post', async done => {
await request(app)
await createHttpRequest(app)
.post('/hello')
.type('form')
.send({ id: '1' })
Expand All @@ -71,9 +70,10 @@ describe('test/faas-v2.test.ts', () => {
})
.catch(err => done(err));
});

it('http post upload', async done => {
const imagePath = join(cwd, '1.jpg');
await request(app)
await createHttpRequest(app)
.post('/upload')
.field('name', 'form')
.attach('file', imagePath)
Expand All @@ -86,9 +86,19 @@ describe('test/faas-v2.test.ts', () => {
})
.catch(err => done(err));
});

it('oth event trigger', async () => {
const instance = await app.getServerlessInstance<EventService>(EventService);
const result = instance.handler({ name: 123 });
assert(result.name === 123);
});

it('should use @ServerlessTrigger with http event', async () => {
const result = await createHttpRequest(app)
.get('/func/http/get')
.query({
name: 'zhangting'
});
expect(result.text).toEqual('user:zhangting');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class ControllerTestService {
@Inject()
ctx;


@Get('/')
async handler() {
return 'user'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Get, Inject, Provide, Query, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/decorator';

@Provide()
export class FuncService {

@Inject()
ctx;

@ServerlessTrigger(ServerlessTriggerType.HTTP, {
method: 'get',
path: '/func/http/get'
})
async handler(@Query() name) {
return 'user:' + name;
}

@Get('/:test')
async test() {
return this.ctx.params.test;
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export * from './util/pathFileUtil';
export * from './features';
export * from './util/webRouterParam';
export * from './util/webRouterCollector';
export * from './util/triggerCollector';
export * from './util/emptyFramework';
export { plainToClass, classToPlain } from 'class-transformer';
export * from './logger';
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, resolve, sep } from 'path';
import { dirname, resolve, sep, posix } from 'path';
import { readFileSync } from 'fs';

export const isDevelopmentEnvironment = env => {
Expand Down Expand Up @@ -71,3 +71,15 @@ export function parsePrefix(provideId: string) {
export function getUserHome() {
return process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];
}

export function joinURLPath(...strArray) {
if (strArray.length === 0) {
return '';
}
let p = posix.join(...strArray);
p = p.replace(/\/+$/, '');
if (!/^\//.test(p)) {
p = '/' + p;
}
return p;
}
20 changes: 20 additions & 0 deletions packages/core/src/util/triggerCollector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { WebRouterCollector } from './webRouterCollector';

export class ServerlessTriggerCollector extends WebRouterCollector {
protected async analyze() {
this.options.includeFunctionRouter = true;
return super.analyze();
}

protected collectRoute(module) {
super.collectRoute(module, true);
}

protected collectFunctionRoute(module) {
super.collectFunctionRoute(module, true);
}

async getFunctionList(): Promise<any[]> {
return this.getFlattenRouterTable();
}
}
Loading

0 comments on commit a85af14

Please sign in to comment.