@athenna/http 1.3.1 → 1.3.4
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/LICENSE.md +3 -15
- package/README.md +8 -5
- package/package.json +95 -106
- package/src/Context/Request.js +168 -137
- package/src/Context/Response.js +102 -94
- package/src/Exceptions/UndefinedMethodException.js +30 -0
- package/src/Facades/Route.js +7 -5
- package/src/Facades/Server.js +7 -5
- package/src/Handlers/FastifyHandler.js +134 -115
- package/src/Handlers/HttpExceptionHandler.js +71 -68
- package/src/Kernels/HttpKernel.js +142 -124
- package/src/Providers/ControllerProvider.js +30 -39
- package/src/Providers/HttpRouteProvider.js +15 -16
- package/src/Providers/HttpServerProvider.js +13 -15
- package/src/Providers/MiddlewareProvider.js +30 -39
- package/src/Router/Route.js +192 -99
- package/src/Router/RouteGroup.js +66 -40
- package/src/Router/RouteResource.js +156 -74
- package/src/Router/Router.js +284 -112
- package/src/Utils/isMiddlewareContract.js +8 -6
- package/src/Utils/removeSlashes.js +21 -0
- package/src/index.d.ts +658 -0
- package/src/index.js +284 -0
- package/index.d.ts +0 -30
- package/index.js +0 -42
- package/src/Context/Request.d.ts +0 -103
- package/src/Context/Response.d.ts +0 -77
- package/src/Contracts/Context/ContextContract.d.ts +0 -17
- package/src/Contracts/Context/ContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorContextContract.d.ts +0 -18
- package/src/Contracts/Context/Error/ErrorContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Error/ErrorHandlerContract.js +0 -10
- package/src/Contracts/Context/HandlerContract.d.ts +0 -12
- package/src/Contracts/Context/HandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.d.ts +0 -23
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.js +0 -10
- package/src/Contracts/Context/NextContract.d.ts +0 -11
- package/src/Contracts/Context/NextContract.js +0 -10
- package/src/Contracts/Context/RequestContract.d.ts +0 -25
- package/src/Contracts/Context/RequestContract.js +0 -10
- package/src/Contracts/Context/ResponseContract.d.ts +0 -20
- package/src/Contracts/Context/ResponseContract.js +0 -2
- package/src/Contracts/FastifyErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyErrorHandlerContract.js +0 -10
- package/src/Contracts/FastifyHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyHandlerContract.js +0 -10
- package/src/Contracts/HttpMethodTypes.d.ts +0 -9
- package/src/Contracts/HttpMethodTypes.js +0 -10
- package/src/Contracts/MiddlewareContract.d.ts +0 -16
- package/src/Contracts/MiddlewareContract.js +0 -10
- package/src/Contracts/MiddlewareTypes.d.ts +0 -9
- package/src/Contracts/MiddlewareTypes.js +0 -10
- package/src/Contracts/MiddlewareTypesContract.d.ts +0 -16
- package/src/Contracts/MiddlewareTypesContract.js +0 -10
- package/src/Contracts/RouteContract.d.ts +0 -16
- package/src/Contracts/RouteContract.js +0 -10
- package/src/Exceptions/BadRequestException.d.ts +0 -12
- package/src/Exceptions/BadRequestException.js +0 -18
- package/src/Exceptions/CannotDefineGroupException.d.ts +0 -12
- package/src/Exceptions/CannotDefineGroupException.js +0 -19
- package/src/Exceptions/Exception.d.ts +0 -12
- package/src/Exceptions/Exception.js +0 -18
- package/src/Exceptions/MiddlewareNotFoundException.d.ts +0 -12
- package/src/Exceptions/MiddlewareNotFoundException.js +0 -19
- package/src/Exceptions/UndefinedControllerMethodException.d.ts +0 -12
- package/src/Exceptions/UndefinedControllerMethodException.js +0 -19
- package/src/Facades/Route.d.ts +0 -10
- package/src/Facades/Server.d.ts +0 -10
- package/src/Handlers/FastifyHandler.d.ts +0 -26
- package/src/Handlers/HttpExceptionHandler.d.ts +0 -27
- package/src/Http.d.ts +0 -167
- package/src/Http.js +0 -223
- package/src/Kernels/HttpKernel.d.ts +0 -54
- package/src/Providers/ControllerProvider.d.ts +0 -17
- package/src/Providers/HttpRouteProvider.d.ts +0 -17
- package/src/Providers/HttpServerProvider.d.ts +0 -17
- package/src/Providers/MiddlewareProvider.d.ts +0 -17
- package/src/Router/Route.d.ts +0 -32
- package/src/Router/RouteGroup.d.ts +0 -24
- package/src/Router/RouteResource.d.ts +0 -29
- package/src/Router/Router.d.ts +0 -39
- package/src/Utils/getAppFiles.d.ts +0 -10
- package/src/Utils/getAppFiles.js +0 -22
- package/src/Utils/isMiddlewareContract.d.ts +0 -10
- package/src/Utils/removeSlash.d.ts +0 -9
- package/src/Utils/removeSlash.js +0 -23
|
@@ -1,135 +1,153 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import { Log } from '@athenna/logger'
|
|
2
|
+
import { Config, Exec, Path, Uuid } from '@secjs/utils'
|
|
3
|
+
|
|
4
|
+
import { Server } from '#src/Facades/Server'
|
|
5
|
+
import { pathToFileURL } from 'url'
|
|
6
|
+
|
|
7
|
+
export class HttpKernel {
|
|
8
|
+
/**
|
|
9
|
+
* The application's global HTTP middlewares.
|
|
10
|
+
*
|
|
11
|
+
* This middlewares are run during every request to your http server.
|
|
12
|
+
*
|
|
13
|
+
* @type {any | Promise<any>}
|
|
14
|
+
*/
|
|
15
|
+
globalMiddlewares
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The application's named HTTP middlewares.
|
|
19
|
+
*
|
|
20
|
+
* Here you define all your named middlewares to use inside routes/http file.
|
|
21
|
+
*
|
|
22
|
+
* @type {Record<string, any | Promise<any>>}
|
|
23
|
+
*/
|
|
24
|
+
namedMiddlewares
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Register all global and named middlewares to the server.
|
|
28
|
+
*
|
|
29
|
+
* @return void
|
|
30
|
+
*/
|
|
31
|
+
async registerMiddlewares() {
|
|
28
32
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @return void
|
|
33
|
+
* Binding the named middlewares inside the container and
|
|
34
|
+
* creating a simple alias to use it inside Route.
|
|
32
35
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Binding the global middlewares inside the container and
|
|
47
|
-
* resolving it inside the Http server using "use" method.
|
|
48
|
-
*/
|
|
49
|
-
for (const module of this.globalMiddlewares) {
|
|
50
|
-
let Middleware = utils_1.resolveModule(await module);
|
|
51
|
-
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
52
|
-
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware);
|
|
53
|
-
}
|
|
54
|
-
Middleware = ioc.safeUse(`App/Http/Middlewares/${Middleware.name}`);
|
|
55
|
-
if (Middleware.handle) {
|
|
56
|
-
Server_1.Server.use(Middleware.handle, 'handle');
|
|
57
|
-
}
|
|
58
|
-
if (Middleware.intercept) {
|
|
59
|
-
Server_1.Server.use(Middleware.intercept, 'intercept');
|
|
60
|
-
}
|
|
61
|
-
if (Middleware.terminate) {
|
|
62
|
-
Server_1.Server.use(Middleware.terminate, 'terminate');
|
|
63
|
-
}
|
|
64
|
-
}
|
|
36
|
+
for (const key of Object.keys(this.namedMiddlewares)) {
|
|
37
|
+
const Middleware = await Exec.getModule(this.namedMiddlewares[key])
|
|
38
|
+
|
|
39
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
40
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
ioc.alias(
|
|
44
|
+
`App/Http/Middlewares/Names/${key}`,
|
|
45
|
+
`App/Http/Middlewares/${Middleware.name}`,
|
|
46
|
+
)
|
|
65
47
|
}
|
|
48
|
+
|
|
66
49
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @return void
|
|
50
|
+
* Binding the global middlewares inside the container and
|
|
51
|
+
* resolving it inside the Http server using "use" method.
|
|
70
52
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
53
|
+
for (const module of this.globalMiddlewares) {
|
|
54
|
+
let Middleware = await Exec.getModule(module)
|
|
55
|
+
|
|
56
|
+
if (!ioc.hasDependency(`App/Http/Middlewares/${Middleware.name}`)) {
|
|
57
|
+
ioc.bind(`App/Http/Middlewares/${Middleware.name}`, Middleware)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Middleware = ioc.safeUse(`App/Http/Middlewares/${Middleware.name}`)
|
|
61
|
+
|
|
62
|
+
if (Middleware.handle) {
|
|
63
|
+
Server.use(Middleware.handle, 'handle')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (Middleware.intercept) {
|
|
67
|
+
Server.use(Middleware.intercept, 'intercept')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (Middleware.terminate) {
|
|
71
|
+
Server.use(Middleware.terminate, 'terminate')
|
|
72
|
+
}
|
|
76
73
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Register cors plugin.
|
|
78
|
+
*
|
|
79
|
+
* @return {Promise<void>}
|
|
80
|
+
*/
|
|
81
|
+
async registerCors() {
|
|
82
|
+
if (Config.get('http.noCors')) {
|
|
83
|
+
return
|
|
87
84
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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));
|
|
85
|
+
|
|
86
|
+
Server.registerCors(Config.get('http.cors'))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Register rate limit plugin.
|
|
91
|
+
*
|
|
92
|
+
* @return {Promise<void>}
|
|
93
|
+
*/
|
|
94
|
+
async registerRateLimit() {
|
|
95
|
+
if (Config.get('http.noRateLimit')) {
|
|
96
|
+
return
|
|
105
97
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}, 'terminate');
|
|
98
|
+
|
|
99
|
+
Server.registerRateLimit(Config.get('http.rateLimit'))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Register the default error handler.
|
|
104
|
+
*
|
|
105
|
+
* @return {Promise<void>}
|
|
106
|
+
*/
|
|
107
|
+
async registerErrorHandler() {
|
|
108
|
+
if (Config.get('http.noErrorHandler')) {
|
|
109
|
+
return
|
|
119
110
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
111
|
+
|
|
112
|
+
const path = Path.app(`Http/Exceptions/Handler.js`)
|
|
113
|
+
|
|
114
|
+
const Handler = await Exec.getModule(import(pathToFileURL(path).href))
|
|
115
|
+
const handler = new Handler()
|
|
116
|
+
|
|
117
|
+
Server.setErrorHandler(handler.handle.bind(handler))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Register log terminate middleware.
|
|
122
|
+
*
|
|
123
|
+
* @return {Promise<void>}
|
|
124
|
+
*/
|
|
125
|
+
async registerLogMiddleware() {
|
|
126
|
+
if (!Config.get('http.logRequests')) {
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
Server.use(async ctx => {
|
|
131
|
+
await Log.channel('request').info(ctx)
|
|
132
|
+
|
|
133
|
+
return ctx.next()
|
|
134
|
+
}, 'terminate')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Register the requestId handle middleware.
|
|
139
|
+
*
|
|
140
|
+
* @return {Promise<void>}
|
|
141
|
+
*/
|
|
142
|
+
async registerRequestIdMiddleware() {
|
|
143
|
+
if (Config.get('http.noRequestId')) {
|
|
144
|
+
return
|
|
133
145
|
}
|
|
146
|
+
|
|
147
|
+
Server.use(async ctx => {
|
|
148
|
+
ctx.data.requestId = Uuid.generate('ath')
|
|
149
|
+
|
|
150
|
+
return ctx.next()
|
|
151
|
+
}, 'handle')
|
|
152
|
+
}
|
|
134
153
|
}
|
|
135
|
-
exports.HttpKernel = HttpKernel;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,43 +6,35 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
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
|
-
});
|
|
9
|
+
|
|
10
|
+
import { Exec, Folder, Path } from '@secjs/utils'
|
|
11
|
+
import { ServiceProvider } from '@athenna/ioc'
|
|
12
|
+
|
|
13
|
+
export class ControllerProvider extends ServiceProvider {
|
|
14
|
+
/**
|
|
15
|
+
* Bootstrap any application services.
|
|
16
|
+
*
|
|
17
|
+
* @return {Promise<void>}
|
|
18
|
+
*/
|
|
19
|
+
async boot() {
|
|
20
|
+
const path = Path.app('Http/Controllers')
|
|
21
|
+
|
|
22
|
+
if (!(await Folder.exists(path))) {
|
|
23
|
+
return
|
|
47
24
|
}
|
|
25
|
+
|
|
26
|
+
const controllers = (await new Folder(path).load()).getFilesByPattern(
|
|
27
|
+
'*/**/*.js',
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const promises = controllers.map(({ href }) => {
|
|
31
|
+
return Exec.getModule(import(href)).then(Controller => {
|
|
32
|
+
const alias = `App/Http/Controllers/${Controller.name}`
|
|
33
|
+
|
|
34
|
+
this.container.singleton(alias, Controller)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
await Promise.all(promises)
|
|
39
|
+
}
|
|
48
40
|
}
|
|
49
|
-
exports.ControllerProvider = ControllerProvider;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,19 +6,19 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class HttpRouteProvider extends
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
|
|
10
|
+
import { Router } from '#src/Router/Router'
|
|
11
|
+
import { ServiceProvider } from '@athenna/ioc'
|
|
12
|
+
|
|
13
|
+
export class HttpRouteProvider extends ServiceProvider {
|
|
14
|
+
/**
|
|
15
|
+
* Bootstrap any application services.
|
|
16
|
+
*
|
|
17
|
+
* @return {void}
|
|
18
|
+
*/
|
|
19
|
+
boot() {
|
|
20
|
+
const http = this.container.safeUse('Athenna/Core/HttpServer')
|
|
21
|
+
|
|
22
|
+
this.container.instance('Athenna/Core/HttpRoute', new Router(http))
|
|
23
|
+
}
|
|
24
24
|
}
|
|
25
|
-
exports.HttpRouteProvider = HttpRouteProvider;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,18 +6,17 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class HttpServerProvider extends
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
|
|
10
|
+
import { Http } from '#src/index'
|
|
11
|
+
import { ServiceProvider } from '@athenna/ioc'
|
|
12
|
+
|
|
13
|
+
export class HttpServerProvider extends ServiceProvider {
|
|
14
|
+
/**
|
|
15
|
+
* Register any application services.
|
|
16
|
+
*
|
|
17
|
+
* @return {void}
|
|
18
|
+
*/
|
|
19
|
+
register() {
|
|
20
|
+
this.container.instance('Athenna/Core/HttpServer', new Http())
|
|
21
|
+
}
|
|
23
22
|
}
|
|
24
|
-
exports.HttpServerProvider = HttpServerProvider;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,43 +6,35 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
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
|
-
});
|
|
9
|
+
|
|
10
|
+
import { ServiceProvider } from '@athenna/ioc'
|
|
11
|
+
import { Exec, Folder, Path } from '@secjs/utils'
|
|
12
|
+
|
|
13
|
+
export class MiddlewareProvider extends ServiceProvider {
|
|
14
|
+
/**
|
|
15
|
+
* Bootstrap any application services.
|
|
16
|
+
*
|
|
17
|
+
* @return {Promise<void>}
|
|
18
|
+
*/
|
|
19
|
+
async boot() {
|
|
20
|
+
const path = Path.app('Http/Middlewares')
|
|
21
|
+
|
|
22
|
+
if (!(await Folder.exists(path))) {
|
|
23
|
+
return
|
|
47
24
|
}
|
|
25
|
+
|
|
26
|
+
const middlewares = (await new Folder(path).load()).getFilesByPattern(
|
|
27
|
+
'*/**/*.js',
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const promises = middlewares.map(({ href }) => {
|
|
31
|
+
return Exec.getModule(import(href)).then(Middleware => {
|
|
32
|
+
const alias = `App/Http/Middlewares/${Middleware.name}`
|
|
33
|
+
|
|
34
|
+
this.container.singleton(alias, Middleware)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
await Promise.all(promises)
|
|
39
|
+
}
|
|
48
40
|
}
|
|
49
|
-
exports.MiddlewareProvider = MiddlewareProvider;
|