Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: complete 2.x beta #630

Merged
merged 50 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3af2cd1
feat: new 2.x
czy88840616 Aug 31, 2020
4645dd0
fix: fix compatibility
czy88840616 Aug 31, 2020
eed4e41
test: pass all case and remove midway-web
czy88840616 Sep 1, 2020
eb6e973
chore: update test
czy88840616 Sep 1, 2020
6d52116
chore: add test node_modules
czy88840616 Sep 2, 2020
2ddfc79
chore: add test node_modules
czy88840616 Sep 2, 2020
53f4be0
test: fix core test
czy88840616 Sep 2, 2020
8da2d01
test: add public dir
czy88840616 Sep 2, 2020
4094e5c
chore: add decorator interface
czy88840616 Sep 3, 2020
0a3a73e
chore: remove eslint
czy88840616 Sep 3, 2020
3f466d8
feat: add koa/express
czy88840616 Sep 3, 2020
21c70f0
refactor: change test framework to jest
czy88840616 Sep 5, 2020
97b6538
refactor: remove definition and add web for koa
czy88840616 Sep 5, 2020
afe304a
test: reactor new faas framework and test passed
czy88840616 Sep 5, 2020
623eba2
refactor: export Framework
czy88840616 Sep 5, 2020
185a0ab
chore: update jest file
czy88840616 Sep 5, 2020
a9cae29
fix: add ignore files
czy88840616 Sep 5, 2020
855c24d
refactor: add express framework
czy88840616 Sep 6, 2020
25a8b73
fix: fix middleware typings
czy88840616 Sep 7, 2020
5bfa5b5
test: add case for @midwayjs/express
czy88840616 Sep 7, 2020
171ae3f
refactor: remove register config
czy88840616 Sep 7, 2020
7d44743
feat: support egg-scripts
czy88840616 Sep 7, 2020
abbaafc
type: update typing with generateController
czy88840616 Sep 8, 2020
c16cc59
chore: fix conflicts
czy88840616 Sep 8, 2020
81e32f5
fix: support cluster
czy88840616 Sep 8, 2020
696d419
feat: support egg-cluster start
czy88840616 Sep 8, 2020
b35305e
chore: update
czy88840616 Sep 9, 2020
8547e57
feat: update midway-bin
czy88840616 Sep 9, 2020
5196196
refactor: remove egg-bin
czy88840616 Sep 9, 2020
472db8f
revert midway-bin
czy88840616 Sep 11, 2020
efeac17
revert midway-bin
czy88840616 Sep 11, 2020
b9a8f4d
feat: add socket.io
czy88840616 Sep 12, 2020
05eb246
test: add case for socket
czy88840616 Sep 12, 2020
f0c8f3f
test: add case for socket.io
czy88840616 Sep 12, 2020
ec0ec6e
feat: add createHttpRequest method
czy88840616 Sep 13, 2020
b240982
chore: remove empty
czy88840616 Sep 13, 2020
4e7c353
fix: fix dep
czy88840616 Sep 13, 2020
5b5354c
chore: ignore err test
czy88840616 Sep 13, 2020
e4c8b60
chore: ignore err test
czy88840616 Sep 13, 2020
f233844
fix: remove setup file
czy88840616 Sep 13, 2020
0c79b2e
fix: remove old test
czy88840616 Sep 13, 2020
76f6ad1
test: add timeout for egg
czy88840616 Sep 13, 2020
6529c1c
test: add timeout for egg
czy88840616 Sep 13, 2020
d97347a
fix: fix arg
czy88840616 Sep 13, 2020
b0bf150
chore: skip
czy88840616 Sep 13, 2020
c459b45
chore: upgrade test
czy88840616 Sep 13, 2020
47526c9
chore: set global timeout
czy88840616 Sep 13, 2020
c42c26c
chore: set global timeout
czy88840616 Sep 13, 2020
ea87e84
chore: set global timeout
czy88840616 Sep 13, 2020
0c3c16f
doc: home (#636)
czy88840616 Sep 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add case for @midwayjs/express
  • Loading branch information
czy88840616 committed Sep 7, 2020
commit 5bfa5b5e4a365e2d392611baddf81f2bd3d83fc7
16 changes: 8 additions & 8 deletions packages/core/src/context/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Container extends BaseApplicationContext implements IContainer {
// inject properties
const metaDatas = recursiveGetMetadata(TAGGED_PROP, target);
for (const metaData of metaDatas) {
this.debugLogger(`inject properties => [${Object.keys(metaData)}]`);
this.debugLogger(` inject properties => [${Object.keys(metaData)}]`);
for (const metaKey in metaData) {
for (const propertyMeta of metaData[ metaKey ]) {
const refManaged = new ManagedReference();
Expand Down Expand Up @@ -109,35 +109,35 @@ export class Container extends BaseApplicationContext implements IContainer {
private convertOptionsToDefinition(options: ObjectDefinitionOptions, definition: ObjectDefinition): ObjectDefinition {
if (options) {
if (options.isAsync) {
this.debugLogger(` register isAsync = true`);
this.debugLogger(` register isAsync = true`);
definition.asynchronous = true;
}

if (options.initMethod) {
this.debugLogger(` register initMethod = ${options.initMethod}`);
this.debugLogger(` register initMethod = ${options.initMethod}`);
definition.initMethod = options.initMethod;
}

if (options.destroyMethod) {
this.debugLogger(` register destroyMethod = ${options.destroyMethod}`);
this.debugLogger(` register destroyMethod = ${options.destroyMethod}`);
definition.destroyMethod = options.destroyMethod;
}

if (options.scope) {
this.debugLogger(` register scope = ${options.scope}`);
this.debugLogger(` register scope = ${options.scope}`);
definition.scope = options.scope;
}

if (options.constructorArgs) {
this.debugLogger(` register constructorArgs = ${options.constructorArgs}`);
this.debugLogger(` register constructorArgs = ${options.constructorArgs}`);
definition.constructorArgs = options.constructorArgs;
}

if (options.isAutowire === false) {
this.debugLogger(` register autowire = ${options.isAutowire}`);
this.debugLogger(` register autowire = ${options.isAutowire}`);
definition.autowire = false;
} else if (options.isAutowire === true) {
this.debugLogger(` register autowire = ${options.isAutowire}`);
this.debugLogger(` register autowire = ${options.isAutowire}`);
definition.autowire = true;
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/context/requestContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export class MidwayRequestContainer extends MidwayContainer {
this.applicationContext = applicationContext;
// register ctx
this.registerObject(REQUEST_CTX_KEY, ctx);
// register contextLogger
this.registerObject('logger', ctx.logger);

if (ctx.logger) {
// register contextLogger
this.registerObject('logger', ctx.logger);
}

const resolverHandler = this.applicationContext.resolverHandler;
this.beforeEachCreated(resolverHandler.beforeEachCreated.bind(resolverHandler));
Expand Down
39 changes: 15 additions & 24 deletions packages/web-express/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
IMidwayExpressRequest
} from './interface';
import type { IRouter, IRouterHandler, RequestHandler } from 'express';
import * as express from "express";
import * as express from 'express';

export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigurationOptions> {
protected app: IMidwayExpressApplication;
Expand Down Expand Up @@ -117,7 +117,7 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
const args = [req, res, next];
if (Array.isArray(routeArgsInfo)) {
await Promise.all(
routeArgsInfo.map(async ({index, type, propertyData}) => {
routeArgsInfo.map(async ({ index, type, propertyData }) => {
args[index] = await extractExpressLikeValue(type, propertyData)(req, res, next);
})
);
Expand All @@ -130,16 +130,13 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
for (const routerRes of routerResponseData) {
switch (routerRes.type) {
case WEB_RESPONSE_HTTP_CODE:
res.status = routerRes.code;
res.status(routerRes.code);
break;
case WEB_RESPONSE_HEADER:
routerRes.setHeaders.forEach((key, value) => {
res.set(key, value);
});
res.set(routerRes.setHeaders);
break;
case WEB_RESPONSE_REDIRECT:
res.status = routerRes.code;
res.redirect(routerRes.url);
res.redirect(routerRes.code, routerRes.url);
return;
}
}
Expand Down Expand Up @@ -218,12 +215,13 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
for (const webRouter of webRouterInfo) {
// get middleware
const middlewares2 = webRouter.middleware as unknown as MiddlewareParamArray;
const methodMiddlewares: MiddlewareParamArray = [];
// const methodMiddlewares: MiddlewareParamArray = [];

await this.handlerWebMiddleware(
middlewares2,
(middlewareImpl: Middleware) => {
methodMiddlewares.push(middlewareImpl);
// methodMiddlewares.push(middlewareImpl);
newRouter.use(middlewareImpl);
}
);

Expand All @@ -242,19 +240,12 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
webRouter.method
) || [];

const routerArgs = [
webRouter.routerName,
webRouter.path,
...methodMiddlewares,
this.generateController(
`${controllerId}.${webRouter.method}`,
routeArgsInfo,
routerResponseData
),
];

// apply controller from request context
newRouter[webRouter.requestMethod].apply(newRouter, routerArgs);
newRouter[webRouter.requestMethod].call(newRouter, webRouter.path, this.generateController(
`${controllerId}.${webRouter.method}`,
routeArgsInfo,
routerResponseData
));
}
}

Expand All @@ -274,10 +265,10 @@ export class MidwayExpressFramework extends BaseFramework<IMidwayExpressConfigur
protected createRouter(controllerOption: ControllerOption): IRouter {
const {
prefix,
routerOptions: {sensitive},
routerOptions: { sensitive },
} = controllerOption;
if (prefix) {
return express.Router({caseSensitive: sensitive});
return express.Router({ caseSensitive: sensitive });
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web-express/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { MidwayExpressFramework } from './framework';
export * from './interface';
export { MidwayExpressFramework as Framework } from './framework';
1 change: 0 additions & 1 deletion packages/web-express/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IMidwayApplication, IMidwayContext } from '@midwayjs/core';
// import { KoaMiddlewareParamArray } from '@midwayjs/decorator';
import { Application, Request, Response, RequestHandler } from 'express';

export type IMidwayExpressRequest = IMidwayContext & Request;
Expand Down
3 changes: 3 additions & 0 deletions packages/web-express/test/fixtures/base-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.hello = {
b: 4,
c: 3,
};
20 changes: 20 additions & 0 deletions packages/web-express/test/fixtures/base-app/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Configuration, App } from '@midwayjs/decorator';
import { IMidwayExpressApplication, IMidwayExpressRequest } from '../../../../src';

@Configuration({
importConfigs: [
'./config'
]
})
export class ContainerConfiguration {

@App()
app: IMidwayExpressApplication;

async onReady() {
this.app.use((req: IMidwayExpressRequest, res, next) => {
console.log('invoke middleware in ready');
next();
});
}
}
40 changes: 40 additions & 0 deletions packages/web-express/test/fixtures/base-app/src/controller/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Controller,
Post,
Get,
Provide,
Inject,
Query,
Body,
HttpCode,
Redirect,
} from '@midwayjs/decorator';
import { UserService } from '../service/user';
import { IMidwayExpressRequest } from '../../../../../src';

@Provide()
@Controller('/')
export class APIController {
@Inject()
req: IMidwayExpressRequest;

@Inject()
userService: UserService;

@Post()
async postData(@Body('bbbbb') bbbb) {
return 'data';
}

@Get('/', { middleware: [] })
@HttpCode(201)
async home(@Query('name') name: string) {
return 'hello world,' + name;
}

@Get('/login')
@Redirect('/')
async redirect() {
}

}
10 changes: 10 additions & 0 deletions packages/web-express/test/fixtures/base-app/src/service/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Provide } from '@midwayjs/decorator';

@Provide()
export class UserService {
async hello(name) {
return {
name,
};
}
}
29 changes: 29 additions & 0 deletions packages/web-express/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as request from 'supertest';
import { closeApp, creatApp } from './utils';
import { IMidwayExpressApplication } from '../src';

describe('/test/feature.test.ts', () => {

describe('test new features', () => {
let app: IMidwayExpressApplication;
beforeAll(async () => {
app = await creatApp('base-app');
});

afterAll(async () => {
await closeApp(app);
});

it('test get method with return value', async () => {
const result = await request(app).get('/').query({ name: 'harry' });
expect(result.status).toBe(201);
expect(result.text).toBe('hello world,harry');
});

it('test get method with redirect', async () => {
const result = await request(app).get('/login');
expect(result.status).toBe(302);
});
});

});
11 changes: 11 additions & 0 deletions packages/web-express/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IMidwayExpressConfigurationOptions, Framework, IMidwayExpressApplication } from '../src';
import { join } from 'path';
import { createApp, close } from '@midwayjs/mock';

export async function creatApp(name: string, options: IMidwayExpressConfigurationOptions = {}): Promise<IMidwayExpressApplication> {
return createApp(join(__dirname, 'fixtures', name), options, Framework);
}

export async function closeApp(app) {
return close(app);
}