@athenna/http 3.0.11 → 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 -77
- 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 -268
- package/src/Context/Response.js +0 -173
- 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 -411
- 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 -1395
- package/src/index.js +0 -377
|
@@ -0,0 +1,96 @@
|
|
|
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 { Route } from '#src/Router/Route';
|
|
10
|
+
import { RouteResource } from '#src/Router/RouteResource';
|
|
11
|
+
import { MiddlewareRouteType } from '#src/Types/Middlewares/MiddlewareRouteType';
|
|
12
|
+
import { TerminatorRouteType } from '#src/Types/Middlewares/TerminatorRouteType';
|
|
13
|
+
import { InterceptorRouteType } from '#src/Types/Middlewares/InterceptorRouteType';
|
|
14
|
+
export declare class RouteGroup {
|
|
15
|
+
/**
|
|
16
|
+
* All routes registered in the group.
|
|
17
|
+
*/
|
|
18
|
+
routes: (Route | RouteGroup | RouteResource)[];
|
|
19
|
+
constructor(routes: (Route | RouteGroup | RouteResource)[]);
|
|
20
|
+
/**
|
|
21
|
+
* Define prefix all the routes in the group.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* Route.group(() => {
|
|
26
|
+
*
|
|
27
|
+
* }).prefix('/api/v1')
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
prefix(prefix: string): RouteGroup;
|
|
31
|
+
/**
|
|
32
|
+
* Add a middleware to all routes in the group.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* Route.group(() => {
|
|
37
|
+
*
|
|
38
|
+
* }).middleware('auth')
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
middleware(middleware: MiddlewareRouteType): RouteGroup;
|
|
42
|
+
/**
|
|
43
|
+
* Add an interceptor to all routes in the group.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* Route.group(() => {
|
|
48
|
+
*
|
|
49
|
+
* }).interceptor('response')
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
interceptor(interceptor: InterceptorRouteType): RouteGroup;
|
|
53
|
+
/**
|
|
54
|
+
* Add a terminator to all routes in the group.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* Route.group(() => {
|
|
59
|
+
*
|
|
60
|
+
* }).terminator('log')
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
terminator(terminator: TerminatorRouteType): RouteGroup;
|
|
64
|
+
/**
|
|
65
|
+
* Set up helmet options for route group.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* Route.group(() => {
|
|
70
|
+
*
|
|
71
|
+
* }).helmet({
|
|
72
|
+
* dnsPrefetchControl: { allow: true }
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
helmet(options: Omit<import('@fastify/helmet').FastifyHelmetOptions, 'global'>): RouteGroup;
|
|
77
|
+
/**
|
|
78
|
+
* Set up rate limit options for route group method.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* Route.group(() => {
|
|
83
|
+
*
|
|
84
|
+
* }).rateLimit({
|
|
85
|
+
* max: 3,
|
|
86
|
+
* timeWindow: '1 minute'
|
|
87
|
+
* })
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
rateLimit(options: import('@fastify/rate-limit').RateLimitOptions): RouteGroup;
|
|
91
|
+
/**
|
|
92
|
+
* Invokes a given method with params on the route instance or route
|
|
93
|
+
* resource instance.
|
|
94
|
+
*/
|
|
95
|
+
private invoke;
|
|
96
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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 { RouteResource } from '#src/Router/RouteResource';
|
|
10
|
+
export class RouteGroup {
|
|
11
|
+
/**
|
|
12
|
+
* All routes registered in the group.
|
|
13
|
+
*/
|
|
14
|
+
// eslint-disable-next-line no-use-before-define
|
|
15
|
+
routes;
|
|
16
|
+
constructor(routes) {
|
|
17
|
+
this.routes = routes;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Define prefix all the routes in the group.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* Route.group(() => {
|
|
25
|
+
*
|
|
26
|
+
* }).prefix('/api/v1')
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
prefix(prefix) {
|
|
30
|
+
this.routes.forEach(route => this.invoke(route, 'prefix', [prefix]));
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Add a middleware to all routes in the group.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* Route.group(() => {
|
|
39
|
+
*
|
|
40
|
+
* }).middleware('auth')
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
middleware(middleware) {
|
|
44
|
+
this.routes.forEach(route => this.invoke(route, 'middleware', [middleware, true]));
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Add an interceptor to all routes in the group.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* Route.group(() => {
|
|
53
|
+
*
|
|
54
|
+
* }).interceptor('response')
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
interceptor(interceptor) {
|
|
58
|
+
this.routes.forEach(route => this.invoke(route, 'interceptor', [interceptor, true]));
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Add a terminator to all routes in the group.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* Route.group(() => {
|
|
67
|
+
*
|
|
68
|
+
* }).terminator('log')
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
terminator(terminator) {
|
|
72
|
+
this.routes.forEach(route => this.invoke(route, 'terminator', [terminator, true]));
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Set up helmet options for route group.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* Route.group(() => {
|
|
81
|
+
*
|
|
82
|
+
* }).helmet({
|
|
83
|
+
* dnsPrefetchControl: { allow: true }
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
helmet(options) {
|
|
88
|
+
this.routes.forEach(route => this.invoke(route, 'helmet', [options]));
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Set up rate limit options for route group method.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* Route.group(() => {
|
|
97
|
+
*
|
|
98
|
+
* }).rateLimit({
|
|
99
|
+
* max: 3,
|
|
100
|
+
* timeWindow: '1 minute'
|
|
101
|
+
* })
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
rateLimit(options) {
|
|
105
|
+
this.routes.forEach(route => this.invoke(route, 'rateLimit', [options]));
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Invokes a given method with params on the route instance or route
|
|
110
|
+
* resource instance.
|
|
111
|
+
*/
|
|
112
|
+
invoke(route, method, params) {
|
|
113
|
+
if (route instanceof RouteResource) {
|
|
114
|
+
route.routes.forEach(child => this.invoke(child, method, params));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (route instanceof RouteGroup) {
|
|
118
|
+
route.routes.forEach(child => this.invoke(child, method, params));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
route[method](...params);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
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 { Route } from '#src/Router/Route';
|
|
10
|
+
import { TerminatorRouteType } from '#src/Types/Middlewares/TerminatorRouteType';
|
|
11
|
+
import { MiddlewareRouteType } from '#src/Types/Middlewares/MiddlewareRouteType';
|
|
12
|
+
import { InterceptorRouteType } from '#src/Types/Middlewares/InterceptorRouteType';
|
|
13
|
+
import { RouteResourceTypes } from '#src/Types/Router/RouteResourceTypes';
|
|
14
|
+
export declare class RouteResource {
|
|
15
|
+
/**
|
|
16
|
+
* All routes registered in the resource.
|
|
17
|
+
*/
|
|
18
|
+
routes: Route[];
|
|
19
|
+
/**
|
|
20
|
+
* The resource name.
|
|
21
|
+
*/
|
|
22
|
+
resource: string;
|
|
23
|
+
/**
|
|
24
|
+
* The resource controller.
|
|
25
|
+
*/
|
|
26
|
+
controller: any;
|
|
27
|
+
constructor(resource: string, controller: any);
|
|
28
|
+
/**
|
|
29
|
+
* Set a middleware for the route resource.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* Route.resource('/test', 'TestController').middleware('auth')
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
middleware(middleware: MiddlewareRouteType, prepend?: boolean): RouteResource;
|
|
37
|
+
/**
|
|
38
|
+
* Set a interceptor for the route resource.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* Route.resource('/test', 'TestController').interceptor('response')
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
interceptor(interceptor: InterceptorRouteType, prepend?: boolean): RouteResource;
|
|
46
|
+
/**
|
|
47
|
+
* Set a terminator for the route resource.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* Route.resource('/test', 'TestController').terminator('response')
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
terminator(terminator: TerminatorRouteType, prepend?: boolean): RouteResource;
|
|
55
|
+
/**
|
|
56
|
+
* Register only the methods in the array.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* Route.resource('/test', 'TestController').only(['index'])
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
only(names: RouteResourceTypes[]): RouteResource;
|
|
64
|
+
/**
|
|
65
|
+
* Register all methods except the methods in the array.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* Route.resource('/test', 'TestController').except(['index'])
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
except(names: RouteResourceTypes[]): RouteResource;
|
|
73
|
+
/**
|
|
74
|
+
* Set up helmet options for route resource.
|
|
75
|
+
*
|
|
76
|
+
*@example
|
|
77
|
+
* ```ts
|
|
78
|
+
* Route.helmet({
|
|
79
|
+
* dnsPrefetchControl: { allow: true }
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
helmet(options: Omit<import('@fastify/helmet').FastifyHelmetOptions, 'global'>): RouteResource;
|
|
84
|
+
/**
|
|
85
|
+
* Set up rate limit options for route resource method.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* Route.rateLimit({
|
|
90
|
+
* max: 3,
|
|
91
|
+
* timeWindow: '1 minute'
|
|
92
|
+
* })
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
rateLimit(options: import('@fastify/rate-limit').RateLimitOptions): RouteResource;
|
|
96
|
+
/**
|
|
97
|
+
* Filter routes by name.
|
|
98
|
+
*/
|
|
99
|
+
private filter;
|
|
100
|
+
/**
|
|
101
|
+
* Create the route.
|
|
102
|
+
*/
|
|
103
|
+
private makeRoute;
|
|
104
|
+
/**
|
|
105
|
+
* Create all the routes.
|
|
106
|
+
*/
|
|
107
|
+
private buildRoutes;
|
|
108
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
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 { Route } from '#src/Router/Route';
|
|
10
|
+
import { Is, String } from '@athenna/common';
|
|
11
|
+
export class RouteResource {
|
|
12
|
+
/**
|
|
13
|
+
* All routes registered in the resource.
|
|
14
|
+
*/
|
|
15
|
+
routes = [];
|
|
16
|
+
/**
|
|
17
|
+
* The resource name.
|
|
18
|
+
*/
|
|
19
|
+
resource;
|
|
20
|
+
/**
|
|
21
|
+
* The resource controller.
|
|
22
|
+
*/
|
|
23
|
+
controller;
|
|
24
|
+
constructor(resource, controller) {
|
|
25
|
+
this.resource = resource;
|
|
26
|
+
this.controller = controller;
|
|
27
|
+
this.buildRoutes();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Set a middleware for the route resource.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* Route.resource('/test', 'TestController').middleware('auth')
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
middleware(middleware, prepend) {
|
|
38
|
+
this.routes.forEach(route => route.middleware(middleware, prepend));
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Set a interceptor for the route resource.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* Route.resource('/test', 'TestController').interceptor('response')
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
interceptor(interceptor, prepend) {
|
|
50
|
+
this.routes.forEach(route => route.interceptor(interceptor, prepend));
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Set a terminator for the route resource.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* Route.resource('/test', 'TestController').terminator('response')
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
terminator(terminator, prepend) {
|
|
62
|
+
this.routes.forEach(route => route.terminator(terminator, prepend));
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Register only the methods in the array.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* Route.resource('/test', 'TestController').only(['index'])
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
only(names) {
|
|
74
|
+
this.filter(names, true).forEach(route => (route.route.deleted = true));
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Register all methods except the methods in the array.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* Route.resource('/test', 'TestController').except(['index'])
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
except(names) {
|
|
86
|
+
this.filter(names, false).forEach(route => (route.route.deleted = true));
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Set up helmet options for route resource.
|
|
91
|
+
*
|
|
92
|
+
*@example
|
|
93
|
+
* ```ts
|
|
94
|
+
* Route.helmet({
|
|
95
|
+
* dnsPrefetchControl: { allow: true }
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
helmet(options) {
|
|
100
|
+
this.routes.forEach(route => route.helmet(options));
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Set up rate limit options for route resource method.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* Route.rateLimit({
|
|
109
|
+
* max: 3,
|
|
110
|
+
* timeWindow: '1 minute'
|
|
111
|
+
* })
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
rateLimit(options) {
|
|
115
|
+
this.routes.forEach(route => route.rateLimit(options));
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Filter routes by name.
|
|
120
|
+
*/
|
|
121
|
+
filter(names, inverse = false) {
|
|
122
|
+
return this.routes.filter(route => {
|
|
123
|
+
const match = names.find(name => route.route.name.endsWith(name));
|
|
124
|
+
return inverse ? !match : match;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Create the route.
|
|
129
|
+
*/
|
|
130
|
+
makeRoute(url, methods, action) {
|
|
131
|
+
let name = '';
|
|
132
|
+
let handler = '';
|
|
133
|
+
if (Is.String(this.controller)) {
|
|
134
|
+
name = `${this.controller}.${action}`;
|
|
135
|
+
handler = `${this.controller}.${action}`;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
name = `${this.controller.constructor.name}.${action}`;
|
|
139
|
+
handler = this.controller[action];
|
|
140
|
+
}
|
|
141
|
+
this.routes.push(new Route(url, methods, handler).name(name));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create all the routes.
|
|
145
|
+
*/
|
|
146
|
+
buildRoutes() {
|
|
147
|
+
const resourceTokens = this.resource.split('.');
|
|
148
|
+
const mainResource = resourceTokens.pop();
|
|
149
|
+
const fullUrl = `${resourceTokens
|
|
150
|
+
.map(string => `${string}/:${String.toSnakeCase(String.singularize(string))}_id`)
|
|
151
|
+
.join('/')}/${mainResource}`;
|
|
152
|
+
this.makeRoute(fullUrl, ['GET'], 'index');
|
|
153
|
+
this.makeRoute(fullUrl, ['POST'], 'store');
|
|
154
|
+
this.makeRoute(`${fullUrl}/:id`, ['GET'], 'show');
|
|
155
|
+
this.makeRoute(`${fullUrl}/:id`, ['PUT', 'PATCH'], 'update');
|
|
156
|
+
this.makeRoute(`${fullUrl}/:id`, ['DELETE'], 'delete');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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 { Route } from '#src/Router/Route';
|
|
10
|
+
import { RouteGroup } from '#src/Router/RouteGroup';
|
|
11
|
+
import { RouteJSON } from '#src/Types/Router/RouteJSON';
|
|
12
|
+
import { RouteResource } from '#src/Router/RouteResource';
|
|
13
|
+
import { RequestHandler } from '#src/Types/Contexts/Context';
|
|
14
|
+
import { RouteHandler } from '#src/Types/Router/RouteHandler';
|
|
15
|
+
import { HTTPMethods, FastifyInstance, RouteOptions } from 'fastify';
|
|
16
|
+
export declare class Router {
|
|
17
|
+
/**
|
|
18
|
+
* All routes registered.
|
|
19
|
+
*/
|
|
20
|
+
routes: (Route | RouteGroup | RouteResource)[];
|
|
21
|
+
/**
|
|
22
|
+
* Route groups opened.
|
|
23
|
+
*/
|
|
24
|
+
private openedGroups;
|
|
25
|
+
/**
|
|
26
|
+
* The controller instance.
|
|
27
|
+
*/
|
|
28
|
+
private controllerInstance;
|
|
29
|
+
/**
|
|
30
|
+
* List the routes registered.
|
|
31
|
+
*/
|
|
32
|
+
list(): RouteJSON[];
|
|
33
|
+
/**
|
|
34
|
+
* Set the controller instance.
|
|
35
|
+
*/
|
|
36
|
+
controller(controller: any): this;
|
|
37
|
+
/**
|
|
38
|
+
* Add route for a given pattern and methods
|
|
39
|
+
*/
|
|
40
|
+
route(pattern: string, methods: HTTPMethods[], handler: RouteHandler): Route;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a vanilla fastify route without using Athenna router.
|
|
43
|
+
*/
|
|
44
|
+
vanillaRoute(options?: RouteOptions): FastifyInstance;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new route resource.
|
|
47
|
+
*/
|
|
48
|
+
resource(resource: string, controller?: any): RouteResource;
|
|
49
|
+
/**
|
|
50
|
+
* Define a route that handles all common HTTP methods.
|
|
51
|
+
*/
|
|
52
|
+
any(pattern: string, handler: RequestHandler): Route;
|
|
53
|
+
/**
|
|
54
|
+
* Define GET route.
|
|
55
|
+
*/
|
|
56
|
+
get(pattern: string, handler: RouteHandler): Route;
|
|
57
|
+
/**
|
|
58
|
+
* Define HEAD route.
|
|
59
|
+
*/
|
|
60
|
+
head(pattern: string, handler: RouteHandler): Route;
|
|
61
|
+
/**
|
|
62
|
+
* Define POST route.
|
|
63
|
+
*/
|
|
64
|
+
post(pattern: string, handler: RouteHandler): Route;
|
|
65
|
+
/**
|
|
66
|
+
* Define PUT route.
|
|
67
|
+
*/
|
|
68
|
+
put(pattern: string, handler: RouteHandler): Route;
|
|
69
|
+
/**
|
|
70
|
+
* Define PATCH route.
|
|
71
|
+
*/
|
|
72
|
+
patch(pattern: string, handler: RouteHandler): Route;
|
|
73
|
+
/**
|
|
74
|
+
* Define DELETE route.
|
|
75
|
+
*/
|
|
76
|
+
delete(pattern: string, handler: RouteHandler): Route;
|
|
77
|
+
/**
|
|
78
|
+
* Define OPTIONS route.
|
|
79
|
+
*/
|
|
80
|
+
options(pattern: string, handler: RouteHandler): Route;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a group of routes. Anything applied in route groups will be applied
|
|
83
|
+
* in the routes that are inside that group.
|
|
84
|
+
*/
|
|
85
|
+
group(callback: () => void): RouteGroup;
|
|
86
|
+
/**
|
|
87
|
+
* Register all the routes inside the http server. After routes are registered,
|
|
88
|
+
* anyone could be registered anymore.
|
|
89
|
+
*/
|
|
90
|
+
register(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Transform some route array to a route json array.
|
|
93
|
+
*/
|
|
94
|
+
toJSON(routes: (Route | RouteGroup | RouteResource)[]): RouteJSON[];
|
|
95
|
+
/**
|
|
96
|
+
* Get the most recent route group created.
|
|
97
|
+
*/
|
|
98
|
+
private getRecentGroup;
|
|
99
|
+
/**
|
|
100
|
+
* Indicates if if a valid controller handler method.
|
|
101
|
+
*/
|
|
102
|
+
private isValidControllerHandler;
|
|
103
|
+
}
|