@athenna/http 3.0.10 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Commands/MakeControllerCommand.d.ts +15 -0
- package/build/Commands/MakeControllerCommand.js +42 -0
- package/build/Commands/MakeInterceptorCommand.d.ts +15 -0
- package/build/Commands/MakeInterceptorCommand.js +42 -0
- package/build/Commands/MakeMiddlewareCommand.d.ts +15 -0
- package/build/Commands/MakeMiddlewareCommand.js +42 -0
- package/build/Commands/MakeTerminatorCommand.d.ts +15 -0
- package/build/Commands/MakeTerminatorCommand.js +42 -0
- package/build/Commands/RouteListCommand.d.ts +19 -0
- package/build/Commands/RouteListCommand.js +62 -0
- package/build/Context/Request.d.ts +144 -0
- package/build/Context/Request.js +219 -0
- package/build/Context/Response.d.ts +67 -0
- package/build/Context/Response.js +102 -0
- package/build/Contracts/InterceptorContract.d.ts +15 -0
- package/build/Contracts/InterceptorContract.js +9 -0
- package/build/Contracts/MiddlewareContract.d.ts +15 -0
- package/build/Contracts/MiddlewareContract.js +9 -0
- package/build/Contracts/TerminatorContract.d.ts +15 -0
- package/build/Contracts/TerminatorContract.js +9 -0
- package/build/Decorators/Controller.d.ts +14 -0
- package/build/Decorators/Controller.js +28 -0
- package/build/Decorators/Interceptor.d.ts +14 -0
- package/build/Decorators/Interceptor.js +37 -0
- package/build/Decorators/Middleware.d.ts +14 -0
- package/build/Decorators/Middleware.js +37 -0
- package/build/Decorators/Terminator.d.ts +14 -0
- package/build/Decorators/Terminator.js +37 -0
- package/build/Exceptions/BadGatewayException.d.ts +20 -0
- package/build/Exceptions/BadGatewayException.js +22 -0
- package/build/Exceptions/BadRequestException.d.ts +20 -0
- package/build/Exceptions/BadRequestException.js +22 -0
- package/build/Exceptions/ForbiddenException.d.ts +20 -0
- package/build/Exceptions/ForbiddenException.js +22 -0
- package/build/Exceptions/HttpException.d.ts +20 -0
- package/build/Exceptions/HttpException.js +27 -0
- package/build/Exceptions/InternalServerException.d.ts +20 -0
- package/build/Exceptions/InternalServerException.js +22 -0
- package/build/Exceptions/MethodNotAllowedException.d.ts +20 -0
- package/build/Exceptions/MethodNotAllowedException.js +22 -0
- package/build/Exceptions/NotAcceptableException.d.ts +20 -0
- package/build/Exceptions/NotAcceptableException.js +22 -0
- package/build/Exceptions/NotFoundException.d.ts +20 -0
- package/build/Exceptions/NotFoundException.js +22 -0
- package/build/Exceptions/NotImplementedException.d.ts +20 -0
- package/build/Exceptions/NotImplementedException.js +22 -0
- package/build/Exceptions/PayloadTooLargeException.d.ts +20 -0
- package/build/Exceptions/PayloadTooLargeException.js +22 -0
- package/build/Exceptions/RequestTimeoutException.d.ts +20 -0
- package/build/Exceptions/RequestTimeoutException.js +22 -0
- package/build/Exceptions/ServiceUnavailableException.d.ts +20 -0
- package/build/Exceptions/ServiceUnavailableException.js +22 -0
- package/build/Exceptions/UnauthorizedException.d.ts +20 -0
- package/build/Exceptions/UnauthorizedException.js +22 -0
- package/build/Exceptions/UndefinedMethodException.d.ts +12 -0
- package/build/Exceptions/UndefinedMethodException.js +19 -0
- package/build/Exceptions/UnprocessableEntityException.d.ts +20 -0
- package/build/Exceptions/UnprocessableEntityException.js +22 -0
- package/build/Facades/Route.d.ts +10 -0
- package/{src → build}/Facades/Route.js +2 -7
- package/build/Facades/Server.d.ts +10 -0
- package/{src → build}/Facades/Server.js +2 -7
- package/build/Handlers/FastifyHandler.d.ts +41 -0
- package/build/Handlers/FastifyHandler.js +116 -0
- package/build/Handlers/HttpExceptionHandler.d.ts +31 -0
- package/build/Handlers/HttpExceptionHandler.js +82 -0
- package/build/Kernels/HttpKernel.d.ts +73 -0
- package/build/Kernels/HttpKernel.js +221 -0
- package/build/Providers/HttpRouteProvider.d.ts +12 -0
- package/build/Providers/HttpRouteProvider.js +15 -0
- package/build/Providers/HttpServerProvider.d.ts +13 -0
- package/build/Providers/HttpServerProvider.js +25 -0
- package/build/Router/Route.d.ts +263 -0
- package/build/Router/Route.js +447 -0
- package/build/Router/RouteGroup.d.ts +96 -0
- package/build/Router/RouteGroup.js +123 -0
- package/build/Router/RouteResource.d.ts +108 -0
- package/build/Router/RouteResource.js +158 -0
- package/build/Router/Router.d.ts +103 -0
- package/build/Router/Router.js +188 -0
- package/build/Server/ServerImpl.d.ts +129 -0
- package/build/Server/ServerImpl.js +213 -0
- package/build/Types/Contexts/Context.d.ts +20 -0
- package/build/Types/Contexts/Context.js +9 -0
- package/build/Types/Contexts/ErrorContext.d.ts +21 -0
- package/build/Types/Contexts/ErrorContext.js +9 -0
- package/build/Types/Contexts/InterceptContext.d.ts +20 -0
- package/build/Types/Contexts/InterceptContext.js +9 -0
- package/build/Types/Contexts/TerminateContext.d.ts +21 -0
- package/build/Types/Contexts/TerminateContext.js +9 -0
- package/build/Types/Controllers/ControllerOptions.d.ts +25 -0
- package/build/Types/Controllers/ControllerOptions.js +9 -0
- package/build/Types/Middlewares/InterceptorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/InterceptorRouteType.js +9 -0
- package/build/Types/Middlewares/MiddlewareHandler.d.ts +20 -0
- package/build/Types/Middlewares/MiddlewareHandler.js +9 -0
- package/build/Types/Middlewares/MiddlewareOptions.d.ts +40 -0
- package/build/Types/Middlewares/MiddlewareOptions.js +9 -0
- package/build/Types/Middlewares/MiddlewareRouteType.d.ts +11 -0
- package/build/Types/Middlewares/MiddlewareRouteType.js +9 -0
- package/build/Types/Middlewares/TerminatorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/TerminatorRouteType.js +9 -0
- package/build/Types/Router/RouteHandler.d.ts +10 -0
- package/build/Types/Router/RouteHandler.js +9 -0
- package/build/Types/Router/RouteJSON.d.ts +21 -0
- package/build/Types/Router/RouteJSON.js +9 -0
- package/build/Types/Router/RouteResourceTypes.d.ts +9 -0
- package/build/Types/Router/RouteResourceTypes.js +9 -0
- package/build/index.d.ts +70 -0
- package/build/index.js +48 -0
- package/package.json +159 -76
- package/templates/controller.edge +8 -36
- package/templates/interceptor.edge +8 -0
- package/templates/middleware.edge +4 -36
- package/templates/terminator.edge +6 -0
- package/src/Commands/Make/Controller.js +0 -64
- package/src/Commands/Make/Middleware.js +0 -80
- package/src/Commands/Route/List.js +0 -107
- package/src/Context/Request.js +0 -223
- package/src/Context/Response.js +0 -137
- package/src/Exceptions/BadGatewayException.js +0 -31
- package/src/Exceptions/BadRequestException.js +0 -31
- package/src/Exceptions/ForbiddenException.js +0 -31
- package/src/Exceptions/HttpException.js +0 -33
- package/src/Exceptions/InternalServerException.js +0 -31
- package/src/Exceptions/MethodNotAllowedException.js +0 -31
- package/src/Exceptions/NotAcceptableException.js +0 -31
- package/src/Exceptions/NotFoundException.js +0 -31
- package/src/Exceptions/NotImplementedException.js +0 -31
- package/src/Exceptions/PayloadTooLargeException.js +0 -31
- package/src/Exceptions/RequestTimeoutException.js +0 -31
- package/src/Exceptions/ServiceUnavailableException.js +0 -31
- package/src/Exceptions/UnauthorizedException.js +0 -31
- package/src/Exceptions/UndefinedMethodException.js +0 -30
- package/src/Exceptions/UnprocessableEntityException.js +0 -31
- package/src/Handlers/FastifyHandler.js +0 -148
- package/src/Handlers/HttpExceptionHandler.js +0 -88
- package/src/Helpers/HttpLoader.js +0 -38
- package/src/Kernels/HttpKernel.js +0 -199
- package/src/Providers/ControllerProvider.js +0 -29
- package/src/Providers/HttpRouteProvider.js +0 -24
- package/src/Providers/HttpServerProvider.js +0 -37
- package/src/Providers/MiddlewareProvider.js +0 -29
- package/src/Router/Route.js +0 -417
- package/src/Router/RouteGroup.js +0 -119
- package/src/Router/RouteResource.js +0 -247
- package/src/Router/Router.js +0 -313
- package/src/Utils/isMiddlewareContract.js +0 -17
- package/src/Utils/removeSlashes.js +0 -30
- package/src/index.d.ts +0 -1331
- package/src/index.js +0 -377
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/http
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { RouteJSON } from '#src/Types/Router/RouteJSON';
|
|
10
|
+
import { RequestHandler } from '#src/Types/Contexts/Context';
|
|
11
|
+
import { RouteHandler } from '#src/Types/Router/RouteHandler';
|
|
12
|
+
import { HTTPMethods, FastifySchema, RouteOptions } from 'fastify';
|
|
13
|
+
import { MiddlewareRecord } from '#src/Types/Middlewares/MiddlewareHandler';
|
|
14
|
+
import { MiddlewareRouteType } from '#src/Types/Middlewares/MiddlewareRouteType';
|
|
15
|
+
import { TerminatorRouteType } from '#src/Types/Middlewares/TerminatorRouteType';
|
|
16
|
+
import { InterceptorRouteType } from '#src/Types/Middlewares/InterceptorRouteType';
|
|
17
|
+
export declare class Route {
|
|
18
|
+
/**
|
|
19
|
+
* Holds all the route implementations to be registered in the Server.
|
|
20
|
+
*/
|
|
21
|
+
route: {
|
|
22
|
+
url?: string;
|
|
23
|
+
methods: HTTPMethods[];
|
|
24
|
+
handler?: RequestHandler;
|
|
25
|
+
name?: string;
|
|
26
|
+
deleted?: boolean;
|
|
27
|
+
prefixes?: string[];
|
|
28
|
+
middlewares?: MiddlewareRecord;
|
|
29
|
+
fastify?: Partial<RouteOptions>;
|
|
30
|
+
};
|
|
31
|
+
constructor(url: string, methods: HTTPMethods[], handler: RouteHandler);
|
|
32
|
+
/**
|
|
33
|
+
* Set the route name.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* Route.name('profile')
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
name(name: string): Route;
|
|
41
|
+
/**
|
|
42
|
+
* Set fastify vanilla route options.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* Route.vanillaOptions({ schema: {} })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
vanillaOptions(options: Partial<RouteOptions>): Route;
|
|
50
|
+
/**
|
|
51
|
+
* Set a prefix for the route.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* Route.prefix('/api/v1')
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
prefix(prefix: string): Route;
|
|
59
|
+
/**
|
|
60
|
+
* Set a named middleware, middleware closure or a MiddlewareContract implementation
|
|
61
|
+
* in the route.
|
|
62
|
+
*/
|
|
63
|
+
middleware(middleware: MiddlewareRouteType, prepend?: boolean): Route;
|
|
64
|
+
/**
|
|
65
|
+
* Set a named interceptor, interceptor closure or a InterceptorContract implementation
|
|
66
|
+
* in the route.
|
|
67
|
+
*/
|
|
68
|
+
interceptor(interceptor: InterceptorRouteType, prepend?: boolean): Route;
|
|
69
|
+
/**
|
|
70
|
+
* Set a named terminator, terminator closure or a TerminatorContract implementation
|
|
71
|
+
* in the route.
|
|
72
|
+
*/
|
|
73
|
+
terminator(terminator: TerminatorRouteType, prepend?: boolean): Route;
|
|
74
|
+
/**
|
|
75
|
+
* Set up all helmet options for route.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* Route.helmet({
|
|
80
|
+
* dnsPrefetchControl: { allow: true }
|
|
81
|
+
* })
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
helmet(options: Omit<import('@fastify/helmet').FastifyHelmetOptions, 'global'>): Route;
|
|
85
|
+
/**
|
|
86
|
+
* Set up all schema options for route.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* Route.schema({
|
|
91
|
+
* body: {
|
|
92
|
+
* type: 'array',
|
|
93
|
+
* items: {
|
|
94
|
+
* oneOf: [{ type: 'string', description: 'User name' }]
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
* })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
schema(options: FastifySchema): Route;
|
|
101
|
+
/**
|
|
102
|
+
* Set up all rate limit options for route.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* Route.rateLimit({
|
|
107
|
+
* max: 3,
|
|
108
|
+
* timeWindow: '1 minute'
|
|
109
|
+
* })
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
rateLimit(options: import('@fastify/rate-limit').RateLimitOptions): Route;
|
|
113
|
+
/**
|
|
114
|
+
* Hide the route in swagger docs.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* Route.hide()
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
hide(): Route;
|
|
122
|
+
/**
|
|
123
|
+
* Set the route as deprecated in swagger docs.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* Route.deprecated()
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
deprecated(): Route;
|
|
131
|
+
/**
|
|
132
|
+
* Set a summary for the route swagger docs.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* Route.summary('List all users')
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
summary(summary: string): Route;
|
|
140
|
+
/**
|
|
141
|
+
* Set a description for the route swagger docs.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* Route.description('List all users')
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
description(description: string): Route;
|
|
149
|
+
/**
|
|
150
|
+
* Set tags for the route swagger docs.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* Route.tags(['v1'])
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
tags(tags: string[]): Route;
|
|
158
|
+
/**
|
|
159
|
+
* Set body param for the route swagger docs.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* Route.body('name', {
|
|
164
|
+
* type: 'string',
|
|
165
|
+
* description: 'User name'
|
|
166
|
+
* })
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
body(name: string, body?: any): Route;
|
|
170
|
+
/**
|
|
171
|
+
* Set param for the route swagger docs.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* Route.param('id', { description: 'Set the user id' })
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
param(name: string, param?: any): Route;
|
|
179
|
+
/**
|
|
180
|
+
* Set query string for the route swagger docs.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* Route.queryString('page', { description: 'Set the pagination page' })
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
queryString(name: string, queryString?: any): Route;
|
|
188
|
+
/**
|
|
189
|
+
* Set response for the route swagger docs.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* For objects
|
|
193
|
+
* ```ts
|
|
194
|
+
* Route.response(200, {
|
|
195
|
+
* description: 'Return one user',
|
|
196
|
+
* properties: {
|
|
197
|
+
* name: { type: 'string', description: 'User name' }
|
|
198
|
+
* }
|
|
199
|
+
* })
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* For arrays
|
|
203
|
+
* ```ts
|
|
204
|
+
* Route.response(200, {
|
|
205
|
+
* description: 'Return users paginated',
|
|
206
|
+
* schema: {
|
|
207
|
+
* type: 'array'
|
|
208
|
+
* oneOf: [
|
|
209
|
+
* { type: 'string', description: 'User name' }
|
|
210
|
+
* ]
|
|
211
|
+
* }
|
|
212
|
+
* })
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
response(statusCode: number, response?: any): Route;
|
|
216
|
+
/**
|
|
217
|
+
* Set the route security in swagger docs.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* Route.security('apiToken', [])
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
security(key: string, values: string[]): Route;
|
|
225
|
+
/**
|
|
226
|
+
* Set the external documentation url in swagger docs.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* Route.externalDocs('https://github.com/athennaio/docs', 'Athenna Framework Documentation')
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
externalDocs(url: string, description?: string): Route;
|
|
234
|
+
/**
|
|
235
|
+
* Set the type of the content that the API consumes in swagger docs.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* Route.consumes(['json', 'yaml', ...])
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
consumes(consumes: string[]): Route;
|
|
243
|
+
/**
|
|
244
|
+
* Set the type of the content that the API produces in swagger docs.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* Route.produces(['json', 'yaml', ...])
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
produces(produces: string[]): Route;
|
|
252
|
+
/**
|
|
253
|
+
* Get the route as JSON.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* Route.toJSON()
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
toJSON(): RouteJSON;
|
|
261
|
+
private getUrl;
|
|
262
|
+
private removeSlashes;
|
|
263
|
+
}
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/http
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { Is, Route as RouteHelper } from '@athenna/common';
|
|
10
|
+
import { UndefinedMethodException } from '#src/Exceptions/UndefinedMethodException';
|
|
11
|
+
export class Route {
|
|
12
|
+
/**
|
|
13
|
+
* Holds all the route implementations to be registered in the Server.
|
|
14
|
+
*/
|
|
15
|
+
route;
|
|
16
|
+
constructor(url, methods, handler) {
|
|
17
|
+
this.route = {
|
|
18
|
+
url,
|
|
19
|
+
methods,
|
|
20
|
+
deleted: false,
|
|
21
|
+
prefixes: [],
|
|
22
|
+
middlewares: {
|
|
23
|
+
middlewares: [],
|
|
24
|
+
terminators: [],
|
|
25
|
+
interceptors: [],
|
|
26
|
+
},
|
|
27
|
+
fastify: { schema: {} },
|
|
28
|
+
};
|
|
29
|
+
if (Is.String(handler)) {
|
|
30
|
+
const [controller, method] = handler.split('.');
|
|
31
|
+
const dependency = ioc.safeUse(`App/Http/Controllers/${controller}`);
|
|
32
|
+
if (!dependency[method]) {
|
|
33
|
+
throw new UndefinedMethodException(method, controller);
|
|
34
|
+
}
|
|
35
|
+
this.route.handler = dependency[method].bind(dependency);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.route.handler = handler;
|
|
39
|
+
}
|
|
40
|
+
RouteHelper.getParamsName(url).forEach(param => this.param(param));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Set the route name.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* Route.name('profile')
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
name(name) {
|
|
51
|
+
this.route.name = name;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Set fastify vanilla route options.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* Route.vanillaOptions({ schema: {} })
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
vanillaOptions(options) {
|
|
63
|
+
this.route.fastify = {
|
|
64
|
+
...this.route.fastify,
|
|
65
|
+
...options,
|
|
66
|
+
};
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Set a prefix for the route.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* Route.prefix('/api/v1')
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
prefix(prefix) {
|
|
78
|
+
this.route.prefixes.push(prefix);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Set a named middleware, middleware closure or a MiddlewareContract implementation
|
|
83
|
+
* in the route.
|
|
84
|
+
*/
|
|
85
|
+
middleware(middleware, prepend = false) {
|
|
86
|
+
const insertionType = prepend ? 'unshift' : 'push';
|
|
87
|
+
if (Is.String(middleware)) {
|
|
88
|
+
const mid = ioc.use(`App/Http/Middlewares/Names/${middleware}`) ||
|
|
89
|
+
ioc.safeUse(`App/Http/Middlewares/${middleware}`);
|
|
90
|
+
this.route.middlewares.middlewares[insertionType](mid.handle.bind(mid));
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
if (Is.Function(middleware)) {
|
|
94
|
+
this.route.middlewares.middlewares[insertionType](middleware);
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
this.route.middlewares.middlewares[insertionType](middleware.handle.bind(middleware));
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Set a named interceptor, interceptor closure or a InterceptorContract implementation
|
|
102
|
+
* in the route.
|
|
103
|
+
*/
|
|
104
|
+
interceptor(interceptor, prepend = false) {
|
|
105
|
+
const insertionType = prepend ? 'unshift' : 'push';
|
|
106
|
+
if (Is.String(interceptor)) {
|
|
107
|
+
const inte = ioc.use(`App/Http/Interceptors/Names/${interceptor}`) ||
|
|
108
|
+
ioc.safeUse(`App/Http/Interceptors/${interceptor}`);
|
|
109
|
+
this.route.middlewares.interceptors[insertionType](inte.intercept.bind(inte));
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
if (Is.Function(interceptor)) {
|
|
113
|
+
this.route.middlewares.interceptors[insertionType](interceptor);
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
this.route.middlewares.interceptors[insertionType](interceptor.intercept.bind(interceptor));
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Set a named terminator, terminator closure or a TerminatorContract implementation
|
|
121
|
+
* in the route.
|
|
122
|
+
*/
|
|
123
|
+
terminator(terminator, prepend = false) {
|
|
124
|
+
const insertionType = prepend ? 'unshift' : 'push';
|
|
125
|
+
if (Is.String(terminator)) {
|
|
126
|
+
const ter = ioc.use(`App/Http/Terminators/Names/${terminator}`) ||
|
|
127
|
+
ioc.safeUse(`App/Http/Terminators/${terminator}`);
|
|
128
|
+
this.route.middlewares.terminators[insertionType](ter.terminate.bind(ter));
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
if (Is.Function(terminator)) {
|
|
132
|
+
this.route.middlewares.terminators[insertionType](terminator);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
this.route.middlewares.terminators[insertionType](terminator.terminate.bind(terminator));
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Set up all helmet options for route.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* Route.helmet({
|
|
144
|
+
* dnsPrefetchControl: { allow: true }
|
|
145
|
+
* })
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
helmet(options) {
|
|
149
|
+
this.route.fastify.helmet = options;
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Set up all schema options for route.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* Route.schema({
|
|
158
|
+
* body: {
|
|
159
|
+
* type: 'array',
|
|
160
|
+
* items: {
|
|
161
|
+
* oneOf: [{ type: 'string', description: 'User name' }]
|
|
162
|
+
* }
|
|
163
|
+
* }
|
|
164
|
+
* })
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
schema(options) {
|
|
168
|
+
this.route.fastify.schema = options;
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Set up all rate limit options for route.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* Route.rateLimit({
|
|
177
|
+
* max: 3,
|
|
178
|
+
* timeWindow: '1 minute'
|
|
179
|
+
* })
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
rateLimit(options) {
|
|
183
|
+
if (!this.route.fastify.config) {
|
|
184
|
+
this.route.fastify.config = {};
|
|
185
|
+
}
|
|
186
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
this.route.fastify.config.rateLimit = options;
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Hide the route in swagger docs.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* Route.hide()
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
hide() {
|
|
200
|
+
this.route.fastify.schema.hide = true;
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Set the route as deprecated in swagger docs.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* Route.deprecated()
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
deprecated() {
|
|
212
|
+
this.route.fastify.schema.deprecated = true;
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Set a summary for the route swagger docs.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* Route.summary('List all users')
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
summary(summary) {
|
|
224
|
+
this.route.fastify.schema.summary = summary;
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Set a description for the route swagger docs.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* Route.description('List all users')
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
description(description) {
|
|
236
|
+
this.route.fastify.schema.description = description;
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Set tags for the route swagger docs.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* Route.tags(['v1'])
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
tags(tags) {
|
|
248
|
+
if (!this.route.fastify.schema.tags) {
|
|
249
|
+
this.route.fastify.schema.tags = [];
|
|
250
|
+
}
|
|
251
|
+
tags.forEach(tag => this.route.fastify.schema.tags.push(tag));
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Set body param for the route swagger docs.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* Route.body('name', {
|
|
260
|
+
* type: 'string',
|
|
261
|
+
* description: 'User name'
|
|
262
|
+
* })
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
body(name, body = {}) {
|
|
266
|
+
if (!this.route.fastify.schema.body) {
|
|
267
|
+
this.route.fastify.schema.body = {
|
|
268
|
+
type: 'object',
|
|
269
|
+
properties: {},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
273
|
+
// @ts-ignore
|
|
274
|
+
this.route.fastify.schema.body.properties[name] = body;
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Set param for the route swagger docs.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* Route.param('id', { description: 'Set the user id' })
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
param(name, param = {}) {
|
|
286
|
+
if (!this.route.fastify.schema.params) {
|
|
287
|
+
this.route.fastify.schema.params = {
|
|
288
|
+
type: 'object',
|
|
289
|
+
properties: {},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
293
|
+
// @ts-ignore
|
|
294
|
+
this.route.fastify.schema.params.properties[name] = param;
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Set query string for the route swagger docs.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* Route.queryString('page', { description: 'Set the pagination page' })
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
queryString(name, queryString = {}) {
|
|
306
|
+
if (!this.route.fastify.schema.querystring) {
|
|
307
|
+
this.route.fastify.schema.querystring = {
|
|
308
|
+
type: 'object',
|
|
309
|
+
properties: {},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
313
|
+
// @ts-ignore
|
|
314
|
+
this.route.fastify.schema.querystring.properties[name] = queryString;
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Set response for the route swagger docs.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* For objects
|
|
322
|
+
* ```ts
|
|
323
|
+
* Route.response(200, {
|
|
324
|
+
* description: 'Return one user',
|
|
325
|
+
* properties: {
|
|
326
|
+
* name: { type: 'string', description: 'User name' }
|
|
327
|
+
* }
|
|
328
|
+
* })
|
|
329
|
+
* ```
|
|
330
|
+
*
|
|
331
|
+
* For arrays
|
|
332
|
+
* ```ts
|
|
333
|
+
* Route.response(200, {
|
|
334
|
+
* description: 'Return users paginated',
|
|
335
|
+
* schema: {
|
|
336
|
+
* type: 'array'
|
|
337
|
+
* oneOf: [
|
|
338
|
+
* { type: 'string', description: 'User name' }
|
|
339
|
+
* ]
|
|
340
|
+
* }
|
|
341
|
+
* })
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
response(statusCode, response = {}) {
|
|
345
|
+
if (!this.route.fastify.schema.response) {
|
|
346
|
+
this.route.fastify.schema.response = {};
|
|
347
|
+
}
|
|
348
|
+
this.route.fastify.schema.response[statusCode] = response;
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Set the route security in swagger docs.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* Route.security('apiToken', [])
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
security(key, values) {
|
|
360
|
+
if (!this.route.fastify.schema.security) {
|
|
361
|
+
this.route.fastify.schema.security = [];
|
|
362
|
+
}
|
|
363
|
+
this.route.fastify.schema.security.push({ [key]: values });
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Set the external documentation url in swagger docs.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* Route.externalDocs('https://github.com/athennaio/docs', 'Athenna Framework Documentation')
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
externalDocs(url, description) {
|
|
375
|
+
if (!this.route.fastify.schema.externalDocs) {
|
|
376
|
+
this.route.fastify.schema.externalDocs = {
|
|
377
|
+
url: '',
|
|
378
|
+
description: '',
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
this.route.fastify.schema.externalDocs.url = url;
|
|
382
|
+
this.route.fastify.schema.externalDocs.description = description;
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Set the type of the content that the API consumes in swagger docs.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```ts
|
|
390
|
+
* Route.consumes(['json', 'yaml', ...])
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
consumes(consumes) {
|
|
394
|
+
this.route.fastify.schema.consumes = consumes;
|
|
395
|
+
return this;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Set the type of the content that the API produces in swagger docs.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* Route.produces(['json', 'yaml', ...])
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
produces(produces) {
|
|
406
|
+
this.route.fastify.schema.produces = produces;
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Get the route as JSON.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* Route.toJSON()
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
toJSON() {
|
|
418
|
+
return {
|
|
419
|
+
url: this.getUrl(),
|
|
420
|
+
methods: this.route.methods,
|
|
421
|
+
name: this.route.name,
|
|
422
|
+
deleted: this.route.deleted,
|
|
423
|
+
prefixes: this.route.prefixes,
|
|
424
|
+
handler: this.route.handler,
|
|
425
|
+
middlewares: this.route.middlewares,
|
|
426
|
+
fastify: this.route.fastify,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
getUrl() {
|
|
430
|
+
const url = this.removeSlashes(this.route.url);
|
|
431
|
+
const prefix = this.route.prefixes
|
|
432
|
+
.reverse()
|
|
433
|
+
.map(p => this.removeSlashes(p))
|
|
434
|
+
.join('');
|
|
435
|
+
return prefix ? `${prefix}${url === '/' ? '' : url}` : url;
|
|
436
|
+
}
|
|
437
|
+
removeSlashes(url) {
|
|
438
|
+
if (url === '/') {
|
|
439
|
+
return url;
|
|
440
|
+
}
|
|
441
|
+
const matcher = url => `/${url.replace(/^\//, '').replace(/\/$/, '')}`;
|
|
442
|
+
if (Is.Array(url)) {
|
|
443
|
+
return url.map(u => matcher(u));
|
|
444
|
+
}
|
|
445
|
+
return matcher(url);
|
|
446
|
+
}
|
|
447
|
+
}
|