UNPKG

3.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PartialType = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5const mapped_types_1 = require("@nestjs/mapped-types");
6const decorators_1 = require("../decorators");
7const metadata_loader_1 = require("../plugin/metadata-loader");
8const plugin_constants_1 = require("../plugin/plugin-constants");
9const get_fields_and_decorator_util_1 = require("../schema-builder/utils/get-fields-and-decorator.util");
10const type_helpers_utils_1 = require("./type-helpers.utils");
11function isPartialTypeOptions(optionsOrDecorator) {
12 return (optionsOrDecorator &&
13 ('decorator' in optionsOrDecorator ||
14 'omitDefaultValues' in optionsOrDecorator));
15}
16function PartialType(classRef, optionsOrDecorator) {
17 const { fields, decoratorFactory } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classRef);
18 class PartialObjectType {
19 constructor() {
20 (0, mapped_types_1.inheritPropertyInitializers)(this, classRef);
21 }
22 }
23 let decorator;
24 let omitDefaultValues = false;
25 if (isPartialTypeOptions(optionsOrDecorator)) {
26 decorator = optionsOrDecorator.decorator;
27 omitDefaultValues = optionsOrDecorator.omitDefaultValues;
28 }
29 else {
30 decorator = optionsOrDecorator;
31 }
32 if (decorator) {
33 decorator({ isAbstract: true })(PartialObjectType);
34 }
35 else {
36 decoratorFactory({ isAbstract: true })(PartialObjectType);
37 }
38 (0, mapped_types_1.inheritValidationMetadata)(classRef, PartialObjectType);
39 (0, mapped_types_1.inheritTransformationMetadata)(classRef, PartialObjectType);
40 function applyFields(fields) {
41 fields.forEach((item) => {
42 if ((0, shared_utils_1.isFunction)(item.typeFn)) {
43 // Execute type function eagerly to update the type options object (before "clone" operation)
44 // when the passed function (e.g., @Field(() => Type)) lazily returns an array.
45 item.typeFn();
46 }
47 (0, decorators_1.Field)(item.typeFn, {
48 ...item.options,
49 nullable: true,
50 defaultValue: omitDefaultValues ? undefined : item.options.defaultValue,
51 })(PartialObjectType.prototype, item.name);
52 (0, mapped_types_1.applyIsOptionalDecorator)(PartialObjectType, item.name);
53 (0, type_helpers_utils_1.applyFieldDecorators)(PartialObjectType, item);
54 });
55 }
56 applyFields(fields);
57 // Register a refresh hook to update the fields when the serialized metadata
58 // is loaded from file.
59 metadata_loader_1.MetadataLoader.addRefreshHook(() => {
60 const { fields } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classRef, {
61 overrideFields: true,
62 });
63 applyFields(fields);
64 });
65 if (PartialObjectType[plugin_constants_1.METADATA_FACTORY_NAME]) {
66 const pluginFields = Object.keys(PartialObjectType[plugin_constants_1.METADATA_FACTORY_NAME]());
67 pluginFields.forEach((key) => (0, mapped_types_1.applyIsOptionalDecorator)(PartialObjectType, key));
68 }
69 Object.defineProperty(PartialObjectType, 'name', {
70 value: `Partial${classRef.name}`,
71 });
72 return PartialObjectType;
73}
74exports.PartialType = PartialType;