UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.OmitType = 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 get_fields_and_decorator_util_1 = require("../schema-builder/utils/get-fields-and-decorator.util");
9const type_helpers_utils_1 = require("./type-helpers.utils");
10function OmitType(classRef, keys, decorator) {
11 const { fields, decoratorFactory } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classRef);
12 const isInheritedPredicate = (propertyKey) => !keys.includes(propertyKey);
13 class OmitObjectType {
14 constructor() {
15 (0, mapped_types_1.inheritPropertyInitializers)(this, classRef, isInheritedPredicate);
16 }
17 }
18 if (decorator) {
19 decorator({ isAbstract: true })(OmitObjectType);
20 }
21 else {
22 decoratorFactory({ isAbstract: true })(OmitObjectType);
23 }
24 (0, mapped_types_1.inheritValidationMetadata)(classRef, OmitObjectType, isInheritedPredicate);
25 (0, mapped_types_1.inheritTransformationMetadata)(classRef, OmitObjectType, isInheritedPredicate);
26 function applyFields(fields) {
27 fields
28 .filter((item) => !keys.includes(item.name))
29 .forEach((item) => {
30 if ((0, shared_utils_1.isFunction)(item.typeFn)) {
31 // Execute type function eagerly to update the type options object (before "clone" operation)
32 // when the passed function (e.g., @Field(() => Type)) lazily returns an array.
33 item.typeFn();
34 }
35 (0, decorators_1.Field)(item.typeFn, { ...item.options })(OmitObjectType.prototype, item.name);
36 (0, type_helpers_utils_1.applyFieldDecorators)(OmitObjectType, item);
37 });
38 }
39 applyFields(fields);
40 // Register a refresh hook to update the fields when the serialized metadata
41 // is loaded from file.
42 metadata_loader_1.MetadataLoader.addRefreshHook(() => {
43 const { fields } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classRef, {
44 overrideFields: true,
45 });
46 applyFields(fields);
47 });
48 return OmitObjectType;
49}
50exports.OmitType = OmitType;