UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PickType = 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 get_fields_and_decorator_util_1 = require("../schema-builder/utils/get-fields-and-decorator.util");
8const type_helpers_utils_1 = require("./type-helpers.utils");
9function PickType(classRef, keys, decorator) {
10 const { fields, decoratorFactory } = (0, get_fields_and_decorator_util_1.getFieldsAndDecoratorForType)(classRef);
11 const isInheritedPredicate = (propertyKey) => keys.includes(propertyKey);
12 class PickObjectType {
13 constructor() {
14 (0, mapped_types_1.inheritPropertyInitializers)(this, classRef, isInheritedPredicate);
15 }
16 }
17 decoratorFactory({ isAbstract: true })(PickObjectType);
18 if (decorator) {
19 decorator({ isAbstract: true })(PickObjectType);
20 }
21 else {
22 decoratorFactory({ isAbstract: true })(PickObjectType);
23 }
24 (0, mapped_types_1.inheritValidationMetadata)(classRef, PickObjectType, isInheritedPredicate);
25 (0, mapped_types_1.inheritTransformationMetadata)(classRef, PickObjectType, isInheritedPredicate);
26 fields
27 .filter((item) => keys.includes(item.name))
28 .forEach((item) => {
29 if ((0, shared_utils_1.isFunction)(item.typeFn)) {
30 /**
31 * Execute type function eagarly to update the type options object (before "clone" operation)
32 * when the passed function (e.g., @Field(() => Type)) lazily returns an array.
33 */
34 item.typeFn();
35 }
36 (0, decorators_1.Field)(item.typeFn, { ...item.options })(PickObjectType.prototype, item.name);
37 (0, type_helpers_utils_1.applyFieldDecorators)(PickObjectType, item);
38 });
39 return PickObjectType;
40}
41exports.PickType = PickType;