UNPKG

mongodb-dynamic-api

Version:

Auto generated CRUD API for MongoDB using NestJS

125 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouteDecoratorsBuilder = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const lodash_1 = require("lodash"); const decorators_1 = require("../../decorators"); const dynamic_api_module_1 = require("../../dynamic-api.module"); const helpers_1 = require("../../helpers"); class RouteDecoratorsBuilder { constructor(routeType, entity, subPath, version, description, isPublic, dTOs = {}, isArrayResponse = false) { this.routeType = routeType; this.entity = entity; this.subPath = subPath; this.version = version; this.description = description; this.isPublic = isPublic; this.dTOs = dTOs; this.isArrayResponse = isArrayResponse; this.responseRouteTypeIsArray = [ 'GetMany', 'CreateMany', 'DuplicateMany', ]; this.bodyRouteTypeIsOptional = [ 'DuplicateOne', 'DuplicateMany', ]; } build() { const [paramKey] = this.dTOs.param ? (0, lodash_1.keys)(new this.dTOs.param()) : []; return [ ...this.getRouteDecorators(paramKey), ...this.getApiDecorators(paramKey), ]; } getRouteDecorators(paramKey) { let routeDecorators = []; const isAuthEnabled = dynamic_api_module_1.DynamicApiModule.state.get('isAuthEnabled'); if (this.isPublic) { routeDecorators.push((0, decorators_1.Public)()); } else if (isAuthEnabled) { routeDecorators.push((0, swagger_1.ApiBearerAuth)()); } const subPath = this.subPath ?? ''; const addSubPath = (before = true) => { if (!this.subPath) { return ''; } return before ? this.subPath + '/' : '/' + this.subPath; }; switch (this.routeType) { case 'GetMany': routeDecorators.push((0, common_1.Get)(subPath)); break; case 'GetOne': routeDecorators.push((0, common_1.Get)(`${addSubPath()}:${paramKey}`)); break; case 'CreateMany': routeDecorators.push((0, common_1.Post)(`many${addSubPath(false)}`)); break; case 'CreateOne': routeDecorators.push((0, common_1.Post)(subPath)); break; case 'UpdateMany': routeDecorators.push((0, common_1.Patch)(subPath)); break; case 'UpdateOne': routeDecorators.push((0, common_1.Patch)(`:${paramKey}${addSubPath(false)}`)); break; case 'ReplaceOne': routeDecorators.push((0, common_1.Put)(`:${paramKey}${addSubPath(false)}`)); break; case 'DuplicateMany': routeDecorators.push((0, common_1.Post)(`duplicate${addSubPath(false)}`)); break; case 'DuplicateOne': routeDecorators.push((0, common_1.Post)(`duplicate/:${paramKey}${addSubPath(false)}`)); break; case 'DeleteMany': routeDecorators.push((0, common_1.Delete)(subPath)); break; case 'DeleteOne': routeDecorators.push((0, common_1.Delete)(`:${paramKey}${addSubPath(false)}`)); break; case 'Aggregate': routeDecorators.push((0, common_1.Get)(subPath)); break; default: throw new Error(`Unexpected route type! Cannot build route decorators. Received: ${this.routeType}`); } return routeDecorators; } getApiDecorators(paramKey) { const feature = this.subPath ? `${(0, helpers_1.pascalCase)(this.subPath)}-${this.entity.name}` : this.entity.name; return [ (0, swagger_1.ApiOperation)({ operationId: `${(0, lodash_1.lowerFirst)(this.routeType)}${feature}${this.version ? 'V' + this.version : ''}`, summary: this.description ?? `${(0, lodash_1.upperFirst)((0, lodash_1.lowerCase)(this.routeType))} ${(0, lodash_1.lowerCase)(feature)}`, }), (0, swagger_1.ApiResponse)({ type: this.dTOs.presenter ?? this.entity, isArray: this.responseRouteTypeIsArray.includes(this.routeType) || this.isArrayResponse, }), ...(this.dTOs.body ? [ (0, swagger_1.ApiBody)({ type: this.dTOs.body, required: !this.bodyRouteTypeIsOptional.includes(this.routeType), }), ] : []), ...(this.dTOs.param && paramKey ? [ (0, swagger_1.ApiParam)({ type: typeof new this.dTOs.param()[paramKey], name: paramKey, }), ] : []), ]; } } exports.RouteDecoratorsBuilder = RouteDecoratorsBuilder; //# sourceMappingURL=route-decorators.builder.js.map