@elsikora/nestjs-crud-automator 1.1.1 → 1.1.2
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/dist/class/api/base-service.class.d.ts +7 -4
- package/dist/class/api/base-service.class.js +11 -2
- package/dist/class/api/base-service.class.js.map +1 -1
- package/dist/decorator/api/controller.decorator.d.ts +10 -0
- package/dist/decorator/api/controller.decorator.js +30 -0
- package/dist/decorator/api/controller.decorator.js.map +1 -0
- package/dist/decorator/api/function/create.decorator.d.ts +4 -0
- package/dist/decorator/api/function/create.decorator.js +45 -0
- package/dist/decorator/api/function/create.decorator.js.map +1 -0
- package/dist/decorator/api/function/decorator.js +19 -1
- package/dist/decorator/api/function/decorator.js.map +1 -1
- package/dist/decorator/api/function/delete.decorator.d.ts +4 -0
- package/dist/decorator/api/function/delete.decorator.js +50 -0
- package/dist/decorator/api/function/delete.decorator.js.map +1 -0
- package/dist/decorator/api/function/get-list.decorator.d.ts +1 -2
- package/dist/decorator/api/function/get-list.decorator.js +2 -2
- package/dist/decorator/api/function/get-list.decorator.js.map +1 -1
- package/dist/decorator/api/function/get.decorator.js +2 -4
- package/dist/decorator/api/function/get.decorator.js.map +1 -1
- package/dist/decorator/api/function/update.decorator.d.ts +4 -0
- package/dist/decorator/api/function/update.decorator.js +60 -0
- package/dist/decorator/api/function/update.decorator.js.map +1 -0
- package/dist/decorator/api/index.d.ts +2 -0
- package/dist/decorator/api/index.js +2 -0
- package/dist/decorator/api/index.js.map +1 -1
- package/dist/decorator/api/method.decorator.js +0 -50
- package/dist/decorator/api/method.decorator.js.map +1 -1
- package/dist/decorator/api/route/decorator.d.ts +18 -0
- package/dist/decorator/api/route/decorator.js +44 -0
- package/dist/decorator/api/route/decorator.js.map +1 -0
- package/dist/decorator/api/route/get.decorator.d.ts +9 -0
- package/dist/decorator/api/route/get.decorator.js +52 -0
- package/dist/decorator/api/route/get.decorator.js.map +1 -0
- package/dist/decorator/api/route/index.d.ts +2 -0
- package/dist/decorator/api/route/index.js +19 -0
- package/dist/decorator/api/route/index.js.map +1 -0
- package/dist/decorator/api/service.decorator.js +82 -1
- package/dist/decorator/api/service.decorator.js.map +1 -1
- package/dist/enum/decorator/api/controller/index.d.ts +1 -0
- package/dist/enum/decorator/api/controller/index.js +3 -0
- package/dist/enum/decorator/api/controller/index.js.map +1 -0
- package/dist/enum/decorator/api/controller/type.enum.d.ts +10 -0
- package/dist/enum/decorator/api/controller/type.enum.js +15 -0
- package/dist/enum/decorator/api/controller/type.enum.js.map +1 -0
- package/dist/enum/decorator/api/index.d.ts +1 -0
- package/dist/enum/decorator/api/index.js +1 -0
- package/dist/enum/decorator/api/index.js.map +1 -1
- package/dist/factory/api/controller.factory.d.ts +1 -0
- package/dist/factory/api/controller.factory.js +3 -0
- package/dist/factory/api/controller.factory.js.map +1 -0
- package/dist/factory/api/index.d.ts +1 -0
- package/dist/factory/api/index.js +18 -0
- package/dist/factory/api/index.js.map +1 -0
- package/dist/factory/index.d.ts +1 -0
- package/dist/factory/index.js +18 -0
- package/dist/factory/index.js.map +1 -0
- package/dist/helper/index.d.ts +1 -0
- package/dist/helper/index.js +18 -0
- package/dist/helper/index.js.map +1 -0
- package/dist/helper/reflector.helper.d.ts +20 -0
- package/dist/helper/reflector.helper.js +77 -0
- package/dist/helper/reflector.helper.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interface/decorator/api/base-controller.interface.d.ts +7 -0
- package/dist/interface/decorator/api/base-controller.interface.js +3 -0
- package/dist/interface/decorator/api/base-controller.interface.js.map +1 -0
- package/dist/interface/decorator/api/controller-properties.interface.d.ts +25 -0
- package/dist/interface/decorator/api/controller-properties.interface.js +4 -0
- package/dist/interface/decorator/api/controller-properties.interface.js.map +1 -0
- package/dist/interface/decorator/api/index.d.ts +1 -0
- package/dist/interface/decorator/api/index.js +1 -0
- package/dist/interface/decorator/api/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/type/api/function/create-properties.type.d.ts +2 -0
- package/dist/type/api/function/create-properties.type.js +3 -0
- package/dist/type/api/function/create-properties.type.js.map +1 -0
- package/dist/type/api/function/index.d.ts +3 -0
- package/dist/type/api/function/index.js +3 -0
- package/dist/type/api/function/index.js.map +1 -1
- package/dist/type/api/function/update-properties.type.d.ts +2 -0
- package/dist/type/api/function/update-properties.type.js +3 -0
- package/dist/type/api/function/update-properties.type.js.map +1 -0
- package/dist/utility/get-entity-columns.utility.d.ts +2 -0
- package/dist/utility/get-entity-columns.utility.js +11 -0
- package/dist/utility/get-entity-columns.utility.js.map +1 -0
- package/dist/utility/index.d.ts +1 -1
- package/dist/utility/index.js +1 -1
- package/dist/utility/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/utility/create-rest-application.utility.d.ts +0 -3
- package/dist/utility/create-rest-application.utility.js +0 -9
- package/dist/utility/create-rest-application.utility.js.map +0 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { IApiGetListResponseResult } from "../../interface";
|
|
2
|
-
import { BaseEntity } from "typeorm";
|
|
3
|
-
import { TApiFunctionGetListProperties } from "../../type";
|
|
2
|
+
import { BaseEntity, type FindOptionsRelations } from "typeorm";
|
|
3
|
+
import { TApiFunctionCreateProperties, TApiFunctionGetListProperties, TApiFunctionUpdateProperties } from "../../type";
|
|
4
4
|
import { TApiFunctionGetProperties } from "../../type/api/function/get-properties.type";
|
|
5
5
|
export declare class BaseApiService<E extends BaseEntity> {
|
|
6
|
-
getList(_properties: TApiFunctionGetListProperties<E>): Promise<IApiGetListResponseResult<E>>;
|
|
7
|
-
get(_id: string, _properties?: TApiFunctionGetProperties<E>): Promise<E>;
|
|
6
|
+
getList(_properties: TApiFunctionGetListProperties<E>, _relations?: FindOptionsRelations<E>): Promise<IApiGetListResponseResult<E>>;
|
|
7
|
+
get(_id: string, _properties?: TApiFunctionGetProperties<E>, _relations?: FindOptionsRelations<E>): Promise<E>;
|
|
8
|
+
create(_properties: TApiFunctionCreateProperties<E>): Promise<E>;
|
|
9
|
+
update(_id: string, _properties: TApiFunctionUpdateProperties<E>): Promise<E>;
|
|
10
|
+
delete(_id: string): Promise<void>;
|
|
8
11
|
}
|
|
@@ -2,10 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseApiService = void 0;
|
|
4
4
|
class BaseApiService {
|
|
5
|
-
getList(_properties) {
|
|
5
|
+
getList(_properties, _relations) {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
8
|
-
get(_id, _properties) {
|
|
8
|
+
get(_id, _properties, _relations) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
create(_properties) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
update(_id, _properties) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
delete(_id) {
|
|
9
18
|
return null;
|
|
10
19
|
}
|
|
11
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-service.class.js","sourceRoot":"/","sources":["class/api/base-service.class.ts"],"names":[],"mappings":";;;AAKA,MAAa,cAAc;IACvB,OAAO,CAAC,WAA6C;
|
|
1
|
+
{"version":3,"file":"base-service.class.js","sourceRoot":"/","sources":["class/api/base-service.class.ts"],"names":[],"mappings":";;;AAKA,MAAa,cAAc;IACvB,OAAO,CAAC,WAA6C,EAAE,UAAoC;QACvF,OAAO,IAAW,CAAC;IACvB,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,WAA0C,EAAE,UAAoC;QAC7F,OAAO,IAAW,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,WAA4C;QAC/C,OAAO,IAAW,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,GAAW,EAAE,WAA4C;QAC5D,OAAO,IAAW,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,GAAW;QACd,OAAO,IAAW,CAAC;IACvB,CAAC;CACJ;AApBD,wCAoBC","sourcesContent":["import { IApiGetListResponseResult } from \"../../interface\";\nimport {BaseEntity, type FindOptionsRelations} from \"typeorm\";\nimport {TApiFunctionCreateProperties, TApiFunctionGetListProperties, TApiFunctionUpdateProperties} from \"../../type\";\nimport {TApiFunctionGetProperties} from \"../../type/api/function/get-properties.type\";\n\nexport class BaseApiService<E extends BaseEntity> {\n getList(_properties: TApiFunctionGetListProperties<E>, _relations?: FindOptionsRelations<E>): Promise<IApiGetListResponseResult<E>> {\n return null as any;\n }\n\n get(_id: string, _properties?: TApiFunctionGetProperties<E>, _relations?: FindOptionsRelations<E>): Promise<E> {\n return null as any;\n }\n\n create(_properties: TApiFunctionCreateProperties<E>): Promise<E> {\n return null as any;\n }\n\n update(_id: string, _properties: TApiFunctionUpdateProperties<E>): Promise<E> {\n return null as any;\n }\n\n delete(_id: string): Promise<void> {\n return null as any;\n }\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseEntity } from "typeorm";
|
|
2
|
+
interface ApiControllerOptions<E extends BaseEntity> {
|
|
3
|
+
entity: new () => E;
|
|
4
|
+
dto: {
|
|
5
|
+
request: any;
|
|
6
|
+
response: any;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export declare function ApiController<E extends BaseEntity>(options: ApiControllerOptions<E>): (target: any) => void;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiController = ApiController;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
6
|
+
const method_decorator_1 = require("./method.decorator");
|
|
7
|
+
const enum_1 = require("../../enum");
|
|
8
|
+
const constants_1 = require("@nestjs/common/constants");
|
|
9
|
+
function ApiController(options) {
|
|
10
|
+
const { entity } = options;
|
|
11
|
+
return (target) => {
|
|
12
|
+
(0, common_1.applyDecorators)((0, swagger_1.ApiTags)(entity.name), (0, common_1.Controller)(entity.name.toLowerCase()))(target);
|
|
13
|
+
target.prototype.get = function (parameters, body) {
|
|
14
|
+
return this.service.get(parameters[entity.name.toLowerCase()], body);
|
|
15
|
+
};
|
|
16
|
+
(0, common_1.applyDecorators)((0, method_decorator_1.ApiMethod)({
|
|
17
|
+
action: enum_1.EApiAction.FETCH,
|
|
18
|
+
entity: options.entity,
|
|
19
|
+
httpCode: common_1.HttpStatus.OK,
|
|
20
|
+
method: common_1.RequestMethod.GET,
|
|
21
|
+
path: `:bank`,
|
|
22
|
+
responses: { internalServerError: true, notFound: true, unauthorized: true },
|
|
23
|
+
responseType: options.dto.response
|
|
24
|
+
}))(target.prototype, 'get', Object.getOwnPropertyDescriptor(target.prototype, 'get'));
|
|
25
|
+
(0, common_1.Param)()(target.prototype, 'get', 0);
|
|
26
|
+
Reflect.defineMetadata(constants_1.PARAMTYPES_METADATA, [options.dto.request], target.prototype, 'get');
|
|
27
|
+
Reflect.defineMetadata(constants_1.PATH_METADATA, ":bank", target.prototype, 'get');
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=controller.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.decorator.js","sourceRoot":"/","sources":["decorator/api/controller.decorator.ts"],"names":[],"mappings":";;AAeA,sCAiCC;AAhDD,2CAA6F;AAE7F,6CAAwC;AACxC,yDAA6C;AAC7C,qCAAsC;AACtC,wDAA4E;AAU5E,SAAgB,aAAa,CAAuB,OAAgC;IAChF,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,OAAO,CAAC,MAAW,EAAE,EAAE;QACnB,IAAA,wBAAe,EACX,IAAA,iBAAO,EAAC,MAAM,CAAC,IAAI,CAAC,EACpB,IAAA,mBAAU,EAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CACxC,CAAC,MAAM,CAAC,CAAC;QAEV,MAAM,CAAC,SAAS,CAAC,GAAG,GAAG,UACnB,UAAsC,EACtC,IAAgC;YAEhC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QACzE,CAAC,CAAC;QAEF,IAAA,wBAAe,EACX,IAAA,4BAAS,EAAC;YACN,MAAM,EAAE,iBAAU,CAAC,KAAK;YACxB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,QAAQ,EAAE,mBAAU,CAAC,EAAE;YACvB,MAAM,EAAE,sBAAa,CAAC,GAAG;YACzB,IAAI,EAAE,OAAO;YACb,SAAS,EAAE,EAAE,mBAAmB,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE;YAC5E,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ;SACrC,CAAC,CACL,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,wBAAwB,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;QAIrF,IAAA,cAAK,GAAE,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QACpC,OAAO,CAAC,cAAc,CAAC,+BAAmB,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;QAC5F,OAAO,CAAC,cAAc,CAAC,yBAAa,EAAE,OAAO,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IAC5E,CAAC,CAAC;AACN,CAAC","sourcesContent":["import {applyDecorators, Controller, HttpStatus, Param, RequestMethod} from \"@nestjs/common\";\nimport {BaseEntity} from \"typeorm\";\nimport {ApiTags} from \"@nestjs/swagger\";\nimport {ApiMethod} from \"./method.decorator\";\nimport {EApiAction} from \"../../enum\";\nimport {PARAMTYPES_METADATA, PATH_METADATA} from \"@nestjs/common/constants\";\n\ninterface ApiControllerOptions<E extends BaseEntity> {\n entity: new () => E;\n dto: {\n request: any;\n response: any;\n };\n}\n\nexport function ApiController<E extends BaseEntity>(options: ApiControllerOptions<E>) {\n const { entity } = options;\n return (target: any) => {\n applyDecorators(\n ApiTags(entity.name),\n Controller(entity.name.toLowerCase()),\n )(target);\n\n target.prototype.get = function (\n parameters: typeof options.dto.request,\n body: typeof options.dto.request\n ): Promise<E> {\n return this.service.get(parameters[entity.name.toLowerCase()], body);\n };\n\n applyDecorators(\n ApiMethod({\n action: EApiAction.FETCH,\n entity: options.entity,\n httpCode: HttpStatus.OK,\n method: RequestMethod.GET,\n path: `:bank`,\n responses: { internalServerError: true, notFound: true, unauthorized: true },\n responseType: options.dto.response\n }),\n )(target.prototype, 'get', Object.getOwnPropertyDescriptor(target.prototype, 'get'));\n\n\n\n Param()(target.prototype, 'get', 0);\n Reflect.defineMetadata(PARAMTYPES_METADATA, [options.dto.request], target.prototype, 'get');\n Reflect.defineMetadata(PATH_METADATA, \":bank\", target.prototype, 'get');\n };\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiFunctionCreate = ApiFunctionCreate;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const enum_1 = require("../../../enum");
|
|
6
|
+
const utility_1 = require("../../../utility");
|
|
7
|
+
const get_entity_columns_utility_1 = require("../../../utility/get-entity-columns.utility");
|
|
8
|
+
async function executor(repository, entityType, properties) {
|
|
9
|
+
try {
|
|
10
|
+
const allColumns = (0, get_entity_columns_utility_1.GetEntityColumns)(entityType);
|
|
11
|
+
const autoGeneratedColumns = (0, get_entity_columns_utility_1.GetEntityColumns)(entityType, true);
|
|
12
|
+
const filteredProperties = Object.keys(properties).reduce((acc, key) => {
|
|
13
|
+
if (allColumns.includes(key) && !autoGeneratedColumns.includes(key)) {
|
|
14
|
+
acc[key] = properties[key];
|
|
15
|
+
}
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
const newEntity = repository.create(filteredProperties);
|
|
19
|
+
const savedEntity = await repository.save(newEntity);
|
|
20
|
+
return savedEntity;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.log(error);
|
|
24
|
+
if (error instanceof common_1.HttpException) {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
throw new common_1.InternalServerErrorException((0, utility_1.ErrorString)({
|
|
28
|
+
entity: entityType,
|
|
29
|
+
type: enum_1.EErrorStringAction.CREATING_ERROR,
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function ApiFunctionCreate(options) {
|
|
34
|
+
return function (_target, _propertyKey, descriptor) {
|
|
35
|
+
descriptor.value = async function (properties) {
|
|
36
|
+
const repository = this.repository;
|
|
37
|
+
if (!repository) {
|
|
38
|
+
throw new Error("Repository is not available in this context");
|
|
39
|
+
}
|
|
40
|
+
return executor(repository, options.model, properties);
|
|
41
|
+
};
|
|
42
|
+
return descriptor;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=create.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.decorator.js","sourceRoot":"/","sources":["decorator/api/function/create.decorator.ts"],"names":[],"mappings":";;AAyCA,8CAkBC;AAzDD,2CAA2E;AAC3E,wCAAiD;AACjD,8CAA6C;AAC7C,4FAA6E;AAG7E,KAAK,UAAU,QAAQ,CACnB,UAAyB,EACzB,UAAuB,EACvB,UAA2C;IAE3C,IAAI,CAAC;QACD,MAAM,UAAU,GAAG,IAAA,6CAAgB,EAAC,UAAU,CAAC,CAAC;QAChD,MAAM,oBAAoB,GAAG,IAAA,6CAAgB,EAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAEhE,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI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CAAC,CAAC;AACN,CAAC","sourcesContent":["import {BaseEntity, DeepPartial, Repository} from \"typeorm\";\nimport {TApiFunctionCreateProperties} from \"../../../type\";\nimport {HttpException, InternalServerErrorException} from \"@nestjs/common\";\nimport {EErrorStringAction} from \"../../../enum\";\nimport {ErrorString} from \"../../../utility\";\nimport {GetEntityColumns} from \"../../../utility/get-entity-columns.utility\";\n\n\nasync function executor<E extends BaseEntity>(\n repository: Repository<E>,\n entityType: new () => E,\n properties: TApiFunctionCreateProperties<E>\n): Promise<E> {\n try {\n const allColumns = GetEntityColumns(entityType);\n const autoGeneratedColumns = GetEntityColumns(entityType, true);\n\n const filteredProperties = Object.keys(properties).reduce((acc, key) => {\n if (allColumns.includes(key as keyof E) && !autoGeneratedColumns.includes(key as keyof E)) {\n (acc as any)[key] = (properties as any)[key];\n }\n return acc;\n }, {} as DeepPartial<E>);\n\n const newEntity = repository.create(filteredProperties);\n const savedEntity = await repository.save(newEntity);\n return savedEntity;\n } catch (error) {\n console.log(error);\n if (error instanceof HttpException) {\n throw error;\n }\n throw new InternalServerErrorException(\n ErrorString({\n entity: entityType,\n type: EErrorStringAction.CREATING_ERROR,\n })\n );\n }\n}\n\nexport function ApiFunctionCreate<E extends BaseEntity>(options: { model: new () => E }) {\n return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n descriptor.value = async function (\n this: {\n repository: Repository<E>;\n },\n properties: TApiFunctionCreateProperties<InstanceType<typeof options.model>>\n ) {\n const repository: Repository<E> = this.repository;\n if (!repository) {\n throw new Error(\"Repository is not available in this context\");\n }\n\n return executor(repository, options.model, properties);\n };\n\n return descriptor;\n };\n}\n"]}
|
|
@@ -4,17 +4,35 @@ exports.ApiFunction = ApiFunction;
|
|
|
4
4
|
const get_list_decorator_1 = require("./get-list.decorator");
|
|
5
5
|
const enum_1 = require("../../../enum");
|
|
6
6
|
const get_decorator_1 = require("./get.decorator");
|
|
7
|
+
const create_decorator_1 = require("./create.decorator");
|
|
8
|
+
const update_decorator_1 = require("./update.decorator");
|
|
9
|
+
const delete_decorator_1 = require("./delete.decorator");
|
|
7
10
|
function ApiFunction(options) {
|
|
8
11
|
return function (_target, _propertyKey, descriptor) {
|
|
9
12
|
const originalMethod = descriptor.value;
|
|
10
13
|
descriptor.value = function (...args) {
|
|
11
14
|
switch (options.type) {
|
|
12
15
|
case enum_1.EApiFunctionType.GET_LIST: {
|
|
13
|
-
return (0, get_list_decorator_1.ApiFunctionGetList)({ model: options.model
|
|
16
|
+
return (0, get_list_decorator_1.ApiFunctionGetList)({ model: options.model })(this, _propertyKey, { value: originalMethod }).value.apply(this, args);
|
|
14
17
|
}
|
|
15
18
|
case enum_1.EApiFunctionType.GET: {
|
|
16
19
|
return (0, get_decorator_1.ApiFunctionGet)({ model: options.model })(this, _propertyKey, { value: originalMethod }).value.apply(this, args);
|
|
17
20
|
}
|
|
21
|
+
case enum_1.EApiFunctionType.CREATE: {
|
|
22
|
+
return (0, create_decorator_1.ApiFunctionCreate)({ model: options.model })(this, _propertyKey, {
|
|
23
|
+
value: originalMethod,
|
|
24
|
+
}).value.apply(this, args);
|
|
25
|
+
}
|
|
26
|
+
case enum_1.EApiFunctionType.UPDATE: {
|
|
27
|
+
return (0, update_decorator_1.ApiFunctionUpdate)({ model: options.model })(this, _propertyKey, {
|
|
28
|
+
value: originalMethod,
|
|
29
|
+
}).value.apply(this, args);
|
|
30
|
+
}
|
|
31
|
+
case enum_1.EApiFunctionType.DELETE: {
|
|
32
|
+
return (0, delete_decorator_1.ApiFunctionDelete)({ model: options.model })(this, _propertyKey, {
|
|
33
|
+
value: originalMethod,
|
|
34
|
+
}).value.apply(this, args);
|
|
35
|
+
}
|
|
18
36
|
default: {
|
|
19
37
|
throw new Error("Unsupported function");
|
|
20
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorator.js","sourceRoot":"/","sources":["decorator/api/function/decorator.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"decorator.js","source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AAM,CAAC,CAAC,CAAC;oBAC9B,OAAO,IAAA,oCAAiB,EAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE;wBACtE,KAAK,EAAE,cAAc;qBACrB,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBAC5B,CAAC;gBAED,KAAK,uBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC9B,OAAO,IAAA,oCAAiB,EAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE;wBACtE,KAAK,EAAE,cAAc;qBACrB,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBAC5B,CAAC;gBAED,OAAO,CAAC,CAAC,CAAC;oBACT,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;gBACzC,CAAC;YACF,CAAC;YAED,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACzC,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACnB,CAAC,CAAC;AACH,CAAC","sourcesContent":["import type {BaseEntity, FindOptionsRelations, Repository} from \"typeorm\";\nimport {ApiFunctionGetList} from \"./get-list.decorator\";\nimport {EApiFunctionType} from \"../../../enum\";\nimport {ApiFunctionGet} from \"./get.decorator\";\nimport {ApiFunctionCreate} from \"./create.decorator\";\nimport {ApiFunctionUpdate} from \"./update.decorator\";\nimport {ApiFunctionDelete} from \"./delete.decorator\";\n\nexport function ApiFunction<E extends BaseEntity>(options: { model: new () => E; type: EApiFunctionType; relations?: FindOptionsRelations<E> }) {\n\treturn function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n\t\tconst originalMethod = descriptor.value;\n\n\t\tdescriptor.value = function (this: { repository: Repository<E> }, ...args: Array<any>) {\n\n\t\t\tswitch (options.type) {\n\t\t\t\tcase EApiFunctionType.GET_LIST: {\n\t\t\t\t\treturn ApiFunctionGetList({ model: options.model })(this, _propertyKey, { value: originalMethod }).value.apply(this, args);\n\t\t\t\t}\n\n\t\t\t\tcase EApiFunctionType.GET: {\n\t\t\t\t\treturn ApiFunctionGet({ model: options.model })(this, _propertyKey, { value: originalMethod }).value.apply(this, args);\n\t\t\t\t}\n\n\t\t\t\tcase EApiFunctionType.CREATE: {\n\t\t\t\t\treturn ApiFunctionCreate({ model: options.model })(this, _propertyKey, {\n\t\t\t\t\t\tvalue: originalMethod,\n\t\t\t\t\t}).value.apply(this, args);\n\t\t\t\t}\n\n\t\t\t\tcase EApiFunctionType.UPDATE: {\n\t\t\t\t\treturn ApiFunctionUpdate({ model: options.model })(this, _propertyKey, {\n\t\t\t\t\t\tvalue: originalMethod,\n\t\t\t\t\t}).value.apply(this, args);\n\t\t\t\t}\n\n\t\t\t\tcase EApiFunctionType.DELETE: {\n\t\t\t\t\treturn ApiFunctionDelete({ model: options.model })(this, _propertyKey, {\n\t\t\t\t\t\tvalue: originalMethod,\n\t\t\t\t\t}).value.apply(this, args);\n\t\t\t\t}\n\n\t\t\t\tdefault: {\n\t\t\t\t\tthrow new Error(\"Unsupported function\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn originalMethod.apply(this, args);\n\t\t};\n\n\t\treturn descriptor;\n\t};\n}\n"]}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiFunctionDelete = ApiFunctionDelete;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const utility_1 = require("../../../utility");
|
|
6
|
+
const enum_1 = require("../../../enum");
|
|
7
|
+
const get_decorator_1 = require("./get.decorator");
|
|
8
|
+
async function executor(repository, entityType, id, getFunction) {
|
|
9
|
+
try {
|
|
10
|
+
const entity = await getFunction(id.toString());
|
|
11
|
+
await repository.remove(entity);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
console.log(error);
|
|
15
|
+
if (error instanceof common_1.HttpException) {
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
throw new common_1.InternalServerErrorException((0, utility_1.ErrorString)({
|
|
19
|
+
entity: entityType,
|
|
20
|
+
type: enum_1.EErrorStringAction.DELETING_ERROR,
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function ApiFunctionDelete(options) {
|
|
25
|
+
return function (_target, _propertyKey, descriptor) {
|
|
26
|
+
const getDecorator = (0, get_decorator_1.ApiFunctionGet)({ model: options.model });
|
|
27
|
+
let getFunction;
|
|
28
|
+
descriptor.value = async function (id) {
|
|
29
|
+
const repository = this.repository;
|
|
30
|
+
if (!repository) {
|
|
31
|
+
throw new Error("Repository is not available in this context");
|
|
32
|
+
}
|
|
33
|
+
if (!getFunction) {
|
|
34
|
+
const getDescriptor = {
|
|
35
|
+
value: function () { return Promise.reject(new Error('Not implemented')); }
|
|
36
|
+
};
|
|
37
|
+
getDecorator(this, 'get', getDescriptor);
|
|
38
|
+
if (getDescriptor.value) {
|
|
39
|
+
getFunction = getDescriptor.value.bind(this);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('Get function is not properly decorated');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return executor(repository, options.model, id, getFunction);
|
|
46
|
+
};
|
|
47
|
+
return descriptor;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=delete.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delete.decorator.js","sourceRoot":"/","sources":["decorator/api/function/delete.decorator.ts"],"names":[],"mappings":";;AA+BA,8CAiCC;AA/DD,2CAA6E;AAC7E,8CAA+C;AAC/C,wCAAmD;AACnD,mDAAiD;AAGjD,KAAK,UAAU,QAAQ,CACnB,UAAyB,EACzB,UAAuB,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AAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACjD,CAAC;qBAAM,CAAC;oBACJ,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;gBAC9D,CAAC;YACL,CAAC;YAED,OAAO,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC;QAChE,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;AACN,CAAC","sourcesContent":["import { BaseEntity, Repository } from \"typeorm\";\nimport { HttpException, InternalServerErrorException } from \"@nestjs/common\";\nimport { ErrorString } from \"../../../utility\";\nimport { EErrorStringAction } from \"../../../enum\";\nimport { ApiFunctionGet } from \"./get.decorator\";\nimport {TApiFunctionGetProperties} from \"../../../type\";\n\nasync function executor<E extends BaseEntity>(\n repository: Repository<E>,\n entityType: new () => E,\n id: string | number,\n getFunction: (id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>\n): Promise<void> {\n try {\n const entity = await getFunction(id.toString());\n\n await repository.remove(entity);\n } catch (error) {\n console.log(error);\n if (error instanceof HttpException) {\n throw error;\n }\n throw new InternalServerErrorException(\n ErrorString({\n entity: entityType,\n type: EErrorStringAction.DELETING_ERROR,\n })\n );\n }\n}\n\nexport function ApiFunctionDelete<E extends BaseEntity>(options: { model: new () => E }) {\n return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n const getDecorator = ApiFunctionGet({ model: options.model });\n let getFunction: (id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>;\n\n descriptor.value = async function (\n this: {\n repository: Repository<E>;\n },\n id: string | number\n ) {\n const repository: Repository<E> = this.repository;\n if (!repository) {\n throw new Error(\"Repository is not available in this context\");\n }\n\n if (!getFunction) {\n const getDescriptor: TypedPropertyDescriptor<(id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>> = {\n value: function() { return Promise.reject(new Error('Not implemented')); }\n };\n getDecorator(this, 'get', getDescriptor);\n if (getDescriptor.value) {\n getFunction = getDescriptor.value.bind(this);\n } else {\n throw new Error('Get function is not properly decorated');\n }\n }\n\n return executor(repository, options.model, id, getFunction);\n };\n\n return descriptor;\n };\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { BaseEntity
|
|
1
|
+
import type { BaseEntity } from "typeorm";
|
|
2
2
|
export declare function ApiFunctionGetList<E extends BaseEntity>(options: {
|
|
3
3
|
model: new () => E;
|
|
4
|
-
relations?: FindOptionsRelations<E>;
|
|
5
4
|
}): (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
@@ -35,13 +35,13 @@ async function executor(repository, entityType, properties, filter) {
|
|
|
35
35
|
}
|
|
36
36
|
function ApiFunctionGetList(options) {
|
|
37
37
|
return function (_target, _propertyKey, descriptor) {
|
|
38
|
-
descriptor.value = async function (properties) {
|
|
38
|
+
descriptor.value = async function (properties, relations) {
|
|
39
39
|
const { limit, orderBy, orderDirection, page, createdAtFrom, createdAtTo, updatedAtFrom, updatedAtTo, receivedAtFrom, receivedAtTo, ...entityProperties } = properties;
|
|
40
40
|
const filter = {
|
|
41
41
|
skip: limit * (page - 1),
|
|
42
42
|
take: limit,
|
|
43
43
|
where: {},
|
|
44
|
-
relations
|
|
44
|
+
relations,
|
|
45
45
|
};
|
|
46
46
|
const typedEntityProperties = entityProperties;
|
|
47
47
|
Object.keys(typedEntityProperties).forEach((key) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-list.decorator.js","sourceRoot":"/","sources":["decorator/api/function/get-list.decorator.ts"],"names":[],"mappings":";;AA0CA,
|
|
1
|
+
{"version":3,"file":"get-list.decorator.js","sourceRoot":"/","sources":["decorator/api/function/get-list.decorator.ts"],"names":[],"mappings":";;AA0CA,gDAsGC;AA/ID,qCAA0E;AAE1E,2CAAgG;AAEhG,8CAA+C;AAC/C,wCAAmD;AACnD,gDAAoE;AAGpE,KAAK,UAAU,QAAQ,CA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YACF,CAAC;YAED,MAAM,UAAU,GAAkB,IAAI,CAAC,UAAU,CAAC;YAClD,IAAI,CAAC,UAAU,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;YAChE,CAAC;YAED,OAAO,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;QAChE,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACnB,CAAC,CAAC;AACH,CAAC","sourcesContent":["import type {BaseEntity, FindManyOptions, FindOptionsRelations, Repository} from \"typeorm\";\nimport { Between, LessThanOrEqual, Like, MoreThanOrEqual } from \"typeorm\";\n\nimport { HttpException, InternalServerErrorException, NotFoundException } from \"@nestjs/common\";\nimport type { IApiGetListResponseResult } from \"../../../interface\";\nimport { ErrorString } from \"../../../utility\";\nimport { EErrorStringAction } from \"../../../enum\";\nimport { API_FUNCTION_DECORATOR_CONSTANT } from \"../../../constant\";\nimport { TApiFunctionGetListProperties } from \"../../../type\";\n\nasync function executor<E extends BaseEntity>(repository: Repository<E>, entityType: new () => E, properties: TApiFunctionGetListProperties<E>, filter: FindManyOptions<E>): Promise<IApiGetListResponseResult<E>> {\n\ttry {\n\t\tconst [items, totalCount] = await repository.findAndCount(filter);\n\n\t\tif (items.length === 0) {\n\t\t\tthrow new NotFoundException(\n\t\t\t\tErrorString({\n\t\t\t\t\tentity: entityType,\n\t\t\t\t\ttype: EErrorStringAction.LIST_PAGE_NOT_FOUND,\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\treturn {\n\t\t\tcount: items.length,\n\t\t\tcurrentPage: properties.page,\n\t\t\titems,\n\t\t\ttotalCount,\n\t\t\ttotalPages: Math.ceil(totalCount / properties.limit),\n\t\t};\n\t} catch (error) {\n\t\tif (error instanceof HttpException) {\n\t\t\tthrow error;\n\t\t}\n\t\tthrow new InternalServerErrorException(\n\t\t\tErrorString({\n\t\t\t\tentity: entityType,\n\t\t\t\ttype: EErrorStringAction.FETCHING_LIST_ERROR,\n\t\t\t}),\n\t\t);\n\t}\n}\nexport function ApiFunctionGetList<E extends BaseEntity>(options: { model: new () => E }) {\n\treturn function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n\t\tdescriptor.value = async function (\n\t\t\tthis: {\n\t\t\t\trepository: Repository<E>;\n\t\t\t},\n\t\t\tproperties: TApiFunctionGetListProperties<InstanceType<typeof options.model>>,\n\t\t\trelations: FindOptionsRelations<E>,\n\t\t) {\n\t\t\tconst { limit, orderBy, orderDirection, page, createdAtFrom, createdAtTo, updatedAtFrom, updatedAtTo, receivedAtFrom, receivedAtTo, ...entityProperties } = properties;\n\n\t\t\tconst filter: FindManyOptions<typeof options.model> = {\n\t\t\t\tskip: limit * (page - 1),\n\t\t\t\ttake: limit,\n\t\t\t\twhere: {},\n\t\t\t\trelations,\n\t\t\t};\n\n\t\t\tconst typedEntityProperties: keyof typeof options.model = entityProperties as Exclude<keyof Omit<E, \"createdAt\" | \"updatedAt\" | \"receivedAt\">, keyof E>;\n\n\t\t\tObject.keys(typedEntityProperties).forEach((key: string) => {\n\t\t\t\tif (typeof typedEntityProperties[key] === \"string\") {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\t[key]: Like(`%${typedEntityProperties[key]}%`),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tif (orderBy) {\n\t\t\t\tfilter.order = {\n\t\t\t\t\t[orderBy as never as string]: orderDirection || API_FUNCTION_DECORATOR_CONSTANT.DEFAULT_FILTER_ORDER_BY_DIRECTION,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (\"createdAt\" in options.model.prototype) {\n\t\t\t\tif (createdAtFrom && createdAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tcreatedAt: Between(createdAtFrom, createdAtTo),\n\t\t\t\t\t};\n\t\t\t\t} else if (createdAtFrom) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tcreatedAt: MoreThanOrEqual(createdAtFrom),\n\t\t\t\t\t};\n\t\t\t\t} else if (createdAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tcreatedAt: LessThanOrEqual(createdAtTo),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\"updatedAt\" in options.model.prototype) {\n\t\t\t\tif (updatedAtFrom && updatedAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tupdatedAt: Between(updatedAtFrom, updatedAtTo),\n\t\t\t\t\t};\n\t\t\t\t} else if (updatedAtFrom) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tupdatedAt: MoreThanOrEqual(updatedAtFrom),\n\t\t\t\t\t};\n\t\t\t\t} else if (updatedAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\tupdatedAt: LessThanOrEqual(updatedAtTo),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\"receivedAt\" in options.model.prototype) {\n\t\t\t\tif (receivedAtFrom && receivedAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\treceivedAt: Between(receivedAtFrom, receivedAtTo),\n\t\t\t\t\t};\n\t\t\t\t} else if (receivedAtFrom) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\treceivedAt: MoreThanOrEqual(receivedAtFrom),\n\t\t\t\t\t};\n\t\t\t\t} else if (receivedAtTo) {\n\t\t\t\t\tfilter.where = {\n\t\t\t\t\t\t...filter.where,\n\t\t\t\t\t\treceivedAt: LessThanOrEqual(receivedAtTo),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst repository: Repository<E> = this.repository;\n\t\t\tif (!repository) {\n\t\t\t\tthrow new Error(\"Repository is not available in this context\");\n\t\t\t}\n\n\t\t\treturn executor(repository, options.model, properties, filter);\n\t\t};\n\n\t\treturn descriptor;\n\t};\n}\n"]}
|
|
@@ -6,8 +6,8 @@ const common_1 = require("@nestjs/common");
|
|
|
6
6
|
const utility_1 = require("../../../utility");
|
|
7
7
|
const enum_1 = require("../../../enum");
|
|
8
8
|
async function executor(repository, model, filter) {
|
|
9
|
+
console.log("FILTER", filter);
|
|
9
10
|
try {
|
|
10
|
-
console.log("FILTER", filter);
|
|
11
11
|
const item = await repository.findOne(filter);
|
|
12
12
|
if (!item) {
|
|
13
13
|
throw new common_1.NotFoundException((0, utility_1.ErrorString)({ entity: model, type: enum_1.EErrorStringAction.NOT_FOUND }));
|
|
@@ -15,11 +15,10 @@ async function executor(repository, model, filter) {
|
|
|
15
15
|
return item;
|
|
16
16
|
}
|
|
17
17
|
catch (error) {
|
|
18
|
-
console.log("
|
|
18
|
+
console.log("FUCKIGH", error);
|
|
19
19
|
if (error instanceof common_1.HttpException) {
|
|
20
20
|
throw error;
|
|
21
21
|
}
|
|
22
|
-
console.log("A TYT CHE ZABYL");
|
|
23
22
|
throw new common_1.InternalServerErrorException((0, utility_1.ErrorString)({
|
|
24
23
|
entity: model,
|
|
25
24
|
type: enum_1.EErrorStringAction.FETCHING_ERROR,
|
|
@@ -51,7 +50,6 @@ function ApiFunctionGet(options) {
|
|
|
51
50
|
}
|
|
52
51
|
return executor(repository, options.model, filter);
|
|
53
52
|
};
|
|
54
|
-
console.log("DESC", descriptor);
|
|
55
53
|
return descriptor;
|
|
56
54
|
};
|
|
57
55
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get.decorator.js","sourceRoot":"/","sources":["decorator/api/function/get.decorator.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"get.decorator.js","sourceRoot":"/","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E7C,MAAM,qBAAqB,GAA+B,gBAA6F,CAAC;gBAExJ,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;oBACvD,IAAI,OAAO,qBAAqB,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;wBACjD,MAAM,CAAC,KAAK,GAAG;4BACX,GAAG,MAAM,CAAC,KAAK;4BACf,CAAC,GAAG,CAAC,EAAE,IAAA,cAAI,EAAC,IAAI,qBAAqB,CAAC,GAAG,CAAC,GAAG,CAAC;yBACjD,CAAC;oBACN,CAAC;gBACL,CAAC,CAAC,CAAC;YACH,CAAC;YAED,MAAM,UAAU,GAAkB,IAAI,CAAC,UAAU,CAAC;YAClD,IAAI,CAAC,UAAU,EAAE,CAAC;gBACd,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;YACnE,CAAC;YAED,OAAO,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACvD,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;AACN,CAAC","sourcesContent":["import type {BaseEntity, FindManyOptions, FindOneOptions, FindOptionsRelations, Repository} from \"typeorm\";\nimport { Like } from \"typeorm\";\n\nimport { HttpException, InternalServerErrorException, NotFoundException } from \"@nestjs/common\";\nimport { ErrorString } from \"../../../utility\";\nimport { EErrorStringAction } from \"../../../enum\";\nimport {TApiFunctionGetProperties} from \"../../../type\";\n\nasync function executor<E extends BaseEntity>(repository: Repository<E>, model: new () => E, filter: FindOneOptions<E>): Promise<E> {\n console.log(\"FILTER\", filter);\n try {\n const item: E | null = await repository.findOne(filter);\n\n if (!item) {\n throw new NotFoundException(ErrorString({ entity: model, type: EErrorStringAction.NOT_FOUND }));\n }\n\n return item;\n } catch (error) {\n console.log(\"FUCKIGH\", error);\n if (error instanceof HttpException) {\n throw error;\n }\n\n throw new InternalServerErrorException(\n ErrorString({\n entity: model,\n type: EErrorStringAction.FETCHING_ERROR,\n }),\n );\n }\n}\nexport function ApiFunctionGet<E extends BaseEntity>(options: { model: new () => E; }) {\n return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n descriptor.value = async function (\n this: {\n repository: Repository<E>;\n },\n id: string,\n properties?: TApiFunctionGetProperties<InstanceType<typeof options.model>>,\n relations?: FindOptionsRelations<E>\n ) {\n\n const filter: FindManyOptions<typeof options.model> = {\n where: { id },\n relations: relations,\n };\n\n if (properties) {\n const {...entityProperties} = properties;\n\n const typedEntityProperties: keyof typeof options.model = entityProperties as Exclude<keyof Omit<E, \"createdAt\" | \"updatedAt\" | \"receivedAt\">, keyof E>;\n\n Object.keys(typedEntityProperties).forEach((key: string) => {\n if (typeof typedEntityProperties[key] === \"string\") {\n filter.where = {\n ...filter.where,\n [key]: Like(`%${typedEntityProperties[key]}%`),\n };\n }\n });\n }\n\n const repository: Repository<E> = this.repository;\n if (!repository) {\n throw new Error(\"Repository is not available in this context\");\n }\n\n return executor(repository, options.model, filter);\n };\n\n return descriptor;\n };\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiFunctionUpdate = ApiFunctionUpdate;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const utility_1 = require("../../../utility");
|
|
6
|
+
const enum_1 = require("../../../enum");
|
|
7
|
+
const get_decorator_1 = require("./get.decorator");
|
|
8
|
+
async function executor(repository, entityType, id, properties, getFunction) {
|
|
9
|
+
try {
|
|
10
|
+
const entity = await getFunction(id.toString());
|
|
11
|
+
const allColumns = (0, utility_1.GetEntityColumns)(entityType);
|
|
12
|
+
const autoGeneratedColumns = (0, utility_1.GetEntityColumns)(entityType, true);
|
|
13
|
+
const filteredProperties = Object.keys(properties).reduce((acc, key) => {
|
|
14
|
+
if (allColumns.includes(key) && !autoGeneratedColumns.includes(key)) {
|
|
15
|
+
acc[key] = properties[key];
|
|
16
|
+
}
|
|
17
|
+
return acc;
|
|
18
|
+
}, {});
|
|
19
|
+
const updatedEntity = repository.merge(entity, filteredProperties);
|
|
20
|
+
const savedEntity = await repository.save(updatedEntity);
|
|
21
|
+
return savedEntity;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.log(error);
|
|
25
|
+
if (error instanceof common_1.HttpException) {
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
throw new common_1.InternalServerErrorException((0, utility_1.ErrorString)({
|
|
29
|
+
entity: entityType,
|
|
30
|
+
type: enum_1.EErrorStringAction.UPDATING_ERROR,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function ApiFunctionUpdate(options) {
|
|
35
|
+
return function (_target, _propertyKey, descriptor) {
|
|
36
|
+
const getDecorator = (0, get_decorator_1.ApiFunctionGet)({ model: options.model });
|
|
37
|
+
let getFunction;
|
|
38
|
+
descriptor.value = async function (id, properties) {
|
|
39
|
+
const repository = this.repository;
|
|
40
|
+
if (!repository) {
|
|
41
|
+
throw new Error("Repository is not available in this context");
|
|
42
|
+
}
|
|
43
|
+
if (!getFunction) {
|
|
44
|
+
const getDescriptor = {
|
|
45
|
+
value: function () { return Promise.reject(new Error('Not implemented')); }
|
|
46
|
+
};
|
|
47
|
+
getDecorator(this, 'get', getDescriptor);
|
|
48
|
+
if (getDescriptor.value) {
|
|
49
|
+
getFunction = getDescriptor.value.bind(this);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error('Get function is not properly decorated');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return executor(repository, options.model, id, properties, getFunction);
|
|
56
|
+
};
|
|
57
|
+
return descriptor;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=update.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update.decorator.js","sourceRo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Exception} from \"@nestjs/common\";\nimport {ErrorString, GetEntityColumns} from \"../../../utility\";\nimport {EErrorStringAction} from \"../../../enum\";\nimport {TApiFunctionGetProperties, TApiFunctionUpdateProperties} from \"../../../type\";\nimport {ApiFunctionGet} from \"./get.decorator\";\n\nasync function executor<E extends BaseEntity>(\n repository: Repository<E>,\n entityType: new () => E,\n id: string | number,\n properties: TApiFunctionUpdateProperties<E>,\n getFunction:(id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>\n): Promise<E> {\n try {\n const entity = await getFunction(id.toString());\n\n const allColumns = GetEntityColumns(entityType);\n const autoGeneratedColumns = GetEntityColumns(entityType, true);\n\n const filteredProperties = Object.keys(properties).reduce((acc, key) => {\n if (allColumns.includes(key as keyof E) && !autoGeneratedColumns.includes(key as keyof E)) {\n (acc as any)[key] = (properties as any)[key];\n }\n return acc;\n }, {} as DeepPartial<E>);\n\n const updatedEntity = repository.merge(entity, filteredProperties);\n const savedEntity = await repository.save(updatedEntity);\n return savedEntity;\n } catch (error) {\n console.log(error);\n if (error instanceof HttpException) {\n throw error;\n }\n throw new InternalServerErrorException(\n ErrorString({\n entity: entityType,\n type: EErrorStringAction.UPDATING_ERROR,\n })\n );\n }\n}\n\nexport function ApiFunctionUpdate<E extends BaseEntity>(options: { model: new () => E }) {\n return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {\n const getDecorator = ApiFunctionGet({ model: options.model });\n let getFunction: (id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>;\n\n descriptor.value = async function (\n this: {\n repository: Repository<E>;\n },\n id: string | number,\n properties: TApiFunctionUpdateProperties<InstanceType<typeof options.model>>\n ) {\n const repository: Repository<E> = this.repository;\n if (!repository) {\n throw new Error(\"Repository is not available in this context\");\n }\n\n if (!getFunction) {\n const getDescriptor: TypedPropertyDescriptor<(id: string, properties?: TApiFunctionGetProperties<E>) => Promise<E>> = {\n value: function() { return Promise.reject(new Error('Not implemented')); }\n };\n getDecorator(this, 'get', getDescriptor);\n if (getDescriptor.value) {\n getFunction = getDescriptor.value.bind(this);\n } else {\n throw new Error('Get function is not properly decorated');\n }\n }\n\n return executor(repository, options.model, id, properties, getFunction);\n };\n\n return descriptor;\n };\n}\n"]}
|
|
@@ -17,4 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./function"), exports);
|
|
18
18
|
__exportStar(require("./property"), exports);
|
|
19
19
|
__exportStar(require("./service.decorator"), exports);
|
|
20
|
+
__exportStar(require("./controller.decorator"), exports);
|
|
21
|
+
__exportStar(require("./route"), exports);
|
|
20
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["decorator/api/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,6CAA2B;AAC3B,sDAAoC","sourcesContent":["export * from \"./function\";\nexport * from \"./property\";\nexport * from \"./service.decorator\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["decorator/api/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,6CAA2B;AAC3B,sDAAoC;AACpC,yDAAuC;AACvC,0CAAwB","sourcesContent":["export * from \"./function\";\nexport * from \"./property\";\nexport * from \"./service.decorator\";\nexport * from \"./controller.decorator\";\nexport * from \"./route\";\n"]}
|
|
@@ -5,12 +5,6 @@ const common_1 = require("@nestjs/common");
|
|
|
5
5
|
const swagger_1 = require("@nestjs/swagger");
|
|
6
6
|
const throttler_1 = require("@nestjs/throttler");
|
|
7
7
|
const enum_1 = require("../../enum");
|
|
8
|
-
const bad_request_dto_1 = require("../../dto/exception/bad-request.dto");
|
|
9
|
-
const forbidden_dto_1 = require("../../dto/exception/forbidden.dto");
|
|
10
|
-
const internal_server_error_dto_1 = require("../../dto/exception/internal-server-error.dto");
|
|
11
|
-
const not_found_dto_1 = require("../../dto/exception/not-found.dto");
|
|
12
|
-
const too_many_requests_dto_1 = require("../../dto/exception/too-many-requests.dto");
|
|
13
|
-
const unauthorized_dto_1 = require("../../dto/exception/unauthorized.dto");
|
|
14
8
|
function ApiMethod(options) {
|
|
15
9
|
let summary = "";
|
|
16
10
|
if (options.action) {
|
|
@@ -129,50 +123,6 @@ function ApiMethod(options) {
|
|
|
129
123
|
if (options.throttler) {
|
|
130
124
|
decorators.push((0, throttler_1.Throttle)({ default: options.throttler }));
|
|
131
125
|
}
|
|
132
|
-
if (options.responses) {
|
|
133
|
-
if (options.responses.unauthorized) {
|
|
134
|
-
decorators.push((0, swagger_1.ApiUnauthorizedResponse)({
|
|
135
|
-
description: "Unauthorized",
|
|
136
|
-
status: common_1.HttpStatus.UNAUTHORIZED,
|
|
137
|
-
type: unauthorized_dto_1.ExceptionUnauthorizedDTO,
|
|
138
|
-
}));
|
|
139
|
-
}
|
|
140
|
-
if (options.responses.forbidden) {
|
|
141
|
-
decorators.push((0, swagger_1.ApiForbiddenResponse)({
|
|
142
|
-
description: "Forbidden",
|
|
143
|
-
status: common_1.HttpStatus.FORBIDDEN,
|
|
144
|
-
type: forbidden_dto_1.ExceptionForbiddenDTO,
|
|
145
|
-
}));
|
|
146
|
-
}
|
|
147
|
-
if (options.responses.internalServerError) {
|
|
148
|
-
decorators.push((0, swagger_1.ApiInternalServerErrorResponse)({
|
|
149
|
-
description: "Internal Server Error",
|
|
150
|
-
status: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
|
|
151
|
-
type: internal_server_error_dto_1.ExceptionInternalServerErrorDTO,
|
|
152
|
-
}));
|
|
153
|
-
}
|
|
154
|
-
if (options.responses.notFound) {
|
|
155
|
-
decorators.push((0, swagger_1.ApiNotFoundResponse)({
|
|
156
|
-
description: "Not Found",
|
|
157
|
-
status: common_1.HttpStatus.NOT_FOUND,
|
|
158
|
-
type: not_found_dto_1.ExceptionNotFoundDTO,
|
|
159
|
-
}));
|
|
160
|
-
}
|
|
161
|
-
if (options.responses.badRequest) {
|
|
162
|
-
decorators.push((0, swagger_1.ApiBadRequestResponse)({
|
|
163
|
-
description: "Bad Request",
|
|
164
|
-
status: common_1.HttpStatus.BAD_REQUEST,
|
|
165
|
-
type: bad_request_dto_1.ExceptionBadRequestDTO,
|
|
166
|
-
}));
|
|
167
|
-
}
|
|
168
|
-
if (options.responses.tooManyRequests) {
|
|
169
|
-
decorators.push((0, swagger_1.ApiBadRequestResponse)({
|
|
170
|
-
description: "Too Many Requests",
|
|
171
|
-
status: common_1.HttpStatus.TOO_MANY_REQUESTS,
|
|
172
|
-
type: too_many_requests_dto_1.ExceptionTooManyRequestsDTO,
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
126
|
switch (options.method) {
|
|
177
127
|
case common_1.RequestMethod.GET: {
|
|
178
128
|
decorators.push((0, common_1.Get)(options.path));
|