1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getTypeIsArrayTuple = exports.createParamDecorator = exports.createMixedDecorator = exports.createPropertyDecorator = exports.createClassDecorator = exports.createMethodDecorator = void 0;
|
4 | const lodash_1 = require("lodash");
|
5 | const constants_1 = require("../constants");
|
6 | const plugin_constants_1 = require("../plugin/plugin-constants");
|
7 | function createMethodDecorator(metakey, metadata) {
|
8 | return (target, key, descriptor) => {
|
9 | Reflect.defineMetadata(metakey, metadata, descriptor.value);
|
10 | return descriptor;
|
11 | };
|
12 | }
|
13 | exports.createMethodDecorator = createMethodDecorator;
|
14 | function createClassDecorator(metakey, metadata = []) {
|
15 | return (target) => {
|
16 | const prevValue = Reflect.getMetadata(metakey, target) || [];
|
17 | Reflect.defineMetadata(metakey, [...prevValue, ...metadata], target);
|
18 | return target;
|
19 | };
|
20 | }
|
21 | exports.createClassDecorator = createClassDecorator;
|
22 | function createPropertyDecorator(metakey, metadata, overrideExisting = true) {
|
23 | return (target, propertyKey) => {
|
24 | var _a, _b, _c, _d;
|
25 | const properties = Reflect.getMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, target) || [];
|
26 | const key = `:${propertyKey}`;
|
27 | if (!properties.includes(key)) {
|
28 | Reflect.defineMetadata(constants_1.DECORATORS.API_MODEL_PROPERTIES_ARRAY, [...properties, `:${propertyKey}`], target);
|
29 | }
|
30 | const existingMetadata = Reflect.getMetadata(metakey, target, propertyKey);
|
31 | if (existingMetadata) {
|
32 | const newMetadata = (0, lodash_1.pickBy)(metadata, (0, lodash_1.negate)(lodash_1.isUndefined));
|
33 | const metadataToSave = overrideExisting
|
34 | ? Object.assign(Object.assign({}, existingMetadata), newMetadata) : Object.assign(Object.assign({}, newMetadata), existingMetadata);
|
35 | Reflect.defineMetadata(metakey, metadataToSave, target, propertyKey);
|
36 | }
|
37 | else {
|
38 | const type = (_d = (_c = (_b = (_a = target === null || target === void 0 ? void 0 : target.constructor) === null || _a === void 0 ? void 0 : _a[plugin_constants_1.METADATA_FACTORY_NAME]) === null || _b === void 0 ? void 0 : _b.call(_a)[propertyKey]) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : Reflect.getMetadata('design:type', target, propertyKey);
|
39 | Reflect.defineMetadata(metakey, Object.assign({ type }, (0, lodash_1.pickBy)(metadata, (0, lodash_1.negate)(lodash_1.isUndefined))), target, propertyKey);
|
40 | }
|
41 | };
|
42 | }
|
43 | exports.createPropertyDecorator = createPropertyDecorator;
|
44 | function createMixedDecorator(metakey, metadata) {
|
45 | return (target, key, descriptor) => {
|
46 | if (descriptor) {
|
47 | let metadatas;
|
48 | if (Array.isArray(metadata)) {
|
49 | const previousMetadata = Reflect.getMetadata(metakey, descriptor.value) || [];
|
50 | metadatas = [...previousMetadata, ...metadata];
|
51 | }
|
52 | else {
|
53 | const previousMetadata = Reflect.getMetadata(metakey, descriptor.value) || {};
|
54 | metadatas = Object.assign(Object.assign({}, previousMetadata), metadata);
|
55 | }
|
56 | Reflect.defineMetadata(metakey, metadatas, descriptor.value);
|
57 | return descriptor;
|
58 | }
|
59 | Reflect.defineMetadata(metakey, metadata, target);
|
60 | return target;
|
61 | };
|
62 | }
|
63 | exports.createMixedDecorator = createMixedDecorator;
|
64 | function createParamDecorator(metadata, initial) {
|
65 | return (target, key, descriptor) => {
|
66 | const parameters = Reflect.getMetadata(constants_1.DECORATORS.API_PARAMETERS, descriptor.value) || [];
|
67 | Reflect.defineMetadata(constants_1.DECORATORS.API_PARAMETERS, [
|
68 | ...parameters,
|
69 | Object.assign(Object.assign({}, initial), (0, lodash_1.pickBy)(metadata, (0, lodash_1.negate)(lodash_1.isUndefined)))
|
70 | ], descriptor.value);
|
71 | return descriptor;
|
72 | };
|
73 | }
|
74 | exports.createParamDecorator = createParamDecorator;
|
75 | function getTypeIsArrayTuple(input, isArrayFlag) {
|
76 | if (!input) {
|
77 | return [input, isArrayFlag];
|
78 | }
|
79 | if (isArrayFlag) {
|
80 | return [input, isArrayFlag];
|
81 | }
|
82 | const isInputArray = (0, lodash_1.isArray)(input);
|
83 | const type = isInputArray ? input[0] : input;
|
84 | return [type, isInputArray];
|
85 | }
|
86 | exports.getTypeIsArrayTuple = getTypeIsArrayTuple;
|