1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ApiResponseProperty = exports.ApiPropertyOptional = exports.createApiPropertyDecorator = exports.ApiProperty = void 0;
|
4 | const constants_1 = require("../constants");
|
5 | const enum_utils_1 = require("../utils/enum.utils");
|
6 | const helpers_1 = require("./helpers");
|
7 | const isEnumArray = (obj) => obj.isArray && !!obj.enum;
|
8 | function ApiProperty(options = {}) {
|
9 | return createApiPropertyDecorator(options);
|
10 | }
|
11 | exports.ApiProperty = ApiProperty;
|
12 | function createApiPropertyDecorator(options = {}, overrideExisting = true) {
|
13 | const [type, isArray] = helpers_1.getTypeIsArrayTuple(options.type, options.isArray);
|
14 | options = Object.assign(Object.assign({}, options), { type,
|
15 | isArray });
|
16 | if (isEnumArray(options)) {
|
17 | options.type = 'array';
|
18 | const enumValues = enum_utils_1.getEnumValues(options.enum);
|
19 | options.items = {
|
20 | type: enum_utils_1.getEnumType(enumValues),
|
21 | enum: enumValues
|
22 | };
|
23 | delete options.enum;
|
24 | }
|
25 | else if (options.enum) {
|
26 | const enumValues = enum_utils_1.getEnumValues(options.enum);
|
27 | options.enum = enumValues;
|
28 | options.type = enum_utils_1.getEnumType(enumValues);
|
29 | }
|
30 | if (Array.isArray(options.type)) {
|
31 | options.type = 'array';
|
32 | options.items = {
|
33 | type: 'array',
|
34 | items: {
|
35 | type: options.type[0]
|
36 | }
|
37 | };
|
38 | }
|
39 | return helpers_1.createPropertyDecorator(constants_1.DECORATORS.API_MODEL_PROPERTIES, options, overrideExisting);
|
40 | }
|
41 | exports.createApiPropertyDecorator = createApiPropertyDecorator;
|
42 | function ApiPropertyOptional(options = {}) {
|
43 | return ApiProperty(Object.assign(Object.assign({}, options), { required: false }));
|
44 | }
|
45 | exports.ApiPropertyOptional = ApiPropertyOptional;
|
46 | function ApiResponseProperty(options = {}) {
|
47 | return ApiProperty(Object.assign({ readOnly: true }, options));
|
48 | }
|
49 | exports.ApiResponseProperty = ApiResponseProperty;
|