quantum-flow 1.0.1
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/.prettierrc.json +9 -0
- package/README.md +37 -0
- package/dist/app/aws/index.d.ts +1 -0
- package/dist/app/aws/index.js +17 -0
- package/dist/app/aws/lambda.d.ts +13 -0
- package/dist/app/aws/lambda.js +241 -0
- package/dist/app/http/Application.d.ts +23 -0
- package/dist/app/http/Application.js +208 -0
- package/dist/app/http/Socket.d.ts +16 -0
- package/dist/app/http/Socket.js +32 -0
- package/dist/app/http/decorators.d.ts +7 -0
- package/dist/app/http/decorators.js +81 -0
- package/dist/app/http/index.d.ts +3 -0
- package/dist/app/http/index.js +19 -0
- package/dist/app/http/websocket/WebsocetService.d.ts +20 -0
- package/dist/app/http/websocket/WebsocetService.js +41 -0
- package/dist/app/http/websocket/WebsocketServer.d.ts +30 -0
- package/dist/app/http/websocket/WebsocketServer.js +221 -0
- package/dist/constants.d.ts +16 -0
- package/dist/constants.js +24 -0
- package/dist/core/Controller.d.ts +43 -0
- package/dist/core/Controller.js +159 -0
- package/dist/core/Endpoint.d.ts +8 -0
- package/dist/core/Endpoint.js +43 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +19 -0
- package/dist/core/utils/extractors.d.ts +15 -0
- package/dist/core/utils/extractors.js +29 -0
- package/dist/core/utils/helpers.d.ts +4 -0
- package/dist/core/utils/helpers.js +22 -0
- package/dist/core/utils/index.d.ts +3 -0
- package/dist/core/utils/index.js +19 -0
- package/dist/core/utils/websocket.d.ts +8 -0
- package/dist/core/utils/websocket.js +45 -0
- package/dist/types/common.d.ts +47 -0
- package/dist/types/common.js +2 -0
- package/dist/types/controller.d.ts +4 -0
- package/dist/types/controller.js +2 -0
- package/dist/types/http.d.ts +18 -0
- package/dist/types/http.js +2 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +21 -0
- package/dist/types/lambda.d.ts +26 -0
- package/dist/types/lambda.js +2 -0
- package/dist/types/websocket.d.ts +55 -0
- package/dist/types/websocket.js +2 -0
- package/dist/utils/controller.d.ts +9 -0
- package/dist/utils/controller.js +111 -0
- package/dist/utils/helper.d.ts +1 -0
- package/dist/utils/helper.js +24 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +23 -0
- package/dist/utils/multipart.d.ts +15 -0
- package/dist/utils/multipart.js +109 -0
- package/dist/utils/parsers.d.ts +4 -0
- package/dist/utils/parsers.js +76 -0
- package/dist/utils/server.d.ts +4 -0
- package/dist/utils/server.js +46 -0
- package/dist/utils/transform.d.ts +1 -0
- package/dist/utils/transform.js +23 -0
- package/dist/utils/validate.d.ts +1 -0
- package/dist/utils/validate.js +46 -0
- package/dist/validators/Validate.d.ts +3 -0
- package/dist/validators/Validate.js +40 -0
- package/dist/validators/index.d.ts +1 -0
- package/dist/validators/index.js +17 -0
- package/eslint.config.mjs +84 -0
- package/nodemon.json +5 -0
- package/package.json +70 -0
- package/src/app/aws/index.ts +1 -0
- package/src/app/aws/lambda.ts +283 -0
- package/src/app/http/Application.ts +250 -0
- package/src/app/http/Socket.ts +38 -0
- package/src/app/http/decorators.ts +115 -0
- package/src/app/http/index.ts +3 -0
- package/src/app/http/websocket/WebsocetService.ts +44 -0
- package/src/app/http/websocket/WebsocketServer.ts +262 -0
- package/src/constants.ts +25 -0
- package/src/core/Controller.ts +229 -0
- package/src/core/Endpoint.ts +39 -0
- package/src/core/index.ts +14 -0
- package/src/core/utils/extractors.ts +32 -0
- package/src/core/utils/helpers.ts +22 -0
- package/src/core/utils/index.ts +3 -0
- package/src/core/utils/websocket.ts +45 -0
- package/src/types/common.ts +60 -0
- package/src/types/controller.ts +2 -0
- package/src/types/http.ts +19 -0
- package/src/types/index.ts +5 -0
- package/src/types/lambda.ts +28 -0
- package/src/types/websocket.ts +57 -0
- package/src/utils/controller.ts +143 -0
- package/src/utils/helper.ts +24 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/multipart.ts +93 -0
- package/src/utils/parsers.ts +87 -0
- package/src/utils/server.ts +49 -0
- package/src/utils/transform.ts +24 -0
- package/src/utils/validate.ts +53 -0
- package/src/validators/Validate.ts +48 -0
- package/src/validators/index.ts +1 -0
- package/tsconfig.json +51 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { plainToInstance } from 'class-transformer';
|
|
2
|
+
import { validate as Validate, ValidationError } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
export async function validate(dtoClass: any, data: any) {
|
|
5
|
+
if (!dtoClass) {
|
|
6
|
+
return data;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if (typeof dtoClass.from === 'function') {
|
|
11
|
+
return dtoClass.from(data);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (typeof dtoClass === 'function') {
|
|
15
|
+
const instance = plainToInstance(dtoClass, data);
|
|
16
|
+
const errors = await Validate(instance);
|
|
17
|
+
|
|
18
|
+
if (errors.length > 0) {
|
|
19
|
+
const formattedErrors = formatValidationErrors(errors);
|
|
20
|
+
|
|
21
|
+
throw {
|
|
22
|
+
status: 400,
|
|
23
|
+
message: 'Validation failed',
|
|
24
|
+
errors: formattedErrors,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return instance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return data;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function formatValidationErrors(errors: ValidationError[]): any[] {
|
|
38
|
+
return errors.map((error) => {
|
|
39
|
+
const constraints = error.constraints || {};
|
|
40
|
+
|
|
41
|
+
const children =
|
|
42
|
+
error.children && error.children.length > 0
|
|
43
|
+
? formatValidationErrors(error.children)
|
|
44
|
+
: undefined;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
property: error.property,
|
|
48
|
+
value: error.value,
|
|
49
|
+
constraints: Object.values(constraints),
|
|
50
|
+
children,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { validate } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
type ToValidate = 'query' | 'body' | 'params' | 'headers';
|
|
5
|
+
|
|
6
|
+
export function Validate(param: ToValidate, dtoClass: any) {
|
|
7
|
+
return function (target: any, propertyKey?: string, descriptor?: PropertyDescriptor): void {
|
|
8
|
+
// METHOD DECORATOR
|
|
9
|
+
if (descriptor) {
|
|
10
|
+
wrapMethod(descriptor, param, dtoClass);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// CLASS DECORATOR
|
|
15
|
+
const prototype = target.prototype;
|
|
16
|
+
|
|
17
|
+
Object.getOwnPropertyNames(prototype).forEach((method) => {
|
|
18
|
+
if (method === 'constructor') return;
|
|
19
|
+
|
|
20
|
+
const desc = Object.getOwnPropertyDescriptor(prototype, method);
|
|
21
|
+
if (!desc || typeof desc.value !== 'function') return;
|
|
22
|
+
|
|
23
|
+
wrapMethod(desc, param, dtoClass);
|
|
24
|
+
Object.defineProperty(prototype, method, desc);
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function wrapMethod(descriptor: PropertyDescriptor, param: any, dtoClass: any) {
|
|
30
|
+
const originalMethod = descriptor.value;
|
|
31
|
+
|
|
32
|
+
descriptor.value = async function (...args: any[]) {
|
|
33
|
+
const value = args[0][param];
|
|
34
|
+
const instance = new dtoClass();
|
|
35
|
+
|
|
36
|
+
Object.entries(value || {}).forEach(([key, val]) => {
|
|
37
|
+
instance[key] = val;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const errors = await validate(instance, { whitelist: true });
|
|
41
|
+
|
|
42
|
+
if (errors.length) {
|
|
43
|
+
throw { status: 422, message: 'Validation failed', data: errors };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return originalMethod.apply(this, args);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Validate';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"moduleResolution": "nodenext",
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"target": "es2022",
|
|
6
|
+
"types": [
|
|
7
|
+
"node"
|
|
8
|
+
],
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"outDir": "./dist",
|
|
11
|
+
"rootDir": "./src",
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"strict": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"experimentalDecorators": true,
|
|
16
|
+
"emitDecoratorMetadata": true,
|
|
17
|
+
"strictPropertyInitialization": false,
|
|
18
|
+
"typeRoots": [
|
|
19
|
+
"./node_modules/@types"
|
|
20
|
+
],
|
|
21
|
+
"paths": {
|
|
22
|
+
"@controllers": [
|
|
23
|
+
"./src/controllers/index.ts"
|
|
24
|
+
],
|
|
25
|
+
"@utils": [
|
|
26
|
+
"./src/utils/index.ts"
|
|
27
|
+
],
|
|
28
|
+
"@types": [
|
|
29
|
+
"./src/types/index.ts"
|
|
30
|
+
],
|
|
31
|
+
"@validators": [
|
|
32
|
+
"./src/validators/index.ts"
|
|
33
|
+
],
|
|
34
|
+
"@constants": [
|
|
35
|
+
"./src/constants.ts"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
"include": [
|
|
40
|
+
"src/**/*"
|
|
41
|
+
],
|
|
42
|
+
"exclude": [
|
|
43
|
+
"node_modules",
|
|
44
|
+
"dist",
|
|
45
|
+
"testing",
|
|
46
|
+
"./testing",
|
|
47
|
+
"**/*.test.ts",
|
|
48
|
+
"**/*.spec.ts",
|
|
49
|
+
"examples"
|
|
50
|
+
]
|
|
51
|
+
}
|