Skip to content

Commit 25fd17b

Browse files
authored
Merge pull request #1 from frouriojs/develop
chore(release): 0.17.0
2 parents d530a76 + 3db3482 commit 25fd17b

File tree

13 files changed

+657
-550
lines changed

13 files changed

+657
-550
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## [0.17.0](https://github.com/frouriojs/frourio-express/compare/v0.16.0...v0.17.0) (2020-10-09)
6+
7+
8+
### Features
9+
10+
* optimize methodToHandler ([77b89ca](https://github.com/frouriojs/frourio-express/commit/77b89cade89d4ac77eb94ca42e07bde7e49f82fe))
11+
12+
13+
### Documentation
14+
15+
* add benchmarks ([d530a76](https://github.com/frouriojs/frourio-express/commit/d530a76a6cec76afb3327f9d7d90bcfb025eb6ad))
16+
517
## [0.16.0](https://github.com/frouriojs/frourio-express/compare/v0.15.0...v0.16.0) (2020-10-07)
618

719

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,6 @@ test('dependency injection into controller', async () => {
931931
const limit = 3
932932
const message = 'test message'
933933
const res = await injectedController.get({
934-
path: '',
935-
method: 'GET',
936934
query: { limit, message },
937935
body: undefined,
938936
headers: undefined

__test__/index.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ test('controller dependency injection', async () => {
130130

131131
await expect(
132132
injectedController.get({
133-
path: '',
134-
method: 'GET',
135133
query: { id, requiredNum: 1, requiredNumArr: [0], disable: 'true' },
136134
body: undefined,
137135
headers: undefined

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frourio-express",
3-
"version": "0.16.0",
3+
"version": "0.17.0",
44
"description": "Perfectly type-checkable REST framework for TypeScript",
55
"author": "Solufa <[email protected]>",
66
"license": "MIT",
@@ -91,22 +91,22 @@
9191
"@types/express": "^4.17.7",
9292
"@types/jest": "^26.0.14",
9393
"@types/multer": "^1.4.4",
94-
"@typescript-eslint/eslint-plugin": "^4.3.0",
95-
"@typescript-eslint/parser": "^4.3.0",
94+
"@typescript-eslint/eslint-plugin": "^4.4.0",
95+
"@typescript-eslint/parser": "^4.4.0",
9696
"axios": "^0.20.0",
9797
"class-validator": "^0.12.2",
9898
"eslint": "^7.10.0",
9999
"eslint-config-prettier": "^6.12.0",
100100
"eslint-config-standard": "^14.1.1",
101101
"eslint-plugin-import": "^2.22.1",
102-
"eslint-plugin-jest": "^24.0.2",
102+
"eslint-plugin-jest": "^24.1.0",
103103
"eslint-plugin-node": "^11.1.0",
104104
"eslint-plugin-prettier": "^3.1.4",
105105
"eslint-plugin-promise": "^4.2.1",
106106
"eslint-plugin-standard": "^4.0.1",
107107
"express": "^4.17.1",
108108
"form-data": "^3.0.0",
109-
"jest": "^26.4.2",
109+
"jest": "^26.5.2",
110110
"multer": "^1.4.2",
111111
"prettier": "^2.1.2",
112112
"standard-version": "^9.0.0",

servers/all/$server.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import path from 'path'
3-
import { LowerHttpMethod, AspidaMethods, HttpMethod, HttpStatusOk, AspidaMethodParams } from 'aspida'
3+
import { LowerHttpMethod, AspidaMethods, HttpStatusOk, AspidaMethodParams } from 'aspida'
44
import express, { Express, RequestHandler, Request } from 'express'
55
import multer, { Options } from 'multer'
66
import { validateOrReject } from 'class-validator'
@@ -64,8 +64,6 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
6464
: T['reqBody']
6565

6666
type RequestParams<T extends AspidaMethodParams> = {
67-
path: string
68-
method: HttpMethod
6967
query: T['query']
7068
body: BlobToFile<T>
7169
headers: T['reqHeaders']
@@ -132,32 +130,6 @@ const createTypedParamsHandler = (numberTypeParams: string[]): RequestHandler =>
132130
const createValidateHandler = (validators: (req: Request) => (Promise<void> | null)[]): RequestHandler =>
133131
(req, res, next) => Promise.all(validators(req)).then(() => next()).catch(() => res.sendStatus(400))
134132

135-
const methodToHandler = (
136-
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
137-
): RequestHandler => async (req, res, next) => {
138-
try {
139-
const result = methodCallback({
140-
query: req.query,
141-
path: req.path,
142-
method: req.method as HttpMethod,
143-
body: req.body,
144-
headers: req.headers,
145-
params: req.params,
146-
user: (req as any).user
147-
})
148-
149-
const { status, body, headers } = result instanceof Promise ? await result : result
150-
151-
for (const key in headers) {
152-
res.setHeader(key, headers[key])
153-
}
154-
155-
res.status(status).send(body)
156-
} catch (e) {
157-
next(e)
158-
}
159-
}
160-
161133
const formatMulterData = (arrayTypeKeys: [string, boolean][]): RequestHandler => ({ body, files }, _res, next) => {
162134
for (const [key] of arrayTypeKeys) {
163135
if (body[key] === undefined) body[key] = []
@@ -181,6 +153,38 @@ const formatMulterData = (arrayTypeKeys: [string, boolean][]): RequestHandler =>
181153
next()
182154
}
183155

156+
const methodToHandler = (
157+
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
158+
): RequestHandler => (req, res, next) => {
159+
try {
160+
const data = methodCallback(req as any) as any
161+
162+
for (const key in data.headers) {
163+
res.setHeader(key, data.headers[key])
164+
}
165+
166+
res.status(data.status).send(data.body)
167+
} catch (e) {
168+
next(e)
169+
}
170+
}
171+
172+
const asyncMethodToHandler = (
173+
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
174+
): RequestHandler => async (req, res, next) => {
175+
try {
176+
const data = await methodCallback(req as any) as any
177+
178+
for (const key in data.headers) {
179+
res.setHeader(key, data.headers[key])
180+
}
181+
182+
res.status(data.status).send(data.body)
183+
} catch (e) {
184+
next(e)
185+
}
186+
}
187+
184188
export default (app: Express, options: FrourioOptions = {}) => {
185189
const basePath = options.basePath ?? ''
186190
const hooks0 = hooksFn0(app)
@@ -207,7 +211,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
207211
createValidateHandler(req => [
208212
Object.keys(req.query).length ? validateOrReject(Object.assign(new Validators.Query(), req.query)) : null
209213
]),
210-
methodToHandler(controller0.get)
214+
asyncMethodToHandler(controller0.get)
211215
])
212216

213217
app.post(`${basePath}/`, [
@@ -278,7 +282,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
278282
hooks1.onRequest,
279283
hooks0.preParsing,
280284
...ctrlHooks1.preHandler,
281-
methodToHandler(controller7.get)
285+
asyncMethodToHandler(controller7.get)
282286
])
283287

284288
app.post(`${basePath}/users`, [

servers/all/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "../../../../tsconfig.json",
2+
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"baseUrl": ".",
55
"paths": {

servers/minimum/$server.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
import { LowerHttpMethod, AspidaMethods, HttpMethod, HttpStatusOk, AspidaMethodParams } from 'aspida'
2+
import { LowerHttpMethod, AspidaMethods, HttpStatusOk, AspidaMethodParams } from 'aspida'
33
import { Express, RequestHandler } from 'express'
44
import controllerFn0 from './api/controller'
55

@@ -36,8 +36,6 @@ type ServerValues = {
3636
}
3737

3838
type RequestParams<T extends AspidaMethodParams> = {
39-
path: string
40-
method: HttpMethod
4139
query: T['query']
4240
body: T['reqBody']
4341
headers: T['reqHeaders']
@@ -51,25 +49,15 @@ export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
5149

5250
const methodToHandler = (
5351
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
54-
): RequestHandler => async (req, res, next) => {
52+
): RequestHandler => (req, res, next) => {
5553
try {
56-
const result = methodCallback({
57-
query: req.query,
58-
path: req.path,
59-
method: req.method as HttpMethod,
60-
body: req.body,
61-
headers: req.headers,
62-
params: req.params,
63-
user: (req as any).user
64-
})
54+
const data = methodCallback(req as any) as any
6555

66-
const { status, body, headers } = result instanceof Promise ? await result : result
67-
68-
for (const key in headers) {
69-
res.setHeader(key, headers[key])
56+
for (const key in data.headers) {
57+
res.setHeader(key, data.headers[key])
7058
}
7159

72-
res.status(status).send(body)
60+
res.status(data.status).send(data.body)
7361
} catch (e) {
7462
next(e)
7563
}

servers/noMulter/$server.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
import { LowerHttpMethod, AspidaMethods, HttpMethod, HttpStatusOk, AspidaMethodParams } from 'aspida'
2+
import { LowerHttpMethod, AspidaMethods, HttpStatusOk, AspidaMethodParams } from 'aspida'
33
import express, { Express, RequestHandler, Request } from 'express'
44
import { validateOrReject } from 'class-validator'
55
import * as Validators from './validators'
@@ -45,8 +45,6 @@ type ServerValues = {
4545
}
4646

4747
type RequestParams<T extends AspidaMethodParams> = {
48-
path: string
49-
method: HttpMethod
5048
query: T['query']
5149
body: T['reqBody']
5250
headers: T['reqHeaders']
@@ -85,25 +83,31 @@ const createValidateHandler = (validators: (req: Request) => (Promise<void> | nu
8583

8684
const methodToHandler = (
8785
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
86+
): RequestHandler => (req, res, next) => {
87+
try {
88+
const data = methodCallback(req as any) as any
89+
90+
for (const key in data.headers) {
91+
res.setHeader(key, data.headers[key])
92+
}
93+
94+
res.status(data.status).send(data.body)
95+
} catch (e) {
96+
next(e)
97+
}
98+
}
99+
100+
const asyncMethodToHandler = (
101+
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
88102
): RequestHandler => async (req, res, next) => {
89103
try {
90-
const result = methodCallback({
91-
query: req.query,
92-
path: req.path,
93-
method: req.method as HttpMethod,
94-
body: req.body,
95-
headers: req.headers,
96-
params: req.params,
97-
user: (req as any).user
98-
})
99-
100-
const { status, body, headers } = result instanceof Promise ? await result : result
101-
102-
for (const key in headers) {
103-
res.setHeader(key, headers[key])
104+
const data = await methodCallback(req as any) as any
105+
106+
for (const key in data.headers) {
107+
res.setHeader(key, data.headers[key])
104108
}
105109

106-
res.status(status).send(body)
110+
res.status(data.status).send(data.body)
107111
} catch (e) {
108112
next(e)
109113
}
@@ -128,7 +132,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
128132
createValidateHandler(req => [
129133
Object.keys(req.query).length ? validateOrReject(Object.assign(new Validators.Query(), req.query)) : null
130134
]),
131-
methodToHandler(controller0.get)
135+
asyncMethodToHandler(controller0.get)
132136
])
133137

134138
app.post(`${basePath}/`, [
@@ -167,7 +171,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
167171
hooks0.onRequest,
168172
hooks1.onRequest,
169173
...ctrlHooks1.preHandler,
170-
methodToHandler(controller4.get)
174+
asyncMethodToHandler(controller4.get)
171175
])
172176

173177
app.post(`${basePath}/users`, [

0 commit comments

Comments
 (0)