1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PartialType = void 0;
|
4 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
5 | const mapped_types_1 = require("@nestjs/mapped-types");
|
6 | const decorators_1 = require("../decorators");
|
7 | const metadata_loader_1 = require("../plugin/metadata-loader");
|
8 | const plugin_constants_1 = require("../plugin/plugin-constants");
|
9 | const get_fields_and_decorator_util_1 = require("../schema-builder/utils/get-fields-and-decorator.util");
|
10 | const type_helpers_utils_1 = require("./type-helpers.utils");
|
11 | function isPartialTypeOptions(optionsOrDecorator) {
|
12 | return (optionsOrDecorator &&
|
13 | ('decorator' in optionsOrDecorator ||
|
14 | 'omitDefaultValues' in optionsOrDecorator));
|
15 | }
|
16 | function 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 |
|
44 |
|
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 |
|
58 |
|
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 | }
|
74 | exports.PartialType = PartialType;
|