@avleon/core 0.0.26 → 0.0.28
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 +601 -561
- package/package.json +38 -6
- package/src/application.ts +104 -125
- package/src/authentication.ts +16 -16
- package/src/cache.ts +91 -91
- package/src/collection.test.ts +71 -0
- package/src/collection.ts +344 -254
- package/src/config.test.ts +35 -0
- package/src/config.ts +85 -42
- package/src/constants.ts +1 -1
- package/src/container.ts +54 -54
- package/src/controller.ts +125 -127
- package/src/decorators.ts +27 -27
- package/src/environment-variables.ts +53 -46
- package/src/exceptions/http-exceptions.ts +86 -86
- package/src/exceptions/index.ts +1 -1
- package/src/exceptions/system-exception.ts +35 -34
- package/src/file-storage.ts +206 -206
- package/src/helpers.ts +324 -328
- package/src/icore.ts +66 -90
- package/src/index.ts +30 -30
- package/src/interfaces/avleon-application.ts +32 -40
- package/src/logger.ts +72 -72
- package/src/map-types.ts +159 -159
- package/src/middleware.ts +119 -98
- package/src/multipart.ts +116 -116
- package/src/openapi.ts +372 -372
- package/src/params.ts +111 -111
- package/src/queue.ts +126 -126
- package/src/response.ts +74 -74
- package/src/results.ts +30 -30
- package/src/route-methods.ts +186 -186
- package/src/swagger-schema.ts +213 -213
- package/src/testing.ts +220 -220
- package/src/types/app-builder.interface.ts +18 -19
- package/src/types/application.interface.ts +7 -9
- package/src/utils/hash.ts +8 -5
- package/src/utils/index.ts +2 -2
- package/src/utils/optional-require.ts +50 -50
- package/src/validation.ts +160 -156
- package/src/validator-extend.ts +25 -25
package/src/middleware.ts
CHANGED
|
@@ -1,98 +1,119 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
import { Service } from "typedi";
|
|
8
|
-
import { IRequest, IResponse } from "./icore";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
import { Service } from "typedi";
|
|
8
|
+
import { IRequest, IResponse } from "./icore";
|
|
9
|
+
import {
|
|
10
|
+
HttpExceptionTypes as HttpException,
|
|
11
|
+
UnauthorizedException,
|
|
12
|
+
} from "./exceptions";
|
|
13
|
+
import Container, { AUTHORIZATION_META_KEY } from "./container";
|
|
14
|
+
|
|
15
|
+
export abstract class AppMiddleware {
|
|
16
|
+
abstract invoke(
|
|
17
|
+
req: IRequest,
|
|
18
|
+
res?: IResponse,
|
|
19
|
+
): Promise<IRequest | HttpException>;
|
|
20
|
+
}
|
|
21
|
+
export type AuthHandler = (
|
|
22
|
+
req: IRequest,
|
|
23
|
+
roles?: string[],
|
|
24
|
+
) => Promise<IRequest | HttpException>;
|
|
25
|
+
|
|
26
|
+
export type Constructor<T> = { new (...args: any[]): T };
|
|
27
|
+
|
|
28
|
+
export abstract class AuthorizeMiddleware {
|
|
29
|
+
abstract authorize(
|
|
30
|
+
roles: string[],
|
|
31
|
+
): (req: IRequest, res?: IResponse) => IRequest | Promise<IRequest>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type AuthReturnTypes = IRequest | Promise<IRequest>;
|
|
35
|
+
|
|
36
|
+
interface AuthorizeClass {
|
|
37
|
+
authorize(req: IRequest, options?: any): AuthReturnTypes;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function Authorize(target: { new (...args: any[]): AuthorizeClass }) {
|
|
41
|
+
if (typeof target.prototype.authorize !== "function") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Class "${target.name}" must implement an "authorize" method.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
Service()(target);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// export function Authorized(target: Function): void;
|
|
50
|
+
export function Authorized(): ClassDecorator & MethodDecorator;
|
|
51
|
+
export function Authorized(options?: any): ClassDecorator & MethodDecorator;
|
|
52
|
+
export function Authorized(
|
|
53
|
+
options: any = {},
|
|
54
|
+
): MethodDecorator | ClassDecorator {
|
|
55
|
+
return function (
|
|
56
|
+
target: any,
|
|
57
|
+
propertyKey?: string | symbol,
|
|
58
|
+
descriptor?: PropertyDescriptor,
|
|
59
|
+
) {
|
|
60
|
+
if (propertyKey && descriptor) {
|
|
61
|
+
Reflect.defineMetadata(
|
|
62
|
+
AUTHORIZATION_META_KEY,
|
|
63
|
+
{ authorize: true, options },
|
|
64
|
+
target.constructor,
|
|
65
|
+
propertyKey,
|
|
66
|
+
);
|
|
67
|
+
} else {
|
|
68
|
+
Reflect.defineMetadata(
|
|
69
|
+
AUTHORIZATION_META_KEY,
|
|
70
|
+
{ authorize: true, options },
|
|
71
|
+
target,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function Middleware(target: Constructor<AppMiddleware>) {
|
|
78
|
+
if (typeof target.prototype.invoke !== "function") {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Class "${target.name}" must implement an "invoke" method.`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Service()(target);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function UseMiddleware<
|
|
88
|
+
T extends AppMiddleware | (new (...args: any[]) => AppMiddleware),
|
|
89
|
+
>(options: T | T[]): MethodDecorator & ClassDecorator {
|
|
90
|
+
return function (
|
|
91
|
+
target: Object | Function,
|
|
92
|
+
propertyKey?: string | symbol,
|
|
93
|
+
descriptor?: PropertyDescriptor,
|
|
94
|
+
) {
|
|
95
|
+
const normalizeMiddleware = (middleware: any) =>
|
|
96
|
+
typeof middleware === "function" ? new middleware() : middleware;
|
|
97
|
+
const middlewareList = (Array.isArray(options) ? options : [options]).map(
|
|
98
|
+
normalizeMiddleware,
|
|
99
|
+
);
|
|
100
|
+
if (typeof target === "function" && !propertyKey) {
|
|
101
|
+
const existingMiddlewares =
|
|
102
|
+
Reflect.getMetadata("controller:middleware", target) || [];
|
|
103
|
+
Reflect.defineMetadata(
|
|
104
|
+
"controller:middleware",
|
|
105
|
+
[...existingMiddlewares, ...middlewareList],
|
|
106
|
+
target,
|
|
107
|
+
);
|
|
108
|
+
} else if (descriptor) {
|
|
109
|
+
const existingMiddlewares =
|
|
110
|
+
Reflect.getMetadata("route:middleware", target, propertyKey!) || [];
|
|
111
|
+
Reflect.defineMetadata(
|
|
112
|
+
"route:middleware",
|
|
113
|
+
[...existingMiddlewares, ...middlewareList],
|
|
114
|
+
target,
|
|
115
|
+
propertyKey!,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
package/src/multipart.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
MultipartFile as FsM,
|
|
10
|
-
MultipartValue,
|
|
11
|
-
SavedMultipartFile,
|
|
12
|
-
} from "@fastify/multipart";
|
|
13
|
-
import { IRequest } from "./icore";
|
|
14
|
-
import fs from "fs";
|
|
15
|
-
import path from "path";
|
|
16
|
-
import { pipeline } from "stream/promises";
|
|
17
|
-
import { InternalErrorException } from "./exceptions";
|
|
18
|
-
import { REQUEST_BODY_FILE_KEY, REQUEST_BODY_FILES_KEY } from "./container";
|
|
19
|
-
|
|
20
|
-
export function UploadFile(fieldName: string) {
|
|
21
|
-
return function (
|
|
22
|
-
target: any,
|
|
23
|
-
propertyKey: string | symbol,
|
|
24
|
-
parameterIndex: number
|
|
25
|
-
) {
|
|
26
|
-
if (!Reflect.hasMetadata(REQUEST_BODY_FILE_KEY, target, propertyKey)) {
|
|
27
|
-
Reflect.defineMetadata(REQUEST_BODY_FILE_KEY, [], target, propertyKey);
|
|
28
|
-
}
|
|
29
|
-
const existingMetadata = Reflect.getMetadata(
|
|
30
|
-
REQUEST_BODY_FILE_KEY,
|
|
31
|
-
target,
|
|
32
|
-
propertyKey
|
|
33
|
-
) as {
|
|
34
|
-
fieldName: string;
|
|
35
|
-
index: number;
|
|
36
|
-
}[];
|
|
37
|
-
existingMetadata.push({ fieldName, index: parameterIndex });
|
|
38
|
-
Reflect.defineMetadata(
|
|
39
|
-
REQUEST_BODY_FILE_KEY,
|
|
40
|
-
existingMetadata,
|
|
41
|
-
target,
|
|
42
|
-
propertyKey
|
|
43
|
-
);
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function UploadFiles(fieldName?: string) {
|
|
48
|
-
return function (
|
|
49
|
-
target: any,
|
|
50
|
-
propertyKey: string | symbol,
|
|
51
|
-
parameterIndex: number
|
|
52
|
-
) {
|
|
53
|
-
if (!Reflect.hasMetadata(REQUEST_BODY_FILES_KEY, target, propertyKey)) {
|
|
54
|
-
Reflect.defineMetadata(REQUEST_BODY_FILES_KEY, [], target, propertyKey);
|
|
55
|
-
}
|
|
56
|
-
const existingMetadata = Reflect.getMetadata(
|
|
57
|
-
REQUEST_BODY_FILES_KEY,
|
|
58
|
-
target,
|
|
59
|
-
propertyKey
|
|
60
|
-
) as {
|
|
61
|
-
fieldName: string;
|
|
62
|
-
index: number;
|
|
63
|
-
}[];
|
|
64
|
-
existingMetadata.push({
|
|
65
|
-
fieldName: fieldName ? fieldName : "all",
|
|
66
|
-
index: parameterIndex,
|
|
67
|
-
});
|
|
68
|
-
Reflect.defineMetadata(
|
|
69
|
-
REQUEST_BODY_FILES_KEY,
|
|
70
|
-
existingMetadata,
|
|
71
|
-
target,
|
|
72
|
-
propertyKey
|
|
73
|
-
);
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
type Foptions = {
|
|
78
|
-
saveAs?: string;
|
|
79
|
-
dest?: true;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export type MultipartFile = FsM | SavedMultipartFile;
|
|
83
|
-
export function UploadFileFromRequest(req: IRequest, options?: Foptions) {
|
|
84
|
-
return Promise.resolve(
|
|
85
|
-
req.file().then(async (f) => {
|
|
86
|
-
if (f && f.file) {
|
|
87
|
-
let fname = f.filename;
|
|
88
|
-
if (options) {
|
|
89
|
-
if (options.dest) {
|
|
90
|
-
fname = options.saveAs
|
|
91
|
-
? options.dest + "/" + options.saveAs
|
|
92
|
-
: options.dest + "/" + f.filename;
|
|
93
|
-
} else {
|
|
94
|
-
fname = path.join(
|
|
95
|
-
process.cwd(),
|
|
96
|
-
`public/${options.saveAs ? options.saveAs : f.filename}
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
fname = path.join(process.cwd(), `public/${f.filename}`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (fs.existsSync(fname)) {
|
|
104
|
-
throw new InternalErrorException("File already exists.");
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
await pipeline(f.file!, fs.createWriteStream(fname));
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
...f,
|
|
111
|
-
filename: options?.saveAs ? options.saveAs : f.filename,
|
|
112
|
-
} as MultipartFile;
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
);
|
|
116
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
MultipartFile as FsM,
|
|
10
|
+
MultipartValue,
|
|
11
|
+
SavedMultipartFile,
|
|
12
|
+
} from "@fastify/multipart";
|
|
13
|
+
import { IRequest } from "./icore";
|
|
14
|
+
import fs from "fs";
|
|
15
|
+
import path from "path";
|
|
16
|
+
import { pipeline } from "stream/promises";
|
|
17
|
+
import { InternalErrorException } from "./exceptions";
|
|
18
|
+
import { REQUEST_BODY_FILE_KEY, REQUEST_BODY_FILES_KEY } from "./container";
|
|
19
|
+
|
|
20
|
+
export function UploadFile(fieldName: string) {
|
|
21
|
+
return function (
|
|
22
|
+
target: any,
|
|
23
|
+
propertyKey: string | symbol,
|
|
24
|
+
parameterIndex: number,
|
|
25
|
+
) {
|
|
26
|
+
if (!Reflect.hasMetadata(REQUEST_BODY_FILE_KEY, target, propertyKey)) {
|
|
27
|
+
Reflect.defineMetadata(REQUEST_BODY_FILE_KEY, [], target, propertyKey);
|
|
28
|
+
}
|
|
29
|
+
const existingMetadata = Reflect.getMetadata(
|
|
30
|
+
REQUEST_BODY_FILE_KEY,
|
|
31
|
+
target,
|
|
32
|
+
propertyKey,
|
|
33
|
+
) as {
|
|
34
|
+
fieldName: string;
|
|
35
|
+
index: number;
|
|
36
|
+
}[];
|
|
37
|
+
existingMetadata.push({ fieldName, index: parameterIndex });
|
|
38
|
+
Reflect.defineMetadata(
|
|
39
|
+
REQUEST_BODY_FILE_KEY,
|
|
40
|
+
existingMetadata,
|
|
41
|
+
target,
|
|
42
|
+
propertyKey,
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function UploadFiles(fieldName?: string) {
|
|
48
|
+
return function (
|
|
49
|
+
target: any,
|
|
50
|
+
propertyKey: string | symbol,
|
|
51
|
+
parameterIndex: number,
|
|
52
|
+
) {
|
|
53
|
+
if (!Reflect.hasMetadata(REQUEST_BODY_FILES_KEY, target, propertyKey)) {
|
|
54
|
+
Reflect.defineMetadata(REQUEST_BODY_FILES_KEY, [], target, propertyKey);
|
|
55
|
+
}
|
|
56
|
+
const existingMetadata = Reflect.getMetadata(
|
|
57
|
+
REQUEST_BODY_FILES_KEY,
|
|
58
|
+
target,
|
|
59
|
+
propertyKey,
|
|
60
|
+
) as {
|
|
61
|
+
fieldName: string;
|
|
62
|
+
index: number;
|
|
63
|
+
}[];
|
|
64
|
+
existingMetadata.push({
|
|
65
|
+
fieldName: fieldName ? fieldName : "all",
|
|
66
|
+
index: parameterIndex,
|
|
67
|
+
});
|
|
68
|
+
Reflect.defineMetadata(
|
|
69
|
+
REQUEST_BODY_FILES_KEY,
|
|
70
|
+
existingMetadata,
|
|
71
|
+
target,
|
|
72
|
+
propertyKey,
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Foptions = {
|
|
78
|
+
saveAs?: string;
|
|
79
|
+
dest?: true;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type MultipartFile = FsM | SavedMultipartFile;
|
|
83
|
+
export function UploadFileFromRequest(req: IRequest, options?: Foptions) {
|
|
84
|
+
return Promise.resolve(
|
|
85
|
+
req.file().then(async (f) => {
|
|
86
|
+
if (f && f.file) {
|
|
87
|
+
let fname = f.filename;
|
|
88
|
+
if (options) {
|
|
89
|
+
if (options.dest) {
|
|
90
|
+
fname = options.saveAs
|
|
91
|
+
? options.dest + "/" + options.saveAs
|
|
92
|
+
: options.dest + "/" + f.filename;
|
|
93
|
+
} else {
|
|
94
|
+
fname = path.join(
|
|
95
|
+
process.cwd(),
|
|
96
|
+
`public/${options.saveAs ? options.saveAs : f.filename}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
fname = path.join(process.cwd(), `public/${f.filename}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (fs.existsSync(fname)) {
|
|
104
|
+
throw new InternalErrorException("File already exists.");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
await pipeline(f.file!, fs.createWriteStream(fname));
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
...f,
|
|
111
|
+
filename: options?.saveAs ? options.saveAs : f.filename,
|
|
112
|
+
} as MultipartFile;
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
}
|