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

Commit

Permalink
feat: support koa app create (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed May 26, 2020
1 parent d2d60fb commit c3793eb
Show file tree
Hide file tree
Showing 16 changed files with 657 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class IndexHandler implements FunctionHandler {
ctx: FaaSContext;

async handler() {
console.log('this.ctx.req.body', this.ctx.req.body, this.ctx.query);
const name = this.ctx.req.body['name'];
console.log('this.ctx.req.body', this.ctx.request.body, this.ctx.query);
const name = this.ctx.request.body['name'];
const action = this.ctx.query['action'];
this.ctx.type = 'text/html; charset=utf-8';
this.ctx.set('x-schema', 'bbb');
Expand Down
20 changes: 20 additions & 0 deletions packages/faas-typings/typings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ interface ContextDelegatedResponse {
* Remove header `field`.
*/
remove(field: string): void;
/**
* Perform a 302 redirect to `url`.
*
* The string "back" is special-cased
* to provide Referrer support, when Referrer
* is not present `alt` or "/" is used.
*
* Examples:
*
* this.redirect('back');
* this.redirect('back', '/index.html');
* this.redirect('/login');
* this.redirect('http://google.com');
*/
redirect(url: string, alt?: string): void;
}

export interface FaaSHTTPResponse extends ContextDelegatedResponse {
Expand Down Expand Up @@ -322,6 +337,11 @@ export interface FaaSHTTPContext
* FaaS original context object.
*/
originContext: FaaSOriginContext;

/**
* FaaS original event object.
*/
originEvent: any;
}

export type FaaSOriginContext = FC.RequestContext | SCF.RequestContext;
6 changes: 3 additions & 3 deletions packages/faas-typings/typings/fc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ export namespace FC {
export interface APIGatewayEvent {
path: string;
httpMethod: string;
headers: object;
queryParameters: object;
pathParameters: object;
headers: { [key: string]: string };
queryParameters: { [key: string]: string };
pathParameters: { [key: string]: string };
body: string;
isBase64Encoded: 'true' | 'false';
}
Expand Down
8 changes: 2 additions & 6 deletions packages/faas-typings/typings/scf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,11 @@ export namespace SCF {
/**
* 记录在 API 网关中配置过的 Path 参数以及实际取值
*/
pathParameters: {
path: string;
};
pathParameters: { [key: string]: string };
/**
* 记录在 API 网关中配置过的 Query 参数以及实际取值
*/
queryStringParameters: {
foo: string;
};
queryStringParameters: { [key: string]: string };
/**
* 记录在 API 网关中配置过的 Header 参数以及实际取值
*/
Expand Down
15 changes: 12 additions & 3 deletions packages/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
FAAS_ARGS_KEY,
ServerlessLightRuntime,
} from '@midwayjs/runtime-engine';
import { Application } from '@midwayjs/serverless-http-parser';
import {
Application,
HTTPRequest,
HTTPResponse,
} from '@midwayjs/serverless-http-parser';
import * as util from 'util';

const isLocalEnv = () => {
Expand Down Expand Up @@ -49,13 +53,18 @@ export class FCRuntime extends ServerlessLightRuntime {
this.respond = this.app.callback();
}

let newReq = null;
const newRes = new HTTPResponse();

if (isHTTPMode) {
// http
// const rawBody = 'test';
// req.rawBody = rawBody;
req.body = await getRawBody(req); // TODO: body parser
newReq = req;
} else {
// api gateway
newReq = new HTTPRequest(req, context);
}

// if (ctx.method === 'GET') {
Expand All @@ -69,8 +78,8 @@ export class FCRuntime extends ServerlessLightRuntime {
// }

return this.respond.apply(this.respond, [
req,
context,
newReq,
newRes,
ctx => {
return this.invokeHandlerWrapper(ctx, async () => {
if (!handler) {
Expand Down
2 changes: 2 additions & 0 deletions packages/serverless-http-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@midwayjs/faas-typings": "^0.2.99",
"accepts": "^1.3.7",
"cache-content-type": "^1.0.1",
"encodeurl": "^1.0.2",
"escape-html": "^1.0.3",
"koa-compose": "^4.1.0",
"querystring": "^0.2.0",
"statuses": "^1.5.0",
Expand Down
31 changes: 16 additions & 15 deletions packages/serverless-http-parser/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from './context';
import { Request } from './request';
import { Response } from './response';
import { context } from './context';
import { request } from './request';
import { response } from './response';
import * as compose from 'koa-compose';

export class Application {
Expand All @@ -16,21 +16,22 @@ export class Application {

constructor(options?) {
options = options || {};
this.context = options?.context || Context;
this.request = options?.request || Request;
this.response = options?.response || Response;
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
}

/**
* Initialize a new context.
*/
createContext(event, faasContext) {
const context = new this.context(event, faasContext);
const request = new this.request(event);
const response = new this.response();
createContext(req, res) {
const context = Object.create(this.context);
const request = (context.request = Object.create(this.request));
const response = (context.response = Object.create(this.response));

context.app = request.app = response.app = this;
context.req = request.req = response.req = request;
context.res = request.res = response.res = response;
context.req = request.req = response.req = req;
context.res = request.res = response.res = res;
request.ctx = response.ctx = context;
request.response = response;
response.request = request;
Expand All @@ -49,7 +50,7 @@ export class Application {
* @api public
*/

use(fn: () => {}) {
use(fn: (...args) => Promise<void>) {
if (typeof fn !== 'function')
throw new TypeError('middleware must be a function!');
this.middleware.push(fn);
Expand All @@ -66,10 +67,10 @@ export class Application {

callback() {
const fn = compose(this.middleware);
return (event, context, respond) => {
return (req, res, respond) => {
// if (!this.listenerCount('error')) this.on('error', this.onerror);
const onerror = err => ctx.onerror(err);
const ctx = this.createContext(event, context);
const ctx = this.createContext(req, res);
return fn(ctx)
.then(() => {
return respond(ctx);
Expand Down
Loading

0 comments on commit c3793eb

Please sign in to comment.