@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,188 @@
|
|
|
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 } from '@athenna/common';
|
|
10
|
+
import { Route } from '#src/Router/Route';
|
|
11
|
+
import { Server } from '#src/Facades/Server';
|
|
12
|
+
import { RouteGroup } from '#src/Router/RouteGroup';
|
|
13
|
+
import { RouteResource } from '#src/Router/RouteResource';
|
|
14
|
+
import { UndefinedMethodException } from '#src/Exceptions/UndefinedMethodException';
|
|
15
|
+
export class Router {
|
|
16
|
+
/**
|
|
17
|
+
* All routes registered.
|
|
18
|
+
*/
|
|
19
|
+
routes = [];
|
|
20
|
+
/**
|
|
21
|
+
* Route groups opened.
|
|
22
|
+
*/
|
|
23
|
+
openedGroups = [];
|
|
24
|
+
/**
|
|
25
|
+
* The controller instance.
|
|
26
|
+
*/
|
|
27
|
+
controllerInstance;
|
|
28
|
+
/**
|
|
29
|
+
* List the routes registered.
|
|
30
|
+
*/
|
|
31
|
+
list() {
|
|
32
|
+
return this.toJSON(this.routes);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Set the controller instance.
|
|
36
|
+
*/
|
|
37
|
+
controller(controller) {
|
|
38
|
+
if (Is.String(controller)) {
|
|
39
|
+
controller = ioc.safeUse(`App/Http/Controllers/${controller}`);
|
|
40
|
+
}
|
|
41
|
+
this.controllerInstance = controller;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Add route for a given pattern and methods
|
|
46
|
+
*/
|
|
47
|
+
route(pattern, methods, handler) {
|
|
48
|
+
if (this.isValidControllerHandler(handler)) {
|
|
49
|
+
if (!this.controllerInstance[handler]) {
|
|
50
|
+
throw new UndefinedMethodException(handler, this.controllerInstance.name);
|
|
51
|
+
}
|
|
52
|
+
handler = this.controllerInstance[handler];
|
|
53
|
+
}
|
|
54
|
+
const route = new Route(pattern, methods, handler);
|
|
55
|
+
const openedGroup = this.getRecentGroup();
|
|
56
|
+
if (openedGroup) {
|
|
57
|
+
openedGroup.routes.push(route);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.routes.push(route);
|
|
61
|
+
}
|
|
62
|
+
return route;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Creates a vanilla fastify route without using Athenna router.
|
|
66
|
+
*/
|
|
67
|
+
vanillaRoute(options) {
|
|
68
|
+
return Server.fastify.route(options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new route resource.
|
|
72
|
+
*/
|
|
73
|
+
resource(resource, controller) {
|
|
74
|
+
const resourceInstance = new RouteResource(resource, controller || this.controllerInstance);
|
|
75
|
+
const openedGroup = this.getRecentGroup();
|
|
76
|
+
if (openedGroup) {
|
|
77
|
+
openedGroup.routes.push(resourceInstance);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
this.routes.push(resourceInstance);
|
|
81
|
+
}
|
|
82
|
+
return resourceInstance;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Define a route that handles all common HTTP methods.
|
|
86
|
+
*/
|
|
87
|
+
any(pattern, handler) {
|
|
88
|
+
return this.route(pattern, ['HEAD', 'OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'], handler);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Define GET route.
|
|
92
|
+
*/
|
|
93
|
+
get(pattern, handler) {
|
|
94
|
+
return this.route(pattern, ['GET', 'HEAD'], handler);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Define HEAD route.
|
|
98
|
+
*/
|
|
99
|
+
head(pattern, handler) {
|
|
100
|
+
return this.route(pattern, ['HEAD'], handler);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Define POST route.
|
|
104
|
+
*/
|
|
105
|
+
post(pattern, handler) {
|
|
106
|
+
return this.route(pattern, ['POST'], handler);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Define PUT route.
|
|
110
|
+
*/
|
|
111
|
+
put(pattern, handler) {
|
|
112
|
+
return this.route(pattern, ['PUT'], handler);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Define PATCH route.
|
|
116
|
+
*/
|
|
117
|
+
patch(pattern, handler) {
|
|
118
|
+
return this.route(pattern, ['PATCH'], handler);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Define DELETE route.
|
|
122
|
+
*/
|
|
123
|
+
delete(pattern, handler) {
|
|
124
|
+
return this.route(pattern, ['DELETE'], handler);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Define OPTIONS route.
|
|
128
|
+
*/
|
|
129
|
+
options(pattern, handler) {
|
|
130
|
+
return this.route(pattern, ['OPTIONS'], handler);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates a group of routes. Anything applied in route groups will be applied
|
|
134
|
+
* in the routes that are inside that group.
|
|
135
|
+
*/
|
|
136
|
+
group(callback) {
|
|
137
|
+
const group = new RouteGroup([]);
|
|
138
|
+
const openedGroup = this.getRecentGroup();
|
|
139
|
+
if (openedGroup) {
|
|
140
|
+
openedGroup.routes.push(group);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.routes.push(group);
|
|
144
|
+
}
|
|
145
|
+
this.openedGroups.push(group);
|
|
146
|
+
callback();
|
|
147
|
+
this.openedGroups.pop();
|
|
148
|
+
return group;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Register all the routes inside the http server. After routes are registered,
|
|
152
|
+
* anyone could be registered anymore.
|
|
153
|
+
*/
|
|
154
|
+
register() {
|
|
155
|
+
this.toJSON(this.routes).forEach(route => Server.route(route));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Transform some route array to a route json array.
|
|
159
|
+
*/
|
|
160
|
+
toJSON(routes) {
|
|
161
|
+
return routes.reduce((list, route) => {
|
|
162
|
+
if (route instanceof RouteGroup) {
|
|
163
|
+
list = list.concat(this.toJSON(route.routes));
|
|
164
|
+
return list;
|
|
165
|
+
}
|
|
166
|
+
if (route instanceof RouteResource) {
|
|
167
|
+
list = list.concat(this.toJSON(route.routes));
|
|
168
|
+
return list;
|
|
169
|
+
}
|
|
170
|
+
if (!route.route.deleted) {
|
|
171
|
+
list.push(route.toJSON());
|
|
172
|
+
}
|
|
173
|
+
return list;
|
|
174
|
+
}, []);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Get the most recent route group created.
|
|
178
|
+
*/
|
|
179
|
+
getRecentGroup() {
|
|
180
|
+
return this.openedGroups[this.openedGroups.length - 1];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Indicates if if a valid controller handler method.
|
|
184
|
+
*/
|
|
185
|
+
isValidControllerHandler(handler) {
|
|
186
|
+
return (this.controllerInstance && Is.String(handler) && !handler.includes('.'));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
10
|
+
import { InjectOptions, FastifyInstance, PrintRoutesOptions, FastifyListenOptions, FastifyServerOptions, LightMyRequestChain, LightMyRequestResponse } from 'fastify';
|
|
11
|
+
import { InterceptHandler, TerminateHandler } from '#src/Types/Middlewares/MiddlewareHandler';
|
|
12
|
+
import { AddressInfo } from 'node:net';
|
|
13
|
+
import { RouteJSON } from '#src/Types/Router/RouteJSON';
|
|
14
|
+
import { RequestHandler } from '#src/Types/Contexts/Context';
|
|
15
|
+
import { ErrorHandler } from '#src/Types/Contexts/ErrorContext';
|
|
16
|
+
export declare class ServerImpl {
|
|
17
|
+
/**
|
|
18
|
+
* Holds the fastify server instance.
|
|
19
|
+
*/
|
|
20
|
+
fastify: FastifyInstance;
|
|
21
|
+
/**
|
|
22
|
+
* Set if the Http server is listening for new requests.
|
|
23
|
+
*/
|
|
24
|
+
isListening: boolean;
|
|
25
|
+
constructor(options?: FastifyServerOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Get the representation of the internal radix tree used by the
|
|
28
|
+
* router.
|
|
29
|
+
*/
|
|
30
|
+
getRoutes(options?: PrintRoutesOptions): string;
|
|
31
|
+
/**
|
|
32
|
+
* Get the address info of the server. This method will return the
|
|
33
|
+
* port used to listen the server, the family (IPv4, IPv6) and the
|
|
34
|
+
* server address (127.0.0.1).
|
|
35
|
+
*/
|
|
36
|
+
getAddressInfo(): AddressInfo;
|
|
37
|
+
/**
|
|
38
|
+
* Get the port where the server is running.
|
|
39
|
+
*/
|
|
40
|
+
getPort(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Get the host where the server is running.
|
|
43
|
+
*/
|
|
44
|
+
getHost(): string;
|
|
45
|
+
/**
|
|
46
|
+
* Get the fastify version that is running the server.
|
|
47
|
+
*/
|
|
48
|
+
getFastifyVersion(): string;
|
|
49
|
+
getSwagger(): import('openapi-types').OpenAPI.Document;
|
|
50
|
+
getSwagger(options: {
|
|
51
|
+
yaml: true;
|
|
52
|
+
}): string;
|
|
53
|
+
getSwagger(options: {
|
|
54
|
+
yaml: false;
|
|
55
|
+
}): import('openapi-types').OpenAPI.Document;
|
|
56
|
+
/**
|
|
57
|
+
* Set the error handler to handle errors that happens inside the server.
|
|
58
|
+
*/
|
|
59
|
+
setErrorHandler(handler: ErrorHandler): ServerImpl;
|
|
60
|
+
/**
|
|
61
|
+
* Register a plugin inside the fastify server.
|
|
62
|
+
*/
|
|
63
|
+
plugin(plugin: any, options?: any): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Create a middleware that will be executed before the request gets
|
|
66
|
+
* inside the route handler.
|
|
67
|
+
*/
|
|
68
|
+
middleware(handler: RequestHandler): ServerImpl;
|
|
69
|
+
/**
|
|
70
|
+
* Create an interceptor that will be executed before the response
|
|
71
|
+
* is returned. At this point you can still make modifications in the
|
|
72
|
+
* response.
|
|
73
|
+
*/
|
|
74
|
+
intercept(handler: InterceptHandler): ServerImpl;
|
|
75
|
+
/**
|
|
76
|
+
* Create and terminator that will be executed after the response
|
|
77
|
+
* is returned. At this point you cannot make modifications in the
|
|
78
|
+
* response.
|
|
79
|
+
*/
|
|
80
|
+
terminate(handler: TerminateHandler): ServerImpl;
|
|
81
|
+
/**
|
|
82
|
+
* Return a request handler to make internal requests to the Http server.
|
|
83
|
+
*/
|
|
84
|
+
request(): LightMyRequestChain;
|
|
85
|
+
/**
|
|
86
|
+
* Return a request handler to make internal requests to the Http server.
|
|
87
|
+
*/
|
|
88
|
+
request(options: InjectOptions): Promise<LightMyRequestResponse>;
|
|
89
|
+
/**
|
|
90
|
+
* Make the server start listening for requests.
|
|
91
|
+
*/
|
|
92
|
+
listen(options: FastifyListenOptions): Promise<any>;
|
|
93
|
+
/**
|
|
94
|
+
* Close the server,
|
|
95
|
+
*/
|
|
96
|
+
close(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Add a new route to the http server.
|
|
99
|
+
*/
|
|
100
|
+
route(options: RouteJSON): void;
|
|
101
|
+
/**
|
|
102
|
+
* Add a new GET route to the http server.
|
|
103
|
+
*/
|
|
104
|
+
get(options: Omit<RouteJSON, 'methods'>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Add a new HEAD route to the http server.
|
|
107
|
+
*/
|
|
108
|
+
head(options: Omit<RouteJSON, 'methods'>): void;
|
|
109
|
+
/**
|
|
110
|
+
* Add a new POST route to the http server.
|
|
111
|
+
*/
|
|
112
|
+
post(options: Omit<RouteJSON, 'methods'>): void;
|
|
113
|
+
/**
|
|
114
|
+
* Add a new PUT route to the http server.
|
|
115
|
+
*/
|
|
116
|
+
put(options: Omit<RouteJSON, 'methods'>): void;
|
|
117
|
+
/**
|
|
118
|
+
* Add a new PATCH route to the http server.
|
|
119
|
+
*/
|
|
120
|
+
patch(options: Omit<RouteJSON, 'methods'>): void;
|
|
121
|
+
/**
|
|
122
|
+
* Add a new DELETE route to the http server.
|
|
123
|
+
*/
|
|
124
|
+
delete(options: Omit<RouteJSON, 'methods'>): void;
|
|
125
|
+
/**
|
|
126
|
+
* Add a new OPTIONS route to the http server.
|
|
127
|
+
*/
|
|
128
|
+
options(options: Omit<RouteJSON, 'methods'>): void;
|
|
129
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
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 fastify from 'fastify';
|
|
10
|
+
import { Options } from '@athenna/common';
|
|
11
|
+
import { FastifyHandler } from '#src/Handlers/FastifyHandler';
|
|
12
|
+
export class ServerImpl {
|
|
13
|
+
/**
|
|
14
|
+
* Holds the fastify server instance.
|
|
15
|
+
*/
|
|
16
|
+
fastify;
|
|
17
|
+
/**
|
|
18
|
+
* Set if the Http server is listening for new requests.
|
|
19
|
+
*/
|
|
20
|
+
isListening;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.fastify = fastify.fastify(options);
|
|
23
|
+
this.isListening = false;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the representation of the internal radix tree used by the
|
|
27
|
+
* router.
|
|
28
|
+
*/
|
|
29
|
+
getRoutes(options) {
|
|
30
|
+
return this.fastify.printRoutes(options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the address info of the server. This method will return the
|
|
34
|
+
* port used to listen the server, the family (IPv4, IPv6) and the
|
|
35
|
+
* server address (127.0.0.1).
|
|
36
|
+
*/
|
|
37
|
+
getAddressInfo() {
|
|
38
|
+
return this.fastify.server.address();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get the port where the server is running.
|
|
42
|
+
*/
|
|
43
|
+
getPort() {
|
|
44
|
+
return this.getAddressInfo()?.port;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get the host where the server is running.
|
|
48
|
+
*/
|
|
49
|
+
getHost() {
|
|
50
|
+
return this.getAddressInfo()?.address;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get the fastify version that is running the server.
|
|
54
|
+
*/
|
|
55
|
+
getFastifyVersion() {
|
|
56
|
+
return this.fastify.version;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Return the swagger documentation generated by routes if using the
|
|
60
|
+
* @fastify/swagger plugin in the server. If the plugin is not installed
|
|
61
|
+
* will return null.
|
|
62
|
+
*/
|
|
63
|
+
getSwagger(options) {
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
const swagger = this.fastify.swagger;
|
|
67
|
+
if (!swagger) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return swagger(options);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Set the error handler to handle errors that happens inside the server.
|
|
74
|
+
*/
|
|
75
|
+
setErrorHandler(handler) {
|
|
76
|
+
this.fastify.setErrorHandler(FastifyHandler.error(handler));
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Register a plugin inside the fastify server.
|
|
81
|
+
*/
|
|
82
|
+
async plugin(plugin, options) {
|
|
83
|
+
await this.fastify.register(plugin, options);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a middleware that will be executed before the request gets
|
|
87
|
+
* inside the route handler.
|
|
88
|
+
*/
|
|
89
|
+
middleware(handler) {
|
|
90
|
+
this.fastify.addHook('preHandler', FastifyHandler.handle(handler));
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Create an interceptor that will be executed before the response
|
|
95
|
+
* is returned. At this point you can still make modifications in the
|
|
96
|
+
* response.
|
|
97
|
+
*/
|
|
98
|
+
intercept(handler) {
|
|
99
|
+
this.fastify.addHook('onSend', FastifyHandler.intercept(handler));
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Create and terminator that will be executed after the response
|
|
104
|
+
* is returned. At this point you cannot make modifications in the
|
|
105
|
+
* response.
|
|
106
|
+
*/
|
|
107
|
+
terminate(handler) {
|
|
108
|
+
this.fastify.addHook('onResponse', FastifyHandler.terminate(handler));
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Return a request handler to make internal requests to the Http server.
|
|
113
|
+
*/
|
|
114
|
+
request(options) {
|
|
115
|
+
if (!options) {
|
|
116
|
+
return this.fastify.inject();
|
|
117
|
+
}
|
|
118
|
+
return this.fastify.inject(options);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Make the server start listening for requests.
|
|
122
|
+
*/
|
|
123
|
+
async listen(options) {
|
|
124
|
+
return this.fastify.listen(options).then(() => (this.isListening = true));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Close the server,
|
|
128
|
+
*/
|
|
129
|
+
async close() {
|
|
130
|
+
if (!this.isListening) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
await this.fastify.close().then(() => (this.isListening = true));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Add a new route to the http server.
|
|
137
|
+
*/
|
|
138
|
+
route(options) {
|
|
139
|
+
if (!options.middlewares) {
|
|
140
|
+
options.middlewares = {};
|
|
141
|
+
}
|
|
142
|
+
options.middlewares = Options.create(options.middlewares, {
|
|
143
|
+
middlewares: [],
|
|
144
|
+
terminators: [],
|
|
145
|
+
interceptors: [],
|
|
146
|
+
});
|
|
147
|
+
if (options.methods.length === 2 && options.methods.includes('HEAD')) {
|
|
148
|
+
this.route({ ...options, methods: ['GET'] });
|
|
149
|
+
this.route({
|
|
150
|
+
...options,
|
|
151
|
+
methods: ['HEAD'],
|
|
152
|
+
fastify: {
|
|
153
|
+
...options.fastify,
|
|
154
|
+
schema: { ...options.fastify.schema, hide: true },
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.fastify.route({
|
|
160
|
+
url: options.url,
|
|
161
|
+
method: options.methods,
|
|
162
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
163
|
+
// @ts-ignore
|
|
164
|
+
handler: FastifyHandler.request(options.handler),
|
|
165
|
+
preHandler: options.middlewares.middlewares.map(m => FastifyHandler.handle(m)),
|
|
166
|
+
onSend: options.middlewares.interceptors.map(m => FastifyHandler.intercept(m)),
|
|
167
|
+
onResponse: options.middlewares.terminators.map(m => FastifyHandler.terminate(m)),
|
|
168
|
+
...options.fastify,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Add a new GET route to the http server.
|
|
173
|
+
*/
|
|
174
|
+
get(options) {
|
|
175
|
+
this.route({ ...options, methods: ['GET'] });
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Add a new HEAD route to the http server.
|
|
179
|
+
*/
|
|
180
|
+
head(options) {
|
|
181
|
+
this.route({ ...options, methods: ['HEAD'] });
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Add a new POST route to the http server.
|
|
185
|
+
*/
|
|
186
|
+
post(options) {
|
|
187
|
+
this.route({ ...options, methods: ['POST'] });
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Add a new PUT route to the http server.
|
|
191
|
+
*/
|
|
192
|
+
put(options) {
|
|
193
|
+
this.route({ ...options, methods: ['PUT'] });
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Add a new PATCH route to the http server.
|
|
197
|
+
*/
|
|
198
|
+
patch(options) {
|
|
199
|
+
this.route({ ...options, methods: ['PATCH'] });
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Add a new DELETE route to the http server.
|
|
203
|
+
*/
|
|
204
|
+
delete(options) {
|
|
205
|
+
this.route({ ...options, methods: ['DELETE'] });
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Add a new OPTIONS route to the http server.
|
|
209
|
+
*/
|
|
210
|
+
options(options) {
|
|
211
|
+
this.route({ ...options, methods: ['OPTIONS'] });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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 { Request } from '#src/Context/Request';
|
|
10
|
+
import { Response } from '#src/Context/Response';
|
|
11
|
+
export type Context = {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
data: any;
|
|
15
|
+
body: any;
|
|
16
|
+
params: any;
|
|
17
|
+
queries: any;
|
|
18
|
+
headers: any;
|
|
19
|
+
};
|
|
20
|
+
export type RequestHandler = (ctx: Context) => any | Promise<any>;
|
|
@@ -0,0 +1,21 @@
|
|
|
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 { Request } from '#src/Context/Request';
|
|
10
|
+
import { Response } from '#src/Context/Response';
|
|
11
|
+
export type ErrorContext = {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
data: any;
|
|
15
|
+
body: any;
|
|
16
|
+
error: any;
|
|
17
|
+
params: any;
|
|
18
|
+
queries: any;
|
|
19
|
+
headers: any;
|
|
20
|
+
};
|
|
21
|
+
export type ErrorHandler = (ctx: ErrorContext) => Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
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 { Request } from '#src/Context/Request';
|
|
10
|
+
import { Response } from '#src/Context/Response';
|
|
11
|
+
export type InterceptContext = {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
data: any;
|
|
15
|
+
body: any;
|
|
16
|
+
params: any;
|
|
17
|
+
queries: any;
|
|
18
|
+
headers: any;
|
|
19
|
+
status: number;
|
|
20
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
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 { Request } from '#src/Context/Request';
|
|
10
|
+
import { Response } from '#src/Context/Response';
|
|
11
|
+
export type TerminateContext = {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
data: any;
|
|
15
|
+
body: any;
|
|
16
|
+
params: any;
|
|
17
|
+
queries: any;
|
|
18
|
+
headers: any;
|
|
19
|
+
status: number;
|
|
20
|
+
responseTime: number;
|
|
21
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export type ControllerOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* The alias that will be used to register the dependency inside
|
|
12
|
+
* the service provider. Athenna will not create camel alias from
|
|
13
|
+
* the alias set here.
|
|
14
|
+
*
|
|
15
|
+
* @default App/Http/Controllers/YourControllerClassName
|
|
16
|
+
*/
|
|
17
|
+
alias?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The registration type that will be used to register your controller
|
|
20
|
+
* inside the service provider.
|
|
21
|
+
*
|
|
22
|
+
* @default transient
|
|
23
|
+
*/
|
|
24
|
+
type?: 'fake' | 'scoped' | 'singleton' | 'transient';
|
|
25
|
+
};
|