@athenna/http 1.0.6 → 1.0.9
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/package.json +4 -4
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +1 -3
- package/src/Contracts/MiddlewareContract.d.ts +1 -1
- package/src/{Contracts/Context/Middlewares/Intercept/NextInterceptContract.d.ts → Exceptions/UndefinedControllerMethodException.d.ts} +3 -2
- package/src/Exceptions/UndefinedControllerMethodException.js +19 -0
- package/src/Http.d.ts +4 -1
- package/src/Http.js +9 -2
- package/src/Providers/HttpRouteProvider.js +1 -1
- package/src/Providers/HttpServerProvider.js +1 -1
- package/src/Router/Route.d.ts +4 -14
- package/src/Router/Route.js +42 -28
- package/src/Router/RouteResource.d.ts +4 -4
- package/src/Router/RouteResource.js +26 -26
- package/src/Router/Router.d.ts +1 -1
- package/src/Router/Router.js +3 -3
- package/src/Utils/FastifyHandler.d.ts +3 -1
- package/src/Utils/FastifyHandler.js +25 -7
- package/src/Contracts/Context/Middlewares/Intercept/NextInterceptContract.js +0 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "The Athenna Http server. Built on top of fastify",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "João Lenon <lenon@athenna.io>",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"homepage": "https://github.com/AthennaIO/Http#readme",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s . -o .",
|
|
12
|
-
"test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose",
|
|
13
|
-
"test:debug": "
|
|
12
|
+
"test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose --runInBand",
|
|
13
|
+
"test:debug": "cross-env DEBUG=api:* && npm run test",
|
|
14
14
|
"lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
@@ -159,7 +159,7 @@
|
|
|
159
159
|
},
|
|
160
160
|
"dependencies": {
|
|
161
161
|
"@athenna/config": "1.0.5",
|
|
162
|
-
"@athenna/ioc": "1.0.
|
|
162
|
+
"@athenna/ioc": "1.0.7",
|
|
163
163
|
"@secjs/utils": "1.8.0",
|
|
164
164
|
"fastify": "3.27.4",
|
|
165
165
|
"reflect-metadata": "0.1.13",
|
|
@@ -7,13 +7,11 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { RequestContract } from '../../RequestContract';
|
|
10
|
-
import { NextInterceptContract } from './NextInterceptContract';
|
|
11
10
|
export interface InterceptContextContract {
|
|
12
11
|
request: RequestContract;
|
|
13
12
|
params: Record<string, string>;
|
|
14
13
|
queries: Record<string, string>;
|
|
15
|
-
body:
|
|
14
|
+
body: any;
|
|
16
15
|
status: number;
|
|
17
16
|
data: Record<string, any>;
|
|
18
|
-
next: NextInterceptContract;
|
|
19
17
|
}
|
|
@@ -11,6 +11,6 @@ import { InterceptContextContract } from './Context/Middlewares/Intercept/Interc
|
|
|
11
11
|
import { TerminateContextContract } from './Context/Middlewares/Terminate/TerminateContextContract';
|
|
12
12
|
export interface MiddlewareContract {
|
|
13
13
|
handle?: (ctx: HandleContextContract) => void | Promise<void>;
|
|
14
|
-
intercept?: (ctx: InterceptContextContract) =>
|
|
14
|
+
intercept?: (ctx: InterceptContextContract) => any | Promise<any>;
|
|
15
15
|
terminate?: (ctx: TerminateContextContract) => void | Promise<void>;
|
|
16
16
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
import { Exception } from '@secjs/utils';
|
|
10
|
+
export declare class UndefinedControllerMethodException extends Exception {
|
|
11
|
+
constructor(method: string, controller: string);
|
|
11
12
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @athenna/http
|
|
4
|
+
*
|
|
5
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.UndefinedControllerMethodException = void 0;
|
|
12
|
+
const utils_1 = require("@secjs/utils");
|
|
13
|
+
class UndefinedControllerMethodException extends utils_1.Exception {
|
|
14
|
+
constructor(method, controller) {
|
|
15
|
+
const content = `The method ${method} is not defined inside your controller ${controller}`;
|
|
16
|
+
super(content, 500, 'UNDEFINED_METHOD_ERROR', `Remember defining the method ${method} inside your controller ${controller}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.UndefinedControllerMethodException = UndefinedControllerMethodException;
|
package/src/Http.d.ts
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
-
import
|
|
9
|
+
import LightMyRequest from 'light-my-request';
|
|
10
|
+
import { FastifyInstance, InjectOptions, PrintRoutesOptions } from 'fastify';
|
|
10
11
|
import { HttpMethodTypes } from './Contracts/HttpMethodTypes';
|
|
11
12
|
import { HandlerContract } from './Contracts/Context/HandlerContract';
|
|
12
13
|
import { MiddlewareTypesContract } from './Contracts/MiddlewareTypesContract';
|
|
@@ -21,6 +22,8 @@ export declare class Http {
|
|
|
21
22
|
getServer(): FastifyInstance;
|
|
22
23
|
getRoutes(options?: PrintRoutesOptions): string;
|
|
23
24
|
use(handler: HandleHandlerContract | InterceptHandlerContract | TerminateHandlerContract, type?: 'handle' | 'intercept' | 'terminate'): void;
|
|
25
|
+
request(): LightMyRequest.Chain;
|
|
26
|
+
request(options: InjectOptions | string): Promise<LightMyRequest.Response>;
|
|
24
27
|
listen(port?: string | number, host?: string): Promise<string>;
|
|
25
28
|
close(cb?: any): Promise<void>;
|
|
26
29
|
route(url: string, methods: HttpMethodTypes[], handler: HandlerContract, middlewares?: MiddlewareTypesContract): void;
|
package/src/Http.js
CHANGED
|
@@ -38,11 +38,18 @@ class Http {
|
|
|
38
38
|
break;
|
|
39
39
|
case 'terminate':
|
|
40
40
|
hookName = 'onResponse';
|
|
41
|
-
handlerType = '
|
|
41
|
+
handlerType = 'createResponseHandler';
|
|
42
42
|
break;
|
|
43
43
|
}
|
|
44
44
|
this.server.addHook(hookName, FastifyHandler_1.FastifyHandler[handlerType](handler));
|
|
45
45
|
}
|
|
46
|
+
request(options) {
|
|
47
|
+
const server = this.getServer();
|
|
48
|
+
if (!options) {
|
|
49
|
+
return server.inject();
|
|
50
|
+
}
|
|
51
|
+
return server.inject(options);
|
|
52
|
+
}
|
|
46
53
|
async listen(port, host) {
|
|
47
54
|
return this.server.listen(port || 1335, host || 'localhost');
|
|
48
55
|
}
|
|
@@ -60,7 +67,7 @@ class Http {
|
|
|
60
67
|
method: methods,
|
|
61
68
|
handler: FastifyHandler_1.FastifyHandler.createRequestHandler(handler),
|
|
62
69
|
preHandler: handlers.map(m => FastifyHandler_1.FastifyHandler.createDoneHandler(m)),
|
|
63
|
-
onResponse: terminators.map(m => FastifyHandler_1.FastifyHandler.
|
|
70
|
+
onResponse: terminators.map(m => FastifyHandler_1.FastifyHandler.createResponseHandler(m)),
|
|
64
71
|
onSend: interceptors.map(m => FastifyHandler_1.FastifyHandler.createOnSendHandler(m)),
|
|
65
72
|
});
|
|
66
73
|
}
|
|
@@ -14,7 +14,7 @@ const ioc_1 = require("@athenna/ioc");
|
|
|
14
14
|
class HttpRouteProvider extends ioc_1.ServiceProvider {
|
|
15
15
|
boot() {
|
|
16
16
|
const http = this.container.safeUse('Athenna/Core/HttpServer');
|
|
17
|
-
this.container.
|
|
17
|
+
this.container.instance('Athenna/Core/HttpRoute', new Router_1.Router(http));
|
|
18
18
|
}
|
|
19
19
|
register() { }
|
|
20
20
|
}
|
|
@@ -14,7 +14,7 @@ const ioc_1 = require("@athenna/ioc");
|
|
|
14
14
|
class HttpServerProvider extends ioc_1.ServiceProvider {
|
|
15
15
|
boot() { }
|
|
16
16
|
register() {
|
|
17
|
-
this.container.
|
|
17
|
+
this.container.instance('Athenna/Core/HttpServer', new Http_1.Http());
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
exports.HttpServerProvider = HttpServerProvider;
|
package/src/Router/Route.d.ts
CHANGED
|
@@ -11,32 +11,22 @@ import { MiddlewareTypes } from '../Contracts/MiddlewareTypes';
|
|
|
11
11
|
import { HttpMethodTypes } from '../Contracts/HttpMethodTypes';
|
|
12
12
|
import { MiddlewareContract } from '../Contracts/MiddlewareContract';
|
|
13
13
|
import { HandlerContract } from '../Contracts/Context/HandlerContract';
|
|
14
|
-
import { MiddlewareTypesContract } from '../Contracts/MiddlewareTypesContract';
|
|
15
14
|
import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract';
|
|
16
15
|
import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
|
|
17
16
|
export declare class Route {
|
|
17
|
+
name: string;
|
|
18
|
+
deleted: boolean;
|
|
18
19
|
private readonly url;
|
|
19
20
|
private readonly handler;
|
|
20
21
|
private readonly methods;
|
|
21
|
-
name: string;
|
|
22
|
-
deleted: boolean;
|
|
23
22
|
private routeMiddlewares;
|
|
24
23
|
private routeNamespace;
|
|
25
24
|
private prefixes;
|
|
26
25
|
constructor(url: string, methods: HttpMethodTypes[], handler: HandlerContract | string);
|
|
27
|
-
private getUrl;
|
|
28
26
|
prefix(prefix: any): this;
|
|
29
27
|
as(name: string, prepend?: boolean): this;
|
|
30
28
|
namespace(namespace: string, overwrite?: boolean): this;
|
|
31
29
|
middleware(middleware: HandlerContract | MiddlewareContract | InterceptHandlerContract | TerminateHandlerContract | string, type?: MiddlewareTypes, prepend?: boolean): this;
|
|
32
|
-
toJSON():
|
|
33
|
-
|
|
34
|
-
url: string;
|
|
35
|
-
handler: HandlerContract;
|
|
36
|
-
methods: HttpMethodTypes[];
|
|
37
|
-
middlewares: MiddlewareTypesContract;
|
|
38
|
-
meta: {
|
|
39
|
-
namespace: string;
|
|
40
|
-
};
|
|
41
|
-
};
|
|
30
|
+
toJSON(): any;
|
|
31
|
+
private getUrl;
|
|
42
32
|
}
|
package/src/Router/Route.js
CHANGED
|
@@ -14,6 +14,7 @@ const utils_1 = require("@secjs/utils");
|
|
|
14
14
|
const removeSlash_1 = require("../Utils/removeSlash");
|
|
15
15
|
const isMiddlewareContract_1 = require("../Utils/isMiddlewareContract");
|
|
16
16
|
const MiddlewareNotFoundException_1 = require("../Exceptions/MiddlewareNotFoundException");
|
|
17
|
+
const UndefinedControllerMethodException_1 = require("../Exceptions/UndefinedControllerMethodException");
|
|
17
18
|
class Route {
|
|
18
19
|
constructor(url, methods, handler) {
|
|
19
20
|
this.url = url;
|
|
@@ -22,21 +23,8 @@ class Route {
|
|
|
22
23
|
this.prefixes = [];
|
|
23
24
|
this.deleted = false;
|
|
24
25
|
this.routeMiddlewares = { handlers: [], terminators: [], interceptors: [] };
|
|
25
|
-
if (utils_1.Is.String(handler)) {
|
|
26
|
-
const [controller, method] = handler.split('.');
|
|
27
|
-
handler = ioc.use(`App/Controllers/${controller}`)[method];
|
|
28
|
-
}
|
|
29
26
|
this.handler = handler;
|
|
30
27
|
}
|
|
31
|
-
getUrl() {
|
|
32
|
-
const url = removeSlash_1.removeSlash(this.url);
|
|
33
|
-
const prefix = this.prefixes
|
|
34
|
-
.slice()
|
|
35
|
-
.reverse()
|
|
36
|
-
.map(p => removeSlash_1.removeSlash(p))
|
|
37
|
-
.join('');
|
|
38
|
-
return prefix ? `${prefix}${url === '/' ? '' : url}` : url;
|
|
39
|
-
}
|
|
40
28
|
prefix(prefix) {
|
|
41
29
|
this.prefixes.push(prefix);
|
|
42
30
|
return this;
|
|
@@ -59,41 +47,67 @@ class Route {
|
|
|
59
47
|
};
|
|
60
48
|
const insertionType = prepend ? 'unshift' : 'push';
|
|
61
49
|
if (utils_1.Is.String(middleware)) {
|
|
62
|
-
const mid = ioc.
|
|
50
|
+
const mid = ioc.safeUse(`App/Middlewares/${middleware}`);
|
|
63
51
|
if (!mid) {
|
|
64
52
|
throw new MiddlewareNotFoundException_1.MiddlewareNotFoundException(middleware);
|
|
65
53
|
}
|
|
66
|
-
if (mid.handle)
|
|
67
|
-
this.routeMiddlewares.handlers[insertionType](mid.handle);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
54
|
+
if (mid.handle) {
|
|
55
|
+
this.routeMiddlewares.handlers[insertionType](mid.handle.bind(mid));
|
|
56
|
+
}
|
|
57
|
+
if (mid.intercept) {
|
|
58
|
+
this.routeMiddlewares.interceptors[insertionType](mid.intercept.bind(mid));
|
|
59
|
+
}
|
|
60
|
+
if (mid.terminate) {
|
|
61
|
+
this.routeMiddlewares.terminators[insertionType](mid.terminate.bind(mid));
|
|
62
|
+
}
|
|
72
63
|
return this;
|
|
73
64
|
}
|
|
74
65
|
if (isMiddlewareContract_1.isMiddlewareContract(middleware)) {
|
|
75
|
-
if (middleware.handle)
|
|
76
|
-
this.routeMiddlewares.handlers[insertionType](middleware.handle);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
if (middleware.handle) {
|
|
67
|
+
this.routeMiddlewares.handlers[insertionType](middleware.handle.bind(middleware));
|
|
68
|
+
}
|
|
69
|
+
if (middleware.intercept) {
|
|
70
|
+
this.routeMiddlewares.interceptors[insertionType](middleware.intercept.bind(middleware));
|
|
71
|
+
}
|
|
72
|
+
if (middleware.terminate) {
|
|
73
|
+
this.routeMiddlewares.terminators[insertionType](middleware.terminate.bind(middleware));
|
|
74
|
+
}
|
|
81
75
|
return this;
|
|
82
76
|
}
|
|
83
77
|
this.routeMiddlewares[dictionary[type]][insertionType](middleware);
|
|
84
78
|
return this;
|
|
85
79
|
}
|
|
86
80
|
toJSON() {
|
|
87
|
-
|
|
81
|
+
const json = {
|
|
88
82
|
name: this.name,
|
|
89
83
|
url: this.getUrl(),
|
|
90
|
-
handler: this.handler,
|
|
91
84
|
methods: this.methods,
|
|
92
85
|
middlewares: this.routeMiddlewares,
|
|
93
86
|
meta: {
|
|
94
87
|
namespace: this.routeNamespace,
|
|
95
88
|
},
|
|
96
89
|
};
|
|
90
|
+
if (utils_1.Is.String(this.handler)) {
|
|
91
|
+
const [controller, method] = this.handler.split('.');
|
|
92
|
+
const dependency = ioc.safeUse(`App/Controllers/${controller}`);
|
|
93
|
+
if (!dependency[method]) {
|
|
94
|
+
throw new UndefinedControllerMethodException_1.UndefinedControllerMethodException(method, controller);
|
|
95
|
+
}
|
|
96
|
+
json.handler = dependency[method].bind(dependency);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
json.handler = this.handler;
|
|
100
|
+
}
|
|
101
|
+
return json;
|
|
102
|
+
}
|
|
103
|
+
getUrl() {
|
|
104
|
+
const url = removeSlash_1.removeSlash(this.url);
|
|
105
|
+
const prefix = this.prefixes
|
|
106
|
+
.slice()
|
|
107
|
+
.reverse()
|
|
108
|
+
.map(p => removeSlash_1.removeSlash(p))
|
|
109
|
+
.join('');
|
|
110
|
+
return prefix ? `${prefix}${url === '/' ? '' : url}` : url;
|
|
97
111
|
}
|
|
98
112
|
}
|
|
99
113
|
exports.Route = Route;
|
|
@@ -13,17 +13,17 @@ import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Inter
|
|
|
13
13
|
import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
|
|
14
14
|
import { MiddlewareContract } from '../Contracts/MiddlewareContract';
|
|
15
15
|
export declare class RouteResource {
|
|
16
|
+
routes: Route[];
|
|
16
17
|
private resource;
|
|
17
18
|
private readonly controller;
|
|
18
19
|
private resourceName;
|
|
19
|
-
routes: Route[];
|
|
20
20
|
constructor(resource: string, controller: any);
|
|
21
|
-
private makeRoute;
|
|
22
|
-
private buildRoutes;
|
|
23
|
-
private filter;
|
|
24
21
|
middleware(middleware: HandlerContract | MiddlewareContract | InterceptHandlerContract | TerminateHandlerContract | string, type?: MiddlewareTypes): this;
|
|
25
22
|
only(names: string[]): this;
|
|
26
23
|
except(names: string[]): this;
|
|
27
24
|
namespace(namespace: string): this;
|
|
28
25
|
as(name: string): this;
|
|
26
|
+
private makeRoute;
|
|
27
|
+
private buildRoutes;
|
|
28
|
+
private filter;
|
|
29
29
|
}
|
|
@@ -22,6 +22,32 @@ class RouteResource {
|
|
|
22
22
|
.join('.');
|
|
23
23
|
this.buildRoutes();
|
|
24
24
|
}
|
|
25
|
+
middleware(middleware, type = 'handle') {
|
|
26
|
+
this.routes.forEach(route => route.middleware(middleware, type));
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
only(names) {
|
|
30
|
+
this.filter(names, true).forEach(route => (route.deleted = true));
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
except(names) {
|
|
34
|
+
this.filter(names, false).forEach(route => (route.deleted = true));
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
namespace(namespace) {
|
|
38
|
+
this.routes.forEach(route => {
|
|
39
|
+
route.namespace(namespace);
|
|
40
|
+
});
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
as(name) {
|
|
44
|
+
name = utils_1.String.toSnakeCase(name);
|
|
45
|
+
this.routes.forEach(route => {
|
|
46
|
+
route.as(route.name.replace(this.resourceName, name), false);
|
|
47
|
+
});
|
|
48
|
+
this.resourceName = name;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
25
51
|
makeRoute(url, methods, action) {
|
|
26
52
|
let handler = '';
|
|
27
53
|
if (utils_1.Is.String(this.controller)) {
|
|
@@ -54,31 +80,5 @@ class RouteResource {
|
|
|
54
80
|
return inverse ? !match : match;
|
|
55
81
|
});
|
|
56
82
|
}
|
|
57
|
-
middleware(middleware, type = 'handle') {
|
|
58
|
-
this.routes.forEach(route => route.middleware(middleware, type));
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
only(names) {
|
|
62
|
-
this.filter(names, true).forEach(route => (route.deleted = true));
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
except(names) {
|
|
66
|
-
this.filter(names, false).forEach(route => (route.deleted = true));
|
|
67
|
-
return this;
|
|
68
|
-
}
|
|
69
|
-
namespace(namespace) {
|
|
70
|
-
this.routes.forEach(route => {
|
|
71
|
-
route.namespace(namespace);
|
|
72
|
-
});
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
as(name) {
|
|
76
|
-
name = utils_1.String.toSnakeCase(name);
|
|
77
|
-
this.routes.forEach(route => {
|
|
78
|
-
route.as(route.name.replace(this.resourceName, name), false);
|
|
79
|
-
});
|
|
80
|
-
this.resourceName = name;
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
83
|
}
|
|
84
84
|
exports.RouteResource = RouteResource;
|
package/src/Router/Router.d.ts
CHANGED
|
@@ -16,7 +16,6 @@ import { HandlerContract } from '../Contracts/Context/HandlerContract';
|
|
|
16
16
|
export declare class Router {
|
|
17
17
|
routes: (Route | RouteResource | RouteGroup)[];
|
|
18
18
|
private readonly openedGroups;
|
|
19
|
-
private getRecentGroup;
|
|
20
19
|
private readonly http;
|
|
21
20
|
private controllerInstance;
|
|
22
21
|
constructor(http: Http);
|
|
@@ -35,4 +34,5 @@ export declare class Router {
|
|
|
35
34
|
any(url: string, handler: HandlerContract | string): Route;
|
|
36
35
|
register(): void;
|
|
37
36
|
toRoutesJSON(routes?: any): any;
|
|
37
|
+
private getRecentGroup;
|
|
38
38
|
}
|
package/src/Router/Router.js
CHANGED
|
@@ -20,9 +20,6 @@ class Router {
|
|
|
20
20
|
this.routes = [];
|
|
21
21
|
this.openedGroups = [];
|
|
22
22
|
}
|
|
23
|
-
getRecentGroup() {
|
|
24
|
-
return this.openedGroups[this.openedGroups.length - 1];
|
|
25
|
-
}
|
|
26
23
|
listRoutes() {
|
|
27
24
|
return this.toRoutesJSON(this.routes);
|
|
28
25
|
}
|
|
@@ -118,5 +115,8 @@ class Router {
|
|
|
118
115
|
return list;
|
|
119
116
|
}, []);
|
|
120
117
|
}
|
|
118
|
+
getRecentGroup() {
|
|
119
|
+
return this.openedGroups[this.openedGroups.length - 1];
|
|
120
|
+
}
|
|
121
121
|
}
|
|
122
122
|
exports.Router = Router;
|
|
@@ -11,14 +11,16 @@ import { HandlerContract } from '../Contracts/Context/HandlerContract';
|
|
|
11
11
|
import { ErrorHandlerContract } from '../Contracts/Context/Error/ErrorHandlerContract';
|
|
12
12
|
import { HandleHandlerContract } from '../Contracts/Context/Middlewares/Handle/HandleHandlerContract';
|
|
13
13
|
import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract';
|
|
14
|
+
import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
|
|
14
15
|
declare module 'fastify' {
|
|
15
16
|
interface FastifyRequest {
|
|
16
17
|
data: Record<string, any>;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
export declare class FastifyHandler {
|
|
20
|
-
static createOnSendHandler(handler: InterceptHandlerContract): (req: any, _res: any, payload: any
|
|
21
|
+
static createOnSendHandler(handler: InterceptHandlerContract): (req: any, _res: any, payload: any) => Promise<any>;
|
|
21
22
|
static createDoneHandler(handler: HandleHandlerContract): (req: any, res: any, done: any) => any;
|
|
23
|
+
static createResponseHandler(handler: TerminateHandlerContract): (req: any, res: any, done: any) => any;
|
|
22
24
|
static createErrorHandler(handler: ErrorHandlerContract): (error: any, req: FastifyRequest, res: FastifyReply) => any;
|
|
23
25
|
static createRequestHandler(handler: HandlerContract): (req: FastifyRequest, res: FastifyReply) => Promise<any>;
|
|
24
26
|
}
|
|
@@ -15,7 +15,7 @@ const Request_1 = require("../Context/Request");
|
|
|
15
15
|
const Response_1 = require("../Context/Response");
|
|
16
16
|
class FastifyHandler {
|
|
17
17
|
static createOnSendHandler(handler) {
|
|
18
|
-
return (req, _res, payload
|
|
18
|
+
return async (req, _res, payload) => {
|
|
19
19
|
const request = new Request_1.Request(req);
|
|
20
20
|
if (!req.data)
|
|
21
21
|
req.data = {};
|
|
@@ -27,19 +27,17 @@ class FastifyHandler {
|
|
|
27
27
|
if (utils_1.Is.Json(payload)) {
|
|
28
28
|
body = JSON.parse(body);
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
body = await handler({
|
|
31
31
|
request,
|
|
32
32
|
body,
|
|
33
33
|
status: _res.statusCode,
|
|
34
34
|
params: req.params,
|
|
35
35
|
queries: req.query,
|
|
36
36
|
data: req.data,
|
|
37
|
-
next: (body, error = null) => {
|
|
38
|
-
if (utils_1.Is.Object(body))
|
|
39
|
-
body = JSON.stringify(body);
|
|
40
|
-
done(error, body);
|
|
41
|
-
},
|
|
42
37
|
});
|
|
38
|
+
if (utils_1.Is.Object(body))
|
|
39
|
+
body = JSON.stringify(body);
|
|
40
|
+
return body;
|
|
43
41
|
};
|
|
44
42
|
}
|
|
45
43
|
static createDoneHandler(handler) {
|
|
@@ -62,6 +60,26 @@ class FastifyHandler {
|
|
|
62
60
|
});
|
|
63
61
|
};
|
|
64
62
|
}
|
|
63
|
+
static createResponseHandler(handler) {
|
|
64
|
+
return (req, res, done) => {
|
|
65
|
+
const request = new Request_1.Request(req);
|
|
66
|
+
const response = new Response_1.Response(res);
|
|
67
|
+
if (!req.data)
|
|
68
|
+
req.data = {};
|
|
69
|
+
if (!req.query)
|
|
70
|
+
req.query = {};
|
|
71
|
+
if (!req.params)
|
|
72
|
+
req.params = {};
|
|
73
|
+
return handler({
|
|
74
|
+
request,
|
|
75
|
+
response,
|
|
76
|
+
params: req.params,
|
|
77
|
+
queries: req.query,
|
|
78
|
+
data: req.data,
|
|
79
|
+
next: done,
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
65
83
|
static createErrorHandler(handler) {
|
|
66
84
|
return (error, req, res) => {
|
|
67
85
|
const request = new Request_1.Request(req);
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @athenna/http
|
|
4
|
-
*
|
|
5
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|