1
1
/* eslint-disable */
2
+ // prettier-ignore
2
3
import path from 'path'
4
+ // prettier-ignore
3
5
import express , { Express , RequestHandler , Request } from 'express'
6
+ // prettier-ignore
4
7
import multer , { Options } from 'multer'
8
+ // prettier-ignore
5
9
import { validateOrReject , ValidatorOptions } from 'class-validator'
10
+ // prettier-ignore
6
11
import fastJson , { Schema } from 'fast-json-stringify'
12
+ // prettier-ignore
7
13
import * as Validators from './validators'
14
+ // prettier-ignore
8
15
import hooksFn0 from './api/hooks'
16
+ // prettier-ignore
9
17
import hooksFn1 from './api/empty/hooks'
18
+ // prettier-ignore
10
19
import hooksFn2 from './api/users/hooks'
20
+ // prettier-ignore
11
21
import hooksFn3 from './api/users/_userId@number/_name/hooks'
22
+ // prettier-ignore
12
23
import controllerFn0 , { hooks as ctrlHooksFn0 , responseSchema as responseSchemaFn0 } from './api/controller'
24
+ // prettier-ignore
13
25
import controllerFn1 from './api/500/controller'
26
+ // prettier-ignore
14
27
import controllerFn2 from './api/empty/noEmpty/controller'
28
+ // prettier-ignore
15
29
import controllerFn3 from './api/multiForm/controller'
30
+ // prettier-ignore
16
31
import controllerFn4 from './api/texts/controller'
32
+ // prettier-ignore
17
33
import controllerFn5 from './api/texts/sample/controller'
34
+ // prettier-ignore
18
35
import controllerFn6 from './api/texts/_label@string/controller'
36
+ // prettier-ignore
19
37
import controllerFn7 , { hooks as ctrlHooksFn1 } from './api/users/controller'
38
+ // prettier-ignore
20
39
import controllerFn8 from './api/users/_userId@number/controller'
40
+ // prettier-ignore
21
41
import controllerFn9 from './api/users/_userId@number/_name/controller'
42
+ // prettier-ignore
22
43
import type { ReadStream } from 'fs'
44
+ // prettier-ignore
23
45
import type { LowerHttpMethod , AspidaMethods , HttpStatusOk , AspidaMethodParams } from 'aspida'
24
46
47
+ // prettier-ignore
25
48
export type FrourioOptions = {
26
49
basePath ?: string
27
50
validator ?: ValidatorOptions
28
51
multer ?: Options
29
52
}
30
53
54
+ // prettier-ignore
31
55
export type MulterFile = Express . Multer . File
32
56
57
+ // prettier-ignore
33
58
type HttpStatusNoOk = 301 | 302 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 409 | 500 | 501 | 502 | 503 | 504 | 505
34
59
60
+ // prettier-ignore
35
61
type PartiallyPartial < T , K extends keyof T > = Omit < T , K > & Partial < Pick < T , K > >
36
62
63
+ // prettier-ignore
37
64
type BaseResponse < T , U , V > = {
38
65
status : V extends number ? V : HttpStatusOk
39
66
body : T
40
67
headers : U
41
68
}
42
69
70
+ // prettier-ignore
43
71
type ServerResponse < K extends AspidaMethodParams > =
44
72
| ( K extends { resBody : K [ 'resBody' ] ; resHeaders : K [ 'resHeaders' ] }
45
73
? BaseResponse < K [ 'resBody' ] , K [ 'resHeaders' ] , K [ 'status' ] >
@@ -53,6 +81,7 @@ type ServerResponse<K extends AspidaMethodParams> =
53
81
> )
54
82
| PartiallyPartial < BaseResponse < any , any , HttpStatusNoOk > , 'body' | 'headers' >
55
83
84
+ // prettier-ignore
56
85
type BlobToFile < T extends AspidaMethodParams > = T [ 'reqFormat' ] extends FormData
57
86
? {
58
87
[ P in keyof T [ 'reqBody' ] ] : Required < T [ 'reqBody' ] > [ P ] extends Blob | ReadStream
@@ -63,6 +92,7 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
63
92
}
64
93
: T [ 'reqBody' ]
65
94
95
+ // prettier-ignore
66
96
type RequestParams < T extends AspidaMethodParams > = Pick < {
67
97
query : T [ 'query' ]
68
98
body : BlobToFile < T >
@@ -73,12 +103,14 @@ type RequestParams<T extends AspidaMethodParams> = Pick<{
73
103
headers : Required < T > [ 'reqHeaders' ] extends { } | null ? 'headers' : never
74
104
} [ 'query' | 'body' | 'headers' ] >
75
105
106
+ // prettier-ignore
76
107
export type ServerMethods < T extends AspidaMethods , U extends Record < string , any > = { } > = {
77
108
[ K in keyof T ] : (
78
109
req : RequestParams < T [ K ] > & U
79
110
) => ServerResponse < T [ K ] > | Promise < ServerResponse < T [ K ] > >
80
111
}
81
112
113
+ // prettier-ignore
82
114
const parseNumberTypeQueryParams = ( numberTypeParams : [ string , boolean , boolean ] [ ] ) : RequestHandler => ( { query } , res , next ) => {
83
115
for ( const [ key , isOptional , isArray ] of numberTypeParams ) {
84
116
const param = query [ key ]
@@ -107,6 +139,7 @@ const parseNumberTypeQueryParams = (numberTypeParams: [string, boolean, boolean]
107
139
next ( )
108
140
}
109
141
142
+ // prettier-ignore
110
143
const parseBooleanTypeQueryParams = ( booleanTypeParams : [ string , boolean , boolean ] [ ] ) : RequestHandler => ( { query } , res , next ) => {
111
144
for ( const [ key , isOptional , isArray ] of booleanTypeParams ) {
112
145
const param = query [ key ]
@@ -135,9 +168,11 @@ const parseBooleanTypeQueryParams = (booleanTypeParams: [string, boolean, boolea
135
168
next ( )
136
169
}
137
170
171
+ // prettier-ignore
138
172
const callParserIfExistsQuery = ( parser : RequestHandler ) : RequestHandler => ( req , res , next ) =>
139
173
Object . keys ( req . query ) . length ? parser ( req , res , next ) : next ( )
140
174
175
+ // prettier-ignore
141
176
const parseJSONBoby : RequestHandler = ( req , res , next ) => {
142
177
express . json ( ) ( req , res , err => {
143
178
if ( err ) return res . sendStatus ( 400 )
@@ -146,6 +181,7 @@ const parseJSONBoby: RequestHandler = (req, res, next) => {
146
181
} )
147
182
}
148
183
184
+ // prettier-ignore
149
185
const createTypedParamsHandler = ( numberTypeParams : string [ ] ) : RequestHandler => ( req , res , next ) => {
150
186
const params : Record < string , string | number > = req . params
151
187
@@ -160,9 +196,11 @@ const createTypedParamsHandler = (numberTypeParams: string[]): RequestHandler =>
160
196
next ( )
161
197
}
162
198
199
+ // prettier-ignore
163
200
const createValidateHandler = ( validators : ( req : Request ) => ( Promise < void > | null ) [ ] ) : RequestHandler =>
164
201
( req , res , next ) => Promise . all ( validators ( req ) ) . then ( ( ) => next ( ) ) . catch ( err => res . status ( 400 ) . send ( err ) )
165
202
203
+ // prettier-ignore
166
204
const formatMulterData = ( arrayTypeKeys : [ string , boolean ] [ ] ) : RequestHandler => ( { body, files } , _res , next ) => {
167
205
for ( const [ key ] of arrayTypeKeys ) {
168
206
if ( body [ key ] === undefined ) body [ key ] = [ ]
@@ -186,6 +224,7 @@ const formatMulterData = (arrayTypeKeys: [string, boolean][]): RequestHandler =>
186
224
next ( )
187
225
}
188
226
227
+ // prettier-ignore
189
228
const methodToHandler = (
190
229
methodCallback : ServerMethods < any , any > [ LowerHttpMethod ]
191
230
) : RequestHandler => ( req , res , next ) => {
@@ -204,6 +243,7 @@ const methodToHandler = (
204
243
}
205
244
}
206
245
246
+ // prettier-ignore
207
247
const asyncMethodToHandler = (
208
248
methodCallback : ServerMethods < any , any > [ LowerHttpMethod ]
209
249
) : RequestHandler => async ( req , res , next ) => {
@@ -222,6 +262,7 @@ const asyncMethodToHandler = (
222
262
}
223
263
}
224
264
265
+ // prettier-ignore
225
266
const asyncMethodToHandlerWithSchema = (
226
267
methodCallback : ServerMethods < any , any > [ LowerHttpMethod ] ,
227
268
schema : { [ K in HttpStatusOk ] ?: Schema }
@@ -261,6 +302,7 @@ const asyncMethodToHandlerWithSchema = (
261
302
}
262
303
}
263
304
305
+ // prettier-ignore
264
306
export default ( app : Express , options : FrourioOptions = { } ) => {
265
307
const basePath = options . basePath ?? ''
266
308
const validatorOptions : ValidatorOptions = { validationError : { target : false } , ...options . validator }
0 commit comments