UNPKG

@joktec/mongo

Version:

JokTec - Mongo Service

123 lines 5.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Prop = void 0; const core_1 = require("@joktec/core"); const utils_1 = require("@joktec/utils"); const typegoose_1 = require("@typegoose/typegoose"); const lodash_1 = require("lodash"); const models_1 = require("../models"); const mongo_constant_1 = require("../mongo.constant"); const props_1 = require("./props"); const isRequired = (opts = {}) => { const required = opts.required; if (!required) return false; if ((0, lodash_1.isBoolean)(required)) return required; if ((0, lodash_1.isArray)(required)) return required[0]; return false; }; const isObjectIdType = (designType) => { return designType === models_1.ObjectId || designType === models_1.ObjectId.prototype.constructor; }; const Prop = (opts = {}, kind) => { return (target, propertyKey) => { let designType = Reflect.getMetadata('design:type', target, propertyKey); ['unique', 'index', 'text'].map(key => (0, lodash_1.unset)(opts, key)); const decorators = [...(0, utils_1.toArray)(opts.decorators)]; const swaggerOptions = { type: isObjectIdType(designType) ? String : designType, required: isRequired(opts), example: !(0, lodash_1.isUndefined)(opts.example) ? opts.example : opts.default, enum: opts.enum, deprecated: (0, lodash_1.isBoolean)(opts.deprecated) ? opts.deprecated : undefined, nullable: !opts.required, description: opts?.comment || undefined, }; let isArrayType = false; if (opts.type) { const typeFunction = (0, lodash_1.isArray)(opts.type) ? opts.type[0] : opts.type; if (isObjectIdType(typeFunction)) decorators.push((0, utils_1.Type)(() => String)); else decorators.push((0, utils_1.Type)(() => typeFunction)); isArrayType = (0, lodash_1.isArray)(opts.type) || kind === typegoose_1.PropType.ARRAY; designType = (0, lodash_1.isArray)(opts.type) ? opts.type[0] : opts.type; swaggerOptions.type = isObjectIdType(typeFunction) ? String : typeFunction; swaggerOptions.isArray = isArrayType; if (isObjectIdType(typeFunction)) { if (isArrayType) opts.example = ['00000020f51bb4362eee2a4d']; else opts.example = '00000020f51bb4362eee2a4d'; } } if (opts.required) { const validatorOption = {}; if ((0, lodash_1.isArray)(opts.required)) validatorOption.message = (0, lodash_1.last)(opts.required); decorators.push((0, utils_1.IsNotEmpty)(validatorOption)); } else { decorators.push((0, utils_1.IsOptional)()); } if (opts.hidden) { decorators.push((0, utils_1.Exclude)({ toPlainOnly: true })); decorators.push((0, core_1.ApiHideProperty)()); } else if (opts.groups?.length) { decorators.push((0, utils_1.Expose)({ groups: opts.groups })); } else { decorators.push((0, utils_1.Expose)()); } if (opts.nested) { decorators.push((0, utils_1.ValidateNested)({ each: isArrayType })); if (!opts.type) decorators.push((0, utils_1.Type)(() => designType)); } if (opts.immutable) { if ((0, lodash_1.isBoolean)(opts.immutable) && swaggerOptions.nullable) { opts.immutable = (v) => !(0, lodash_1.isNil)(v); } } if (kind === typegoose_1.PropType.MAP) opts.allowMixed = typegoose_1.Severity.ALLOW; if (isArrayType) decorators.push(...(0, props_1.ArrayProps)(opts, swaggerOptions)); if (opts.enum) { decorators.push(...(0, props_1.EnumProps)(opts, swaggerOptions)); if (!isRequired(opts)) opts.addNullToEnum = true; } else if (designType === String) decorators.push(...(0, props_1.StringProps)(opts, swaggerOptions)); else if (designType === Number) decorators.push(...(0, props_1.NumberProps)(opts, swaggerOptions)); else if (designType === Date) decorators.push(...(0, props_1.DateProps)(opts, swaggerOptions)); else if (designType === Boolean) decorators.push(...(0, props_1.BoolProps)(opts, swaggerOptions)); else if (isObjectIdType(designType)) decorators.push((0, utils_1.IsMongoId)({ each: swaggerOptions.isArray })); const mongooseOpts = { ...opts }; if (mongooseOpts.ref) { delete mongooseOpts.type; if (opts.example) swaggerOptions.example = opts.example; else swaggerOptions.example = isArrayType ? [] : {}; } else { if (!isArrayType && (0, lodash_1.isArray)(opts.example)) { swaggerOptions.example = opts.example[0]; swaggerOptions.examples = opts.example; } } (0, core_1.applyDecorators)((0, typegoose_1.prop)(mongooseOpts, kind), ...decorators, (0, core_1.ApiProperty)((0, lodash_1.merge)(swaggerOptions, opts.swagger)))(target, propertyKey); Reflect.defineMetadata(mongo_constant_1.PROP_DESIGN_TYPE_KEY, designType, target, propertyKey); }; }; exports.Prop = Prop; //# sourceMappingURL=prop.decorator.js.map