UNPKG

3.05 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.IntersectionType = 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 IntersectionType(classARef, classBRef, decorator) {
11 const { decoratorFactory, fields: fieldsA } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classARef);
12 const { fields: fieldsB } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classBRef);
13 const fields = [...fieldsA, ...fieldsB];
14 class IntersectionObjectType {
15 constructor() {
16 (0, mapped_types_1.inheritPropertyInitializers)(this, classARef);
17 (0, mapped_types_1.inheritPropertyInitializers)(this, classBRef);
18 }
19 }
20 if (decorator) {
21 decorator({ isAbstract: true })(IntersectionObjectType);
22 }
23 else {
24 decoratorFactory({ isAbstract: true })(IntersectionObjectType);
25 }
26 (0, mapped_types_1.inheritValidationMetadata)(classARef, IntersectionObjectType);
27 (0, mapped_types_1.inheritTransformationMetadata)(classARef, IntersectionObjectType);
28 (0, mapped_types_1.inheritValidationMetadata)(classBRef, IntersectionObjectType);
29 (0, mapped_types_1.inheritTransformationMetadata)(classBRef, IntersectionObjectType);
30 function applyFields(fields) {
31 fields.forEach((item) => {
32 if ((0, shared_utils_1.isFunction)(item.typeFn)) {
33 // Execute type function eagerly to update the type options object (before "clone" operation)
34 // when the passed function (e.g., @Field(() => Type)) lazily returns an array.
35 item.typeFn();
36 }
37 (0, decorators_1.Field)(item.typeFn, { ...item.options })(IntersectionObjectType.prototype, item.name);
38 (0, type_helpers_utils_1.applyFieldDecorators)(IntersectionObjectType, item);
39 });
40 }
41 applyFields(fields);
42 // Register a refresh hook to update the fields when the serialized metadata
43 // is loaded from file.
44 metadata_loader_1.MetadataLoader.addRefreshHook(() => {
45 const { fields: fieldsA } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classARef, {
46 overrideFields: true,
47 });
48 const { fields: fieldsB } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classBRef, {
49 overrideFields: true,
50 });
51 const fields = [...fieldsA, ...fieldsB];
52 applyFields(fields);
53 });
54 Object.defineProperty(IntersectionObjectType, 'name', {
55 value: `Intersection${classARef.name}${classBRef.name}`,
56 });
57 return IntersectionObjectType;
58}
59exports.IntersectionType = IntersectionType;