@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/config.ts
CHANGED
|
@@ -1,42 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
import { Container, Service, Constructable } from "typedi";
|
|
8
|
-
import { Environment } from "./environment-variables";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
import { Container, Service, Constructable, Token } from "typedi";
|
|
8
|
+
import { Environment } from "./environment-variables";
|
|
9
|
+
import { inject } from "./helpers";
|
|
10
|
+
|
|
11
|
+
export interface IConfig<T = any> {
|
|
12
|
+
config(env: Environment): T;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function Config<T extends IConfig>(target: Constructable<T>) {
|
|
16
|
+
Container.set({ id: target, type: target });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class AppConfig {
|
|
20
|
+
get<T extends IConfig<R>, R>(configClass: Constructable<T>): R {
|
|
21
|
+
const instance = Container.get(configClass);
|
|
22
|
+
if (!instance) {
|
|
23
|
+
throw new Error(`Configuration for ${configClass.name} not found.`);
|
|
24
|
+
}
|
|
25
|
+
return instance.config(new Environment());
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// export function GetConfig<
|
|
30
|
+
// T extends IConfig<R>,
|
|
31
|
+
// R = ReturnType<InstanceType<Constructable<T>>["config"]>,
|
|
32
|
+
// >(ConfigClass: Constructable<T>): R {
|
|
33
|
+
// const instance = Container.get(ConfigClass);
|
|
34
|
+
// if (!instance) {
|
|
35
|
+
// throw new Error(
|
|
36
|
+
// `Class "${ConfigClass.name}" is not registered as a config.`,
|
|
37
|
+
// );
|
|
38
|
+
// }
|
|
39
|
+
// return instance.config(new Environment());
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
export function GetConfig<
|
|
43
|
+
T extends IConfig<R>,
|
|
44
|
+
R = ReturnType<InstanceType<Constructable<T>>["config"]>,
|
|
45
|
+
>(ConfigClass: Constructable<T>): R;
|
|
46
|
+
|
|
47
|
+
export function GetConfig<T = any>(config: string | symbol): T;
|
|
48
|
+
|
|
49
|
+
// Implementation
|
|
50
|
+
export function GetConfig<R>(token: any): R {
|
|
51
|
+
// 1. Class‐based: token.prototype.config is a function
|
|
52
|
+
if (
|
|
53
|
+
typeof token === "function" &&
|
|
54
|
+
token.prototype != null &&
|
|
55
|
+
typeof token.prototype.config === "function"
|
|
56
|
+
) {
|
|
57
|
+
const instance = Container.get(token as Constructable<any>);
|
|
58
|
+
if (!instance) {
|
|
59
|
+
throw new Error(`Class "${token.name}" is not registered as a config.`);
|
|
60
|
+
}
|
|
61
|
+
return instance.config(inject(Environment));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 2. Functional: token is the callback itself
|
|
65
|
+
const stored = Container.get(token);
|
|
66
|
+
if (!stored) {
|
|
67
|
+
throw new Error("Config object is not registered.");
|
|
68
|
+
}
|
|
69
|
+
return stored as R;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function CreateConfig<T>(
|
|
73
|
+
token: string | symbol,
|
|
74
|
+
callback: (env: Environment) => T,
|
|
75
|
+
) {
|
|
76
|
+
let env!: Environment;
|
|
77
|
+
try {
|
|
78
|
+
env = Container.get(Environment);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
env = new Environment();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let config: T = callback(env);
|
|
84
|
+
Container.set<T>(token as Token<T>, config);
|
|
85
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const TEST_DATASOURCE_OPTIONS_KEY = Symbol("itestdatasource");
|
|
1
|
+
export const TEST_DATASOURCE_OPTIONS_KEY = Symbol("itestdatasource");
|
package/src/container.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
import TypediContainer, { ContainerInstance, Token } from "typedi";
|
|
8
|
-
import { DataSource } from "typeorm";
|
|
9
|
-
|
|
10
|
-
export const FEATURE_KEY = Symbol.for("features");
|
|
11
|
-
export const ROUTE_META_KEY = Symbol("iroute:options");
|
|
12
|
-
export const CONTROLLER_META_KEY = Symbol("icontroller:options");
|
|
13
|
-
export const PARAM_META_KEY = Symbol("iparam:options");
|
|
14
|
-
export const QUERY_META_KEY = Symbol("iparam:options");
|
|
15
|
-
export const REQUEST_BODY_META_KEY = Symbol("iparam:options");
|
|
16
|
-
export const REQUEST_BODY_FILE_KEY = Symbol("iparam:options");
|
|
17
|
-
export const REQUEST_BODY_FILES_KEY = Symbol("iparam:options");
|
|
18
|
-
export const REQUEST_USER_META_KEY = Symbol("iparam:options");
|
|
19
|
-
export const REQUEST_HEADER_META_KEY = Symbol("iheader:options");
|
|
20
|
-
export const DATASOURCE_META_KEY = Symbol("idatasource:options");
|
|
21
|
-
export const AUTHORIZATION_META_KEY = Symbol("idatasource:authorization");
|
|
22
|
-
|
|
23
|
-
const controllerRegistry = new Set<Function>();
|
|
24
|
-
const serviceRegistry = new Set<Function>();
|
|
25
|
-
const optionsRegistry = new Map<string, any>();
|
|
26
|
-
|
|
27
|
-
const Container = TypediContainer;
|
|
28
|
-
|
|
29
|
-
export function registerController(controller: Function) {
|
|
30
|
-
controllerRegistry.add(controller);
|
|
31
|
-
}
|
|
32
|
-
export function registerService(service: Function) {
|
|
33
|
-
Container.set(service, service);
|
|
34
|
-
serviceRegistry.add(service);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function getRegisteredServices(): Function[] {
|
|
38
|
-
return Array.from(serviceRegistry);
|
|
39
|
-
}
|
|
40
|
-
export function getRegisteredControllers(): Function[] {
|
|
41
|
-
return Array.from(controllerRegistry);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const API_CONTROLLER_METADATA_KEY = Symbol("apiController");
|
|
45
|
-
|
|
46
|
-
export function isApiController(target: Function): boolean {
|
|
47
|
-
return Reflect.getMetadata(API_CONTROLLER_METADATA_KEY, target) === true;
|
|
48
|
-
}
|
|
49
|
-
Container.set<string>("appName", "Iqra");
|
|
50
|
-
|
|
51
|
-
export function registerDataSource(dataSource: any) {
|
|
52
|
-
Container.set<DataSource>("idatasource", dataSource);
|
|
53
|
-
}
|
|
54
|
-
export default Container;
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
import TypediContainer, { ContainerInstance, Token } from "typedi";
|
|
8
|
+
import { DataSource } from "typeorm";
|
|
9
|
+
|
|
10
|
+
export const FEATURE_KEY = Symbol.for("features");
|
|
11
|
+
export const ROUTE_META_KEY = Symbol("iroute:options");
|
|
12
|
+
export const CONTROLLER_META_KEY = Symbol("icontroller:options");
|
|
13
|
+
export const PARAM_META_KEY = Symbol("iparam:options");
|
|
14
|
+
export const QUERY_META_KEY = Symbol("iparam:options");
|
|
15
|
+
export const REQUEST_BODY_META_KEY = Symbol("iparam:options");
|
|
16
|
+
export const REQUEST_BODY_FILE_KEY = Symbol("iparam:options");
|
|
17
|
+
export const REQUEST_BODY_FILES_KEY = Symbol("iparam:options");
|
|
18
|
+
export const REQUEST_USER_META_KEY = Symbol("iparam:options");
|
|
19
|
+
export const REQUEST_HEADER_META_KEY = Symbol("iheader:options");
|
|
20
|
+
export const DATASOURCE_META_KEY = Symbol("idatasource:options");
|
|
21
|
+
export const AUTHORIZATION_META_KEY = Symbol("idatasource:authorization");
|
|
22
|
+
|
|
23
|
+
const controllerRegistry = new Set<Function>();
|
|
24
|
+
const serviceRegistry = new Set<Function>();
|
|
25
|
+
const optionsRegistry = new Map<string, any>();
|
|
26
|
+
|
|
27
|
+
const Container = TypediContainer;
|
|
28
|
+
|
|
29
|
+
export function registerController(controller: Function) {
|
|
30
|
+
controllerRegistry.add(controller);
|
|
31
|
+
}
|
|
32
|
+
export function registerService(service: Function) {
|
|
33
|
+
Container.set(service, service);
|
|
34
|
+
serviceRegistry.add(service);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getRegisteredServices(): Function[] {
|
|
38
|
+
return Array.from(serviceRegistry);
|
|
39
|
+
}
|
|
40
|
+
export function getRegisteredControllers(): Function[] {
|
|
41
|
+
return Array.from(controllerRegistry);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const API_CONTROLLER_METADATA_KEY = Symbol("apiController");
|
|
45
|
+
|
|
46
|
+
export function isApiController(target: Function): boolean {
|
|
47
|
+
return Reflect.getMetadata(API_CONTROLLER_METADATA_KEY, target) === true;
|
|
48
|
+
}
|
|
49
|
+
Container.set<string>("appName", "Iqra");
|
|
50
|
+
|
|
51
|
+
export function registerDataSource(dataSource: any) {
|
|
52
|
+
Container.set<DataSource>("idatasource", dataSource);
|
|
53
|
+
}
|
|
54
|
+
export default Container;
|
package/src/controller.ts
CHANGED
|
@@ -1,127 +1,125 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import Container, { Service } from "typedi";
|
|
9
|
-
import container, {
|
|
10
|
-
API_CONTROLLER_METADATA_KEY,
|
|
11
|
-
CONTROLLER_META_KEY,
|
|
12
|
-
registerController,
|
|
13
|
-
} from "./container";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Options for configuring a controller.
|
|
17
|
-
* @remarks
|
|
18
|
-
* Controller default options
|
|
19
|
-
* @type {Object} ControllerOptions
|
|
20
|
-
* @property {string} [name] - The name of the controller.
|
|
21
|
-
* @property {string} [path] - The base path for the controller's routes.
|
|
22
|
-
* @property {string} [version] - The version of the controller. If not provided, it will default to the version from `package.json`.
|
|
23
|
-
* @property {string} [since] - The date or version since the controller was introduced.
|
|
24
|
-
* @property {any} [meta] - Additional metadata associated with the controller.
|
|
25
|
-
*/
|
|
26
|
-
export type ControllerOptions = {
|
|
27
|
-
/**
|
|
28
|
-
*@property {string} name
|
|
29
|
-
*@description Name of the controller. If specified it'll used as swagger tags
|
|
30
|
-
*@default Contorller class name
|
|
31
|
-
* */
|
|
32
|
-
name?: string;
|
|
33
|
-
path?: string;
|
|
34
|
-
version?: string; // Will look at package.json if not set
|
|
35
|
-
since?: string;
|
|
36
|
-
meta?: any;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export function createControllerDecorator(
|
|
40
|
-
type: "api" | "web" = "web",
|
|
41
|
-
): (
|
|
42
|
-
pathOrOptions?: string | ControllerOptions,
|
|
43
|
-
maybeOptions?: ControllerOptions,
|
|
44
|
-
) => ClassDecorator {
|
|
45
|
-
return function (
|
|
46
|
-
pathOrOptions?: string | ControllerOptions,
|
|
47
|
-
maybeOptions?: ControllerOptions,
|
|
48
|
-
): ClassDecorator {
|
|
49
|
-
return function (target: Function) {
|
|
50
|
-
let path = "/";
|
|
51
|
-
let options: ControllerOptions = {};
|
|
52
|
-
|
|
53
|
-
if (typeof pathOrOptions === "string") {
|
|
54
|
-
path = pathOrOptions;
|
|
55
|
-
options = maybeOptions || {};
|
|
56
|
-
} else if (typeof pathOrOptions === "object") {
|
|
57
|
-
options = pathOrOptions;
|
|
58
|
-
path = options.path || "/";
|
|
59
|
-
}
|
|
60
|
-
Reflect.defineMetadata(API_CONTROLLER_METADATA_KEY, true, target);
|
|
61
|
-
// Ensure Service is applied as a ClassDecorator
|
|
62
|
-
if (typeof Service === "function") {
|
|
63
|
-
registerController(target); // Add to custom registry
|
|
64
|
-
Service()(target); // Apply DI decorator
|
|
65
|
-
Reflect.defineMetadata(
|
|
66
|
-
CONTROLLER_META_KEY,
|
|
67
|
-
{ type, path, options },
|
|
68
|
-
target,
|
|
69
|
-
);
|
|
70
|
-
} else {
|
|
71
|
-
throw new Error("Service decorator is not a function");
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Predefined Controller Decorators
|
|
78
|
-
//export const Controller = createControllerDecorator("web");
|
|
79
|
-
/**
|
|
80
|
-
*@description Api controller's are used for rest . It will populate
|
|
81
|
-
* json on return and all it http methods {get} {post} etc must return
|
|
82
|
-
*Results.*
|
|
83
|
-
* @param path {string} this will used as route prefix
|
|
84
|
-
*
|
|
85
|
-
**/
|
|
86
|
-
|
|
87
|
-
export function ApiController(target: Function): void;
|
|
88
|
-
export function ApiController(path: string): ClassDecorator;
|
|
89
|
-
/**
|
|
90
|
-
*@description Api controller's are used for rest . It will populate
|
|
91
|
-
* json on return and all it http methods {get} {post} etc must return
|
|
92
|
-
* Results.*
|
|
93
|
-
* @param {ControllerOptions} options this will used as route prefix
|
|
94
|
-
*
|
|
95
|
-
**/
|
|
96
|
-
export function ApiController(options: ControllerOptions): ClassDecorator;
|
|
97
|
-
export function ApiController(
|
|
98
|
-
path: string,
|
|
99
|
-
options?: ControllerOptions,
|
|
100
|
-
): ClassDecorator;
|
|
101
|
-
export function ApiController(
|
|
102
|
-
pathOrOptions: Function | string | ControllerOptions = "/",
|
|
103
|
-
mayBeOptions?: ControllerOptions,
|
|
104
|
-
): any {
|
|
105
|
-
if (typeof pathOrOptions ==
|
|
106
|
-
Reflect.defineMetadata(API_CONTROLLER_METADATA_KEY, true, pathOrOptions);
|
|
107
|
-
// Ensure Service is applied as a ClassDecorator
|
|
108
|
-
if (typeof Service === "function") {
|
|
109
|
-
registerController(pathOrOptions); // Add to custom registry
|
|
110
|
-
Service()(pathOrOptions); // Apply DI decorator
|
|
111
|
-
Reflect.defineMetadata(
|
|
112
|
-
CONTROLLER_META_KEY,
|
|
113
|
-
{ type:
|
|
114
|
-
pathOrOptions,
|
|
115
|
-
);
|
|
116
|
-
} else {
|
|
117
|
-
throw new Error("Service decorator is not a function");
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
if (mayBeOptions) {
|
|
121
|
-
return createControllerDecorator("api")(pathOrOptions, mayBeOptions);
|
|
122
|
-
}
|
|
123
|
-
return createControllerDecorator("api")(pathOrOptions);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import Container, { Service } from "typedi";
|
|
9
|
+
import container, {
|
|
10
|
+
API_CONTROLLER_METADATA_KEY,
|
|
11
|
+
CONTROLLER_META_KEY,
|
|
12
|
+
registerController,
|
|
13
|
+
} from "./container";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for configuring a controller.
|
|
17
|
+
* @remarks
|
|
18
|
+
* Controller default options
|
|
19
|
+
* @type {Object} ControllerOptions
|
|
20
|
+
* @property {string} [name] - The name of the controller.
|
|
21
|
+
* @property {string} [path] - The base path for the controller's routes.
|
|
22
|
+
* @property {string} [version] - The version of the controller. If not provided, it will default to the version from `package.json`.
|
|
23
|
+
* @property {string} [since] - The date or version since the controller was introduced.
|
|
24
|
+
* @property {any} [meta] - Additional metadata associated with the controller.
|
|
25
|
+
*/
|
|
26
|
+
export type ControllerOptions = {
|
|
27
|
+
/**
|
|
28
|
+
*@property {string} name
|
|
29
|
+
*@description Name of the controller. If specified it'll used as swagger tags
|
|
30
|
+
*@default Contorller class name
|
|
31
|
+
* */
|
|
32
|
+
name?: string;
|
|
33
|
+
path?: string;
|
|
34
|
+
version?: string; // Will look at package.json if not set
|
|
35
|
+
since?: string;
|
|
36
|
+
meta?: any;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function createControllerDecorator(
|
|
40
|
+
type: "api" | "web" = "web",
|
|
41
|
+
): (
|
|
42
|
+
pathOrOptions?: string | ControllerOptions,
|
|
43
|
+
maybeOptions?: ControllerOptions,
|
|
44
|
+
) => ClassDecorator {
|
|
45
|
+
return function (
|
|
46
|
+
pathOrOptions?: string | ControllerOptions,
|
|
47
|
+
maybeOptions?: ControllerOptions,
|
|
48
|
+
): ClassDecorator {
|
|
49
|
+
return function (target: Function) {
|
|
50
|
+
let path = "/";
|
|
51
|
+
let options: ControllerOptions = {};
|
|
52
|
+
|
|
53
|
+
if (typeof pathOrOptions === "string") {
|
|
54
|
+
path = pathOrOptions;
|
|
55
|
+
options = maybeOptions || {};
|
|
56
|
+
} else if (typeof pathOrOptions === "object") {
|
|
57
|
+
options = pathOrOptions;
|
|
58
|
+
path = options.path || "/";
|
|
59
|
+
}
|
|
60
|
+
Reflect.defineMetadata(API_CONTROLLER_METADATA_KEY, true, target);
|
|
61
|
+
// Ensure Service is applied as a ClassDecorator
|
|
62
|
+
if (typeof Service === "function") {
|
|
63
|
+
registerController(target); // Add to custom registry
|
|
64
|
+
Service()(target); // Apply DI decorator
|
|
65
|
+
Reflect.defineMetadata(
|
|
66
|
+
CONTROLLER_META_KEY,
|
|
67
|
+
{ type, path, options },
|
|
68
|
+
target,
|
|
69
|
+
);
|
|
70
|
+
} else {
|
|
71
|
+
throw new Error("Service decorator is not a function");
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Predefined Controller Decorators
|
|
78
|
+
//export const Controller = createControllerDecorator("web");
|
|
79
|
+
/**
|
|
80
|
+
*@description Api controller's are used for rest . It will populate
|
|
81
|
+
* json on return and all it http methods {get} {post} etc must return
|
|
82
|
+
*Results.*
|
|
83
|
+
* @param path {string} this will used as route prefix
|
|
84
|
+
*
|
|
85
|
+
**/
|
|
86
|
+
|
|
87
|
+
export function ApiController(target: Function): void;
|
|
88
|
+
export function ApiController(path: string): ClassDecorator;
|
|
89
|
+
/**
|
|
90
|
+
*@description Api controller's are used for rest . It will populate
|
|
91
|
+
* json on return and all it http methods {get} {post} etc must return
|
|
92
|
+
* Results.*
|
|
93
|
+
* @param {ControllerOptions} options this will used as route prefix
|
|
94
|
+
*
|
|
95
|
+
**/
|
|
96
|
+
export function ApiController(options: ControllerOptions): ClassDecorator;
|
|
97
|
+
export function ApiController(
|
|
98
|
+
path: string,
|
|
99
|
+
options?: ControllerOptions,
|
|
100
|
+
): ClassDecorator;
|
|
101
|
+
export function ApiController(
|
|
102
|
+
pathOrOptions: Function | string | ControllerOptions = "/",
|
|
103
|
+
mayBeOptions?: ControllerOptions,
|
|
104
|
+
): any {
|
|
105
|
+
if (typeof pathOrOptions == "function") {
|
|
106
|
+
Reflect.defineMetadata(API_CONTROLLER_METADATA_KEY, true, pathOrOptions);
|
|
107
|
+
// Ensure Service is applied as a ClassDecorator
|
|
108
|
+
if (typeof Service === "function") {
|
|
109
|
+
registerController(pathOrOptions); // Add to custom registry
|
|
110
|
+
Service()(pathOrOptions); // Apply DI decorator
|
|
111
|
+
Reflect.defineMetadata(
|
|
112
|
+
CONTROLLER_META_KEY,
|
|
113
|
+
{ type: "api", path: "/", options: {} },
|
|
114
|
+
pathOrOptions,
|
|
115
|
+
);
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error("Service decorator is not a function");
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
if (mayBeOptions) {
|
|
121
|
+
return createControllerDecorator("api")(pathOrOptions, mayBeOptions);
|
|
122
|
+
}
|
|
123
|
+
return createControllerDecorator("api")(pathOrOptions);
|
|
124
|
+
}
|
|
125
|
+
}
|
package/src/decorators.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { Service as _service } from "typedi";
|
|
9
|
-
import container, { registerService } from "./container";
|
|
10
|
-
export function AppService(target: any): void;
|
|
11
|
-
export function AppService(): any;
|
|
12
|
-
export function AppService(target?: any) {
|
|
13
|
-
if (target) {
|
|
14
|
-
_service()(target);
|
|
15
|
-
} else {
|
|
16
|
-
return function (tg: any) {
|
|
17
|
-
_service()(tg);
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export * from "./controller";
|
|
23
|
-
export * from "./route-methods";
|
|
24
|
-
export * from "./openapi";
|
|
25
|
-
export const Utility = _service;
|
|
26
|
-
export const Helper = _service;
|
|
27
|
-
export * from "./params";
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Service as _service } from "typedi";
|
|
9
|
+
import container, { registerService } from "./container";
|
|
10
|
+
export function AppService(target: any): void;
|
|
11
|
+
export function AppService(): any;
|
|
12
|
+
export function AppService(target?: any) {
|
|
13
|
+
if (target) {
|
|
14
|
+
_service()(target);
|
|
15
|
+
} else {
|
|
16
|
+
return function (tg: any) {
|
|
17
|
+
_service()(tg);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export * from "./controller";
|
|
23
|
+
export * from "./route-methods";
|
|
24
|
+
export * from "./openapi";
|
|
25
|
+
export const Utility = _service;
|
|
26
|
+
export const Helper = _service;
|
|
27
|
+
export * from "./params";
|
|
@@ -1,46 +1,53 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import dotenv from "dotenv";
|
|
9
|
-
import path from "path";
|
|
10
|
-
import fs from "fs";
|
|
11
|
-
import { Service } from "typedi";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import dotenv from "dotenv";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import fs, { existsSync } from "fs";
|
|
11
|
+
import { Service } from "typedi";
|
|
12
|
+
import {
|
|
13
|
+
EnvironmentVariableNotFound,
|
|
14
|
+
SystemUseError,
|
|
15
|
+
} from "./exceptions/system-exception";
|
|
16
|
+
|
|
17
|
+
dotenv.config({ path: path.join(process.cwd(), ".env") });
|
|
18
|
+
|
|
19
|
+
@Service()
|
|
20
|
+
export class Environment {
|
|
21
|
+
private parseEnvFile(filePath: string): any {
|
|
22
|
+
try {
|
|
23
|
+
const isExis = existsSync(filePath);
|
|
24
|
+
if (!isExis) {
|
|
25
|
+
return { ...process.env };
|
|
26
|
+
}
|
|
27
|
+
const fileContent = fs.readFileSync(filePath, "utf8");
|
|
28
|
+
const parsedEnv = dotenv.parse(fileContent);
|
|
29
|
+
return { ...parsedEnv, ...process.env };
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error(`Error parsing .env file: ${error}`);
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get<T = any>(key: string): T {
|
|
37
|
+
const parsedEnv = this.parseEnvFile(path.join(process.cwd(), ".env"));
|
|
38
|
+
return parsedEnv[key] as T;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getOrThrow<T = any>(key: string): T {
|
|
42
|
+
const parsedEnv = this.parseEnvFile(path.join(process.cwd(), ".env"));
|
|
43
|
+
if (!Object(parsedEnv).hasOwnProperty(key)) {
|
|
44
|
+
throw new EnvironmentVariableNotFound(key);
|
|
45
|
+
}
|
|
46
|
+
return parsedEnv[key] as T;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getAll<T = any>(): T {
|
|
50
|
+
const parsedEnv = this.parseEnvFile(path.join(process.cwd(), ".env"));
|
|
51
|
+
return parsedEnv as T;
|
|
52
|
+
}
|
|
53
|
+
}
|