@increase21/simplenodejs 1.0.3 → 1.0.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/dist/index.d.ts +2 -4
- package/dist/index.js +5 -18
- package/dist/router.d.ts +2 -5
- package/dist/router.js +5 -1
- package/dist/typings/general.d.ts +7 -6
- package/dist/typings/simpletypes.d.ts +4 -0
- package/dist/utils/simpleController.d.ts +7 -3
- package/dist/utils/simpleController.js +10 -5
- package/dist/utils/simplePlugins.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
export { CreateSimpleJsHttpServer } from "./server";
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export * from "./utils/simplePlugins";
|
|
5
|
-
export { Middleware, ErrorMiddleware, SimpleJsServer } from "./typings/general";
|
|
2
|
+
export { SetRequestCORS, SetRateLimiter, SetBodyParser } from "./utils/helpers";
|
|
3
|
+
export type { SimpleJsPrivateMethodProps } from "./typings/general";
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.CreateSimpleJsHttpServer = void 0;
|
|
3
|
+
exports.SetBodyParser = exports.SetRateLimiter = exports.SetRequestCORS = exports.CreateSimpleJsHttpServer = void 0;
|
|
18
4
|
var server_1 = require("./server");
|
|
19
5
|
Object.defineProperty(exports, "CreateSimpleJsHttpServer", { enumerable: true, get: function () { return server_1.CreateSimpleJsHttpServer; } });
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
var helpers_1 = require("./utils/helpers");
|
|
7
|
+
Object.defineProperty(exports, "SetRequestCORS", { enumerable: true, get: function () { return helpers_1.SetRequestCORS; } });
|
|
8
|
+
Object.defineProperty(exports, "SetRateLimiter", { enumerable: true, get: function () { return helpers_1.SetRateLimiter; } });
|
|
9
|
+
Object.defineProperty(exports, "SetBodyParser", { enumerable: true, get: function () { return helpers_1.SetBodyParser; } });
|
package/dist/router.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { RequestObject, ResponseObject } from "./typings/general";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Controller: any;
|
|
5
|
-
}
|
|
6
|
-
export declare function loadControllers(root?: string): Map<string, ControllerMeta>;
|
|
2
|
+
import { SimpleJsControllerMeta } from "./typings/simpletypes";
|
|
3
|
+
export declare function loadControllers(root?: string): Map<string, SimpleJsControllerMeta>;
|
|
7
4
|
export declare function setControllersDir(dir: string): void;
|
|
8
5
|
export declare function route(req: RequestObject, res: ResponseObject, controllersDir?: string): Promise<void>;
|
package/dist/router.js
CHANGED
|
@@ -70,7 +70,11 @@ async function route(req, res, controllersDir) {
|
|
|
70
70
|
if (id && id.length && (!controller[methodName].length || controller[methodName].length < id.length)) {
|
|
71
71
|
return res.status(404).json({ error: "The requested resource does not exist. Kindly check your url" });
|
|
72
72
|
}
|
|
73
|
-
controller
|
|
73
|
+
//bind the controller to use the global properties
|
|
74
|
+
controller.__bindContext({ req, res });
|
|
75
|
+
////if there's protected function to run
|
|
76
|
+
if (typeof controller.__checkContext === "function")
|
|
77
|
+
controller.__checkContext();
|
|
74
78
|
const result = await controller[methodName](...(id || []));
|
|
75
79
|
//if it's not responded
|
|
76
80
|
if (!res.writableEnded && result) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import http, { IncomingMessage, ServerResponse } from "http";
|
|
2
2
|
export type Next = () => Promise<void> | void;
|
|
3
3
|
export type Plugin = (app: SimpleJsServer, opts?: any) => Promise<void> | void;
|
|
4
|
+
export type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
4
5
|
export type ObjectPayload = {
|
|
5
6
|
[key: string]: any;
|
|
6
7
|
};
|
|
@@ -25,13 +26,13 @@ export interface ResponseObject extends ServerResponse {
|
|
|
25
26
|
text: (value?: string) => void;
|
|
26
27
|
}
|
|
27
28
|
export interface SubRequestHandler {
|
|
28
|
-
post?: (params:
|
|
29
|
-
put?: (params:
|
|
30
|
-
get?: (params:
|
|
31
|
-
delete?: (params:
|
|
32
|
-
patch?: (params:
|
|
29
|
+
post?: (params: SimpleJsPrivateMethodProps) => void;
|
|
30
|
+
put?: (params: SimpleJsPrivateMethodProps) => void;
|
|
31
|
+
get?: (params: SimpleJsPrivateMethodProps) => void;
|
|
32
|
+
delete?: (params: SimpleJsPrivateMethodProps) => void;
|
|
33
|
+
patch?: (params: SimpleJsPrivateMethodProps) => void;
|
|
33
34
|
}
|
|
34
|
-
export interface
|
|
35
|
+
export interface SimpleJsPrivateMethodProps {
|
|
35
36
|
body: ObjectPayload;
|
|
36
37
|
res: ResponseObject;
|
|
37
38
|
req: RequestObject;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { RequestObject, ResponseObject } from "../typings/general";
|
|
2
|
-
type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
1
|
+
import { HttpMethod, ObjectPayload, RequestObject, ResponseObject } from "../typings/general";
|
|
3
2
|
type SubRequestHandler = Partial<Record<HttpMethod, (params: PrivateMethodProps) => any>>;
|
|
4
3
|
interface PrivateMethodProps {
|
|
5
4
|
id?: string;
|
|
@@ -8,11 +7,16 @@ interface PrivateMethodProps {
|
|
|
8
7
|
export declare class SimpleNodeJsController {
|
|
9
8
|
protected req: RequestObject;
|
|
10
9
|
protected res: ResponseObject;
|
|
10
|
+
protected body: ObjectPayload;
|
|
11
|
+
protected query: ObjectPayload;
|
|
12
|
+
protected method: HttpMethod;
|
|
13
|
+
protected _custom_data: any;
|
|
11
14
|
/** framework-internal method */
|
|
12
|
-
|
|
15
|
+
__bindContext(ctx: {
|
|
13
16
|
req: RequestObject;
|
|
14
17
|
res: ResponseObject;
|
|
15
18
|
}): void;
|
|
19
|
+
__checkContext(): void;
|
|
16
20
|
protected RunRequest(handlers: SubRequestHandler, params?: PrivateMethodProps): any;
|
|
17
21
|
}
|
|
18
22
|
export {};
|
|
@@ -3,25 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SimpleNodeJsController = void 0;
|
|
4
4
|
class SimpleNodeJsController {
|
|
5
5
|
/** framework-internal method */
|
|
6
|
-
|
|
6
|
+
__bindContext(ctx) {
|
|
7
7
|
this.req = ctx.req;
|
|
8
8
|
this.res = ctx.res;
|
|
9
|
+
this.body = ctx.req.body;
|
|
10
|
+
this.query = ctx.req.query;
|
|
11
|
+
this.method = (ctx.req.method || "").toLocaleLowerCase();
|
|
12
|
+
this._custom_data = ctx.req._custom_data;
|
|
9
13
|
}
|
|
14
|
+
__checkContext() { }
|
|
10
15
|
RunRequest(handlers, params) {
|
|
11
16
|
const method = this.req.method?.toLowerCase();
|
|
12
17
|
if (!method) {
|
|
13
|
-
return this.res.status(400).json({ code: 400,
|
|
18
|
+
return this.res.status(400).json({ code: 400, error: "Invalid HTTP Method" });
|
|
14
19
|
}
|
|
15
20
|
const runFn = handlers[method];
|
|
16
21
|
if (typeof runFn !== "function") {
|
|
17
|
-
return this.res.status(405).json({ code: 405,
|
|
22
|
+
return this.res.status(405).json({ code: 405, error: "Method Not Allowed" });
|
|
18
23
|
}
|
|
19
24
|
// ID validation rules
|
|
20
25
|
if (params && params.id && (!params.idMethod || !params.idMethod[method])) {
|
|
21
|
-
return this.res.status(404).json({ code: 404,
|
|
26
|
+
return this.res.status(404).json({ code: 404, error: "Resource not found" });
|
|
22
27
|
}
|
|
23
28
|
if (params && params.idMethod?.[method] === "required" && !params.id) {
|
|
24
|
-
return this.res.status(404).json({ code: 404,
|
|
29
|
+
return this.res.status(404).json({ code: 404, error: "Resource not found" });
|
|
25
30
|
}
|
|
26
31
|
return runFn({
|
|
27
32
|
...(params || {}),
|
|
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SimpleJsSecurityPlugin = SimpleJsSecurityPlugin;
|
|
4
4
|
const helpers_1 = require("./helpers");
|
|
5
5
|
function SimpleJsSecurityPlugin(app, opts) {
|
|
6
|
-
app.use((0, helpers_1.SetRequestCORS)(opts.cors || []));
|
|
7
|
-
app.use((0, helpers_1.SetRateLimiter)(opts.rateLimit || { windowMs: 1000, max: 100 }));
|
|
6
|
+
opts.cors && app.use((0, helpers_1.SetRequestCORS)(opts.cors || []));
|
|
7
|
+
opts.rateLimit && app.use((0, helpers_1.SetRateLimiter)(opts.rateLimit || { windowMs: 1000, max: 100 }));
|
|
8
8
|
}
|
package/package.json
CHANGED