@athenna/http 1.2.6 → 1.3.1
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/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +2 -2
- package/src/Contracts/Context/Error/ErrorContextContract.d.ts +3 -3
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +3 -1
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.d.ts +1 -1
- package/src/Exceptions/Exception.d.ts +12 -0
- package/src/Exceptions/Exception.js +18 -0
- package/src/Handlers/FastifyHandler.d.ts +2 -2
- package/src/Handlers/FastifyHandler.js +4 -2
- package/src/Handlers/HttpExceptionHandler.d.ts +27 -0
- package/src/Handlers/HttpExceptionHandler.js +71 -0
- package/src/Http.d.ts +4 -1
- package/src/Http.js +0 -7
- package/src/Kernels/HttpKernel.d.ts +6 -0
- package/src/Kernels/HttpKernel.js +48 -9
- package/src/Providers/ControllerProvider.d.ts +17 -0
- package/src/Providers/ControllerProvider.js +49 -0
- package/src/Providers/MiddlewareProvider.d.ts +17 -0
- package/src/Providers/MiddlewareProvider.js +49 -0
- package/src/Router/Route.js +3 -3
- package/src/Router/RouteGroup.d.ts +1 -1
- package/src/Router/RouteGroup.js +14 -14
- package/src/Router/RouteResource.d.ts +1 -1
- package/src/Utils/getAppFiles.d.ts +10 -0
- package/src/Utils/getAppFiles.js +22 -0
- package/src/Handlers/HttpErrorHandler.d.ts +0 -12
- package/src/Handlers/HttpErrorHandler.js +0 -46
package/index.d.ts
CHANGED
|
@@ -11,8 +11,10 @@ export * from './src/Facades/Server';
|
|
|
11
11
|
export * from './src/Http';
|
|
12
12
|
export * from './src/Router/Router';
|
|
13
13
|
export * from './src/Kernels/HttpKernel';
|
|
14
|
+
export * from './src/Exceptions/Exception';
|
|
14
15
|
export * from './src/Providers/HttpRouteProvider';
|
|
15
16
|
export * from './src/Providers/HttpServerProvider';
|
|
17
|
+
export * from './src/Handlers/HttpExceptionHandler';
|
|
16
18
|
export * from './src/Contracts/MiddlewareContract';
|
|
17
19
|
export * from './src/Contracts/Context/ContextContract';
|
|
18
20
|
export * from './src/Contracts/Context/HandlerContract';
|
package/index.js
CHANGED
|
@@ -23,8 +23,10 @@ __exportStar(require("./src/Facades/Server"), exports);
|
|
|
23
23
|
__exportStar(require("./src/Http"), exports);
|
|
24
24
|
__exportStar(require("./src/Router/Router"), exports);
|
|
25
25
|
__exportStar(require("./src/Kernels/HttpKernel"), exports);
|
|
26
|
+
__exportStar(require("./src/Exceptions/Exception"), exports);
|
|
26
27
|
__exportStar(require("./src/Providers/HttpRouteProvider"), exports);
|
|
27
28
|
__exportStar(require("./src/Providers/HttpServerProvider"), exports);
|
|
29
|
+
__exportStar(require("./src/Handlers/HttpExceptionHandler"), exports);
|
|
28
30
|
__exportStar(require("./src/Contracts/MiddlewareContract"), exports);
|
|
29
31
|
__exportStar(require("./src/Contracts/Context/ContextContract"), exports);
|
|
30
32
|
__exportStar(require("./src/Contracts/Context/HandlerContract"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
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>",
|
|
@@ -160,7 +160,7 @@
|
|
|
160
160
|
"dependencies": {
|
|
161
161
|
"@athenna/config": "1.0.8",
|
|
162
162
|
"@athenna/ioc": "1.1.3",
|
|
163
|
-
"@athenna/logger": "1.1.
|
|
163
|
+
"@athenna/logger": "1.1.8",
|
|
164
164
|
"@secjs/utils": "1.8.3",
|
|
165
165
|
"fastify": "3.27.4",
|
|
166
166
|
"fastify-cors": "6.0.3",
|
|
@@ -11,8 +11,8 @@ import { ResponseContract } from '../ResponseContract';
|
|
|
11
11
|
export interface ErrorContextContract {
|
|
12
12
|
request: RequestContract;
|
|
13
13
|
response: ResponseContract;
|
|
14
|
-
params:
|
|
15
|
-
queries:
|
|
16
|
-
data
|
|
14
|
+
params: any;
|
|
15
|
+
queries: any;
|
|
16
|
+
data: any;
|
|
17
17
|
error: any;
|
|
18
18
|
}
|
|
@@ -7,11 +7,13 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { RequestContract } from '../../RequestContract';
|
|
10
|
+
import { ResponseContract } from '../../ResponseContract';
|
|
10
11
|
export interface InterceptContextContract {
|
|
11
12
|
request: RequestContract;
|
|
13
|
+
response: ResponseContract;
|
|
12
14
|
params: any;
|
|
13
15
|
queries: any;
|
|
14
16
|
body: any;
|
|
15
17
|
status: number;
|
|
16
|
-
data:
|
|
18
|
+
data: any;
|
|
17
19
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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 { Exception as SecException } from '@secjs/utils';
|
|
10
|
+
export declare class Exception extends SecException {
|
|
11
|
+
constructor(message?: string, statusCode?: number, code?: string, help?: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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.Exception = void 0;
|
|
12
|
+
const utils_1 = require("@secjs/utils");
|
|
13
|
+
class Exception extends utils_1.Exception {
|
|
14
|
+
constructor(message = 'Internal server exception.', statusCode = 500, code = 'INTERNAL_SERVER_ERROR', help) {
|
|
15
|
+
super(message, statusCode, code, help);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.Exception = Exception;
|
|
@@ -14,11 +14,11 @@ import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Inter
|
|
|
14
14
|
import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
|
|
15
15
|
declare module 'fastify' {
|
|
16
16
|
interface FastifyRequest {
|
|
17
|
-
data:
|
|
17
|
+
data: any;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
export declare class FastifyHandler {
|
|
21
|
-
static createOnSendHandler(handler: InterceptHandlerContract): (req: FastifyRequest,
|
|
21
|
+
static createOnSendHandler(handler: InterceptHandlerContract): (req: FastifyRequest, res: FastifyReply, payload: any) => Promise<any>;
|
|
22
22
|
static createDoneHandler(handler: HandleHandlerContract): (req: FastifyRequest, res: FastifyReply, done: any) => any;
|
|
23
23
|
static createResponseHandler(handler: TerminateHandlerContract): (req: FastifyRequest, res: FastifyReply, done: any) => any;
|
|
24
24
|
static createErrorHandler(handler: ErrorHandlerContract): (error: any, req: FastifyRequest, res: FastifyReply) => any;
|
|
@@ -15,8 +15,9 @@ 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 async (req,
|
|
18
|
+
return async (req, res, payload) => {
|
|
19
19
|
const request = new Request_1.Request(req);
|
|
20
|
+
const response = new Response_1.Response(res);
|
|
20
21
|
if (!req.data)
|
|
21
22
|
req.data = {};
|
|
22
23
|
if (!req.query)
|
|
@@ -29,8 +30,9 @@ class FastifyHandler {
|
|
|
29
30
|
}
|
|
30
31
|
body = await handler({
|
|
31
32
|
request,
|
|
33
|
+
response,
|
|
32
34
|
body,
|
|
33
|
-
status:
|
|
35
|
+
status: res.statusCode,
|
|
34
36
|
params: req.params,
|
|
35
37
|
queries: req.query,
|
|
36
38
|
data: req.data,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ErrorContextContract } from '../Contracts/Context/Error/ErrorContextContract';
|
|
2
|
+
export declare class HttpExceptionHandler {
|
|
3
|
+
/**
|
|
4
|
+
* Set if error logs will come with stack.
|
|
5
|
+
*
|
|
6
|
+
* @protected
|
|
7
|
+
*/
|
|
8
|
+
protected addStack: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Error codes that should be ignored by Log.
|
|
11
|
+
*
|
|
12
|
+
* @protected
|
|
13
|
+
*/
|
|
14
|
+
protected ignoreCodes: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Error statuses that should be ignored by Log.
|
|
17
|
+
*
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
protected ignoreStatuses: string[];
|
|
21
|
+
/**
|
|
22
|
+
* The global exception handler of all HTTP requests.
|
|
23
|
+
*
|
|
24
|
+
* @param ctx
|
|
25
|
+
*/
|
|
26
|
+
handle({ error, response }: ErrorContextContract): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpExceptionHandler = void 0;
|
|
4
|
+
const logger_1 = require("@athenna/logger");
|
|
5
|
+
const utils_1 = require("@secjs/utils");
|
|
6
|
+
const config_1 = require("@athenna/config");
|
|
7
|
+
class HttpExceptionHandler {
|
|
8
|
+
constructor() {
|
|
9
|
+
/**
|
|
10
|
+
* Set if error logs will come with stack.
|
|
11
|
+
*
|
|
12
|
+
* @protected
|
|
13
|
+
*/
|
|
14
|
+
this.addStack = false;
|
|
15
|
+
/**
|
|
16
|
+
* Error codes that should be ignored by Log.
|
|
17
|
+
*
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
this.ignoreCodes = [];
|
|
21
|
+
/**
|
|
22
|
+
* Error statuses that should be ignored by Log.
|
|
23
|
+
*
|
|
24
|
+
* @protected
|
|
25
|
+
*/
|
|
26
|
+
this.ignoreStatuses = [];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The global exception handler of all HTTP requests.
|
|
30
|
+
*
|
|
31
|
+
* @param ctx
|
|
32
|
+
*/
|
|
33
|
+
async handle({ error, response }) {
|
|
34
|
+
const code = error.code || error.name;
|
|
35
|
+
const statusCode = error.statusCode || error.status || 500;
|
|
36
|
+
const body = {
|
|
37
|
+
statusCode,
|
|
38
|
+
code: utils_1.String.toSnakeCase(`${code}`).toUpperCase(),
|
|
39
|
+
name: error.name,
|
|
40
|
+
message: error.message,
|
|
41
|
+
stack: error.stack,
|
|
42
|
+
};
|
|
43
|
+
if (error.help) {
|
|
44
|
+
body.help = error.help;
|
|
45
|
+
}
|
|
46
|
+
const isInternalServerError = statusCode === 500;
|
|
47
|
+
const isDebugMode = config_1.Config.get('app.debug');
|
|
48
|
+
if (isInternalServerError && !isDebugMode) {
|
|
49
|
+
body.name = 'Internal server error';
|
|
50
|
+
body.message = 'An internal server exception has occurred.';
|
|
51
|
+
delete body.stack;
|
|
52
|
+
}
|
|
53
|
+
if (this.ignoreCodes.includes(code) ||
|
|
54
|
+
this.ignoreStatuses.includes(statusCode)) {
|
|
55
|
+
return response.status(statusCode).send(body);
|
|
56
|
+
}
|
|
57
|
+
const logConfig = {
|
|
58
|
+
formatterConfig: {
|
|
59
|
+
context: 'ExceptionHandler',
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
if (this.addStack) {
|
|
63
|
+
logger_1.Log.error(`[${body.statusCode}] ${body.code}::${body.message}\nStack: ${body.stack}`, logConfig);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
logger_1.Log.error(`[${body.statusCode}] ${body.code}::${body.message}`, logConfig);
|
|
67
|
+
}
|
|
68
|
+
return response.status(statusCode).send(body);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.HttpExceptionHandler = HttpExceptionHandler;
|
package/src/Http.d.ts
CHANGED
|
@@ -70,7 +70,10 @@ export declare class Http {
|
|
|
70
70
|
* @param type {handle,intercept,terminate}
|
|
71
71
|
* @return void
|
|
72
72
|
*/
|
|
73
|
-
use(handler: HandleHandlerContract
|
|
73
|
+
use(handler: HandleHandlerContract): any;
|
|
74
|
+
use(handler: HandleHandlerContract, type: 'handle'): any;
|
|
75
|
+
use(handler: InterceptHandlerContract, type: 'intercept'): any;
|
|
76
|
+
use(handler: TerminateHandlerContract, type: 'terminate'): any;
|
|
74
77
|
request(): LightMyRequest.Chain;
|
|
75
78
|
request(options: InjectOptions | string): Promise<LightMyRequest.Response>;
|
|
76
79
|
/**
|
package/src/Http.js
CHANGED
|
@@ -71,13 +71,6 @@ class Http {
|
|
|
71
71
|
getRoutes(options) {
|
|
72
72
|
return this.server.printRoutes(options);
|
|
73
73
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Print all routes registered.
|
|
76
|
-
*
|
|
77
|
-
* @param handler {HandleHandlerContract,InterceptHandlerContract,TerminateHandlerContract}
|
|
78
|
-
* @param type {handle,intercept,terminate}
|
|
79
|
-
* @return void
|
|
80
|
-
*/
|
|
81
74
|
use(handler, type = 'handle') {
|
|
82
75
|
let hookName = 'preHandler';
|
|
83
76
|
let handlerType = 'createDoneHandler';
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
2
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
22
|
exports.HttpKernel = void 0;
|
|
4
23
|
const logger_1 = require("@athenna/logger");
|
|
5
24
|
const config_1 = require("@athenna/config");
|
|
6
|
-
const utils_1 = require("@secjs/utils");
|
|
7
|
-
const HttpErrorHandler_1 = require("../Handlers/HttpErrorHandler");
|
|
8
25
|
const Server_1 = require("../Facades/Server");
|
|
26
|
+
const utils_1 = require("@secjs/utils");
|
|
9
27
|
class HttpKernel {
|
|
10
28
|
/**
|
|
11
29
|
* Register all global and named middlewares to the server.
|
|
@@ -19,10 +37,10 @@ class HttpKernel {
|
|
|
19
37
|
*/
|
|
20
38
|
for (const key of Object.keys(this.namedMiddlewares)) {
|
|
21
39
|
const Middleware = utils_1.resolveModule(await this.namedMiddlewares[key]);
|
|
22
|
-
if (!ioc.hasDependency(`App/Middlewares/${Middleware.name}`)) {
|
|
23
|
-
ioc.bind(`App/Middlewares/${Middleware.name}`, Middleware);
|
|
40
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
41
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware);
|
|
24
42
|
}
|
|
25
|
-
ioc.alias(`App/Middlewares/Names/${key}`, `App/Middlewares/${Middleware.name}`);
|
|
43
|
+
ioc.alias(`App/Http/Middlewares/Names/${key}`, `App/Http/Middlewares/${Middleware.name}`);
|
|
26
44
|
}
|
|
27
45
|
/**
|
|
28
46
|
* Binding the global middlewares inside the container and
|
|
@@ -30,10 +48,10 @@ class HttpKernel {
|
|
|
30
48
|
*/
|
|
31
49
|
for (const module of this.globalMiddlewares) {
|
|
32
50
|
let Middleware = utils_1.resolveModule(await module);
|
|
33
|
-
if (!ioc.hasDependency(`App/Middlewares/${Middleware.name}`)) {
|
|
34
|
-
ioc.bind(`App/Middlewares/${Middleware.name}`, Middleware);
|
|
51
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
52
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware);
|
|
35
53
|
}
|
|
36
|
-
Middleware = ioc.safeUse(`App/Middlewares/${Middleware.name}`);
|
|
54
|
+
Middleware = ioc.safeUse(`App/Http/Middlewares/${Middleware.name}`);
|
|
37
55
|
if (Middleware.handle) {
|
|
38
56
|
Server_1.Server.use(Middleware.handle, 'handle');
|
|
39
57
|
}
|
|
@@ -76,7 +94,14 @@ class HttpKernel {
|
|
|
76
94
|
if (config_1.Config.get('http.noErrorHandler')) {
|
|
77
95
|
return;
|
|
78
96
|
}
|
|
79
|
-
|
|
97
|
+
let extension = 'js';
|
|
98
|
+
if (config_1.Env('NODE_TS') === 'true') {
|
|
99
|
+
extension = 'ts';
|
|
100
|
+
}
|
|
101
|
+
const path = utils_1.Path.app(`Http/Exceptions/Handler.${extension}`);
|
|
102
|
+
const Handler = utils_1.resolveModule(await Promise.resolve().then(() => __importStar(require(path))));
|
|
103
|
+
const handler = new Handler();
|
|
104
|
+
Server_1.Server.setErrorHandler(handler.handle.bind(handler));
|
|
80
105
|
}
|
|
81
106
|
/**
|
|
82
107
|
* Register log terminate middleware
|
|
@@ -92,5 +117,19 @@ class HttpKernel {
|
|
|
92
117
|
return ctx.next();
|
|
93
118
|
}, 'terminate');
|
|
94
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Register the requestId handle middleware.
|
|
122
|
+
*
|
|
123
|
+
* @return void
|
|
124
|
+
*/
|
|
125
|
+
async registerRequestIdMiddleware() {
|
|
126
|
+
if (config_1.Config.get('http.noRequestId')) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
Server_1.Server.use(async (ctx) => {
|
|
130
|
+
ctx.data.requestId = utils_1.Token.generate('ath');
|
|
131
|
+
return ctx.next();
|
|
132
|
+
}, 'handle');
|
|
133
|
+
}
|
|
95
134
|
}
|
|
96
135
|
exports.HttpKernel = HttpKernel;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { ServiceProvider } from '@athenna/ioc';
|
|
10
|
+
export declare class ControllerProvider extends ServiceProvider {
|
|
11
|
+
/**
|
|
12
|
+
* Bootstrap any application services.
|
|
13
|
+
*
|
|
14
|
+
* @return void
|
|
15
|
+
*/
|
|
16
|
+
boot(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
+
if (mod && mod.__esModule) return mod;
|
|
24
|
+
var result = {};
|
|
25
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
+
__setModuleDefault(result, mod);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ControllerProvider = void 0;
|
|
31
|
+
const ioc_1 = require("@athenna/ioc");
|
|
32
|
+
const utils_1 = require("@secjs/utils");
|
|
33
|
+
const getAppFiles_1 = require("../Utils/getAppFiles");
|
|
34
|
+
class ControllerProvider extends ioc_1.ServiceProvider {
|
|
35
|
+
/**
|
|
36
|
+
* Bootstrap any application services.
|
|
37
|
+
*
|
|
38
|
+
* @return void
|
|
39
|
+
*/
|
|
40
|
+
async boot() {
|
|
41
|
+
let controllers = getAppFiles_1.getAppFiles(utils_1.Path.app('Http/Controllers'));
|
|
42
|
+
controllers = await Promise.all(controllers.map(File => Promise.resolve().then(() => __importStar(require(File.path)))));
|
|
43
|
+
controllers.forEach(Module => {
|
|
44
|
+
const Controller = utils_1.resolveModule(Module);
|
|
45
|
+
this.container.bind(`App/Http/Controllers/${Controller.name}`, Controller);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.ControllerProvider = ControllerProvider;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { ServiceProvider } from '@athenna/ioc';
|
|
10
|
+
export declare class MiddlewareProvider extends ServiceProvider {
|
|
11
|
+
/**
|
|
12
|
+
* Bootstrap any application services.
|
|
13
|
+
*
|
|
14
|
+
* @return void
|
|
15
|
+
*/
|
|
16
|
+
boot(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
+
if (mod && mod.__esModule) return mod;
|
|
24
|
+
var result = {};
|
|
25
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
+
__setModuleDefault(result, mod);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.MiddlewareProvider = void 0;
|
|
31
|
+
const ioc_1 = require("@athenna/ioc");
|
|
32
|
+
const utils_1 = require("@secjs/utils");
|
|
33
|
+
const getAppFiles_1 = require("../Utils/getAppFiles");
|
|
34
|
+
class MiddlewareProvider extends ioc_1.ServiceProvider {
|
|
35
|
+
/**
|
|
36
|
+
* Bootstrap any application services.
|
|
37
|
+
*
|
|
38
|
+
* @return void
|
|
39
|
+
*/
|
|
40
|
+
async boot() {
|
|
41
|
+
let middlewares = getAppFiles_1.getAppFiles(utils_1.Path.app('Http/Middlewares'));
|
|
42
|
+
middlewares = await Promise.all(middlewares.map(File => Promise.resolve().then(() => __importStar(require(File.path)))));
|
|
43
|
+
middlewares.forEach(Module => {
|
|
44
|
+
const Controller = utils_1.resolveModule(Module);
|
|
45
|
+
this.container.bind(`App/Http/Middlewares/${Controller.name}`, Controller);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.MiddlewareProvider = MiddlewareProvider;
|
package/src/Router/Route.js
CHANGED
|
@@ -47,8 +47,8 @@ class Route {
|
|
|
47
47
|
};
|
|
48
48
|
const insertionType = prepend ? 'unshift' : 'push';
|
|
49
49
|
if (utils_1.Is.String(middleware)) {
|
|
50
|
-
const mid = ioc.use(`App/Middlewares/Names/${middleware}`) ||
|
|
51
|
-
ioc.safeUse(`App/Middlewares/${middleware}`);
|
|
50
|
+
const mid = ioc.use(`App/Http/Middlewares/Names/${middleware}`) ||
|
|
51
|
+
ioc.safeUse(`App/Http/Middlewares/${middleware}`);
|
|
52
52
|
if (!mid) {
|
|
53
53
|
throw new MiddlewareNotFoundException_1.MiddlewareNotFoundException(middleware);
|
|
54
54
|
}
|
|
@@ -90,7 +90,7 @@ class Route {
|
|
|
90
90
|
};
|
|
91
91
|
if (utils_1.Is.String(this.handler)) {
|
|
92
92
|
const [controller, method] = this.handler.split('.');
|
|
93
|
-
const dependency = ioc.safeUse(`App/Controllers/${controller}`);
|
|
93
|
+
const dependency = ioc.safeUse(`App/Http/Controllers/${controller}`);
|
|
94
94
|
if (!dependency[method]) {
|
|
95
95
|
throw new UndefinedControllerMethodException_1.UndefinedControllerMethodException(method, controller);
|
|
96
96
|
}
|
|
@@ -16,9 +16,9 @@ import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Termi
|
|
|
16
16
|
export declare class RouteGroup {
|
|
17
17
|
routes: (Route | RouteResource | RouteGroup)[];
|
|
18
18
|
constructor(routes: (Route | RouteResource | RouteGroup)[]);
|
|
19
|
-
private invoke;
|
|
20
19
|
prefix(prefix: string): this;
|
|
21
20
|
as(name: string): this;
|
|
22
21
|
namespace(namespace: string): this;
|
|
23
22
|
middleware(middleware: HandlerContract | MiddlewareContract | InterceptHandlerContract | TerminateHandlerContract | string, type?: MiddlewareTypes, prepend?: boolean): this;
|
|
23
|
+
private invoke;
|
|
24
24
|
}
|
package/src/Router/RouteGroup.js
CHANGED
|
@@ -15,20 +15,6 @@ class RouteGroup {
|
|
|
15
15
|
constructor(routes) {
|
|
16
16
|
this.routes = routes;
|
|
17
17
|
}
|
|
18
|
-
invoke(route, method, params) {
|
|
19
|
-
if (route instanceof RouteResource_1.RouteResource) {
|
|
20
|
-
route.routes.forEach(child => this.invoke(child, method, params));
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (route instanceof RouteGroup) {
|
|
24
|
-
route.routes.forEach(child => this.invoke(child, method, params));
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (method === 'as' && !route.name) {
|
|
28
|
-
throw new CannotDefineGroupException_1.CannotDefineGroupException();
|
|
29
|
-
}
|
|
30
|
-
route[method](...params);
|
|
31
|
-
}
|
|
32
18
|
prefix(prefix) {
|
|
33
19
|
this.routes.forEach(route => this.invoke(route, 'prefix', [prefix]));
|
|
34
20
|
return this;
|
|
@@ -47,5 +33,19 @@ class RouteGroup {
|
|
|
47
33
|
});
|
|
48
34
|
return this;
|
|
49
35
|
}
|
|
36
|
+
invoke(route, method, params) {
|
|
37
|
+
if (route instanceof RouteResource_1.RouteResource) {
|
|
38
|
+
route.routes.forEach(child => this.invoke(child, method, params));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (route instanceof RouteGroup) {
|
|
42
|
+
route.routes.forEach(child => this.invoke(child, method, params));
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (method === 'as' && !route.name) {
|
|
46
|
+
throw new CannotDefineGroupException_1.CannotDefineGroupException();
|
|
47
|
+
}
|
|
48
|
+
route[method](...params);
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
51
|
exports.RouteGroup = RouteGroup;
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Route } from './Route';
|
|
10
10
|
import { MiddlewareTypes } from '../Contracts/MiddlewareTypes';
|
|
11
|
+
import { MiddlewareContract } from '../Contracts/MiddlewareContract';
|
|
11
12
|
import { HandlerContract } from '../Contracts/Context/HandlerContract';
|
|
12
13
|
import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract';
|
|
13
14
|
import { TerminateHandlerContract } from '../Contracts/Context/Middlewares/Terminate/TerminateHandlerContract';
|
|
14
|
-
import { MiddlewareContract } from '../Contracts/MiddlewareContract';
|
|
15
15
|
export declare class RouteResource {
|
|
16
16
|
routes: Route[];
|
|
17
17
|
private resource;
|
|
@@ -0,0 +1,10 @@
|
|
|
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 { File } from '@secjs/utils';
|
|
10
|
+
export declare function getAppFiles(path: string): File[];
|
|
@@ -0,0 +1,22 @@
|
|
|
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.getAppFiles = void 0;
|
|
12
|
+
const fs_1 = require("fs");
|
|
13
|
+
const utils_1 = require("@secjs/utils");
|
|
14
|
+
function getAppFiles(path) {
|
|
15
|
+
if (!fs_1.existsSync(path))
|
|
16
|
+
return [];
|
|
17
|
+
return (new utils_1.Folder(path)
|
|
18
|
+
.loadSync()
|
|
19
|
+
// Get all .js and .ts files but not the .d.ts.
|
|
20
|
+
.getFilesByPattern('!(*.d)*.*(js|ts)'));
|
|
21
|
+
}
|
|
22
|
+
exports.getAppFiles = getAppFiles;
|
|
@@ -1,12 +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
|
-
import { ErrorContextContract } from '../Contracts/Context/Error/ErrorContextContract';
|
|
10
|
-
export declare class HttpErrorHandler {
|
|
11
|
-
static handler({ error, response }: ErrorContextContract): void | Promise<void>;
|
|
12
|
-
}
|
|
@@ -1,46 +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 });
|
|
11
|
-
exports.HttpErrorHandler = void 0;
|
|
12
|
-
const utils_1 = require("@secjs/utils");
|
|
13
|
-
const logger_1 = require("@athenna/logger");
|
|
14
|
-
const config_1 = require("@athenna/config");
|
|
15
|
-
class HttpErrorHandler {
|
|
16
|
-
static handler({ error, response }) {
|
|
17
|
-
const code = error.code || error.name;
|
|
18
|
-
const statusCode = error.statusCode || error.status || 500;
|
|
19
|
-
const body = {
|
|
20
|
-
statusCode,
|
|
21
|
-
code: utils_1.String.toSnakeCase(`${code}`).toUpperCase(),
|
|
22
|
-
name: error.name,
|
|
23
|
-
message: error.message,
|
|
24
|
-
stack: error.stack,
|
|
25
|
-
};
|
|
26
|
-
if (error.help) {
|
|
27
|
-
body.help = error.help;
|
|
28
|
-
}
|
|
29
|
-
const isInternalServerError = statusCode === 500;
|
|
30
|
-
const isDebugMode = config_1.Config.get('app.debug');
|
|
31
|
-
if (isInternalServerError && !isDebugMode) {
|
|
32
|
-
body.name = 'Internal server error';
|
|
33
|
-
body.message = 'An internal server exception has occurred.';
|
|
34
|
-
delete body.stack;
|
|
35
|
-
}
|
|
36
|
-
if (isDebugMode) {
|
|
37
|
-
logger_1.Log.error(`Error: ${JSON.stringify(body, null, 2)}`, {
|
|
38
|
-
formatterConfig: {
|
|
39
|
-
context: HttpErrorHandler.name,
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
return response.status(statusCode).send(body);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.HttpErrorHandler = HttpErrorHandler;
|