@athenna/http 1.0.7 → 1.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/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/Http.d.ts +4 -1
- package/src/Http.js +9 -2
- package/src/Providers/HttpRouteProvider.d.ts +5 -1
- package/src/Providers/HttpRouteProvider.js +6 -2
- package/src/Providers/HttpServerProvider.d.ts +5 -1
- package/src/Providers/HttpServerProvider.js +6 -2
- package/src/Router/Route.js +2 -2
- 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.d.ts +0 -11
- 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.1.0",
|
|
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.8",
|
|
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
|
}
|
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
|
}
|
|
@@ -12,10 +12,14 @@ exports.HttpRouteProvider = void 0;
|
|
|
12
12
|
const Router_1 = require("../Router/Router");
|
|
13
13
|
const ioc_1 = require("@athenna/ioc");
|
|
14
14
|
class HttpRouteProvider extends ioc_1.ServiceProvider {
|
|
15
|
+
/**
|
|
16
|
+
* Bootstrap any application services.
|
|
17
|
+
*
|
|
18
|
+
* @return void
|
|
19
|
+
*/
|
|
15
20
|
boot() {
|
|
16
21
|
const http = this.container.safeUse('Athenna/Core/HttpServer');
|
|
17
|
-
this.container.
|
|
22
|
+
this.container.instance('Athenna/Core/HttpRoute', new Router_1.Router(http));
|
|
18
23
|
}
|
|
19
|
-
register() { }
|
|
20
24
|
}
|
|
21
25
|
exports.HttpRouteProvider = HttpRouteProvider;
|
|
@@ -12,9 +12,13 @@ exports.HttpServerProvider = void 0;
|
|
|
12
12
|
const Http_1 = require("../Http");
|
|
13
13
|
const ioc_1 = require("@athenna/ioc");
|
|
14
14
|
class HttpServerProvider extends ioc_1.ServiceProvider {
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Register any application services.
|
|
17
|
+
*
|
|
18
|
+
* @return void
|
|
19
|
+
*/
|
|
16
20
|
register() {
|
|
17
|
-
this.container.
|
|
21
|
+
this.container.instance('Athenna/Core/HttpServer', new Http_1.Http());
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
exports.HttpServerProvider = HttpServerProvider;
|
package/src/Router/Route.js
CHANGED
|
@@ -47,7 +47,7 @@ class Route {
|
|
|
47
47
|
};
|
|
48
48
|
const insertionType = prepend ? 'unshift' : 'push';
|
|
49
49
|
if (utils_1.Is.String(middleware)) {
|
|
50
|
-
const mid = ioc.
|
|
50
|
+
const mid = ioc.safeUse(`App/Middlewares/${middleware}`);
|
|
51
51
|
if (!mid) {
|
|
52
52
|
throw new MiddlewareNotFoundException_1.MiddlewareNotFoundException(middleware);
|
|
53
53
|
}
|
|
@@ -89,7 +89,7 @@ class Route {
|
|
|
89
89
|
};
|
|
90
90
|
if (utils_1.Is.String(this.handler)) {
|
|
91
91
|
const [controller, method] = this.handler.split('.');
|
|
92
|
-
const dependency = ioc.
|
|
92
|
+
const dependency = ioc.safeUse(`App/Controllers/${controller}`);
|
|
93
93
|
if (!dependency[method]) {
|
|
94
94
|
throw new UndefinedControllerMethodException_1.UndefinedControllerMethodException(method, controller);
|
|
95
95
|
}
|
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,11 +0,0 @@
|
|
|
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 interface NextInterceptContract {
|
|
10
|
-
(body: Record<string, any>, error?: any): Promise<void> | void;
|
|
11
|
-
}
|
|
@@ -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 });
|