@avleon/core 0.0.4
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 +1 -0
- package/dist/config.d.ts +30 -0
- package/dist/config.js +19 -0
- package/dist/container.d.ts +19 -0
- package/dist/container.js +51 -0
- package/dist/controller.d.ts +47 -0
- package/dist/controller.js +38 -0
- package/dist/decorators.d.ts +8 -0
- package/dist/decorators.js +25 -0
- package/dist/exceptions/http-exceptions.d.ts +21 -0
- package/dist/exceptions/http-exceptions.js +38 -0
- package/dist/exceptions/index.d.ts +1 -0
- package/dist/exceptions/index.js +17 -0
- package/dist/exceptions/system-exception.d.ts +13 -0
- package/dist/exceptions/system-exception.js +18 -0
- package/dist/helpers.d.ts +24 -0
- package/dist/helpers.js +192 -0
- package/dist/icore.d.ts +58 -0
- package/dist/icore.js +239 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +33 -0
- package/dist/map-types.d.ts +12 -0
- package/dist/map-types.js +85 -0
- package/dist/openapi.d.ts +333 -0
- package/dist/openapi.js +27 -0
- package/dist/params.d.ts +16 -0
- package/dist/params.js +48 -0
- package/dist/queue.d.ts +0 -0
- package/dist/queue.js +1 -0
- package/dist/repository.d.ts +0 -0
- package/dist/repository.js +1 -0
- package/dist/response.d.ts +9 -0
- package/dist/response.js +36 -0
- package/dist/results.d.ts +20 -0
- package/dist/results.js +32 -0
- package/dist/route-methods.d.ts +56 -0
- package/dist/route-methods.js +83 -0
- package/dist/swagger-schema.d.ts +1 -0
- package/dist/swagger-schema.js +29 -0
- package/dist/validator-extend.d.ts +1 -0
- package/dist/validator-extend.js +22 -0
- package/jest.config.ts +9 -0
- package/package.json +39 -0
- package/tsconfig.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# avleon-core
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface IDBConfig {
|
|
2
|
+
type: string;
|
|
3
|
+
host: string;
|
|
4
|
+
port: number | string;
|
|
5
|
+
username: string;
|
|
6
|
+
password: string;
|
|
7
|
+
database: string;
|
|
8
|
+
synchronize: boolean;
|
|
9
|
+
logging: boolean;
|
|
10
|
+
entities: any[];
|
|
11
|
+
migrations?: string[];
|
|
12
|
+
subscribers?: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface Environment extends NodeJS.ProcessEnv {
|
|
15
|
+
[key: string]: string | undefined;
|
|
16
|
+
}
|
|
17
|
+
export interface IAppConfig {
|
|
18
|
+
apiKey: string;
|
|
19
|
+
timezone: string;
|
|
20
|
+
}
|
|
21
|
+
interface ConfigClass {
|
|
22
|
+
invoke(env: Environment): object;
|
|
23
|
+
}
|
|
24
|
+
export declare function Config(target: {
|
|
25
|
+
new (...args: any[]): ConfigClass;
|
|
26
|
+
}): void;
|
|
27
|
+
export declare function GetConfig<T extends ConfigClass>(ConfigClass: {
|
|
28
|
+
new (...args: any[]): T;
|
|
29
|
+
}): ReturnType<T['invoke']>;
|
|
30
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Config = Config;
|
|
4
|
+
exports.GetConfig = GetConfig;
|
|
5
|
+
const CONFIG_METADATA_KEY = Symbol('config');
|
|
6
|
+
function Config(target) {
|
|
7
|
+
if (typeof target.prototype.invoke !== 'function') {
|
|
8
|
+
throw new Error(`Class "${target.name}" must implement an "invoke" method.`);
|
|
9
|
+
}
|
|
10
|
+
Reflect.defineMetadata(CONFIG_METADATA_KEY, target, target);
|
|
11
|
+
}
|
|
12
|
+
function GetConfig(ConfigClass) {
|
|
13
|
+
const ConfigCtor = Reflect.getMetadata(CONFIG_METADATA_KEY, ConfigClass);
|
|
14
|
+
if (!ConfigCtor) {
|
|
15
|
+
throw new Error(`Class "${ConfigClass.name}" is not decorated with @Config.`);
|
|
16
|
+
}
|
|
17
|
+
const instance = new ConfigCtor();
|
|
18
|
+
return instance.invoke(process.env);
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ContainerInstance } from "typedi";
|
|
2
|
+
export declare const ROUTE_META_KEY: unique symbol;
|
|
3
|
+
export declare const CONTROLLER_META_KEY: unique symbol;
|
|
4
|
+
export declare const PARAM_META_KEY: unique symbol;
|
|
5
|
+
export declare const QUERY_META_KEY: unique symbol;
|
|
6
|
+
export declare const REQUEST_BODY_META_KEY: unique symbol;
|
|
7
|
+
export declare const REQUEST_HEADER_META_KEY: unique symbol;
|
|
8
|
+
export declare const DATASOURCE_META_KEY: unique symbol;
|
|
9
|
+
export interface IContainer extends ContainerInstance {
|
|
10
|
+
registerHandler: IContainer;
|
|
11
|
+
}
|
|
12
|
+
declare const Container: IContainer;
|
|
13
|
+
export declare function registerController(controller: Function): void;
|
|
14
|
+
export declare function registerService(service: Function): void;
|
|
15
|
+
export declare function getRegisteredServices(): Function[];
|
|
16
|
+
export declare function getRegisteredControllers(): Function[];
|
|
17
|
+
export declare const API_CONTROLLER_METADATA_KEY: unique symbol;
|
|
18
|
+
export declare function isApiController(target: Function): boolean;
|
|
19
|
+
export default Container;
|
|
@@ -0,0 +1,51 @@
|
|
|
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.API_CONTROLLER_METADATA_KEY = exports.DATASOURCE_META_KEY = exports.REQUEST_HEADER_META_KEY = exports.REQUEST_BODY_META_KEY = exports.QUERY_META_KEY = exports.PARAM_META_KEY = exports.CONTROLLER_META_KEY = exports.ROUTE_META_KEY = void 0;
|
|
7
|
+
exports.registerController = registerController;
|
|
8
|
+
exports.registerService = registerService;
|
|
9
|
+
exports.getRegisteredServices = getRegisteredServices;
|
|
10
|
+
exports.getRegisteredControllers = getRegisteredControllers;
|
|
11
|
+
exports.isApiController = isApiController;
|
|
12
|
+
const typedi_1 = __importDefault(require("typedi"));
|
|
13
|
+
exports.ROUTE_META_KEY = Symbol("iroute:options");
|
|
14
|
+
exports.CONTROLLER_META_KEY = Symbol("icontroller:options");
|
|
15
|
+
exports.PARAM_META_KEY = Symbol("iparam:options");
|
|
16
|
+
exports.QUERY_META_KEY = Symbol("iparam:options");
|
|
17
|
+
exports.REQUEST_BODY_META_KEY = Symbol("iparam:options");
|
|
18
|
+
exports.REQUEST_HEADER_META_KEY = Symbol("iheader:options");
|
|
19
|
+
exports.DATASOURCE_META_KEY = Symbol("idatasource:options");
|
|
20
|
+
const controllerRegistry = new Set();
|
|
21
|
+
const serviceRegistry = new Set();
|
|
22
|
+
const optionsRegistry = new Map();
|
|
23
|
+
const Container = typedi_1.default.of("avContriner");
|
|
24
|
+
function registerController(controller) {
|
|
25
|
+
controllerRegistry.add(controller);
|
|
26
|
+
}
|
|
27
|
+
function registerService(service) {
|
|
28
|
+
Container.set(service, service);
|
|
29
|
+
serviceRegistry.add(service);
|
|
30
|
+
}
|
|
31
|
+
function getRegisteredServices() {
|
|
32
|
+
return Array.from(serviceRegistry);
|
|
33
|
+
}
|
|
34
|
+
function getRegisteredControllers() {
|
|
35
|
+
return Array.from(controllerRegistry);
|
|
36
|
+
}
|
|
37
|
+
// export const APP_DC_TOKEN = new Token<DataSource>();
|
|
38
|
+
// Container.set(APP_DC_TOKEN, DataSource);
|
|
39
|
+
exports.API_CONTROLLER_METADATA_KEY = Symbol('apiController');
|
|
40
|
+
// export type DBContext = Record<string, ReturnType<DataSource['getRepository']>>;
|
|
41
|
+
// export function registerDataSource(dc: DataSource) {
|
|
42
|
+
// Container.set({
|
|
43
|
+
// id: APP_DC_TOKEN,
|
|
44
|
+
// factory: () => dc,
|
|
45
|
+
// });
|
|
46
|
+
// }
|
|
47
|
+
function isApiController(target) {
|
|
48
|
+
return Reflect.getMetadata(exports.API_CONTROLLER_METADATA_KEY, target) === true;
|
|
49
|
+
}
|
|
50
|
+
Container.set('appName', 'Iqra');
|
|
51
|
+
exports.default = Container;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Options for configuring a controller.
|
|
9
|
+
* @remarks
|
|
10
|
+
* Controller default options
|
|
11
|
+
* @type {Object} ControllerOptions
|
|
12
|
+
* @property {string} [name] - The name of the controller.
|
|
13
|
+
* @property {string} [path] - The base path for the controller's routes.
|
|
14
|
+
* @property {string} [version] - The version of the controller. If not provided, it will default to the version from `package.json`.
|
|
15
|
+
* @property {string} [since] - The date or version since the controller was introduced.
|
|
16
|
+
* @property {any} [meta] - Additional metadata associated with the controller.
|
|
17
|
+
*/
|
|
18
|
+
export type ControllerOptions = {
|
|
19
|
+
/**
|
|
20
|
+
*@property {string} name
|
|
21
|
+
*@description Name of the controller. If specified it'll used as swagger tags
|
|
22
|
+
*@default Contorller class name
|
|
23
|
+
* */
|
|
24
|
+
name?: string;
|
|
25
|
+
path?: string;
|
|
26
|
+
version?: string;
|
|
27
|
+
since?: string;
|
|
28
|
+
meta?: any;
|
|
29
|
+
};
|
|
30
|
+
export declare function createControllerDecorator(type?: "api" | "web"): (pathOrOptions?: string | ControllerOptions, maybeOptions?: ControllerOptions) => ClassDecorator;
|
|
31
|
+
/**
|
|
32
|
+
*@description Api controller's are used for rest . It will populate
|
|
33
|
+
* json on return and all it http methods {get} {post} etc must return
|
|
34
|
+
*Results.*
|
|
35
|
+
* @param path {string} this will used as route prefix
|
|
36
|
+
*
|
|
37
|
+
**/
|
|
38
|
+
export declare function ApiController(path?: string): ClassDecorator;
|
|
39
|
+
/**
|
|
40
|
+
*@description Api controller's are used for rest . It will populate
|
|
41
|
+
* json on return and all it http methods {get} {post} etc must return
|
|
42
|
+
* Results.*
|
|
43
|
+
* @param {ControllerOptions} options this will used as route prefix
|
|
44
|
+
*
|
|
45
|
+
**/
|
|
46
|
+
export declare function ApiController(options: ControllerOptions): ClassDecorator;
|
|
47
|
+
export declare function ApiController(path: string, options?: ControllerOptions): ClassDecorator;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createControllerDecorator = createControllerDecorator;
|
|
4
|
+
exports.ApiController = ApiController;
|
|
5
|
+
const typedi_1 = require("typedi");
|
|
6
|
+
const container_1 = require("./container");
|
|
7
|
+
function createControllerDecorator(type = "web") {
|
|
8
|
+
return function (pathOrOptions, maybeOptions) {
|
|
9
|
+
return function (target) {
|
|
10
|
+
let path = "/";
|
|
11
|
+
let options = {};
|
|
12
|
+
if (typeof pathOrOptions === "string") {
|
|
13
|
+
path = pathOrOptions;
|
|
14
|
+
options = maybeOptions || {};
|
|
15
|
+
}
|
|
16
|
+
else if (typeof pathOrOptions === "object") {
|
|
17
|
+
options = pathOrOptions;
|
|
18
|
+
path = options.path || "/";
|
|
19
|
+
}
|
|
20
|
+
Reflect.defineMetadata(container_1.API_CONTROLLER_METADATA_KEY, true, target);
|
|
21
|
+
// Ensure Service is applied as a ClassDecorator
|
|
22
|
+
if (typeof typedi_1.Service === "function") {
|
|
23
|
+
(0, container_1.registerController)(target); // Add to custom registry
|
|
24
|
+
(0, typedi_1.Service)()(target); // Apply DI decorator
|
|
25
|
+
Reflect.defineMetadata(container_1.CONTROLLER_META_KEY, { type, path, options }, target);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error("Service decorator is not a function");
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function ApiController(pathOrOptions = "/", mayBeOptions) {
|
|
34
|
+
if (mayBeOptions) {
|
|
35
|
+
return createControllerDecorator("api")(pathOrOptions, mayBeOptions);
|
|
36
|
+
}
|
|
37
|
+
return createControllerDecorator("api")(pathOrOptions);
|
|
38
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Service as _service } from "typedi";
|
|
2
|
+
export declare const AppService: typeof _service;
|
|
3
|
+
export * from "./controller";
|
|
4
|
+
export * from "./route-methods";
|
|
5
|
+
export * from "./openapi";
|
|
6
|
+
export declare const Utility: typeof _service;
|
|
7
|
+
export declare const Helper: typeof _service;
|
|
8
|
+
export * from "./params";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Helper = exports.Utility = exports.AppService = void 0;
|
|
18
|
+
const typedi_1 = require("typedi");
|
|
19
|
+
exports.AppService = typedi_1.Service;
|
|
20
|
+
__exportStar(require("./controller"), exports);
|
|
21
|
+
__exportStar(require("./route-methods"), exports);
|
|
22
|
+
__exportStar(require("./openapi"), exports);
|
|
23
|
+
exports.Utility = typedi_1.Service;
|
|
24
|
+
exports.Helper = typedi_1.Service;
|
|
25
|
+
__exportStar(require("./params"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class BaseHttpException extends Error {
|
|
2
|
+
code: number;
|
|
3
|
+
name: string;
|
|
4
|
+
constructor(message: any);
|
|
5
|
+
isCustomException(): boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare class BadRequestException extends BaseHttpException {
|
|
8
|
+
name: string;
|
|
9
|
+
code: number;
|
|
10
|
+
constructor(message: any);
|
|
11
|
+
}
|
|
12
|
+
export declare class InternalErrorException extends BaseHttpException {
|
|
13
|
+
name: string;
|
|
14
|
+
code: number;
|
|
15
|
+
constructor(message?: any);
|
|
16
|
+
}
|
|
17
|
+
export declare class NotFoundException extends BaseHttpException {
|
|
18
|
+
name: string;
|
|
19
|
+
code: number;
|
|
20
|
+
constructor(message: any);
|
|
21
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundException = exports.InternalErrorException = exports.BadRequestException = exports.BaseHttpException = void 0;
|
|
4
|
+
class BaseHttpException extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = 500;
|
|
8
|
+
this.name = "HttpException";
|
|
9
|
+
}
|
|
10
|
+
isCustomException() {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.BaseHttpException = BaseHttpException;
|
|
15
|
+
class BadRequestException extends BaseHttpException {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "BadRequest";
|
|
19
|
+
this.code = 400;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.BadRequestException = BadRequestException;
|
|
23
|
+
class InternalErrorException extends BaseHttpException {
|
|
24
|
+
constructor(message = "Something going wrong") {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "InternalError";
|
|
27
|
+
this.code = 500;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.InternalErrorException = InternalErrorException;
|
|
31
|
+
class NotFoundException extends BaseHttpException {
|
|
32
|
+
constructor(message) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "NotFound";
|
|
35
|
+
this.code = 404;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.NotFoundException = NotFoundException;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './http-exceptions';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./http-exceptions"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface IRouteDuplicateErr {
|
|
2
|
+
path: string;
|
|
3
|
+
mpath: string;
|
|
4
|
+
method: string;
|
|
5
|
+
controller: string;
|
|
6
|
+
inverseController?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class SystemUseError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class DuplicateRouteException extends Error {
|
|
12
|
+
constructor(params: IRouteDuplicateErr);
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DuplicateRouteException = exports.SystemUseError = void 0;
|
|
4
|
+
class SystemUseError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.SystemUseError = SystemUseError;
|
|
10
|
+
class DuplicateRouteException extends Error {
|
|
11
|
+
constructor(params) {
|
|
12
|
+
let sameController = params.controller == params.inverseController;
|
|
13
|
+
let message = `Duplicate route found for method ${params.method.toUpperCase()}:${params.path == "" ? "'/'" : params.path} `;
|
|
14
|
+
message += sameController ? `in ${params.controller}` : `both in ${params.controller} and ${params.inverseController}`;
|
|
15
|
+
super(message);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.DuplicateRouteException = DuplicateRouteException;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare function inject<T>(cls: new (...args: any[]) => T): T;
|
|
2
|
+
export type Constructor<T = any> = new (...args: any[]) => T;
|
|
3
|
+
export declare function formatUrl(path: string): string;
|
|
4
|
+
export declare function parsedPath(ipath: string): string;
|
|
5
|
+
export declare const isClassValidator: (target: Constructor) => boolean;
|
|
6
|
+
export interface MatchLocation {
|
|
7
|
+
line: number;
|
|
8
|
+
column: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const getLineNumber: (filePath: string, rpath: string | RegExp) => MatchLocation[] | null;
|
|
11
|
+
export declare function normalizePath(base?: string, subPath?: string): string;
|
|
12
|
+
export declare function extrctParamFromUrl(url: string): {
|
|
13
|
+
key: string;
|
|
14
|
+
required: boolean;
|
|
15
|
+
}[];
|
|
16
|
+
export declare function findDuplicates(arr: string[]): string[];
|
|
17
|
+
export declare function getDataType(expectedType: any): any;
|
|
18
|
+
export declare function isValidType(value: any, expectedType: any): boolean;
|
|
19
|
+
export declare function isValidJsonString(value: string): object | boolean;
|
|
20
|
+
export declare function jsonToJs(value: string): any;
|
|
21
|
+
export declare function jsonToInstance(value: string, instance: Constructor): any;
|
|
22
|
+
export declare function transformObjectByInstanceToObject(instance: Constructor, value: object): Record<string, any>;
|
|
23
|
+
export declare const isClassValidatorClass: (target: Constructor) => boolean;
|
|
24
|
+
export declare function validateObjectByInstance(target: Constructor, value: object): Promise<any>;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
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 = void 0;
|
|
7
|
+
exports.inject = inject;
|
|
8
|
+
exports.formatUrl = formatUrl;
|
|
9
|
+
exports.parsedPath = parsedPath;
|
|
10
|
+
exports.normalizePath = normalizePath;
|
|
11
|
+
exports.extrctParamFromUrl = extrctParamFromUrl;
|
|
12
|
+
exports.findDuplicates = findDuplicates;
|
|
13
|
+
exports.getDataType = getDataType;
|
|
14
|
+
exports.isValidType = isValidType;
|
|
15
|
+
exports.isValidJsonString = isValidJsonString;
|
|
16
|
+
exports.jsonToJs = jsonToJs;
|
|
17
|
+
exports.jsonToInstance = jsonToInstance;
|
|
18
|
+
exports.transformObjectByInstanceToObject = transformObjectByInstanceToObject;
|
|
19
|
+
exports.validateObjectByInstance = validateObjectByInstance;
|
|
20
|
+
const class_transformer_1 = require("class-transformer");
|
|
21
|
+
const exceptions_1 = require("./exceptions");
|
|
22
|
+
const fs_1 = __importDefault(require("fs"));
|
|
23
|
+
const container_1 = __importDefault(require("./container"));
|
|
24
|
+
function inject(cls) {
|
|
25
|
+
return container_1.default.get(cls);
|
|
26
|
+
}
|
|
27
|
+
function formatUrl(path) {
|
|
28
|
+
if (typeof path !== 'string') {
|
|
29
|
+
throw new Error('The path must be a string');
|
|
30
|
+
}
|
|
31
|
+
path = path.trim();
|
|
32
|
+
if (!path.startsWith('/')) {
|
|
33
|
+
path = '/' + path;
|
|
34
|
+
}
|
|
35
|
+
path = path.replace(/\/\/+/g, '/');
|
|
36
|
+
if (path.endsWith('/')) {
|
|
37
|
+
path = path.slice(0, -1);
|
|
38
|
+
}
|
|
39
|
+
return path;
|
|
40
|
+
}
|
|
41
|
+
function parsedPath(ipath) {
|
|
42
|
+
return !ipath.startsWith('/') ? '/' + ipath : ipath;
|
|
43
|
+
}
|
|
44
|
+
const isClassValidator = (target) => {
|
|
45
|
+
try {
|
|
46
|
+
const clsval = require("class-validator");
|
|
47
|
+
const result = clsval.getMetadataStorage().getTargetValidationMetadatas(target, undefined, false, false);
|
|
48
|
+
return result.length > 0;
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
console.log(err);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
exports.isClassValidator = isClassValidator;
|
|
56
|
+
const getLineNumber = (filePath, rpath) => {
|
|
57
|
+
var _a;
|
|
58
|
+
let numbers = [];
|
|
59
|
+
try {
|
|
60
|
+
const fileContent = fs_1.default.readFileSync(filePath, 'utf8'); // Read file content
|
|
61
|
+
const lines = fileContent.split('\n'); // Split content into line
|
|
62
|
+
for (let i = 0; i < lines.length; i++) {
|
|
63
|
+
const match = lines[i].match(rpath);
|
|
64
|
+
if (match) {
|
|
65
|
+
console.log(match);
|
|
66
|
+
numbers.push({
|
|
67
|
+
line: i + 1, // 1-based line number
|
|
68
|
+
column: (_a = match.index) !== null && _a !== void 0 ? _a : 0 // 0-based column index
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return numbers; // Class not found
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
//console.error(`Error reading file: ${error.message}`);
|
|
76
|
+
return numbers;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
exports.getLineNumber = getLineNumber;
|
|
80
|
+
function normalizePath(base = "/", subPath = "/") {
|
|
81
|
+
return `/${base}/${subPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
82
|
+
}
|
|
83
|
+
function extrctParamFromUrl(url) {
|
|
84
|
+
const splitPart = url.split("/").filter(x => x.startsWith(':') || x.startsWith("?:"));
|
|
85
|
+
return splitPart.map(f => ({
|
|
86
|
+
key: f.replace(/(\?|:)/g, ""),
|
|
87
|
+
required: !f.startsWith("?:")
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
function findDuplicates(arr) {
|
|
91
|
+
const seen = new Set();
|
|
92
|
+
const duplicates = new Set();
|
|
93
|
+
for (const str of arr) {
|
|
94
|
+
if (seen.has(str)) {
|
|
95
|
+
duplicates.add(str);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
seen.add(str);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Array.from(duplicates); // Convert duplicates to an array if needed
|
|
102
|
+
}
|
|
103
|
+
function getDataType(expectedType) {
|
|
104
|
+
switch (expectedType.name) {
|
|
105
|
+
case "Object":
|
|
106
|
+
if (Array.isArray(expectedType)) {
|
|
107
|
+
return 'array';
|
|
108
|
+
}
|
|
109
|
+
return "object";
|
|
110
|
+
case "String":
|
|
111
|
+
return "string";
|
|
112
|
+
case "Number":
|
|
113
|
+
return "number";
|
|
114
|
+
case "Boolean":
|
|
115
|
+
return "boolean";
|
|
116
|
+
default:
|
|
117
|
+
return expectedType;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function isValidType(value, expectedType) {
|
|
121
|
+
if (value === undefined || value === null)
|
|
122
|
+
return true;
|
|
123
|
+
switch (expectedType.name) {
|
|
124
|
+
case "String":
|
|
125
|
+
return typeof value === "string";
|
|
126
|
+
case "Number":
|
|
127
|
+
return typeof value === "number" || !isNaN(Number(value));
|
|
128
|
+
case "Boolean":
|
|
129
|
+
return typeof value === "boolean";
|
|
130
|
+
default:
|
|
131
|
+
return value instanceof expectedType;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function isValidJsonString(value) {
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(value);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function jsonToJs(value) {
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(value);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function jsonToInstance(value, instance) {
|
|
151
|
+
try {
|
|
152
|
+
const parsedValue = JSON.parse(value);
|
|
153
|
+
return (0, class_transformer_1.plainToInstance)(instance, parsedValue);
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function transformObjectByInstanceToObject(instance, value) {
|
|
160
|
+
return (0, class_transformer_1.instanceToPlain)((0, class_transformer_1.plainToInstance)(instance, value), { excludeExtraneousValues: true, exposeUnsetFields: true });
|
|
161
|
+
}
|
|
162
|
+
const isClassValidatorClass = (target) => {
|
|
163
|
+
try {
|
|
164
|
+
const clsval = require("class-validator");
|
|
165
|
+
const result = clsval.getMetadataStorage().getTargetValidationMetadatas(target, undefined, false, false);
|
|
166
|
+
return result.length > 0;
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
exports.isClassValidatorClass = isClassValidatorClass;
|
|
173
|
+
async function validateObjectByInstance(target, value) {
|
|
174
|
+
try {
|
|
175
|
+
const { validateOrReject } = require("class-validator");
|
|
176
|
+
const { plainToInstance } = require("class-transformer");
|
|
177
|
+
await validateOrReject(plainToInstance(target, value));
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (typeof error == 'object' && Array.isArray(error)) {
|
|
181
|
+
const errors = error.reduce((acc, x) => {
|
|
182
|
+
//acc[x.property] = Object.values(x.constraints);
|
|
183
|
+
acc[x.property] = x.constraints;
|
|
184
|
+
return acc;
|
|
185
|
+
}, {});
|
|
186
|
+
return errors;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
throw new exceptions_1.InternalErrorException("Can't validate object");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
package/dist/icore.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
+
export interface IRequest extends FastifyRequest {
|
|
3
|
+
params: any;
|
|
4
|
+
query: any;
|
|
5
|
+
body: any;
|
|
6
|
+
headers: any;
|
|
7
|
+
}
|
|
8
|
+
export interface IResponse extends FastifyReply {
|
|
9
|
+
}
|
|
10
|
+
export interface ParamMetaOptions {
|
|
11
|
+
index: number;
|
|
12
|
+
key: string;
|
|
13
|
+
name: string;
|
|
14
|
+
required: boolean;
|
|
15
|
+
validate: boolean;
|
|
16
|
+
dataType: any;
|
|
17
|
+
validatorClass: boolean;
|
|
18
|
+
type: "route:param" | "route:query" | "route:body" | "route:header";
|
|
19
|
+
}
|
|
20
|
+
export interface MethodParamMeta {
|
|
21
|
+
params: ParamMetaOptions[];
|
|
22
|
+
query: ParamMetaOptions[];
|
|
23
|
+
body: ParamMetaOptions[];
|
|
24
|
+
headers: ParamMetaOptions[];
|
|
25
|
+
}
|
|
26
|
+
declare class _InternalApplication {
|
|
27
|
+
private static instance;
|
|
28
|
+
private static buildOptions;
|
|
29
|
+
private app;
|
|
30
|
+
private routeSet;
|
|
31
|
+
private alreadyRun;
|
|
32
|
+
private routes;
|
|
33
|
+
private constructor();
|
|
34
|
+
static getInternalApp(buildOptions: any): _InternalApplication;
|
|
35
|
+
private buildController;
|
|
36
|
+
private _mapArgs;
|
|
37
|
+
private _processMeta;
|
|
38
|
+
mapControllers(): Promise<void>;
|
|
39
|
+
mapGroup(path: string): Promise<void>;
|
|
40
|
+
handleRoute(args: any): Promise<void>;
|
|
41
|
+
mapGet(path: string | undefined, fn: Function): Promise<void>;
|
|
42
|
+
mapPost(): Promise<void>;
|
|
43
|
+
mapPut(): Promise<void>;
|
|
44
|
+
mapDelete(): Promise<void>;
|
|
45
|
+
private _mapControllers;
|
|
46
|
+
run(port?: number): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
export declare class AppBuilder {
|
|
49
|
+
private static instance;
|
|
50
|
+
private alreadyBuilt;
|
|
51
|
+
private databse;
|
|
52
|
+
private constructor();
|
|
53
|
+
static createBuilder(): AppBuilder;
|
|
54
|
+
registerPlugin<T extends Function, S extends {}>(plugin: T, options: S): Promise<void>;
|
|
55
|
+
useDatabase(): Promise<void>;
|
|
56
|
+
build(): Promise<_InternalApplication>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|