Skip to content

Commit 77b89ca

Browse files
committed
feat: optimize methodToHandler
1 parent 6cf32b3 commit 77b89ca

File tree

10 files changed

+217
-170
lines changed

10 files changed

+217
-170
lines changed

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

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`, [

servers/noTypedParams/$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'
@@ -60,8 +60,6 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
6060
: T['reqBody']
6161

6262
type RequestParams<T extends AspidaMethodParams> = {
63-
path: string
64-
method: HttpMethod
6563
query: T['query']
6664
body: BlobToFile<T>
6765
headers: T['reqHeaders']
@@ -84,32 +82,6 @@ const parseJSONBoby: RequestHandler = (req, res, next) => {
8482
const createValidateHandler = (validators: (req: Request) => (Promise<void> | null)[]): RequestHandler =>
8583
(req, res, next) => Promise.all(validators(req)).then(() => next()).catch(() => res.sendStatus(400))
8684

87-
const methodToHandler = (
88-
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
89-
): RequestHandler => async (req, res, next) => {
90-
try {
91-
const result = methodCallback({
92-
query: req.query,
93-
path: req.path,
94-
method: req.method as HttpMethod,
95-
body: req.body,
96-
headers: req.headers,
97-
params: req.params,
98-
user: (req as any).user
99-
})
100-
101-
const { status, body, headers } = result instanceof Promise ? await result : result
102-
103-
for (const key in headers) {
104-
res.setHeader(key, headers[key])
105-
}
106-
107-
res.status(status).send(body)
108-
} catch (e) {
109-
next(e)
110-
}
111-
}
112-
11385
const formatMulterData = (arrayTypeKeys: [string, boolean][]): RequestHandler => ({ body, files }, _res, next) => {
11486
for (const [key] of arrayTypeKeys) {
11587
if (body[key] === undefined) body[key] = []
@@ -133,6 +105,38 @@ const formatMulterData = (arrayTypeKeys: [string, boolean][]): RequestHandler =>
133105
next()
134106
}
135107

108+
const methodToHandler = (
109+
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
110+
): RequestHandler => (req, res, next) => {
111+
try {
112+
const data = methodCallback(req as any) as any
113+
114+
for (const key in data.headers) {
115+
res.setHeader(key, data.headers[key])
116+
}
117+
118+
res.status(data.status).send(data.body)
119+
} catch (e) {
120+
next(e)
121+
}
122+
}
123+
124+
const asyncMethodToHandler = (
125+
methodCallback: ServerMethods<any, any>[LowerHttpMethod]
126+
): RequestHandler => async (req, res, next) => {
127+
try {
128+
const data = await methodCallback(req as any) as any
129+
130+
for (const key in data.headers) {
131+
res.setHeader(key, data.headers[key])
132+
}
133+
134+
res.status(data.status).send(data.body)
135+
} catch (e) {
136+
next(e)
137+
}
138+
}
139+
136140
export default (app: Express, options: FrourioOptions = {}) => {
137141
const basePath = options.basePath ?? ''
138142
const hooks0 = hooksFn0(app)
@@ -153,7 +157,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
153157
createValidateHandler(req => [
154158
Object.keys(req.query).length ? validateOrReject(Object.assign(new Validators.Query(), req.query)) : null
155159
]),
156-
methodToHandler(controller0.get)
160+
asyncMethodToHandler(controller0.get)
157161
])
158162

159163
app.post(`${basePath}/`, [
@@ -203,7 +207,7 @@ export default (app: Express, options: FrourioOptions = {}) => {
203207
hooks0.onRequest,
204208
hooks1.onRequest,
205209
...ctrlHooks1.preHandler,
206-
methodToHandler(controller5.get)
210+
asyncMethodToHandler(controller5.get)
207211
])
208212

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

0 commit comments

Comments
 (0)