@athenna/http 1.2.8 → 1.3.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 +1 -1
- 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/Handlers/FastifyHandler.d.ts +2 -2
- package/src/Handlers/FastifyHandler.js +4 -2
- package/src/Kernels/HttpKernel.js +6 -6
- 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/package.json
CHANGED
|
@@ -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
|
}
|
|
@@ -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,
|
|
@@ -19,10 +19,10 @@ class HttpKernel {
|
|
|
19
19
|
*/
|
|
20
20
|
for (const key of Object.keys(this.namedMiddlewares)) {
|
|
21
21
|
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);
|
|
22
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
23
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware);
|
|
24
24
|
}
|
|
25
|
-
ioc.alias(`App/Middlewares/Names/${key}`, `App/Middlewares/${Middleware.name}`);
|
|
25
|
+
ioc.alias(`App/Http/Middlewares/Names/${key}`, `App/Http/Middlewares/${Middleware.name}`);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Binding the global middlewares inside the container and
|
|
@@ -30,10 +30,10 @@ class HttpKernel {
|
|
|
30
30
|
*/
|
|
31
31
|
for (const module of this.globalMiddlewares) {
|
|
32
32
|
let Middleware = utils_1.resolveModule(await module);
|
|
33
|
-
if (!ioc.hasDependency(`App/Middlewares/${Middleware.name}`)) {
|
|
34
|
-
ioc.bind(`App/Middlewares/${Middleware.name}`, Middleware);
|
|
33
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
34
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware);
|
|
35
35
|
}
|
|
36
|
-
Middleware = ioc.safeUse(`App/Middlewares/${Middleware.name}`);
|
|
36
|
+
Middleware = ioc.safeUse(`App/Http/Middlewares/${Middleware.name}`);
|
|
37
37
|
if (Middleware.handle) {
|
|
38
38
|
Server_1.Server.use(Middleware.handle, 'handle');
|
|
39
39
|
}
|
|
@@ -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;
|