quantum-flow 1.4.0 → 1.7.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/README.md +20 -11
- package/dist/app/aws/lambda.d.ts +0 -3
- package/dist/app/aws/lambda.js +16 -119
- package/dist/app/aws/utils/helpers.d.ts +4 -0
- package/dist/app/aws/utils/helpers.js +83 -0
- package/dist/app/aws/utils/index.d.ts +3 -0
- package/dist/app/aws/utils/index.js +19 -0
- package/dist/app/aws/utils/request.d.ts +23 -0
- package/dist/app/aws/utils/request.js +96 -0
- package/dist/app/aws/utils/response.d.ts +13 -6
- package/dist/app/aws/utils/response.js +32 -8
- package/dist/app/http/Application.d.ts +1 -1
- package/dist/app/http/Application.js +29 -24
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -1
- package/dist/core/Controller.d.ts +6 -17
- package/dist/core/Controller.js +80 -80
- package/dist/core/Endpoint.js +1 -2
- package/dist/core/index.d.ts +1 -1
- package/dist/core/utils/index.d.ts +0 -3
- package/dist/core/utils/index.js +0 -3
- package/dist/examples/app.d.ts +0 -1
- package/dist/examples/app.js +69 -10
- package/dist/examples/controllers/user.js +43 -8
- package/dist/examples/controllers/userMetadata.js +54 -12
- package/dist/middlewares/catch.d.ts +2 -0
- package/dist/middlewares/catch.js +10 -0
- package/dist/middlewares/cors.d.ts +2 -0
- package/dist/{core/utils → middlewares}/cors.js +4 -4
- package/dist/middlewares/index.d.ts +5 -0
- package/dist/middlewares/index.js +21 -0
- package/dist/middlewares/sanitize.d.ts +2 -0
- package/dist/middlewares/sanitize.js +15 -0
- package/dist/{core/utils/helpers.js → middlewares/status.js} +1 -1
- package/dist/middlewares/use.d.ts +2 -0
- package/dist/middlewares/use.js +11 -0
- package/dist/types/common.d.ts +15 -19
- package/dist/types/controller.d.ts +4 -5
- package/dist/types/http.d.ts +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/lambda.d.ts +7 -22
- package/dist/types/multipart.d.ts +8 -0
- package/dist/types/multipart.js +2 -0
- package/dist/types/sanitize.d.ts +8 -0
- package/dist/types/sanitize.js +2 -0
- package/dist/utils/controller.d.ts +20 -4
- package/dist/utils/controller.js +64 -22
- package/dist/utils/cors.d.ts +2 -2
- package/dist/utils/cors.js +2 -1
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.js +9 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/multipart.d.ts +1 -8
- package/dist/utils/multipart.js +0 -1
- package/dist/utils/sanitize.d.ts +30 -0
- package/dist/utils/sanitize.js +134 -0
- package/dist/utils/server.js +4 -0
- package/package.json +9 -2
- package/dist/app/aws/helpers.d.ts +0 -25
- package/dist/app/aws/helpers.js +0 -46
- package/dist/core/utils/cors.d.ts +0 -2
- package/dist/core/utils/middlewares.d.ts +0 -3
- package/dist/core/utils/middlewares.js +0 -22
- /package/dist/{core/utils/helpers.d.ts → middlewares/status.d.ts} +0 -0
|
@@ -1,10 +1,43 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
2
18
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
19
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
20
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
21
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
22
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
23
|
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
8
41
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
42
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
43
|
};
|
|
@@ -14,15 +47,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
48
|
exports.UserMetadata = void 0;
|
|
16
49
|
const class_validator_1 = require("class-validator");
|
|
50
|
+
const Joi = __importStar(require("joi"));
|
|
17
51
|
const core_1 = require("quantum-flow/core");
|
|
52
|
+
const middlewares_1 = require("quantum-flow/middlewares");
|
|
18
53
|
class DTO {
|
|
19
|
-
|
|
20
|
-
meta;
|
|
54
|
+
name;
|
|
21
55
|
}
|
|
22
56
|
__decorate([
|
|
23
57
|
(0, class_validator_1.IsString)(),
|
|
24
58
|
__metadata("design:type", String)
|
|
25
|
-
], DTO.prototype, "
|
|
59
|
+
], DTO.prototype, "name", void 0);
|
|
60
|
+
const userSchema = Joi.object({
|
|
61
|
+
name: Joi.string().trim().min(2).max(50).required(),
|
|
62
|
+
});
|
|
26
63
|
let UserMetadata = class UserMetadata {
|
|
27
64
|
async getUserMetadata(params) {
|
|
28
65
|
return params;
|
|
@@ -40,10 +77,17 @@ __decorate([
|
|
|
40
77
|
__metadata("design:returntype", Promise)
|
|
41
78
|
], UserMetadata.prototype, "getUserMetadata", null);
|
|
42
79
|
__decorate([
|
|
43
|
-
(0, core_1.POST)('/:meta'),
|
|
80
|
+
(0, core_1.POST)('/:meta', [function s4() { }]),
|
|
81
|
+
(0, middlewares_1.Sanitize)({
|
|
82
|
+
schema: userSchema,
|
|
83
|
+
action: 'both',
|
|
84
|
+
options: { abortEarly: false },
|
|
85
|
+
stripUnknown: true,
|
|
86
|
+
type: 'body',
|
|
87
|
+
}),
|
|
44
88
|
__param(0, (0, core_1.Multipart)()),
|
|
45
|
-
__param(1, (0, core_1.Body)()),
|
|
46
|
-
__param(2, (0, core_1.Params)(
|
|
89
|
+
__param(1, (0, core_1.Body)(DTO)),
|
|
90
|
+
__param(2, (0, core_1.Params)('meta')),
|
|
47
91
|
__metadata("design:type", Function),
|
|
48
92
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
49
93
|
__metadata("design:returntype", Promise)
|
|
@@ -51,10 +95,8 @@ __decorate([
|
|
|
51
95
|
exports.UserMetadata = UserMetadata = __decorate([
|
|
52
96
|
(0, core_1.Controller)({
|
|
53
97
|
prefix: 'metadata',
|
|
54
|
-
middlewares: [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
],
|
|
59
|
-
})
|
|
98
|
+
middlewares: [function s2() { }],
|
|
99
|
+
}),
|
|
100
|
+
(0, middlewares_1.Use)([function s3() { }]),
|
|
101
|
+
(0, middlewares_1.Cors)({ origin: '*' })
|
|
60
102
|
], UserMetadata);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Catch = Catch;
|
|
4
|
+
const _constants_1 = require("../constants.js");
|
|
5
|
+
function Catch(handler) {
|
|
6
|
+
return function (target) {
|
|
7
|
+
Reflect.defineMetadata(_constants_1.CATCH, handler, target);
|
|
8
|
+
return target;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const _constants_1 = require("
|
|
5
|
-
const _types_1 = require("
|
|
6
|
-
function
|
|
3
|
+
exports.Cors = Cors;
|
|
4
|
+
const _constants_1 = require("../constants.js");
|
|
5
|
+
const _types_1 = require("../types/index.js");
|
|
6
|
+
function Cors(config = {}) {
|
|
7
7
|
return function (target, propertyKey, descriptor) {
|
|
8
8
|
const defaultConfig = {
|
|
9
9
|
origin: '*',
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./catch"), exports);
|
|
18
|
+
__exportStar(require("./cors"), exports);
|
|
19
|
+
__exportStar(require("./sanitize"), exports);
|
|
20
|
+
__exportStar(require("./status"), exports);
|
|
21
|
+
__exportStar(require("./use"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Sanitize = Sanitize;
|
|
4
|
+
const _constants_1 = require("../constants.js");
|
|
5
|
+
function Sanitize(sanitizeConfig) {
|
|
6
|
+
return function (target, propertyKey, descriptor) {
|
|
7
|
+
const configs = Array.isArray(sanitizeConfig) ? sanitizeConfig : [sanitizeConfig];
|
|
8
|
+
if (propertyKey && descriptor) {
|
|
9
|
+
Reflect.defineMetadata(_constants_1.SANITIZE, configs, target, propertyKey);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
Reflect.defineMetadata(_constants_1.SANITIZE, configs, target.prototype || target);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Ok204 = exports.Ok201 = exports.Ok200 = void 0;
|
|
4
4
|
exports.Status = Status;
|
|
5
|
-
const _constants_1 = require("
|
|
5
|
+
const _constants_1 = require("../constants.js");
|
|
6
6
|
/**
|
|
7
7
|
* Method or class decorator to set the HTTP status code for the response.
|
|
8
8
|
* @param status HTTP status code to set.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Use = Use;
|
|
4
|
+
const _constants_1 = require("../constants.js");
|
|
5
|
+
function Use(middleware) {
|
|
6
|
+
return function (target) {
|
|
7
|
+
const existed = Reflect.getMetadata(_constants_1.USE_MIDDLEWARE, target) || [];
|
|
8
|
+
Reflect.defineMetadata(_constants_1.USE_MIDDLEWARE, existed.concat(middleware).reverse(), target);
|
|
9
|
+
return target;
|
|
10
|
+
};
|
|
11
|
+
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
import { LambdaRequest } from './lambda';
|
|
3
|
+
import { MultipartFile } from './multipart';
|
|
3
4
|
export interface HttpError extends Error {
|
|
4
5
|
statusCode?: number;
|
|
5
6
|
status: number;
|
|
@@ -9,25 +10,21 @@ export interface HttpError extends Error {
|
|
|
9
10
|
message: string;
|
|
10
11
|
}>;
|
|
11
12
|
}
|
|
12
|
-
export type AppRequest
|
|
13
|
+
export type AppRequest = {
|
|
14
|
+
requestUrl: URL;
|
|
13
15
|
method: HTTP_METHODS;
|
|
14
|
-
url: URL;
|
|
15
16
|
path?: string;
|
|
16
|
-
headers:
|
|
17
|
-
query?:
|
|
18
|
-
params?:
|
|
19
|
-
body:
|
|
17
|
+
headers: Record<string, string | string[]>;
|
|
18
|
+
query?: Record<string, string | string[]>;
|
|
19
|
+
params?: Record<string, string>;
|
|
20
|
+
body: any;
|
|
20
21
|
rawBody: Buffer<ArrayBufferLike>;
|
|
21
22
|
isBase64Encoded?: boolean;
|
|
22
23
|
cookies: Record<string, string>;
|
|
24
|
+
multipart?: Record<string, MultipartFile | MultipartFile[]>;
|
|
23
25
|
_startTime: number;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
requestId?: string;
|
|
27
|
-
stage?: string;
|
|
28
|
-
sourceIp?: string;
|
|
29
|
-
userAgent?: string;
|
|
30
|
-
};
|
|
26
|
+
end: () => any;
|
|
27
|
+
} & (IncomingMessage | LambdaRequest);
|
|
31
28
|
export type Router = (req: AppRequest, res?: ServerResponse) => Promise<{
|
|
32
29
|
status: number;
|
|
33
30
|
data: any;
|
|
@@ -41,9 +38,9 @@ export type EndpointResponse<T = any> = {
|
|
|
41
38
|
export interface IController {
|
|
42
39
|
handleRequest: Router;
|
|
43
40
|
}
|
|
44
|
-
export type MiddlewareCB = (
|
|
45
|
-
export type InterceptorCB = (data: any, req?:
|
|
46
|
-
export type ErrorCB = (error: HttpError, req?:
|
|
41
|
+
export type MiddlewareCB = (request: AppRequest, response: ServerResponse, next: (args?: any) => any) => void | Promise<AppRequest> | AppRequest;
|
|
42
|
+
export type InterceptorCB = (data: any, req?: AppRequest, res?: ServerResponse) => Promise<unknown> | unknown;
|
|
43
|
+
export type ErrorCB = (error: HttpError, req?: AppRequest, res?: ServerResponse) => Promise<ResponseWithStatus> | ResponseWithStatus;
|
|
47
44
|
export type ParamDecoratorType = 'body' | 'params' | 'query' | 'request' | 'headers' | 'cookies' | 'response' | 'multipart' | 'event' | 'context';
|
|
48
45
|
export interface ParamMetadata {
|
|
49
46
|
index: number;
|
|
@@ -65,4 +62,3 @@ export declare enum HTTP_METHODS {
|
|
|
65
62
|
OPTIONS = "OPTIONS",
|
|
66
63
|
HEAD = "HEAD"
|
|
67
64
|
}
|
|
68
|
-
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AppRequest, ErrorCB, HTTP_METHODS, InterceptorCB, MiddlewareCB } from './common';
|
|
1
|
+
import { ErrorCB, HTTP_METHODS, InterceptorCB, MiddlewareCB } from './common';
|
|
3
2
|
import { CORSConfig } from './cors';
|
|
3
|
+
import { SanitizerConfig } from './sanitize';
|
|
4
4
|
export type ControllerClass = {
|
|
5
5
|
new (...args: any[]): any;
|
|
6
6
|
};
|
|
@@ -18,6 +18,7 @@ export type ControllerMetadata = {
|
|
|
18
18
|
subControllers: ControllerClass[];
|
|
19
19
|
errorHandler?: ErrorCB;
|
|
20
20
|
cors?: CORSConfig;
|
|
21
|
+
sanitizers: SanitizerConfig[];
|
|
21
22
|
};
|
|
22
23
|
export interface ControllerConfig {
|
|
23
24
|
prefix: string;
|
|
@@ -30,12 +31,10 @@ export type RouteContext = {
|
|
|
30
31
|
controllerMeta: ControllerMetadata;
|
|
31
32
|
path: string;
|
|
32
33
|
method: string;
|
|
33
|
-
appRequest: AppRequest;
|
|
34
|
-
request?: IncomingMessage;
|
|
35
|
-
response?: ServerResponse;
|
|
36
34
|
middlewareChain: MiddlewareCB[];
|
|
37
35
|
interceptorChain: InterceptorCB[];
|
|
38
36
|
corsChain: CORSConfig[];
|
|
39
37
|
errorHandlerChain: ErrorCB[];
|
|
40
38
|
subPath: string;
|
|
39
|
+
sanitizersChain: SanitizerConfig[];
|
|
41
40
|
};
|
package/dist/types/http.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ErrorCB, InterceptorCB, MiddlewareCB } from './common';
|
|
2
2
|
import { CORSConfig } from './cors';
|
|
3
|
+
import { SanitizerConfig } from './sanitize';
|
|
3
4
|
export interface ServerConfig {
|
|
4
5
|
port?: number;
|
|
5
6
|
host?: string;
|
|
@@ -8,6 +9,7 @@ export interface ServerConfig {
|
|
|
8
9
|
errorHandler?: ErrorCB;
|
|
9
10
|
controllers?: (new (...args: any[]) => any)[];
|
|
10
11
|
cors?: CORSConfig;
|
|
12
|
+
sanitizers?: SanitizerConfig[];
|
|
11
13
|
websocket?: {
|
|
12
14
|
enabled: boolean;
|
|
13
15
|
path?: string;
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -19,4 +19,6 @@ __exportStar(require("./controller"), exports);
|
|
|
19
19
|
__exportStar(require("./cors"), exports);
|
|
20
20
|
__exportStar(require("./http"), exports);
|
|
21
21
|
__exportStar(require("./lambda"), exports);
|
|
22
|
+
__exportStar(require("./multipart"), exports);
|
|
23
|
+
__exportStar(require("./sanitize"), exports);
|
|
22
24
|
__exportStar(require("./websocket"), exports);
|
package/dist/types/lambda.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIGatewayProxyEvent, APIGatewayProxyEventV2
|
|
1
|
+
import { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from 'aws-lambda';
|
|
2
2
|
export interface LambdaFunctionUrlEvent {
|
|
3
3
|
version: string;
|
|
4
4
|
routeKey: string;
|
|
@@ -40,27 +40,12 @@ export interface NormalizedEvent {
|
|
|
40
40
|
requestContext: any;
|
|
41
41
|
}
|
|
42
42
|
export interface LambdaRequest {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
cookies: Record<string, string>;
|
|
50
|
-
context: Context;
|
|
51
|
-
isBase64Encoded: boolean;
|
|
52
|
-
requestId: string;
|
|
53
|
-
stage: string;
|
|
54
|
-
sourceIp: string;
|
|
55
|
-
userAgent: string;
|
|
56
|
-
url: URL;
|
|
57
|
-
event: NormalizedEvent;
|
|
58
|
-
_startTime: number;
|
|
59
|
-
routeInfo?: {
|
|
60
|
-
controller: any;
|
|
61
|
-
methodName: string;
|
|
62
|
-
pathParams: Record<string, string>;
|
|
63
|
-
};
|
|
43
|
+
event?: any;
|
|
44
|
+
context?: any;
|
|
45
|
+
requestId?: string;
|
|
46
|
+
stage?: string;
|
|
47
|
+
sourceIp?: string;
|
|
48
|
+
userAgent?: string;
|
|
64
49
|
}
|
|
65
50
|
export interface LambdaResponse {
|
|
66
51
|
statusCode: number;
|
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
import { ControllerInstance, ControllerMethods, CORSConfig, MiddlewareCB } from '../types/index.js';
|
|
2
|
-
import {
|
|
3
|
-
export declare const executeControllerMethod: (controller: ControllerInstance, propertyName: string,
|
|
1
|
+
import { AppRequest, ControllerInstance, ControllerMethods, CORSConfig, InterceptorCB, MiddlewareCB, SanitizerConfig } from '../types/index.js';
|
|
2
|
+
import { ServerResponse } from 'http';
|
|
3
|
+
export declare const executeControllerMethod: (controller: ControllerInstance, propertyName: string, request: AppRequest, response: ServerResponse) => Promise<any>;
|
|
4
4
|
export declare const getControllerMethods: (controller: ControllerInstance) => ControllerMethods;
|
|
5
5
|
export declare const getAllMethods: (obj: any) => string[];
|
|
6
6
|
export declare const findRouteInController: (instance: any, path: string, route: string, method: string) => {
|
|
7
7
|
name: string;
|
|
8
8
|
pathParams: Record<string, string>;
|
|
9
9
|
priority: number;
|
|
10
|
-
|
|
10
|
+
middlewares: MiddlewareCB[];
|
|
11
11
|
cors?: CORSConfig;
|
|
12
|
+
sanitizers: SanitizerConfig[];
|
|
12
13
|
};
|
|
14
|
+
export declare const NextFN: (error: any) => void;
|
|
15
|
+
export declare const getResponse: (data: {
|
|
16
|
+
controllerInstance: ControllerInstance;
|
|
17
|
+
name: string;
|
|
18
|
+
interceptors: InterceptorCB[];
|
|
19
|
+
request: AppRequest;
|
|
20
|
+
response: ServerResponse;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
status: number;
|
|
23
|
+
data: any;
|
|
24
|
+
}>;
|
|
25
|
+
export declare const applyMiddlewaresVsSanitizers: (request: AppRequest, response: ServerResponse, functions: {
|
|
26
|
+
sanitizers: SanitizerConfig[][];
|
|
27
|
+
middlewares: MiddlewareCB[][];
|
|
28
|
+
}) => Promise<void>;
|
package/dist/utils/controller.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findRouteInController = exports.getAllMethods = exports.getControllerMethods = exports.executeControllerMethod = void 0;
|
|
3
|
+
exports.applyMiddlewaresVsSanitizers = exports.getResponse = exports.NextFN = exports.findRouteInController = exports.getAllMethods = exports.getControllerMethods = exports.executeControllerMethod = void 0;
|
|
4
4
|
const _constants_1 = require("../constants.js");
|
|
5
5
|
const _types_1 = require("../types/index.js");
|
|
6
6
|
const WebsocketService_1 = require("../app/http/websocket/WebsocketService");
|
|
7
7
|
const helper_1 = require("./helper");
|
|
8
8
|
const multipart_1 = require("./multipart");
|
|
9
|
+
const sanitize_1 = require("./sanitize");
|
|
9
10
|
const validate_1 = require("./validate");
|
|
10
|
-
const getBodyAndMultipart = (
|
|
11
|
-
let body =
|
|
11
|
+
const getBodyAndMultipart = (request) => {
|
|
12
|
+
let body = request.body;
|
|
12
13
|
let multipart;
|
|
13
|
-
if (multipart_1.MultipartProcessor.isMultipart({ headers:
|
|
14
|
+
if (multipart_1.MultipartProcessor.isMultipart({ headers: request.headers })) {
|
|
14
15
|
try {
|
|
15
16
|
const { fields, files } = multipart_1.MultipartProcessor.parse({
|
|
16
|
-
body:
|
|
17
|
-
headers:
|
|
18
|
-
isBase64Encoded:
|
|
17
|
+
body: request.rawBody || request.body,
|
|
18
|
+
headers: request.headers,
|
|
19
|
+
isBase64Encoded: request.isBase64Encoded,
|
|
19
20
|
});
|
|
20
21
|
multipart = files;
|
|
21
22
|
body = fields;
|
|
@@ -27,7 +28,7 @@ const getBodyAndMultipart = (payload) => {
|
|
|
27
28
|
}
|
|
28
29
|
return { multipart, body };
|
|
29
30
|
};
|
|
30
|
-
const executeControllerMethod = async (controller, propertyName,
|
|
31
|
+
const executeControllerMethod = async (controller, propertyName, request, response) => {
|
|
31
32
|
const fn = controller[propertyName];
|
|
32
33
|
if (typeof fn !== 'function')
|
|
33
34
|
return null;
|
|
@@ -35,19 +36,15 @@ const executeControllerMethod = async (controller, propertyName, payload, reques
|
|
|
35
36
|
if (!endpointMeta)
|
|
36
37
|
return null;
|
|
37
38
|
const methodMiddlewares = Reflect.getMetadata(_constants_1.MIDDLEWARES, controller, propertyName) || [];
|
|
38
|
-
for (let
|
|
39
|
-
|
|
40
|
-
const result = await middleware(payload);
|
|
41
|
-
if (result) {
|
|
42
|
-
payload = { ...payload, ...result };
|
|
43
|
-
}
|
|
39
|
+
for (let middleware of methodMiddlewares) {
|
|
40
|
+
await middleware(request, response, exports.NextFN);
|
|
44
41
|
}
|
|
45
42
|
const prototype = Object.getPrototypeOf(controller);
|
|
46
43
|
const paramMetadata = Reflect.getMetadata(_constants_1.PARAM_METADATA_KEY, prototype, propertyName) || [];
|
|
47
44
|
if (paramMetadata.length === 0) {
|
|
48
|
-
return fn.call(controller,
|
|
45
|
+
return fn.call(controller, request, response);
|
|
49
46
|
}
|
|
50
|
-
const { body, multipart } = getBodyAndMultipart(
|
|
47
|
+
const { body, multipart } = getBodyAndMultipart(request);
|
|
51
48
|
const args = [];
|
|
52
49
|
const wsParams = Reflect.getMetadata(_constants_1.WS_SERVICE_KEY, controller, propertyName) || [];
|
|
53
50
|
const totalParams = Math.max(paramMetadata.length ? Math.max(...paramMetadata.map((p) => p.index)) + 1 : 0, wsParams.length ? Math.max(...wsParams.map((p) => p.index)) + 1 : 0);
|
|
@@ -62,13 +59,12 @@ const executeControllerMethod = async (controller, propertyName, payload, reques
|
|
|
62
59
|
args[i] = undefined;
|
|
63
60
|
continue;
|
|
64
61
|
}
|
|
65
|
-
let value = param.name
|
|
62
|
+
let value = param.name
|
|
63
|
+
? request[param.type]?.[param.name]
|
|
64
|
+
: request[param.type];
|
|
66
65
|
if (param.type === 'multipart') {
|
|
67
66
|
value = multipart;
|
|
68
67
|
}
|
|
69
|
-
if (param.type === 'request') {
|
|
70
|
-
value = payload;
|
|
71
|
-
}
|
|
72
68
|
if (param.type === 'request') {
|
|
73
69
|
value = request;
|
|
74
70
|
}
|
|
@@ -139,9 +135,10 @@ const findRouteInController = (instance, path, route, method) => {
|
|
|
139
135
|
].includes(name))
|
|
140
136
|
continue;
|
|
141
137
|
const endpointMeta = Reflect.getMetadata(_constants_1.ENDPOINT, prototype, name) || [];
|
|
138
|
+
const sanitizers = Reflect.getMetadata(_constants_1.SANITIZE, prototype, name) ?? [];
|
|
142
139
|
if (endpointMeta.length === 0)
|
|
143
140
|
continue;
|
|
144
|
-
const [httpMethod, routePattern] = endpointMeta;
|
|
141
|
+
const [httpMethod, routePattern, middlewares] = endpointMeta;
|
|
145
142
|
if (httpMethod !== method && httpMethod !== 'USE') {
|
|
146
143
|
continue;
|
|
147
144
|
}
|
|
@@ -159,7 +156,8 @@ const findRouteInController = (instance, path, route, method) => {
|
|
|
159
156
|
pathParams,
|
|
160
157
|
priority,
|
|
161
158
|
cors: Reflect.getMetadata(_constants_1.CORS_METADATA, prototype, name),
|
|
162
|
-
|
|
159
|
+
middlewares,
|
|
160
|
+
sanitizers,
|
|
163
161
|
});
|
|
164
162
|
}
|
|
165
163
|
}
|
|
@@ -167,3 +165,47 @@ const findRouteInController = (instance, path, route, method) => {
|
|
|
167
165
|
return matches[0] || null;
|
|
168
166
|
};
|
|
169
167
|
exports.findRouteInController = findRouteInController;
|
|
168
|
+
const NextFN = (error) => {
|
|
169
|
+
if (error)
|
|
170
|
+
throw { status: error.status ?? 500, message: error.message ?? error };
|
|
171
|
+
};
|
|
172
|
+
exports.NextFN = NextFN;
|
|
173
|
+
const getResponse = async (data) => {
|
|
174
|
+
try {
|
|
175
|
+
let appResponse = await (0, exports.executeControllerMethod)(data.controllerInstance, data.name, data.request, data.response);
|
|
176
|
+
data.response.statusCode = appResponse.status ?? 200;
|
|
177
|
+
const isError = !_constants_1.OK_STATUSES.includes(data.response.statusCode);
|
|
178
|
+
const interceptors = data.interceptors.reverse();
|
|
179
|
+
for (let index = 0; index < interceptors?.length && !isError; index++) {
|
|
180
|
+
const interceptor = interceptors[index];
|
|
181
|
+
appResponse = await interceptor(appResponse, data.request, data.response);
|
|
182
|
+
}
|
|
183
|
+
const propertyName = data.name;
|
|
184
|
+
const prototype = Object.getPrototypeOf(data.controllerInstance);
|
|
185
|
+
const methodOkStatus = Reflect.getMetadata(_constants_1.OK_METADATA_KEY, data.controllerInstance, propertyName);
|
|
186
|
+
if (methodOkStatus) {
|
|
187
|
+
!isError && (data.response.statusCode = methodOkStatus);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const classOkStatus = Reflect.getMetadata(_constants_1.OK_METADATA_KEY, prototype);
|
|
191
|
+
!isError && classOkStatus && (data.response.statusCode = classOkStatus);
|
|
192
|
+
}
|
|
193
|
+
return { status: data.response.statusCode, data: appResponse };
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
exports.getResponse = getResponse;
|
|
200
|
+
const applyMiddlewaresVsSanitizers = async (request, response, functions) => {
|
|
201
|
+
const length = Math.max(functions.sanitizers.length, functions.middlewares.length);
|
|
202
|
+
for (let i = 0; i < length; i++) {
|
|
203
|
+
const mws = functions.middlewares[i] ?? [];
|
|
204
|
+
const sntzs = functions.sanitizers[i] ?? [];
|
|
205
|
+
(0, sanitize_1.sanitizeRequest)(request, sntzs);
|
|
206
|
+
for (let middleware of mws) {
|
|
207
|
+
await middleware(request, response, exports.NextFN);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
exports.applyMiddlewaresVsSanitizers = applyMiddlewaresVsSanitizers;
|
package/dist/utils/cors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CORSConfig } from '../types/index.js';
|
|
2
|
-
export declare function handleCORS(req:
|
|
1
|
+
import { AppRequest, CORSConfig } from '../types/index.js';
|
|
2
|
+
export declare function handleCORS(req: AppRequest, res: any, config: CORSConfig): {
|
|
3
3
|
permitted: boolean;
|
|
4
4
|
continue: boolean;
|
|
5
5
|
};
|
package/dist/utils/cors.js
CHANGED
|
@@ -4,8 +4,9 @@ exports.getCORSHeaders = exports.getCORSConfig = void 0;
|
|
|
4
4
|
exports.handleCORS = handleCORS;
|
|
5
5
|
exports.isPreflightRequest = isPreflightRequest;
|
|
6
6
|
const _constants_1 = require("../constants.js");
|
|
7
|
+
const headers_1 = require("./headers");
|
|
7
8
|
function handleCORS(req, res, config) {
|
|
8
|
-
const origin =
|
|
9
|
+
const origin = (0, headers_1.getOrigin)(req);
|
|
9
10
|
function isOriginAllowed() {
|
|
10
11
|
if (!origin)
|
|
11
12
|
return false;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOrigin = void 0;
|
|
4
|
+
const getOrigin = (req) => {
|
|
5
|
+
let originHeader = req.headers.origin || req.headers.Origin || req.requestUrl.origin;
|
|
6
|
+
const origin = (Array.isArray(originHeader) ? originHeader[0] : originHeader);
|
|
7
|
+
return origin;
|
|
8
|
+
};
|
|
9
|
+
exports.getOrigin = getOrigin;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export * from './controller';
|
|
2
2
|
export * from './cors';
|
|
3
3
|
export * from './endpoint';
|
|
4
|
+
export * from './headers';
|
|
4
5
|
export * from './helper';
|
|
5
6
|
export * from './lambda';
|
|
6
7
|
export * from './multipart';
|
|
7
8
|
export * from './parsers';
|
|
9
|
+
export * from './sanitize';
|
|
8
10
|
export * from './server';
|
|
9
11
|
export * from './transform';
|
|
10
12
|
export * from './validate';
|
package/dist/utils/index.js
CHANGED
|
@@ -17,10 +17,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./controller"), exports);
|
|
18
18
|
__exportStar(require("./cors"), exports);
|
|
19
19
|
__exportStar(require("./endpoint"), exports);
|
|
20
|
+
__exportStar(require("./headers"), exports);
|
|
20
21
|
__exportStar(require("./helper"), exports);
|
|
21
22
|
__exportStar(require("./lambda"), exports);
|
|
22
23
|
__exportStar(require("./multipart"), exports);
|
|
23
24
|
__exportStar(require("./parsers"), exports);
|
|
25
|
+
__exportStar(require("./sanitize"), exports);
|
|
24
26
|
__exportStar(require("./server"), exports);
|
|
25
27
|
__exportStar(require("./transform"), exports);
|
|
26
28
|
__exportStar(require("./validate"), exports);
|