1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PartialType = void 0;
|
4 | const mapped_types_1 = require("@nestjs/mapped-types");
|
5 | const lodash_1 = require("lodash");
|
6 | const constants_1 = require("../constants");
|
7 | const decorators_1 = require("../decorators");
|
8 | const plugin_constants_1 = require("../plugin/plugin-constants");
|
9 | const model_properties_accessor_1 = require("../services/model-properties-accessor");
|
10 | const mapped_types_utils_1 = require("./mapped-types.utils");
|
11 | const modelPropertiesAccessor = new model_properties_accessor_1.ModelPropertiesAccessor();
|
12 | function PartialType(classRef) {
|
13 | const fields = modelPropertiesAccessor.getModelProperties(classRef.prototype);
|
14 | class PartialTypeClass {
|
15 | constructor() {
|
16 | mapped_types_1.inheritPropertyInitializers(this, classRef);
|
17 | }
|
18 | }
|
19 | mapped_types_1.inheritValidationMetadata(classRef, PartialTypeClass);
|
20 | mapped_types_1.inheritTransformationMetadata(classRef, PartialTypeClass);
|
21 | mapped_types_utils_1.clonePluginMetadataFactory(PartialTypeClass, classRef.prototype, (metadata) => lodash_1.mapValues(metadata, (item) => (Object.assign(Object.assign({}, item), { required: false }))));
|
22 | fields.forEach((key) => {
|
23 | const metadata = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES, classRef.prototype, key) || {};
|
24 | const decoratorFactory = decorators_1.ApiProperty(Object.assign(Object.assign({}, metadata), { required: false }));
|
25 | decoratorFactory(PartialTypeClass.prototype, key);
|
26 | mapped_types_1.applyIsOptionalDecorator(PartialTypeClass, key);
|
27 | });
|
28 | if (PartialTypeClass[plugin_constants_1.METADATA_FACTORY_NAME]) {
|
29 | const pluginFields = Object.keys(PartialTypeClass[plugin_constants_1.METADATA_FACTORY_NAME]());
|
30 | pluginFields.forEach((key) => mapped_types_1.applyIsOptionalDecorator(PartialTypeClass, key));
|
31 | }
|
32 | return PartialTypeClass;
|
33 | }
|
34 | exports.PartialType = PartialType;
|