@increase21/simplenodejs 1.0.2 → 1.0.3
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/dist/router.d.ts +1 -0
- package/dist/router.js +5 -1
- package/dist/server.d.ts +3 -1
- package/dist/server.js +6 -3
- package/dist/typings/general.d.ts +3 -2
- package/dist/utils/simplePlugins.d.ts +1 -1
- package/dist/utils/simplePlugins.js +2 -2
- package/package.json +10 -2
package/dist/router.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export interface ControllerMeta {
|
|
|
4
4
|
Controller: any;
|
|
5
5
|
}
|
|
6
6
|
export declare function loadControllers(root?: string): Map<string, ControllerMeta>;
|
|
7
|
+
export declare function setControllersDir(dir: string): void;
|
|
7
8
|
export declare function route(req: RequestObject, res: ResponseObject, controllersDir?: string): Promise<void>;
|
package/dist/router.js
CHANGED
|
@@ -4,10 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.loadControllers = loadControllers;
|
|
7
|
+
exports.setControllersDir = setControllersDir;
|
|
7
8
|
exports.route = route;
|
|
8
9
|
// router.ts
|
|
9
10
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
11
|
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
+
let controllers = new Map();
|
|
11
13
|
function loadControllers(root = "controllers") {
|
|
12
14
|
const base = node_path_1.default.resolve(process.cwd(), root);
|
|
13
15
|
const map = new Map();
|
|
@@ -37,7 +39,9 @@ function loadControllers(root = "controllers") {
|
|
|
37
39
|
walk(base);
|
|
38
40
|
return map;
|
|
39
41
|
}
|
|
40
|
-
|
|
42
|
+
function setControllersDir(dir) {
|
|
43
|
+
controllers = loadControllers(dir);
|
|
44
|
+
}
|
|
41
45
|
async function route(req, res, controllersDir) {
|
|
42
46
|
// console.log("Router is called")
|
|
43
47
|
const url = new URL(req.url, "http://localhost");
|
package/dist/server.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from "http";
|
|
2
2
|
import { SimpleJsServer } from "./typings/general";
|
|
3
|
-
export declare const CreateSimpleJsHttpServer: (handler?: (req: IncomingMessage, res: ServerResponse) => void
|
|
3
|
+
export declare const CreateSimpleJsHttpServer: (handler?: (req: IncomingMessage, res: ServerResponse) => void, opts?: {
|
|
4
|
+
controllersDir: string;
|
|
5
|
+
}) => SimpleJsServer;
|
package/dist/server.js
CHANGED
|
@@ -29,10 +29,13 @@ const extension = (req, res) => {
|
|
|
29
29
|
req.id = node_crypto_1.default.randomUUID();
|
|
30
30
|
res.setHeader("X-Request-Id", req.id);
|
|
31
31
|
};
|
|
32
|
-
const CreateSimpleJsHttpServer = (handler) => {
|
|
32
|
+
const CreateSimpleJsHttpServer = (handler, opts) => {
|
|
33
33
|
const middlewares = [];
|
|
34
34
|
const errorMiddlewares = [];
|
|
35
35
|
const plugins = [];
|
|
36
|
+
//set the director of the controller
|
|
37
|
+
if (opts?.controllersDir)
|
|
38
|
+
(0, router_1.setControllersDir)(opts.controllersDir);
|
|
36
39
|
const server = http_1.default.createServer(async (req, res) => {
|
|
37
40
|
extension(req, res);
|
|
38
41
|
const run = (0, helpers_1.composeWithError)(middlewares, errorMiddlewares);
|
|
@@ -60,9 +63,9 @@ const CreateSimpleJsHttpServer = (handler) => {
|
|
|
60
63
|
});
|
|
61
64
|
server.use = mw => { middlewares.push(mw); };
|
|
62
65
|
server.useError = mw => errorMiddlewares.push(mw);
|
|
63
|
-
server.
|
|
66
|
+
server.registerPlugin = async (plugin) => {
|
|
64
67
|
plugins.push(plugin);
|
|
65
|
-
await plugin(server
|
|
68
|
+
await plugin(server);
|
|
66
69
|
};
|
|
67
70
|
return server;
|
|
68
71
|
};
|
|
@@ -9,14 +9,15 @@ export type ErrorMiddleware = (err: any, req: RequestObject, res: ResponseObject
|
|
|
9
9
|
export interface SimpleJsServer extends http.Server {
|
|
10
10
|
use(mw: Middleware): Promise<any> | void;
|
|
11
11
|
useError: (mw: ErrorMiddleware) => void;
|
|
12
|
-
|
|
12
|
+
registerPlugin: (plugin: Plugin) => Promise<void>;
|
|
13
13
|
_environment: 'dev' | 'stag' | 'live';
|
|
14
14
|
}
|
|
15
15
|
export interface RequestObject extends IncomingMessage {
|
|
16
16
|
body?: any;
|
|
17
17
|
query?: any;
|
|
18
|
-
_server_environment?: 'dev' | 'stag' | 'live';
|
|
19
18
|
id?: string;
|
|
19
|
+
_server_environment?: 'dev' | 'stag' | 'live';
|
|
20
|
+
_custom_data?: ObjectPayload;
|
|
20
21
|
}
|
|
21
22
|
export interface ResponseObject extends ServerResponse {
|
|
22
23
|
status: (value: number) => ResponseObject;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SimpleJsServer } from "../typings/general";
|
|
2
2
|
import { SimpleJSRateLimitType } from "../typings/simpletypes";
|
|
3
|
-
export declare function
|
|
3
|
+
export declare function SimpleJsSecurityPlugin(app: SimpleJsServer, opts: {
|
|
4
4
|
cors?: {
|
|
5
5
|
name: string;
|
|
6
6
|
value: string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.SimpleJsSecurityPlugin = SimpleJsSecurityPlugin;
|
|
4
4
|
const helpers_1 = require("./helpers");
|
|
5
|
-
function
|
|
5
|
+
function SimpleJsSecurityPlugin(app, opts) {
|
|
6
6
|
app.use((0, helpers_1.SetRequestCORS)(opts.cors || []));
|
|
7
7
|
app.use((0, helpers_1.SetRateLimiter)(opts.rateLimit || { windowMs: 1000, max: 100 }));
|
|
8
8
|
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@increase21/simplenodejs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Lightweight Node.js HTTP framework with middleware and plugins",
|
|
5
5
|
"dev": "dist/index.js",
|
|
6
6
|
"bugs": "https://github.com/increase21/simplenodejs/issues",
|
|
7
|
+
"main": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
8
16
|
"repository": {
|
|
9
17
|
"type": "git",
|
|
10
18
|
"url": "https://github.com/increase21/simplenodejs.git",
|
|
11
19
|
"directory": ""
|
|
12
20
|
},
|
|
13
21
|
"files": [
|
|
14
|
-
"dist
|
|
22
|
+
"dist"
|
|
15
23
|
],
|
|
16
24
|
"scripts": {
|
|
17
25
|
"build": "tsc",
|