arikajs 0.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.
Potentially problematic release.
This version of arikajs might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +419 -0
- package/dist/Application.d.ts +32 -0
- package/dist/Application.d.ts.map +1 -0
- package/dist/Application.js +122 -0
- package/dist/Application.js.map +1 -0
- package/dist/Contracts/Application.d.ts +17 -0
- package/dist/Contracts/Application.d.ts.map +1 -0
- package/dist/Contracts/Application.js +3 -0
- package/dist/Contracts/Application.js.map +1 -0
- package/dist/createApp.d.ts +6 -0
- package/dist/createApp.d.ts.map +1 -0
- package/dist/createApp.js +11 -0
- package/dist/createApp.js.map +1 -0
- package/dist/http/Handler.d.ts +36 -0
- package/dist/http/Handler.d.ts.map +1 -0
- package/dist/http/Handler.js +95 -0
- package/dist/http/Handler.js.map +1 -0
- package/dist/http/Kernel.d.ts +33 -0
- package/dist/http/Kernel.d.ts.map +1 -0
- package/dist/http/Kernel.js +82 -0
- package/dist/http/Kernel.js.map +1 -0
- package/dist/http/Middleware/RequestLoggingMiddleware.d.ts +4 -0
- package/dist/http/Middleware/RequestLoggingMiddleware.d.ts.map +1 -0
- package/dist/http/Middleware/RequestLoggingMiddleware.js +17 -0
- package/dist/http/Middleware/RequestLoggingMiddleware.js.map +1 -0
- package/dist/http/Middleware/ValidateRequestMiddleware.d.ts +13 -0
- package/dist/http/Middleware/ValidateRequestMiddleware.d.ts.map +1 -0
- package/dist/http/Middleware/ValidateRequestMiddleware.js +34 -0
- package/dist/http/Middleware/ValidateRequestMiddleware.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/AuthServiceProvider.d.ts +12 -0
- package/dist/providers/AuthServiceProvider.d.ts.map +1 -0
- package/dist/providers/AuthServiceProvider.js +28 -0
- package/dist/providers/AuthServiceProvider.js.map +1 -0
- package/dist/providers/DatabaseServiceProvider.d.ts +12 -0
- package/dist/providers/DatabaseServiceProvider.d.ts.map +1 -0
- package/dist/providers/DatabaseServiceProvider.js +39 -0
- package/dist/providers/DatabaseServiceProvider.js.map +1 -0
- package/dist/providers/FrameworkServiceProvider.d.ts +6 -0
- package/dist/providers/FrameworkServiceProvider.d.ts.map +1 -0
- package/dist/providers/FrameworkServiceProvider.js +28 -0
- package/dist/providers/FrameworkServiceProvider.js.map +1 -0
- package/dist/providers/HttpServiceProvider.d.ts +13 -0
- package/dist/providers/HttpServiceProvider.d.ts.map +1 -0
- package/dist/providers/HttpServiceProvider.js +34 -0
- package/dist/providers/HttpServiceProvider.js.map +1 -0
- package/dist/providers/LoggingServiceProvider.d.ts +6 -0
- package/dist/providers/LoggingServiceProvider.d.ts.map +1 -0
- package/dist/providers/LoggingServiceProvider.js +19 -0
- package/dist/providers/LoggingServiceProvider.js.map +1 -0
- package/dist/providers/ValidationServiceProvider.d.ts +8 -0
- package/dist/providers/ValidationServiceProvider.d.ts.map +1 -0
- package/dist/providers/ValidationServiceProvider.js +26 -0
- package/dist/providers/ValidationServiceProvider.js.map +1 -0
- package/package.json +38 -0
- package/package.json.backup +38 -0
- package/src/Application.ts +101 -0
- package/src/Contracts/Application.ts +18 -0
- package/src/createApp.ts +9 -0
- package/src/http/Handler.ts +101 -0
- package/src/http/Kernel.ts +90 -0
- package/src/http/Middleware/RequestLoggingMiddleware.ts +17 -0
- package/src/http/Middleware/ValidateRequestMiddleware.ts +35 -0
- package/src/index.ts +33 -0
- package/src/providers/AuthServiceProvider.ts +28 -0
- package/src/providers/DatabaseServiceProvider.ts +40 -0
- package/src/providers/FrameworkServiceProvider.ts +26 -0
- package/src/providers/HttpServiceProvider.ts +35 -0
- package/src/providers/LoggingServiceProvider.ts +17 -0
- package/src/providers/ValidationServiceProvider.ts +24 -0
- package/tests/Framework.test.ts +120 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpServiceProvider = void 0;
|
|
4
|
+
const foundation_1 = require("@arikajs/foundation");
|
|
5
|
+
const router_1 = require("@arikajs/router");
|
|
6
|
+
const Kernel_1 = require("../http/Kernel");
|
|
7
|
+
const Handler_1 = require("../http/Handler");
|
|
8
|
+
class HttpServiceProvider extends foundation_1.ServiceProvider {
|
|
9
|
+
/**
|
|
10
|
+
* Register the service provider.
|
|
11
|
+
*/
|
|
12
|
+
async register() {
|
|
13
|
+
// Register the Router as a singleton
|
|
14
|
+
this.app.singleton(router_1.Router, () => {
|
|
15
|
+
return new router_1.Router(this.app.getContainer());
|
|
16
|
+
});
|
|
17
|
+
// Register the Kernel
|
|
18
|
+
this.app.singleton(Kernel_1.Kernel, () => {
|
|
19
|
+
return new Kernel_1.Kernel(this.app);
|
|
20
|
+
});
|
|
21
|
+
// Register the Exception Handler
|
|
22
|
+
this.app.singleton(Handler_1.Handler, () => {
|
|
23
|
+
return new Handler_1.Handler();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Boot the service provider.
|
|
28
|
+
*/
|
|
29
|
+
async boot() {
|
|
30
|
+
// Any HTTP-specific booting logic can go here
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.HttpServiceProvider = HttpServiceProvider;
|
|
34
|
+
//# sourceMappingURL=HttpServiceProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpServiceProvider.js","sourceRoot":"","sources":["../../src/providers/HttpServiceProvider.ts"],"names":[],"mappings":";;;AACA,oDAAsD;AACtD,4CAAyC;AACzC,2CAAwC;AACxC,6CAA0C;AAG1C,MAAa,mBAAoB,SAAQ,4BAA4B;IACjE;;OAEG;IACI,KAAK,CAAC,QAAQ;QACjB,qCAAqC;QACrC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,eAAM,EAAE,GAAG,EAAE;YAC5B,OAAO,IAAI,eAAM,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC;QAC/C,CAAC,CAAC,CAAC;QAEH,sBAAsB;QACtB,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,eAAM,EAAE,GAAG,EAAE;YAC5B,OAAO,IAAI,eAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC;QAEH,iCAAiC;QACjC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,iBAAO,EAAE,GAAG,EAAE;YAC7B,OAAO,IAAI,iBAAO,EAAE,CAAC;QACzB,CAAC,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,IAAI;QACb,8CAA8C;IAClD,CAAC;CACJ;AA3BD,kDA2BC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoggingServiceProvider.d.ts","sourceRoot":"","sources":["../../src/providers/LoggingServiceProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAGtD,qBAAa,sBAAuB,SAAQ,eAAe;IAC1C,QAAQ;IAOR,IAAI;CAIpB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LoggingServiceProvider = void 0;
|
|
4
|
+
const foundation_1 = require("@arikajs/foundation");
|
|
5
|
+
const logging_1 = require("@arikajs/logging");
|
|
6
|
+
class LoggingServiceProvider extends foundation_1.ServiceProvider {
|
|
7
|
+
async register() {
|
|
8
|
+
this.app.singleton('log', () => {
|
|
9
|
+
const config = this.app.config().get('logging');
|
|
10
|
+
return new logging_1.LogManager(config);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async boot() {
|
|
14
|
+
const logManager = this.app.make('log');
|
|
15
|
+
logging_1.Log.setManager(logManager);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.LoggingServiceProvider = LoggingServiceProvider;
|
|
19
|
+
//# sourceMappingURL=LoggingServiceProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoggingServiceProvider.js","sourceRoot":"","sources":["../../src/providers/LoggingServiceProvider.ts"],"names":[],"mappings":";;;AACA,oDAAsD;AACtD,8CAAmD;AAEnD,MAAa,sBAAuB,SAAQ,4BAAe;IAChD,KAAK,CAAC,QAAQ;QACjB,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE;YAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAChD,OAAO,IAAI,oBAAU,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,KAAK,CAAC,IAAI;QACb,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAa,KAAK,CAAC,CAAC;QACpD,aAAG,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;IAC/B,CAAC;CACJ;AAZD,wDAYC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationServiceProvider.d.ts","sourceRoot":"","sources":["../../src/providers/ValidationServiceProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAItD,qBAAa,yBAA0B,SAAQ,eAAe;IAC1D;;OAEG;IACU,QAAQ;CAcxB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationServiceProvider = void 0;
|
|
4
|
+
const foundation_1 = require("@arikajs/foundation");
|
|
5
|
+
const validation_1 = require("@arikajs/validation");
|
|
6
|
+
const ValidateRequestMiddleware_1 = require("../http/Middleware/ValidateRequestMiddleware");
|
|
7
|
+
class ValidationServiceProvider extends foundation_1.ServiceProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Register validation services.
|
|
10
|
+
*/
|
|
11
|
+
async register() {
|
|
12
|
+
this.app.singleton('validator', () => {
|
|
13
|
+
return {
|
|
14
|
+
make: (data, rules, messages = {}) => {
|
|
15
|
+
return new validation_1.Validator(data, rules, messages);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
// Register the validation middleware
|
|
20
|
+
this.app.bind(ValidateRequestMiddleware_1.ValidateRequestMiddleware, () => {
|
|
21
|
+
return new ValidateRequestMiddleware_1.ValidateRequestMiddleware();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ValidationServiceProvider = ValidationServiceProvider;
|
|
26
|
+
//# sourceMappingURL=ValidationServiceProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationServiceProvider.js","sourceRoot":"","sources":["../../src/providers/ValidationServiceProvider.ts"],"names":[],"mappings":";;;AACA,oDAAsD;AACtD,oDAAgD;AAChD,4FAAyF;AAEzF,MAAa,yBAA0B,SAAQ,4BAAe;IAC1D;;OAEG;IACI,KAAK,CAAC,QAAQ;QACjB,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,GAAG,EAAE;YACjC,OAAO;gBACH,IAAI,EAAE,CAAC,IAAS,EAAE,KAAU,EAAE,WAAgB,EAAE,EAAE,EAAE;oBAChD,OAAO,IAAI,sBAAS,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;gBAChD,CAAC;aACJ,CAAC;QACN,CAAC,CAAC,CAAC;QAEH,qCAAqC;QACrC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,qDAAyB,EAAE,GAAG,EAAE;YAC1C,OAAO,IAAI,qDAAyB,EAAE,CAAC;QAC3C,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AAlBD,8DAkBC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "arikajs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The ArikaJS Framework.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -p tsconfig.json",
|
|
10
|
+
"clean": "rm -rf dist",
|
|
11
|
+
"test": "npx tsx tests/Framework.test.ts"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@arikajs/auth": "file:../auth",
|
|
15
|
+
"@arikajs/authorization": "file:../authorization",
|
|
16
|
+
"@arikajs/cache": "file:../cache",
|
|
17
|
+
"@arikajs/cli": "file:../cli",
|
|
18
|
+
"@arikajs/console": "file:../console",
|
|
19
|
+
"@arikajs/database": "file:../database",
|
|
20
|
+
"@arikajs/dispatcher": "file:../dispatcher",
|
|
21
|
+
"@arikajs/encryption": "file:../encryption",
|
|
22
|
+
"@arikajs/events": "file:../events",
|
|
23
|
+
"@arikajs/foundation": "file:../foundation",
|
|
24
|
+
"@arikajs/http": "file:../http",
|
|
25
|
+
"@arikajs/logging": "file:../logging",
|
|
26
|
+
"@arikajs/mail": "file:../mail",
|
|
27
|
+
"@arikajs/middleware": "file:../middleware",
|
|
28
|
+
"@arikajs/queue": "file:../queue",
|
|
29
|
+
"@arikajs/router": "file:../router",
|
|
30
|
+
"@arikajs/storage": "file:../storage",
|
|
31
|
+
"@arikajs/validation": "file:../validation",
|
|
32
|
+
"@arikajs/view": "file:../view"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^20.11.24",
|
|
36
|
+
"typescript": "^5.3.3"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "arikajs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The ArikaJS Framework.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -p tsconfig.json",
|
|
10
|
+
"clean": "rm -rf dist",
|
|
11
|
+
"test": "npx tsx tests/Framework.test.ts"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@arikajs/auth": "file:../auth",
|
|
15
|
+
"@arikajs/authorization": "file:../authorization",
|
|
16
|
+
"@arikajs/cache": "file:../cache",
|
|
17
|
+
"@arikajs/cli": "file:../cli",
|
|
18
|
+
"@arikajs/console": "file:../console",
|
|
19
|
+
"@arikajs/database": "file:../database",
|
|
20
|
+
"@arikajs/dispatcher": "file:../dispatcher",
|
|
21
|
+
"@arikajs/encryption": "file:../encryption",
|
|
22
|
+
"@arikajs/events": "file:../events",
|
|
23
|
+
"@arikajs/foundation": "file:../foundation",
|
|
24
|
+
"@arikajs/http": "file:../http",
|
|
25
|
+
"@arikajs/logging": "file:../logging",
|
|
26
|
+
"@arikajs/mail": "file:../mail",
|
|
27
|
+
"@arikajs/middleware": "file:../middleware",
|
|
28
|
+
"@arikajs/queue": "file:../queue",
|
|
29
|
+
"@arikajs/router": "file:../router",
|
|
30
|
+
"@arikajs/storage": "file:../storage",
|
|
31
|
+
"@arikajs/validation": "file:../validation",
|
|
32
|
+
"@arikajs/view": "file:../view"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^20.11.24",
|
|
36
|
+
"typescript": "^5.3.3"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Application as FoundationApplication } from '@arikajs/foundation';
|
|
2
|
+
import { Router, Route } from '@arikajs/router';
|
|
3
|
+
import { FrameworkServiceProvider } from './providers/FrameworkServiceProvider';
|
|
4
|
+
import { Log } from '@arikajs/logging';
|
|
5
|
+
import { Application as ApplicationContract } from './Contracts/Application';
|
|
6
|
+
|
|
7
|
+
export class Application extends FoundationApplication implements ApplicationContract {
|
|
8
|
+
protected router: Router;
|
|
9
|
+
|
|
10
|
+
constructor(basePath: string = process.cwd()) {
|
|
11
|
+
super(basePath);
|
|
12
|
+
|
|
13
|
+
// Initialize Core Components
|
|
14
|
+
this.router = new Router(this.getContainer());
|
|
15
|
+
|
|
16
|
+
// Register within container
|
|
17
|
+
this.instance(Router, this.router);
|
|
18
|
+
|
|
19
|
+
// Register Core Framework Provider
|
|
20
|
+
this.register(FrameworkServiceProvider);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Map a GET route.
|
|
25
|
+
*/
|
|
26
|
+
public get(path: string, handler: any) {
|
|
27
|
+
return Route.get(path, handler);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Map a POST route.
|
|
32
|
+
*/
|
|
33
|
+
public post(path: string, handler: any) {
|
|
34
|
+
return Route.post(path, handler);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Map a PUT route.
|
|
39
|
+
*/
|
|
40
|
+
public put(path: string, handler: any) {
|
|
41
|
+
return Route.put(path, handler);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Map a DELETE route.
|
|
46
|
+
*/
|
|
47
|
+
public delete(path: string, handler: any) {
|
|
48
|
+
return Route.delete(path, handler);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Start the HTTP server.
|
|
53
|
+
*/
|
|
54
|
+
public async listen(port: number = 3000) {
|
|
55
|
+
if (!this.isBooted()) {
|
|
56
|
+
await this.boot();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const http = await import('node:http');
|
|
60
|
+
const { Request, Response } = await import('@arikajs/http');
|
|
61
|
+
const { Kernel } = await import('./http/Kernel');
|
|
62
|
+
|
|
63
|
+
// Resolve Kernel from the container
|
|
64
|
+
const kernel = this.make(Kernel);
|
|
65
|
+
|
|
66
|
+
const server = http.createServer(async (req, res) => {
|
|
67
|
+
// Instantiate Request and Response
|
|
68
|
+
// In a future refactor, these could also be resolved from factories
|
|
69
|
+
const request = new Request(this, req);
|
|
70
|
+
const response = new Response(res);
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const finalResponse = await kernel.handle(request, response);
|
|
74
|
+
kernel.terminate(request, finalResponse);
|
|
75
|
+
} catch (error: any) {
|
|
76
|
+
if (!res.headersSent) {
|
|
77
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
78
|
+
res.end(JSON.stringify({
|
|
79
|
+
error: 'Internal Server Error',
|
|
80
|
+
message: error.message,
|
|
81
|
+
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return new Promise<void>((resolve) => {
|
|
88
|
+
server.listen(port, () => {
|
|
89
|
+
Log.info(`ArikaJS application listening on http://localhost:${port}`);
|
|
90
|
+
resolve();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get the router instance.
|
|
97
|
+
*/
|
|
98
|
+
public getRouter(): Router {
|
|
99
|
+
return this.router;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
import { Router } from '@arikajs/router';
|
|
3
|
+
import { Application as HttpApplicationContract } from '@arikajs/http';
|
|
4
|
+
|
|
5
|
+
export interface Application extends HttpApplicationContract {
|
|
6
|
+
getRouter(): Router;
|
|
7
|
+
getContainer(): any;
|
|
8
|
+
getBasePath(): string;
|
|
9
|
+
make<T = any>(token: any): T;
|
|
10
|
+
singleton<T = any>(token: any, factory: any): void;
|
|
11
|
+
instance<T = any>(token: any, value: T): void;
|
|
12
|
+
bind<T = any>(token: any, factory: any): void;
|
|
13
|
+
resolve<T = any>(token: any): T;
|
|
14
|
+
register(provider: any): void;
|
|
15
|
+
boot(): Promise<void>;
|
|
16
|
+
run(): Promise<void>;
|
|
17
|
+
isBooted(): boolean;
|
|
18
|
+
}
|
package/src/createApp.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
|
|
2
|
+
import { Request, Response, HttpException } from '@arikajs/http';
|
|
3
|
+
import { Log } from '@arikajs/logging';
|
|
4
|
+
|
|
5
|
+
export class Handler {
|
|
6
|
+
/**
|
|
7
|
+
* A list of the exception types that should not be reported.
|
|
8
|
+
*/
|
|
9
|
+
protected dontReport: any[] = [];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Custom renderers for specific exception types.
|
|
13
|
+
*/
|
|
14
|
+
protected renderers: Map<any, (request: Request, error: any, response: Response) => Response> = new Map();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Report or log an exception.
|
|
18
|
+
*/
|
|
19
|
+
public report(error: any): void {
|
|
20
|
+
if (this.shouldReport(error)) {
|
|
21
|
+
Log.error(error.message || 'Error', {
|
|
22
|
+
stack: error.stack,
|
|
23
|
+
name: error.name || 'Error',
|
|
24
|
+
originalError: error.originalError
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Render an exception into an HTTP response.
|
|
31
|
+
*/
|
|
32
|
+
public render(request: Request, error: any, response: Response): Response {
|
|
33
|
+
// 1. Check if the error has a custom renderer
|
|
34
|
+
for (const [type, renderer] of this.renderers.entries()) {
|
|
35
|
+
if (error instanceof type) {
|
|
36
|
+
return renderer(request, error, response);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 2. Check if the error is "renderable" (has a render method)
|
|
41
|
+
if (typeof error.render === 'function') {
|
|
42
|
+
return error.render(request, response);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 3. Handle HttpException specifically
|
|
46
|
+
if (error instanceof HttpException) {
|
|
47
|
+
return response.status(error.getStatusCode()).json({
|
|
48
|
+
error: true,
|
|
49
|
+
message: error.message,
|
|
50
|
+
...(this.shouldDisplayStackTrace() ? { trace: error.stack } : {})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 4. Default error handling
|
|
55
|
+
const status = error.statusCode || error.status || 500;
|
|
56
|
+
const message = status === 500 && !this.shouldDisplayStackTrace()
|
|
57
|
+
? 'Internal Server Error'
|
|
58
|
+
: error.message || 'Unknown Error';
|
|
59
|
+
|
|
60
|
+
return response.status(status).json({
|
|
61
|
+
error: true,
|
|
62
|
+
message: message,
|
|
63
|
+
...(this.shouldDisplayStackTrace() ? {
|
|
64
|
+
name: error.name,
|
|
65
|
+
trace: error.stack
|
|
66
|
+
} : {})
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Determine if the exception should be reported.
|
|
72
|
+
*/
|
|
73
|
+
protected shouldReport(error: any): boolean {
|
|
74
|
+
return !this.dontReport.some(type => error instanceof type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Determine if the stack trace should be displayed.
|
|
79
|
+
*/
|
|
80
|
+
protected shouldDisplayStackTrace(): boolean {
|
|
81
|
+
return process.env.NODE_ENV === 'development' || process.env.APP_DEBUG === 'true';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Register a custom renderer for an exception type.
|
|
86
|
+
*/
|
|
87
|
+
public renderable(type: any, renderer: (request: Request, error: any, response: Response) => Response): this {
|
|
88
|
+
this.renderers.set(type, renderer);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Add an exception type to the dontReport list.
|
|
94
|
+
*/
|
|
95
|
+
public ignore(type: any): this {
|
|
96
|
+
if (!this.dontReport.includes(type)) {
|
|
97
|
+
this.dontReport.push(type);
|
|
98
|
+
}
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Application } from '../Contracts/Application';
|
|
2
|
+
import { Request, Response, NotFoundHttpException } from '@arikajs/http';
|
|
3
|
+
import { Pipeline } from '@arikajs/middleware';
|
|
4
|
+
import { Dispatcher } from '@arikajs/dispatcher';
|
|
5
|
+
import { RequestLoggingMiddleware } from './Middleware/RequestLoggingMiddleware';
|
|
6
|
+
import { BodyParserMiddleware, CorsMiddleware } from '@arikajs/http';
|
|
7
|
+
import { Handler } from './Handler';
|
|
8
|
+
|
|
9
|
+
export class Kernel {
|
|
10
|
+
/**
|
|
11
|
+
* The application's global HTTP middleware stack.
|
|
12
|
+
*/
|
|
13
|
+
protected middleware: any[] = [
|
|
14
|
+
new CorsMiddleware(),
|
|
15
|
+
new RequestLoggingMiddleware(),
|
|
16
|
+
new BodyParserMiddleware(),
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The application's route middleware groups.
|
|
21
|
+
*/
|
|
22
|
+
protected middlewareGroups: Record<string, any[]> = {
|
|
23
|
+
web: [],
|
|
24
|
+
api: [],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The application's route middleware.
|
|
29
|
+
*/
|
|
30
|
+
protected routeMiddleware: Record<string, any> = {};
|
|
31
|
+
|
|
32
|
+
protected handler: Handler;
|
|
33
|
+
|
|
34
|
+
constructor(protected app: Application) {
|
|
35
|
+
try {
|
|
36
|
+
this.handler = this.app.make(Handler);
|
|
37
|
+
} catch (e) {
|
|
38
|
+
this.handler = new Handler();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Handle an incoming HTTP request.
|
|
44
|
+
*/
|
|
45
|
+
public async handle(request: Request, response: Response): Promise<Response> {
|
|
46
|
+
try {
|
|
47
|
+
const pipeline = new Pipeline<Request, Response>(this.app.getContainer());
|
|
48
|
+
pipeline.setMiddlewareGroups(this.middlewareGroups);
|
|
49
|
+
pipeline.setAliases(this.routeMiddleware);
|
|
50
|
+
|
|
51
|
+
return await pipeline.pipe(this.middleware)
|
|
52
|
+
.handle(request, async (req: Request) => {
|
|
53
|
+
return this.dispatchToRouter(req, response);
|
|
54
|
+
}, response);
|
|
55
|
+
} catch (error: any) {
|
|
56
|
+
this.handler.report(error);
|
|
57
|
+
return this.handler.render(request, error, response);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Dispatch the request to the router.
|
|
63
|
+
*/
|
|
64
|
+
protected async dispatchToRouter(request: Request, response: Response): Promise<Response> {
|
|
65
|
+
const router = this.app.getRouter();
|
|
66
|
+
|
|
67
|
+
// Ensure router's dispatcher has kernel's middleware configuration
|
|
68
|
+
if ((router as any).setMiddlewareGroups) {
|
|
69
|
+
(router as any).setMiddlewareGroups(this.middlewareGroups);
|
|
70
|
+
}
|
|
71
|
+
if ((router as any).setRouteMiddleware) {
|
|
72
|
+
(router as any).setRouteMiddleware(this.routeMiddleware);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = await router.dispatch(request, response);
|
|
76
|
+
|
|
77
|
+
if (result === null) {
|
|
78
|
+
throw new NotFoundHttpException(`Route not found: [${request.method()}] ${request.path()}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return result as Response;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Send the response back to the client.
|
|
86
|
+
*/
|
|
87
|
+
public terminate(request: Request, response: Response): void {
|
|
88
|
+
response.terminate();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
import { Log } from '@arikajs/logging';
|
|
3
|
+
|
|
4
|
+
export class RequestLoggingMiddleware {
|
|
5
|
+
public async handle(request: any, next: (request: any) => Promise<any>): Promise<any> {
|
|
6
|
+
const start = Date.now();
|
|
7
|
+
const method = request.method();
|
|
8
|
+
const url = request.path();
|
|
9
|
+
|
|
10
|
+
const response = await next(request);
|
|
11
|
+
|
|
12
|
+
const duration = Date.now() - start;
|
|
13
|
+
Log.info(`${method} ${url} - ${duration}ms`);
|
|
14
|
+
|
|
15
|
+
return response;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
import { Request, Response } from '@arikajs/http';
|
|
3
|
+
import { Validator } from '@arikajs/validation';
|
|
4
|
+
|
|
5
|
+
export class ValidateRequestMiddleware {
|
|
6
|
+
protected rules: Record<string, any> = {};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Set the validation rules.
|
|
10
|
+
*/
|
|
11
|
+
public using(rules: Record<string, any>): this {
|
|
12
|
+
this.rules = rules;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Handle the incoming request.
|
|
18
|
+
*/
|
|
19
|
+
public async handle(request: Request, next: (request: Request) => Promise<Response>, response: Response): Promise<Response> {
|
|
20
|
+
if (Object.keys(this.rules).length === 0) {
|
|
21
|
+
return next(request);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const validator = new Validator(request.all(), this.rules);
|
|
25
|
+
|
|
26
|
+
if (await validator.fails()) {
|
|
27
|
+
return response.status(422).json({
|
|
28
|
+
message: 'The given data was invalid.',
|
|
29
|
+
errors: validator.errors()
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return next(request);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
export * from './Application';
|
|
3
|
+
export * from './createApp';
|
|
4
|
+
|
|
5
|
+
// Re-export common foundation items for convenience
|
|
6
|
+
export { ServiceProvider } from '@arikajs/foundation';
|
|
7
|
+
export { Container } from '@arikajs/foundation';
|
|
8
|
+
export { Repository as Config } from '@arikajs/foundation';
|
|
9
|
+
|
|
10
|
+
// Re-export HTTP items
|
|
11
|
+
export { Request, Response } from '@arikajs/http';
|
|
12
|
+
export { Kernel } from './http/Kernel';
|
|
13
|
+
|
|
14
|
+
// Re-export Routing items
|
|
15
|
+
export { Route } from '@arikajs/router';
|
|
16
|
+
|
|
17
|
+
// Re-export Middleware items
|
|
18
|
+
export { Pipeline, MiddlewareHandler } from '@arikajs/middleware';
|
|
19
|
+
|
|
20
|
+
// Re-export Database items
|
|
21
|
+
export { Model, Database as DB, Schema } from '@arikajs/database';
|
|
22
|
+
|
|
23
|
+
// Re-export Cache items
|
|
24
|
+
export { Cache } from '@arikajs/cache';
|
|
25
|
+
|
|
26
|
+
// Re-export Queue items
|
|
27
|
+
export { Queue, BaseJob } from '@arikajs/queue';
|
|
28
|
+
|
|
29
|
+
// Re-export Encryption items
|
|
30
|
+
export { Encrypter } from '@arikajs/encryption';
|
|
31
|
+
|
|
32
|
+
// Re-export Logging items
|
|
33
|
+
export { Log } from '@arikajs/logging';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
import { ServiceProvider } from '@arikajs/foundation';
|
|
3
|
+
import { AuthManager, Authenticate } from '@arikajs/auth';
|
|
4
|
+
|
|
5
|
+
export class AuthServiceProvider extends ServiceProvider {
|
|
6
|
+
/**
|
|
7
|
+
* Register the authentication services.
|
|
8
|
+
*/
|
|
9
|
+
public async register() {
|
|
10
|
+
this.app.singleton('auth', () => {
|
|
11
|
+
return new AuthManager(this.app.config().get('auth', {}));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
this.app.bind(AuthManager, () => this.app.resolve('auth'));
|
|
15
|
+
|
|
16
|
+
// Register the authentication middleware
|
|
17
|
+
this.app.bind(Authenticate, () => {
|
|
18
|
+
return new Authenticate(this.app.resolve(AuthManager));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Boot the authentication services.
|
|
24
|
+
*/
|
|
25
|
+
public async boot() {
|
|
26
|
+
// Here we could register default guards etc.
|
|
27
|
+
}
|
|
28
|
+
}
|