1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.addFieldMetadata = exports.Field = void 0;
|
10 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
11 | const lazy_metadata_storage_1 = require("../schema-builder/storages/lazy-metadata.storage");
|
12 | const type_metadata_storage_1 = require("../schema-builder/storages/type-metadata.storage");
|
13 | const reflection_utilts_1 = require("../utils/reflection.utilts");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Field(typeOrOptions, fieldOptions) {
|
19 | return (prototype, propertyKey, descriptor) => {
|
20 | addFieldMetadata(typeOrOptions, fieldOptions, prototype, propertyKey, descriptor);
|
21 | };
|
22 | }
|
23 | exports.Field = Field;
|
24 | function addFieldMetadata(typeOrOptions, fieldOptions, prototype, propertyKey, descriptor, loadEagerly) {
|
25 | const [typeFunc, options = {}] = (0, shared_utils_1.isFunction)(typeOrOptions)
|
26 | ? [typeOrOptions, fieldOptions]
|
27 | : [undefined, typeOrOptions];
|
28 | const applyMetadataFn = () => {
|
29 | const isResolver = !!descriptor;
|
30 | const isResolverMethod = !!(descriptor && descriptor.value);
|
31 | const { typeFn: getType, options: typeOptions } = (0, reflection_utilts_1.reflectTypeFromMetadata)({
|
32 | metadataKey: isResolverMethod ? 'design:returntype' : 'design:type',
|
33 | prototype,
|
34 | propertyKey,
|
35 | explicitTypeFn: typeFunc,
|
36 | typeOptions: options,
|
37 | ignoreOnUndefinedType: loadEagerly,
|
38 | });
|
39 | type_metadata_storage_1.TypeMetadataStorage.addClassFieldMetadata({
|
40 | name: propertyKey,
|
41 | schemaName: options.name || propertyKey,
|
42 | typeFn: getType,
|
43 | options: typeOptions,
|
44 | target: prototype.constructor,
|
45 | description: options.description,
|
46 | deprecationReason: options.deprecationReason,
|
47 | complexity: options.complexity,
|
48 | middleware: options.middleware,
|
49 | });
|
50 | if (isResolver) {
|
51 | type_metadata_storage_1.TypeMetadataStorage.addResolverPropertyMetadata({
|
52 | kind: 'internal',
|
53 | methodName: propertyKey,
|
54 | schemaName: options.name || propertyKey,
|
55 | target: prototype.constructor,
|
56 | complexity: options.complexity,
|
57 | });
|
58 | }
|
59 | };
|
60 | if (loadEagerly) {
|
61 | applyMetadataFn();
|
62 | }
|
63 | else {
|
64 | lazy_metadata_storage_1.LazyMetadataStorage.store(prototype.constructor, applyMetadataFn, { isField: true });
|
65 | }
|
66 | }
|
67 | exports.addFieldMetadata = addFieldMetadata;
|