json-api-nestjs
Version:
JsonApi Plugin for NestJs
80 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerBindService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const constants_1 = require("@nestjs/swagger/dist/constants");
const swagger_1 = require("@nestjs/swagger");
const core_1 = require("@nestjs/core");
const nestjs_shared_1 = require("../../../utils/nestjs-shared");
const constants_2 = require("@nestjs/common/constants");
const constants_3 = require("../../../constants");
const helper_1 = require("../helper");
const json_base_controller_1 = require("../controller/json-base.controller");
const filter_operand_model_1 = require("./filter-operand-model");
const utils_1 = require("./utils");
const bindings_1 = require("../config/bindings");
const method_1 = require("./method");
let SwaggerBindService = class SwaggerBindService {
entity;
discoveryService;
config;
mapEntity;
onModuleInit() {
this.initSwagger();
}
initSwagger() {
const controllerName = (0, helper_1.nameIt)((0, helper_1.getProviderName)(this.entity.name, constants_3.JSON_API_CONTROLLER_POSTFIX), json_base_controller_1.JsonBaseController).name;
const controllerInst = this.discoveryService
.getControllers()
.find((i) => i.name === controllerName ||
this.entity ===
Reflect.getMetadata(constants_3.JSON_API_DECORATOR_ENTITY, i.instance.constructor));
if (!controllerInst)
throw new Error(`Controller for ${this.entity.name} is empty`);
const mapProps = this.mapEntity.get(this.entity);
if (!mapProps)
throw new Error(`ZodEntityProps for ${this.entity.name} is empty`);
const controller = controllerInst.instance.constructor;
const apiTag = Reflect.getMetadata(constants_1.DECORATORS.API_TAGS, controller);
if (!apiTag) {
(0, swagger_1.ApiTags)(this.config['overrideRoute'] || this.entity.name)(controller);
}
(0, swagger_1.ApiTags)(this.entity.name)(controller);
(0, swagger_1.ApiExtraModels)(filter_operand_model_1.FilterOperand)(controller);
(0, swagger_1.ApiExtraModels)((0, utils_1.createApiModels)(this.entity, mapProps))(controller);
const { allowMethod = nestjs_shared_1.ObjectTyped.keys(bindings_1.Bindings) } = this.config;
for (const method of nestjs_shared_1.ObjectTyped.keys(bindings_1.Bindings)) {
if (!allowMethod.includes(method))
continue;
if (!(method in method_1.swaggerMethod))
continue;
const descriptor = Reflect.getOwnPropertyDescriptor(controller.prototype, method);
if (!descriptor)
throw new Error(`Descriptor for entity controller ${this.entity.name} is empty`);
method_1.swaggerMethod[method](controller.prototype, descriptor, this.entity, this.mapEntity, method);
Reflect.defineMetadata(constants_2.PARAMTYPES_METADATA, [Object], controller.prototype, method);
}
}
};
exports.SwaggerBindService = SwaggerBindService;
tslib_1.__decorate([
(0, common_1.Inject)(constants_3.CURRENT_ENTITY),
tslib_1.__metadata("design:type", Object)
], SwaggerBindService.prototype, "entity", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(core_1.DiscoveryService),
tslib_1.__metadata("design:type", core_1.DiscoveryService)
], SwaggerBindService.prototype, "discoveryService", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(constants_3.CONTROL_OPTIONS_TOKEN),
tslib_1.__metadata("design:type", Object)
], SwaggerBindService.prototype, "config", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(constants_3.ENTITY_MAP_PROPS),
tslib_1.__metadata("design:type", Map)
], SwaggerBindService.prototype, "mapEntity", void 0);
exports.SwaggerBindService = SwaggerBindService = tslib_1.__decorate([
(0, common_1.Injectable)()
], SwaggerBindService);
//# sourceMappingURL=swagger-bind.service.js.map