@avleon/core 0.0.45 → 0.0.48
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 +355 -369
- package/dist/chunk-9hOWP6kD.cjs +64 -0
- package/dist/chunk-DORXReHP.js +37 -0
- package/dist/index-BxIMWhgy.d.ts +1284 -0
- package/dist/index-DPn7qtzq.d.cts +1283 -0
- package/dist/index.cjs +3194 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +3022 -83
- package/dist/index.js.map +1 -0
- package/dist/lib-Bk8hUm06.cjs +7847 -0
- package/dist/lib-Bk8hUm06.cjs.map +1 -0
- package/dist/lib-CvDxBMkR.js +7843 -0
- package/dist/lib-CvDxBMkR.js.map +1 -0
- package/package.json +67 -116
- package/dist/application.d.ts +0 -47
- package/dist/application.js +0 -50
- package/dist/authentication.d.ts +0 -13
- package/dist/authentication.js +0 -16
- package/dist/cache.d.ts +0 -12
- package/dist/cache.js +0 -78
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -36
- package/dist/collection.d.ts +0 -43
- package/dist/collection.js +0 -231
- package/dist/collection.test.d.ts +0 -1
- package/dist/collection.test.js +0 -59
- package/dist/config.d.ts +0 -18
- package/dist/config.js +0 -58
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -40
- package/dist/constants.d.ts +0 -1
- package/dist/constants.js +0 -4
- package/dist/container.d.ts +0 -30
- package/dist/container.js +0 -55
- package/dist/controller.d.ts +0 -50
- package/dist/controller.js +0 -71
- package/dist/controller.test.d.ts +0 -1
- package/dist/controller.test.js +0 -111
- package/dist/decorators.d.ts +0 -15
- package/dist/decorators.js +0 -41
- package/dist/environment-variables.d.ts +0 -49
- package/dist/environment-variables.js +0 -130
- package/dist/environment-variables.test.d.ts +0 -1
- package/dist/environment-variables.test.js +0 -70
- package/dist/event-dispatcher.d.ts +0 -23
- package/dist/event-dispatcher.js +0 -100
- package/dist/event-subscriber.d.ts +0 -14
- package/dist/event-subscriber.js +0 -87
- package/dist/exceptions/http-exceptions.d.ts +0 -50
- package/dist/exceptions/http-exceptions.js +0 -85
- package/dist/exceptions/index.d.ts +0 -1
- package/dist/exceptions/index.js +0 -17
- package/dist/exceptions/system-exception.d.ts +0 -22
- package/dist/exceptions/system-exception.js +0 -26
- package/dist/file-storage.d.ts +0 -69
- package/dist/file-storage.js +0 -323
- package/dist/file-storage.test.d.ts +0 -1
- package/dist/file-storage.test.js +0 -104
- package/dist/helpers.d.ts +0 -44
- package/dist/helpers.js +0 -419
- package/dist/helpers.test.d.ts +0 -1
- package/dist/helpers.test.js +0 -95
- package/dist/icore.d.ts +0 -226
- package/dist/icore.js +0 -968
- package/dist/icore.test.d.ts +0 -1
- package/dist/icore.test.js +0 -14
- package/dist/index.d.ts +0 -55
- package/dist/interfaces/avleon-application.d.ts +0 -27
- package/dist/interfaces/avleon-application.js +0 -1
- package/dist/kenx-provider.d.ts +0 -7
- package/dist/kenx-provider.js +0 -44
- package/dist/kenx-provider.test.d.ts +0 -1
- package/dist/kenx-provider.test.js +0 -36
- package/dist/logger.d.ts +0 -12
- package/dist/logger.js +0 -87
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -42
- package/dist/map-types.d.ts +0 -17
- package/dist/map-types.js +0 -89
- package/dist/middleware.d.ts +0 -27
- package/dist/middleware.js +0 -64
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -121
- package/dist/multipart.d.ts +0 -17
- package/dist/multipart.js +0 -70
- package/dist/multipart.test.d.ts +0 -1
- package/dist/multipart.test.js +0 -87
- package/dist/openapi.d.ts +0 -343
- package/dist/openapi.js +0 -27
- package/dist/openapi.test.d.ts +0 -1
- package/dist/openapi.test.js +0 -111
- package/dist/params.d.ts +0 -17
- package/dist/params.js +0 -64
- package/dist/params.test.d.ts +0 -1
- package/dist/params.test.js +0 -83
- package/dist/queue.d.ts +0 -29
- package/dist/queue.js +0 -84
- package/dist/response.d.ts +0 -16
- package/dist/response.js +0 -56
- package/dist/results.d.ts +0 -20
- package/dist/results.js +0 -32
- package/dist/route-methods.d.ts +0 -25
- package/dist/route-methods.js +0 -49
- package/dist/route-methods.test.d.ts +0 -1
- package/dist/route-methods.test.js +0 -129
- package/dist/swagger-schema.d.ts +0 -43
- package/dist/swagger-schema.js +0 -452
- package/dist/swagger-schema.test.d.ts +0 -1
- package/dist/swagger-schema.test.js +0 -105
- package/dist/testing.d.ts +0 -55
- package/dist/testing.js +0 -196
- package/dist/types/app-builder.interface.d.ts +0 -15
- package/dist/types/app-builder.interface.js +0 -8
- package/dist/types/application.interface.d.ts +0 -8
- package/dist/types/application.interface.js +0 -2
- package/dist/utils/hash.d.ts +0 -4
- package/dist/utils/hash.js +0 -15
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/optional-require.d.ts +0 -8
- package/dist/utils/optional-require.js +0 -70
- package/dist/validation.d.ts +0 -39
- package/dist/validation.js +0 -111
- package/dist/validation.test.d.ts +0 -1
- package/dist/validation.test.js +0 -61
- package/dist/validator-extend.d.ts +0 -7
- package/dist/validator-extend.js +0 -28
- package/dist/websocket.d.ts +0 -7
- package/dist/websocket.js +0 -20
- package/dist/websocket.test.d.ts +0 -1
- package/dist/websocket.test.js +0 -27
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const fs_1 = __importDefault(require("fs"));
|
|
7
|
-
const stream_1 = require("stream");
|
|
8
|
-
const file_storage_1 = require("./file-storage");
|
|
9
|
-
const system_exception_1 = require("./exceptions/system-exception");
|
|
10
|
-
const promises_1 = require("stream/promises");
|
|
11
|
-
jest.mock("fs");
|
|
12
|
-
jest.mock("stream/promises", () => ({
|
|
13
|
-
pipeline: jest.fn((...args) => Promise.resolve()),
|
|
14
|
-
}));
|
|
15
|
-
const mockFs = fs_1.default;
|
|
16
|
-
describe("FileStorage", () => {
|
|
17
|
-
let fileStorage;
|
|
18
|
-
const testFile = {
|
|
19
|
-
type: "file",
|
|
20
|
-
filename: "test.txt",
|
|
21
|
-
file: new stream_1.Readable({ read() { } }),
|
|
22
|
-
};
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
jest.clearAllMocks();
|
|
25
|
-
fileStorage = new file_storage_1.FileStorage();
|
|
26
|
-
mockFs.existsSync.mockReturnValue(false);
|
|
27
|
-
mockFs.createWriteStream.mockReturnValue({});
|
|
28
|
-
mockFs.createReadStream.mockReturnValue({});
|
|
29
|
-
mockFs.unlinkSync.mockImplementation(() => { });
|
|
30
|
-
mockFs.mkdirSync.mockImplementation(() => undefined);
|
|
31
|
-
});
|
|
32
|
-
describe("save", () => {
|
|
33
|
-
it("should save a file successfully", async () => {
|
|
34
|
-
await expect(fileStorage.save(testFile)).resolves.toEqual(testFile);
|
|
35
|
-
});
|
|
36
|
-
it("should throw SystemUseError if file exists and overwrite is false", async () => {
|
|
37
|
-
mockFs.existsSync.mockReturnValue(true);
|
|
38
|
-
await expect(fileStorage.save(testFile, { overwrite: false })).rejects.toThrow(system_exception_1.SystemUseError);
|
|
39
|
-
});
|
|
40
|
-
it("should throw SystemUseError on pipeline error", async () => {
|
|
41
|
-
promises_1.pipeline.mockImplementationOnce(() => {
|
|
42
|
-
throw new Error("Pipeline failed");
|
|
43
|
-
});
|
|
44
|
-
await expect(fileStorage.save(testFile)).rejects.toThrow(system_exception_1.SystemUseError);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
describe("remove", () => {
|
|
48
|
-
it("should remove a file successfully", async () => {
|
|
49
|
-
mockFs.existsSync.mockReturnValue(true);
|
|
50
|
-
await expect(fileStorage.remove("test.txt")).resolves.toBeUndefined();
|
|
51
|
-
expect(mockFs.unlinkSync).toHaveBeenCalled();
|
|
52
|
-
});
|
|
53
|
-
it("should throw SystemUseError if file does not exist", async () => {
|
|
54
|
-
mockFs.existsSync.mockReturnValue(false);
|
|
55
|
-
await expect(fileStorage.remove("test.txt")).rejects.toThrow(system_exception_1.SystemUseError);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
describe("saveAll", () => {
|
|
59
|
-
it("should save multiple files successfully", async () => {
|
|
60
|
-
const files = [
|
|
61
|
-
{ ...testFile, filename: "a.txt" },
|
|
62
|
-
{ ...testFile, filename: "b.txt" },
|
|
63
|
-
];
|
|
64
|
-
await expect(fileStorage.saveAll(files)).resolves.toEqual(files);
|
|
65
|
-
});
|
|
66
|
-
it("should throw SystemUseError on error", async () => {
|
|
67
|
-
promises_1.pipeline.mockImplementationOnce(() => {
|
|
68
|
-
throw new Error("Pipeline failed");
|
|
69
|
-
});
|
|
70
|
-
await expect(fileStorage.saveAll([testFile])).rejects.toThrow(system_exception_1.SystemUseError);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe("transform", () => {
|
|
74
|
-
it("should set transform options and return itself", () => {
|
|
75
|
-
const options = { resize: { width: 100, height: 100 }, format: "jpeg" };
|
|
76
|
-
const result = fileStorage.transform(options);
|
|
77
|
-
expect(result).toBe(fileStorage);
|
|
78
|
-
// @ts-ignore
|
|
79
|
-
expect(fileStorage.transformOptions).toEqual(options);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
// describe("processImage", () => {
|
|
83
|
-
// it("should throw InternalErrorException if sharp is not installed", async () => {
|
|
84
|
-
// jest.resetModules();
|
|
85
|
-
// const fileStorage = new (require("./file-storage").FileStorage)();
|
|
86
|
-
// fileStorage.transform({ format: "jpeg" });
|
|
87
|
-
// await expect(
|
|
88
|
-
// fileStorage["processImage"](new Readable({ read() {} }), "output.jpg"),
|
|
89
|
-
// ).rejects.toThrow(InternalErrorException);
|
|
90
|
-
// });
|
|
91
|
-
// });
|
|
92
|
-
describe("ensureDirectoryExists", () => {
|
|
93
|
-
it("should create directory if not exists", async () => {
|
|
94
|
-
mockFs.existsSync.mockReturnValue(false);
|
|
95
|
-
await fileStorage["ensureDirectoryExists"]("public/test.txt");
|
|
96
|
-
expect(mockFs.mkdirSync).toHaveBeenCalled();
|
|
97
|
-
});
|
|
98
|
-
it("should not create directory if exists", async () => {
|
|
99
|
-
mockFs.existsSync.mockReturnValue(true);
|
|
100
|
-
await fileStorage["ensureDirectoryExists"]("public/test.txt");
|
|
101
|
-
expect(mockFs.mkdirSync).not.toHaveBeenCalled();
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
});
|
package/dist/helpers.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export declare const uuid: `${string}-${string}-${string}-${string}-${string}`;
|
|
2
|
-
export declare function inject<T>(cls: new (...args: any[]) => T): T;
|
|
3
|
-
export type Constructor<T = any> = new (...args: any[]) => T;
|
|
4
|
-
export declare function isConstructor(func: any): boolean;
|
|
5
|
-
export declare function formatUrl(path: string): string;
|
|
6
|
-
export declare function parsedPath(ipath: string): string;
|
|
7
|
-
export declare const isClassValidator: (target: Constructor) => boolean;
|
|
8
|
-
export interface MatchLocation {
|
|
9
|
-
line: number;
|
|
10
|
-
column: number;
|
|
11
|
-
}
|
|
12
|
-
export declare const getLineNumber: (filePath: string, rpath: string | RegExp) => MatchLocation[] | null;
|
|
13
|
-
export declare function normalizePath(base?: string, subPath?: string): string;
|
|
14
|
-
export declare function extrctParamFromUrl(url: string): {
|
|
15
|
-
key: string;
|
|
16
|
-
required: boolean;
|
|
17
|
-
}[];
|
|
18
|
-
export declare function findDuplicates(arr: string[]): string[];
|
|
19
|
-
export declare function getDataType(expectedType: any): any;
|
|
20
|
-
export declare function isValidType(value: any, expectedType: any): boolean;
|
|
21
|
-
export declare function isValidJsonString(value: string): object | boolean;
|
|
22
|
-
export declare function jsonToJs(value: string): any;
|
|
23
|
-
export declare function jsonToInstance(value: string, instance: Constructor): any;
|
|
24
|
-
export declare function transformObjectByInstanceToObject(instance: Constructor, value: object): Record<string, any>;
|
|
25
|
-
export declare const isClassValidatorClass: (target: Constructor) => boolean;
|
|
26
|
-
export declare function validateObjectByInstance(target: Constructor, value?: object, options?: "object" | "array"): Promise<any>;
|
|
27
|
-
type ValidationError = {
|
|
28
|
-
count: number;
|
|
29
|
-
errors: any;
|
|
30
|
-
};
|
|
31
|
-
export declare function validateRequestBody(target: Constructor, value: object, options?: "object" | "array"): ValidationError;
|
|
32
|
-
export declare function pick<T extends object>(obj: T, paths: string[]): Partial<T>;
|
|
33
|
-
export declare function exclude<T extends object>(obj: T | T[], paths: string[]): Partial<T> | Partial<T>[];
|
|
34
|
-
export declare function autoCast(value: any, typeHint?: any, schema?: any): any;
|
|
35
|
-
/**
|
|
36
|
-
* Deeply normalizes query strings into nested JS objects.
|
|
37
|
-
* Supports:
|
|
38
|
-
* - filter[name]=john
|
|
39
|
-
* - filter[user][age]=25
|
|
40
|
-
* - filter[tags][]=a&filter[tags][]=b
|
|
41
|
-
* - filter=name&filter=sorna
|
|
42
|
-
*/
|
|
43
|
-
export declare function normalizeQueryDeep(query: Record<string, any>): Record<string, any>;
|
|
44
|
-
export {};
|
package/dist/helpers.js
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isClassValidatorClass = exports.getLineNumber = exports.isClassValidator = exports.uuid = void 0;
|
|
7
|
-
exports.inject = inject;
|
|
8
|
-
exports.isConstructor = isConstructor;
|
|
9
|
-
exports.formatUrl = formatUrl;
|
|
10
|
-
exports.parsedPath = parsedPath;
|
|
11
|
-
exports.normalizePath = normalizePath;
|
|
12
|
-
exports.extrctParamFromUrl = extrctParamFromUrl;
|
|
13
|
-
exports.findDuplicates = findDuplicates;
|
|
14
|
-
exports.getDataType = getDataType;
|
|
15
|
-
exports.isValidType = isValidType;
|
|
16
|
-
exports.isValidJsonString = isValidJsonString;
|
|
17
|
-
exports.jsonToJs = jsonToJs;
|
|
18
|
-
exports.jsonToInstance = jsonToInstance;
|
|
19
|
-
exports.transformObjectByInstanceToObject = transformObjectByInstanceToObject;
|
|
20
|
-
exports.validateObjectByInstance = validateObjectByInstance;
|
|
21
|
-
exports.validateRequestBody = validateRequestBody;
|
|
22
|
-
exports.pick = pick;
|
|
23
|
-
exports.exclude = exclude;
|
|
24
|
-
exports.autoCast = autoCast;
|
|
25
|
-
exports.normalizeQueryDeep = normalizeQueryDeep;
|
|
26
|
-
/**
|
|
27
|
-
* @copyright 2024
|
|
28
|
-
* @author Tareq Hossain
|
|
29
|
-
* @email xtrinsic96@gmail.com
|
|
30
|
-
* @url https://github.com/xtareq
|
|
31
|
-
*/
|
|
32
|
-
const class_transformer_1 = require("class-transformer");
|
|
33
|
-
const exceptions_1 = require("./exceptions");
|
|
34
|
-
const fs_1 = __importDefault(require("fs"));
|
|
35
|
-
const container_1 = __importDefault(require("./container"));
|
|
36
|
-
const system_exception_1 = require("./exceptions/system-exception");
|
|
37
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
38
|
-
const class_validator_1 = require("class-validator");
|
|
39
|
-
exports.uuid = crypto_1.default.randomUUID();
|
|
40
|
-
function inject(cls) {
|
|
41
|
-
try {
|
|
42
|
-
return container_1.default.get(cls);
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
throw new system_exception_1.SystemUseError("Not a project class. Maybe you wanna register it first.");
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function isConstructor(func) {
|
|
49
|
-
if (typeof func !== "function") {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
if (func === Function.prototype.bind || func instanceof RegExp) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
if (func.prototype && typeof func.prototype === "object") {
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
const instance = new func();
|
|
60
|
-
return typeof instance === "object";
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function formatUrl(path) {
|
|
67
|
-
if (typeof path !== "string") {
|
|
68
|
-
throw new Error("The path must be a string");
|
|
69
|
-
}
|
|
70
|
-
path = path.trim();
|
|
71
|
-
if (!path.startsWith("/")) {
|
|
72
|
-
path = "/" + path;
|
|
73
|
-
}
|
|
74
|
-
path = path.replace(/\/\/+/g, "/");
|
|
75
|
-
if (path.endsWith("/")) {
|
|
76
|
-
path = path.slice(0, -1);
|
|
77
|
-
}
|
|
78
|
-
return path;
|
|
79
|
-
}
|
|
80
|
-
function parsedPath(ipath) {
|
|
81
|
-
return !ipath.startsWith("/") ? "/" + ipath : ipath;
|
|
82
|
-
}
|
|
83
|
-
const isClassValidator = (target) => {
|
|
84
|
-
try {
|
|
85
|
-
const clsval = require("class-validator");
|
|
86
|
-
const result = (0, class_validator_1.getMetadataStorage)().getTargetValidationMetadatas(target, "", false, false);
|
|
87
|
-
return result.length > 0;
|
|
88
|
-
}
|
|
89
|
-
catch (err) {
|
|
90
|
-
console.log(err);
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
exports.isClassValidator = isClassValidator;
|
|
95
|
-
const getLineNumber = (filePath, rpath) => {
|
|
96
|
-
var _a;
|
|
97
|
-
let numbers = [];
|
|
98
|
-
try {
|
|
99
|
-
const fileContent = fs_1.default.readFileSync(filePath, "utf8");
|
|
100
|
-
const lines = fileContent.split("\n");
|
|
101
|
-
for (let i = 0; i < lines.length; i++) {
|
|
102
|
-
const match = lines[i].match(rpath);
|
|
103
|
-
if (match) {
|
|
104
|
-
console.log(match);
|
|
105
|
-
numbers.push({
|
|
106
|
-
line: i + 1,
|
|
107
|
-
column: (_a = match.index) !== null && _a !== void 0 ? _a : 0,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return numbers;
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
return numbers;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
exports.getLineNumber = getLineNumber;
|
|
118
|
-
function normalizePath(base = "/", subPath = "/") {
|
|
119
|
-
return `/${base}/${subPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
120
|
-
}
|
|
121
|
-
function extrctParamFromUrl(url) {
|
|
122
|
-
const splitPart = url
|
|
123
|
-
.split("/")
|
|
124
|
-
.filter((x) => x.startsWith(":") || x.startsWith("?:"));
|
|
125
|
-
return splitPart.map((f) => ({
|
|
126
|
-
key: f.replace(/(\?|:)/g, ""),
|
|
127
|
-
required: !f.startsWith("?:"),
|
|
128
|
-
}));
|
|
129
|
-
}
|
|
130
|
-
function findDuplicates(arr) {
|
|
131
|
-
const seen = new Set();
|
|
132
|
-
const duplicates = new Set();
|
|
133
|
-
for (const str of arr) {
|
|
134
|
-
if (seen.has(str)) {
|
|
135
|
-
duplicates.add(str);
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
seen.add(str);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return Array.from(duplicates);
|
|
142
|
-
}
|
|
143
|
-
function getDataType(expectedType) {
|
|
144
|
-
switch (expectedType.name) {
|
|
145
|
-
case "Object":
|
|
146
|
-
if (Array.isArray(expectedType)) {
|
|
147
|
-
return "array";
|
|
148
|
-
}
|
|
149
|
-
return "object";
|
|
150
|
-
case "String":
|
|
151
|
-
return "string";
|
|
152
|
-
case "Number":
|
|
153
|
-
return "number";
|
|
154
|
-
case "Boolean":
|
|
155
|
-
return "boolean";
|
|
156
|
-
default:
|
|
157
|
-
return expectedType;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
function isValidType(value, expectedType) {
|
|
161
|
-
if (value === undefined || value === null)
|
|
162
|
-
return true;
|
|
163
|
-
switch (expectedType.name) {
|
|
164
|
-
case "String":
|
|
165
|
-
return typeof value === "string";
|
|
166
|
-
case "Number":
|
|
167
|
-
return typeof value === "number" || !isNaN(Number(value));
|
|
168
|
-
case "Boolean":
|
|
169
|
-
return typeof value === "boolean";
|
|
170
|
-
default:
|
|
171
|
-
return value instanceof expectedType;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
function isValidJsonString(value) {
|
|
175
|
-
try {
|
|
176
|
-
return JSON.parse(value);
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
function jsonToJs(value) {
|
|
183
|
-
try {
|
|
184
|
-
return JSON.parse(value);
|
|
185
|
-
}
|
|
186
|
-
catch (err) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
function jsonToInstance(value, instance) {
|
|
191
|
-
try {
|
|
192
|
-
const parsedValue = JSON.parse(value);
|
|
193
|
-
return (0, class_transformer_1.plainToInstance)(instance, parsedValue);
|
|
194
|
-
}
|
|
195
|
-
catch (err) {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
function transformObjectByInstanceToObject(instance, value) {
|
|
200
|
-
return (0, class_transformer_1.instanceToPlain)((0, class_transformer_1.plainToInstance)(instance, value), {
|
|
201
|
-
excludeExtraneousValues: true,
|
|
202
|
-
exposeUnsetFields: true,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
const isClassValidatorClass = (target) => {
|
|
206
|
-
try {
|
|
207
|
-
const clsval = require("class-validator");
|
|
208
|
-
const result = clsval
|
|
209
|
-
.getMetadataStorage()
|
|
210
|
-
.getTargetValidationMetadatas(target, undefined, false, false);
|
|
211
|
-
return result.length > 0;
|
|
212
|
-
}
|
|
213
|
-
catch (err) {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
exports.isClassValidatorClass = isClassValidatorClass;
|
|
218
|
-
async function validateObjectByInstance(target, value = {}, options = "array") {
|
|
219
|
-
try {
|
|
220
|
-
const { validateOrReject } = require("class-validator");
|
|
221
|
-
const { plainToInstance } = require("class-transformer");
|
|
222
|
-
await validateOrReject(plainToInstance(target, value));
|
|
223
|
-
}
|
|
224
|
-
catch (error) {
|
|
225
|
-
if (typeof error == "object" && Array.isArray(error)) {
|
|
226
|
-
const errors = options == "object"
|
|
227
|
-
? error.reduce((acc, x) => {
|
|
228
|
-
//acc[x.property] = Object.values(x.constraints);
|
|
229
|
-
acc[x.property] = x.constraints;
|
|
230
|
-
return acc;
|
|
231
|
-
}, {})
|
|
232
|
-
: error.map((x) => ({
|
|
233
|
-
path: x.property,
|
|
234
|
-
constraints: x.constraints,
|
|
235
|
-
}));
|
|
236
|
-
return errors;
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
throw new exceptions_1.InternalErrorException("Can't validate object");
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
function validateRequestBody(target, value, options = "array") {
|
|
244
|
-
if (!(0, exports.isClassValidatorClass)(target))
|
|
245
|
-
return { count: 0, errors: {} };
|
|
246
|
-
const error = (0, class_validator_1.validateSync)((0, class_transformer_1.plainToInstance)(target, value ? value : {}));
|
|
247
|
-
const errors = options == "object"
|
|
248
|
-
? error.reduce((acc, x) => {
|
|
249
|
-
//acc[x.property] = Object.values(x.constraints);
|
|
250
|
-
acc[x.property] = x.constraints;
|
|
251
|
-
return acc;
|
|
252
|
-
}, {})
|
|
253
|
-
: error.map((x) => ({ path: x.property, constraints: x.constraints }));
|
|
254
|
-
return { count: error.length, errors };
|
|
255
|
-
}
|
|
256
|
-
function pick(obj, paths) {
|
|
257
|
-
const result = {};
|
|
258
|
-
for (const path of paths) {
|
|
259
|
-
const keys = path.split(".");
|
|
260
|
-
let source = obj;
|
|
261
|
-
let target = result;
|
|
262
|
-
for (let i = 0; i < keys.length; i++) {
|
|
263
|
-
const key = keys[i];
|
|
264
|
-
if (!(key in source))
|
|
265
|
-
break;
|
|
266
|
-
if (i === keys.length - 1) {
|
|
267
|
-
target[key] = source[key];
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
source = source[key];
|
|
271
|
-
target[key] = target[key] || {};
|
|
272
|
-
target = target[key];
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return result;
|
|
277
|
-
}
|
|
278
|
-
function exclude(obj, paths) {
|
|
279
|
-
if (Array.isArray(obj)) {
|
|
280
|
-
return obj.map((item) => exclude(item, paths));
|
|
281
|
-
}
|
|
282
|
-
const clone = structuredClone(obj); // Or use lodash.cloneDeep
|
|
283
|
-
for (const path of paths) {
|
|
284
|
-
const keys = path.split(".");
|
|
285
|
-
let target = clone;
|
|
286
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
287
|
-
if (!(keys[i] in target))
|
|
288
|
-
break;
|
|
289
|
-
target = target[keys[i]];
|
|
290
|
-
}
|
|
291
|
-
target === null || target === void 0 ? true : delete target[keys[keys.length - 1]];
|
|
292
|
-
}
|
|
293
|
-
return clone;
|
|
294
|
-
}
|
|
295
|
-
function autoCast(value, typeHint, schema) {
|
|
296
|
-
var _a, _b;
|
|
297
|
-
if (value === null || value === undefined)
|
|
298
|
-
return value;
|
|
299
|
-
if (Array.isArray(value)) {
|
|
300
|
-
const elementType = Array.isArray(typeHint) ? typeHint[0] : undefined;
|
|
301
|
-
return value.map((v) => autoCast(v, elementType));
|
|
302
|
-
}
|
|
303
|
-
if (typeof value === "object" && !(value instanceof Date)) {
|
|
304
|
-
const result = {};
|
|
305
|
-
for (const [key, val] of Object.entries(value)) {
|
|
306
|
-
let fieldType = undefined;
|
|
307
|
-
if ((_b = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.type) {
|
|
308
|
-
const t = schema.properties[key].type;
|
|
309
|
-
fieldType =
|
|
310
|
-
t === "integer" || t === "number"
|
|
311
|
-
? Number
|
|
312
|
-
: t === "boolean"
|
|
313
|
-
? Boolean
|
|
314
|
-
: t === "array"
|
|
315
|
-
? Array
|
|
316
|
-
: t === "object"
|
|
317
|
-
? Object
|
|
318
|
-
: String;
|
|
319
|
-
}
|
|
320
|
-
result[key] = autoCast(val, fieldType);
|
|
321
|
-
}
|
|
322
|
-
return result;
|
|
323
|
-
}
|
|
324
|
-
if (typeof value !== "string")
|
|
325
|
-
return value;
|
|
326
|
-
const trimmed = value.trim();
|
|
327
|
-
if (typeHint === Boolean || trimmed.toLowerCase() === "true")
|
|
328
|
-
return true;
|
|
329
|
-
if (trimmed.toLowerCase() === "false")
|
|
330
|
-
return false;
|
|
331
|
-
if (typeHint === Number || (!isNaN(Number(trimmed)) && trimmed !== "")) {
|
|
332
|
-
const n = Number(trimmed);
|
|
333
|
-
if (!isNaN(n))
|
|
334
|
-
return n;
|
|
335
|
-
}
|
|
336
|
-
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
337
|
-
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
338
|
-
try {
|
|
339
|
-
const parsed = JSON.parse(trimmed);
|
|
340
|
-
return autoCast(parsed, typeHint, schema);
|
|
341
|
-
}
|
|
342
|
-
catch (_c) {
|
|
343
|
-
return trimmed;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
if (typeHint === Date ||
|
|
347
|
-
/^\d{4}-\d{2}-\d{2}([Tt]\d{2}:\d{2})?/.test(trimmed)) {
|
|
348
|
-
const d = new Date(trimmed);
|
|
349
|
-
if (!isNaN(d.getTime()))
|
|
350
|
-
return d;
|
|
351
|
-
}
|
|
352
|
-
return trimmed;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Deeply normalizes query strings into nested JS objects.
|
|
356
|
-
* Supports:
|
|
357
|
-
* - filter[name]=john
|
|
358
|
-
* - filter[user][age]=25
|
|
359
|
-
* - filter[tags][]=a&filter[tags][]=b
|
|
360
|
-
* - filter=name&filter=sorna
|
|
361
|
-
*/
|
|
362
|
-
function normalizeQueryDeep(query) {
|
|
363
|
-
const result = {};
|
|
364
|
-
const setDeep = (obj, path, value) => {
|
|
365
|
-
let current = obj;
|
|
366
|
-
for (let i = 0; i < path.length; i++) {
|
|
367
|
-
const key = path[i];
|
|
368
|
-
const nextKey = path[i + 1];
|
|
369
|
-
if (i === path.length - 1) {
|
|
370
|
-
if (key === "") {
|
|
371
|
-
if (!Array.isArray(current))
|
|
372
|
-
current = [];
|
|
373
|
-
current.push(value);
|
|
374
|
-
}
|
|
375
|
-
else if (Array.isArray(current[key])) {
|
|
376
|
-
current[key].push(value);
|
|
377
|
-
}
|
|
378
|
-
else if (current[key] !== undefined) {
|
|
379
|
-
current[key] = [current[key], value];
|
|
380
|
-
}
|
|
381
|
-
else {
|
|
382
|
-
current[key] = value;
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
if (!current[key]) {
|
|
387
|
-
current[key] = nextKey === "" || /^\d+$/.test(nextKey) ? [] : {};
|
|
388
|
-
}
|
|
389
|
-
current = current[key];
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
for (const [rawKey, rawValue] of Object.entries(query)) {
|
|
394
|
-
const path = [];
|
|
395
|
-
const regex = /([^\[\]]+)|(\[\])/g;
|
|
396
|
-
let match;
|
|
397
|
-
while ((match = regex.exec(rawKey)) !== null) {
|
|
398
|
-
if (match[1])
|
|
399
|
-
path.push(match[1]);
|
|
400
|
-
else if (match[2])
|
|
401
|
-
path.push("");
|
|
402
|
-
}
|
|
403
|
-
if (path.length === 0) {
|
|
404
|
-
if (result[rawKey]) {
|
|
405
|
-
if (Array.isArray(result[rawKey]))
|
|
406
|
-
result[rawKey].push(rawValue);
|
|
407
|
-
else
|
|
408
|
-
result[rawKey] = [result[rawKey], rawValue];
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
result[rawKey] = rawValue;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
setDeep(result, path, rawValue);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
return result;
|
|
419
|
-
}
|
package/dist/helpers.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/helpers.test.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const helpers_1 = require("./helpers");
|
|
4
|
-
describe("helpers.ts", () => {
|
|
5
|
-
describe("formatUrl", () => {
|
|
6
|
-
it("should format URLs correctly", () => {
|
|
7
|
-
expect((0, helpers_1.formatUrl)("test")).toBe("/test");
|
|
8
|
-
expect((0, helpers_1.formatUrl)("/test")).toBe("/test");
|
|
9
|
-
expect((0, helpers_1.formatUrl)("///test//")).toBe("/test");
|
|
10
|
-
expect(() => (0, helpers_1.formatUrl)(123)).toThrow();
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
describe("parsedPath", () => {
|
|
14
|
-
it("should ensure path starts with /", () => {
|
|
15
|
-
expect((0, helpers_1.parsedPath)("abc")).toBe("/abc");
|
|
16
|
-
expect((0, helpers_1.parsedPath)("/abc")).toBe("/abc");
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
describe("findDuplicates", () => {
|
|
20
|
-
it("should find duplicates in array", () => {
|
|
21
|
-
expect((0, helpers_1.findDuplicates)(["a", "b", "a", "c", "b"])).toEqual(["a", "b"]);
|
|
22
|
-
expect((0, helpers_1.findDuplicates)(["a", "b", "c"])).toEqual([]);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
describe("getDataType", () => {
|
|
26
|
-
it("should return correct data type", () => {
|
|
27
|
-
expect((0, helpers_1.getDataType)(String)).toBe("string");
|
|
28
|
-
expect((0, helpers_1.getDataType)(Number)).toBe("number");
|
|
29
|
-
expect((0, helpers_1.getDataType)(Boolean)).toBe("boolean");
|
|
30
|
-
expect((0, helpers_1.getDataType)(Object)).toBe("object");
|
|
31
|
-
expect((0, helpers_1.getDataType)(Array)).toBe(Array);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
describe("isValidType", () => {
|
|
35
|
-
it("should validate types correctly", () => {
|
|
36
|
-
expect((0, helpers_1.isValidType)("abc", String)).toBe(true);
|
|
37
|
-
expect((0, helpers_1.isValidType)(123, Number)).toBe(true);
|
|
38
|
-
expect((0, helpers_1.isValidType)(true, Boolean)).toBe(true);
|
|
39
|
-
expect((0, helpers_1.isValidType)({}, Object)).toBe(true);
|
|
40
|
-
expect((0, helpers_1.isValidType)(undefined, String)).toBe(true);
|
|
41
|
-
expect((0, helpers_1.isValidType)(null, Number)).toBe(true);
|
|
42
|
-
expect((0, helpers_1.isValidType)("123", Number)).toBe(true);
|
|
43
|
-
expect((0, helpers_1.isValidType)("abc", Number)).toBe(false);
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
describe("isValidJsonString", () => {
|
|
47
|
-
it("should validate JSON strings", () => {
|
|
48
|
-
expect((0, helpers_1.isValidJsonString)('{"a":1}')).toEqual({ a: 1 });
|
|
49
|
-
expect((0, helpers_1.isValidJsonString)("not json")).toBe(false);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
describe("jsonToJs", () => {
|
|
53
|
-
it("should parse JSON to JS object", () => {
|
|
54
|
-
expect((0, helpers_1.jsonToJs)('{"a":1}')).toEqual({ a: 1 });
|
|
55
|
-
expect((0, helpers_1.jsonToJs)("bad json")).toBe(false);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
// describe("normalizePath", () => {
|
|
59
|
-
// it("should normalize paths", () => {
|
|
60
|
-
// expect(normalizePath("api", "v1")).toBe("/api/v1");
|
|
61
|
-
// expect(normalizePath("/", "/")).toBe("/");
|
|
62
|
-
// expect(normalizePath("api/", "/v1/")).toBe("/api/v1");
|
|
63
|
-
// });
|
|
64
|
-
// });
|
|
65
|
-
describe("extrctParamFromUrl", () => {
|
|
66
|
-
it("should extract params from URL", () => {
|
|
67
|
-
expect((0, helpers_1.extrctParamFromUrl)("/user/:id/:name")).toEqual([
|
|
68
|
-
{ key: "id", required: true },
|
|
69
|
-
{ key: "name", required: true },
|
|
70
|
-
]);
|
|
71
|
-
expect((0, helpers_1.extrctParamFromUrl)("/user/?:id")).toEqual([
|
|
72
|
-
{ key: "id", required: false },
|
|
73
|
-
]);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
describe("pick", () => {
|
|
77
|
-
it("should pick properties from object", () => {
|
|
78
|
-
const obj = { a: 1, b: { c: 2, d: 3 }, e: 4 };
|
|
79
|
-
expect((0, helpers_1.pick)(obj, ["a"])).toEqual({ a: 1 });
|
|
80
|
-
expect((0, helpers_1.pick)(obj, ["b.c"])).toEqual({ b: { c: 2 } });
|
|
81
|
-
expect((0, helpers_1.pick)(obj, ["b.d", "e"])).toEqual({ b: { d: 3 }, e: 4 });
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
describe("exclude", () => {
|
|
85
|
-
it("should exclude properties from object", () => {
|
|
86
|
-
const obj = { a: 1, b: { c: 2, d: 3 }, e: 4 };
|
|
87
|
-
expect((0, helpers_1.exclude)(obj, ["a"])).toEqual({ b: { c: 2, d: 3 }, e: 4 });
|
|
88
|
-
expect((0, helpers_1.exclude)(obj, ["b.c"])).toEqual({ a: 1, b: { d: 3 }, e: 4 });
|
|
89
|
-
expect((0, helpers_1.exclude)([obj, obj], ["e"])).toEqual([
|
|
90
|
-
{ a: 1, b: { c: 2, d: 3 } },
|
|
91
|
-
{ a: 1, b: { c: 2, d: 3 } },
|
|
92
|
-
]);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
});
|