@avleon/core 0.0.20 → 0.0.24
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 +559 -2
- package/dist/application.d.ts +26 -0
- package/dist/application.js +50 -0
- package/dist/collection.js +3 -2
- package/dist/container.d.ts +1 -0
- package/dist/container.js +2 -1
- package/dist/environment-variables.js +1 -1
- package/dist/file-storage.js +0 -128
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.js +41 -4
- package/dist/icore.d.ts +68 -26
- package/dist/icore.js +235 -212
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -2
- package/dist/middleware.js +0 -8
- package/dist/multipart.js +4 -1
- package/dist/openapi.d.ts +37 -37
- package/dist/params.js +2 -2
- package/dist/response.js +6 -2
- package/dist/route-methods.js +1 -1
- package/dist/swagger-schema.js +4 -1
- package/dist/testing.js +5 -2
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/optional-require.d.ts +8 -0
- package/dist/utils/optional-require.js +70 -0
- package/dist/validation.d.ts +4 -1
- package/dist/validation.js +10 -5
- package/package.json +19 -11
- package/src/application.ts +96 -0
- package/src/authentication.ts +16 -0
- package/src/collection.ts +254 -0
- package/src/config.ts +39 -0
- package/src/constants.ts +1 -0
- package/src/container.ts +54 -0
- package/src/controller.ts +128 -0
- package/src/decorators.ts +27 -0
- package/src/environment-variables.ts +46 -0
- package/src/exceptions/http-exceptions.ts +86 -0
- package/src/exceptions/index.ts +1 -0
- package/src/exceptions/system-exception.ts +34 -0
- package/src/file-storage.ts +206 -0
- package/src/helpers.ts +328 -0
- package/src/icore.ts +1064 -0
- package/src/index.ts +30 -0
- package/src/logger.ts +72 -0
- package/src/map-types.ts +159 -0
- package/src/middleware.ts +98 -0
- package/src/multipart.ts +116 -0
- package/src/openapi.ts +372 -0
- package/src/params.ts +111 -0
- package/src/queue.ts +126 -0
- package/src/response.ts +117 -0
- package/src/results.ts +30 -0
- package/src/route-methods.ts +186 -0
- package/src/swagger-schema.ts +213 -0
- package/src/testing.ts +220 -0
- package/src/types/app-builder.interface.ts +19 -0
- package/src/types/application.interface.ts +9 -0
- package/src/utils/hash.ts +5 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/optional-require.ts +50 -0
- package/src/validation.ts +156 -0
- package/src/validator-extend.ts +25 -0
- package/dist/classToOpenapi.d.ts +0 -0
- package/dist/classToOpenapi.js +0 -1
- package/dist/render.d.ts +0 -1
- package/dist/render.js +0 -8
- package/jest.config.ts +0 -9
- package/tsconfig.json +0 -25
- /package/dist/{security.d.ts → utils/hash.d.ts} +0 -0
- /package/dist/{security.js → utils/hash.js} +0 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
import * as sw from "./swagger-schema";
|
|
8
|
+
export * from "./icore";
|
|
9
|
+
export * from "./testing";
|
|
10
|
+
export { inject, validateRequestBody,pick, exclude } from "./helpers";
|
|
11
|
+
export * from "./decorators";
|
|
12
|
+
export * from "./middleware";
|
|
13
|
+
export * from "./config";
|
|
14
|
+
export * from "./openapi";
|
|
15
|
+
export * from "./map-types";
|
|
16
|
+
export * from "./response";
|
|
17
|
+
export * from "./exceptions";
|
|
18
|
+
export * from "./validator-extend";
|
|
19
|
+
export * from "./validation";
|
|
20
|
+
export * from "./environment-variables";
|
|
21
|
+
export * from "./collection";
|
|
22
|
+
export * from "./queue";
|
|
23
|
+
export * from "./utils/hash";
|
|
24
|
+
export * from "./multipart";
|
|
25
|
+
export * from "./file-storage";
|
|
26
|
+
export * from "./logger";
|
|
27
|
+
|
|
28
|
+
export const GetSchema = sw.generateSwaggerSchema;
|
|
29
|
+
|
|
30
|
+
export { default as Container } from "./container";
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
import { AppService } from "./decorators";
|
|
3
|
+
|
|
4
|
+
@AppService
|
|
5
|
+
export class LoggerService {
|
|
6
|
+
private logger: pino.Logger;
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.logger = pino({
|
|
10
|
+
level: process.env.LOG_LEVEL || "info",
|
|
11
|
+
transport: {
|
|
12
|
+
target: "pino-pretty",
|
|
13
|
+
options: {
|
|
14
|
+
translateTime: "SYS:standard",
|
|
15
|
+
ignore: "pid,hostname",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getLogger(): pino.Logger {
|
|
22
|
+
return this.logger;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
info(message: string, obj?: any): void {
|
|
26
|
+
if (obj) {
|
|
27
|
+
this.logger.info(obj, message);
|
|
28
|
+
} else {
|
|
29
|
+
this.logger.info(message);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
error(message: string, obj?: any): void {
|
|
34
|
+
if (obj) {
|
|
35
|
+
this.logger.error(obj, message);
|
|
36
|
+
} else {
|
|
37
|
+
this.logger.error(message);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
warn(message: string, obj?: any): void {
|
|
42
|
+
if (obj) {
|
|
43
|
+
this.logger.warn(obj, message);
|
|
44
|
+
} else {
|
|
45
|
+
this.logger.warn(message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
debug(message: string, obj?: any): void {
|
|
50
|
+
if (obj) {
|
|
51
|
+
this.logger.debug(obj, message);
|
|
52
|
+
} else {
|
|
53
|
+
this.logger.debug(message);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fatal(message: string, obj?: any): void {
|
|
58
|
+
if (obj) {
|
|
59
|
+
this.logger.fatal(obj, message);
|
|
60
|
+
} else {
|
|
61
|
+
this.logger.fatal(message);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
trace(message: string, obj?: any): void {
|
|
66
|
+
if (obj) {
|
|
67
|
+
this.logger.trace(obj, message);
|
|
68
|
+
} else {
|
|
69
|
+
this.logger.trace(message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/map-types.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
//
|
|
9
|
+
type Constructor<T = any> = new (...args: any[]) => T;
|
|
10
|
+
|
|
11
|
+
export function PartialType<T>(
|
|
12
|
+
BaseClass: Constructor<T>,
|
|
13
|
+
): Constructor<Partial<T>> {
|
|
14
|
+
const baseProperties: string[] = [];
|
|
15
|
+
let currentPrototype = BaseClass.prototype;
|
|
16
|
+
|
|
17
|
+
// Collect properties from the base class (including inherited ones)
|
|
18
|
+
while (currentPrototype && currentPrototype !== Object.prototype) {
|
|
19
|
+
const properties = Object.getOwnPropertyNames(currentPrototype).filter(
|
|
20
|
+
(prop) => prop !== "constructor", // Exclude the constructor
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
// Retrieve metadata for each property
|
|
24
|
+
properties.forEach((key) => {
|
|
25
|
+
// Check if the property has type metadata (design:type)
|
|
26
|
+
const designType = Reflect.getMetadata(
|
|
27
|
+
"design:type",
|
|
28
|
+
currentPrototype,
|
|
29
|
+
key,
|
|
30
|
+
);
|
|
31
|
+
if (designType) {
|
|
32
|
+
baseProperties.push(key);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Retrieve validation metadata (class-validator)
|
|
36
|
+
const validationMetadata = Reflect.getMetadataKeys(currentPrototype, key);
|
|
37
|
+
validationMetadata.forEach((metadataKey) => {});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
currentPrototype = Object.getPrototypeOf(currentPrototype); // Move up the prototype chain
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class PartialClass {}
|
|
44
|
+
|
|
45
|
+
// Define properties as optional and copy metadata
|
|
46
|
+
baseProperties.forEach((key) => {
|
|
47
|
+
const propertyType = Reflect.getMetadata(
|
|
48
|
+
"design:type",
|
|
49
|
+
BaseClass.prototype,
|
|
50
|
+
key,
|
|
51
|
+
);
|
|
52
|
+
Reflect.defineMetadata(
|
|
53
|
+
"design:type",
|
|
54
|
+
propertyType,
|
|
55
|
+
PartialClass.prototype,
|
|
56
|
+
key,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Propagate class-validator metadata to the new class
|
|
60
|
+
const validationMetadataKeys =
|
|
61
|
+
Reflect.getMetadataKeys(BaseClass.prototype, key) || [];
|
|
62
|
+
validationMetadataKeys.forEach((metadataKey) => {
|
|
63
|
+
const metadataValue = Reflect.getMetadata(
|
|
64
|
+
metadataKey,
|
|
65
|
+
BaseClass.prototype,
|
|
66
|
+
key,
|
|
67
|
+
);
|
|
68
|
+
Reflect.defineMetadata(
|
|
69
|
+
metadataKey,
|
|
70
|
+
metadataValue,
|
|
71
|
+
PartialClass.prototype,
|
|
72
|
+
key,
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Copy other metadata from the base class (non-property metadata)
|
|
78
|
+
Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
|
|
79
|
+
const metadataValue = Reflect.getMetadata(key, BaseClass.prototype);
|
|
80
|
+
Reflect.defineMetadata(key, metadataValue, PartialClass.prototype);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return PartialClass as Constructor<Partial<T>>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Utility to pick specific properties from a class.
|
|
88
|
+
*/
|
|
89
|
+
export function PickType<T, K extends keyof T>(
|
|
90
|
+
BaseClass: Constructor<T>,
|
|
91
|
+
keys: K[],
|
|
92
|
+
): Constructor<Pick<T, K>> {
|
|
93
|
+
class PickClass {
|
|
94
|
+
constructor() {
|
|
95
|
+
(keys as string[]).forEach((key: string) => {
|
|
96
|
+
Reflect.defineMetadata(
|
|
97
|
+
"design:type",
|
|
98
|
+
Reflect.getMetadata("design:type", BaseClass.prototype, key),
|
|
99
|
+
this,
|
|
100
|
+
key,
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
Reflect.decorate([Reflect.metadata("design:properties", keys)], PickClass);
|
|
107
|
+
|
|
108
|
+
// Copy metadata from BaseClass to PickClass
|
|
109
|
+
Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
|
|
110
|
+
Reflect.defineMetadata(
|
|
111
|
+
key,
|
|
112
|
+
Reflect.getMetadata(key, BaseClass.prototype),
|
|
113
|
+
PickClass.prototype,
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return PickClass as Constructor<Pick<T, K>>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Utility to omit specific properties from a class.
|
|
122
|
+
*/
|
|
123
|
+
export function OmitType<T, K extends keyof T>(
|
|
124
|
+
BaseClass: Constructor<T>,
|
|
125
|
+
keys: K[],
|
|
126
|
+
): Constructor<Omit<T, K>> {
|
|
127
|
+
const allKeys = Reflect.getMetadata("design:properties", BaseClass) || [];
|
|
128
|
+
const omitKeys = new Set(keys);
|
|
129
|
+
const finalKeys = allKeys.filter((key: string) => !omitKeys.has(key as any));
|
|
130
|
+
|
|
131
|
+
class OmitClass {
|
|
132
|
+
constructor() {
|
|
133
|
+
finalKeys.forEach((key: string) => {
|
|
134
|
+
Reflect.defineMetadata(
|
|
135
|
+
"design:type",
|
|
136
|
+
Reflect.getMetadata("design:type", BaseClass.prototype, key),
|
|
137
|
+
this,
|
|
138
|
+
key,
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
Reflect.decorate(
|
|
145
|
+
[Reflect.metadata("design:properties", finalKeys)],
|
|
146
|
+
OmitClass,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Copy metadata from BaseClass to OmitClass
|
|
150
|
+
Reflect.getMetadataKeys(BaseClass.prototype).forEach((key) => {
|
|
151
|
+
Reflect.defineMetadata(
|
|
152
|
+
key,
|
|
153
|
+
Reflect.getMetadata(key, BaseClass.prototype),
|
|
154
|
+
OmitClass.prototype,
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return OmitClass as Constructor<Omit<T, K>>;
|
|
159
|
+
}
|
|
@@ -0,0 +1,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 { HttpException, UnauthorizedException } from "./exceptions";
|
|
10
|
+
import Container, { AUTHORIZATION_META_KEY } from "./container";
|
|
11
|
+
|
|
12
|
+
export abstract class AppMiddleware {
|
|
13
|
+
abstract invoke(req: IRequest, res?: IResponse): Promise<IRequest|HttpException>;
|
|
14
|
+
}
|
|
15
|
+
export type AuthHandler = (req: IRequest, roles?: string[]) => Promise<IRequest | HttpException>;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export type Constructor<T> = { new(...args: any[]): T };
|
|
20
|
+
|
|
21
|
+
export abstract class AuthorizeMiddleware {
|
|
22
|
+
abstract authorize(roles: string[]): (req: IRequest, res?: IResponse) => IRequest | Promise<IRequest>;
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export type AuthReturnTypes = IRequest | Promise<IRequest>
|
|
28
|
+
|
|
29
|
+
interface AuthorizeClass {
|
|
30
|
+
authorize(req: IRequest, options?:any): AuthReturnTypes;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function Authorize(target: { new (...args: any[]): AuthorizeClass }) {
|
|
34
|
+
if (typeof target.prototype.authorize !== "function") {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Class "${target.name}" must implement an "authorize" method.`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
Service()(target);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// export function Authorized(target: Function): void;
|
|
44
|
+
export function Authorized(): ClassDecorator & MethodDecorator;
|
|
45
|
+
export function Authorized(options?: any): ClassDecorator & MethodDecorator;
|
|
46
|
+
export function Authorized(options: any = {}): MethodDecorator | ClassDecorator {
|
|
47
|
+
return function (target: any, propertyKey?: string | symbol, descriptor?: PropertyDescriptor) {
|
|
48
|
+
if (propertyKey && descriptor) {
|
|
49
|
+
Reflect.defineMetadata(AUTHORIZATION_META_KEY, { authorize: true, options}, target.constructor, propertyKey);
|
|
50
|
+
} else {
|
|
51
|
+
Reflect.defineMetadata(AUTHORIZATION_META_KEY, { authorize: true, options}, target);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
export function Middleware(target: Constructor<AppMiddleware>) {
|
|
58
|
+
if (typeof target.prototype.invoke !== "function") {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Class "${target.name}" must implement an "invoke" method.`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
Service()(target);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
export function UseMiddleware<T extends AppMiddleware | (new (...args: any[]) => AppMiddleware)>(
|
|
69
|
+
options: T | T[],
|
|
70
|
+
): MethodDecorator & ClassDecorator {
|
|
71
|
+
return function (
|
|
72
|
+
target: Object | Function,
|
|
73
|
+
propertyKey?: string | symbol,
|
|
74
|
+
descriptor?: PropertyDescriptor,
|
|
75
|
+
) {
|
|
76
|
+
const normalizeMiddleware = (middleware: any) =>
|
|
77
|
+
typeof middleware === "function" ? new middleware() : middleware;
|
|
78
|
+
const middlewareList = (Array.isArray(options) ? options : [options]).map(normalizeMiddleware);
|
|
79
|
+
if (typeof target === "function" && !propertyKey) {
|
|
80
|
+
const existingMiddlewares =
|
|
81
|
+
Reflect.getMetadata("controller:middleware", target) || [];
|
|
82
|
+
Reflect.defineMetadata(
|
|
83
|
+
"controller:middleware",
|
|
84
|
+
[...existingMiddlewares, ...middlewareList],
|
|
85
|
+
target
|
|
86
|
+
);
|
|
87
|
+
} else if (descriptor) {
|
|
88
|
+
const existingMiddlewares =
|
|
89
|
+
Reflect.getMetadata("route:middleware", target, propertyKey!) || [];
|
|
90
|
+
Reflect.defineMetadata(
|
|
91
|
+
"route:middleware",
|
|
92
|
+
[...existingMiddlewares, ...middlewareList],
|
|
93
|
+
target,
|
|
94
|
+
propertyKey!
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
package/src/multipart.ts
ADDED
|
@@ -0,0 +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
|
+
}
|