-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathenum.utils.ts
More file actions
105 lines (90 loc) · 3.08 KB
/
enum.utils.ts
File metadata and controls
105 lines (90 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { isString } from 'lodash';
import { SchemaObject } from '../interfaces/open-api-spec.interface';
import { SchemaObjectMetadata } from '../interfaces/schema-object-metadata.interface';
import { SwaggerEnumType } from '../types/swagger-enum.type';
export function getEnumValues(
enumType: SwaggerEnumType | (() => SwaggerEnumType)
): string[] | number[] | boolean[] {
if (typeof enumType === 'function') {
return getEnumValues(enumType());
}
if (Array.isArray(enumType)) {
return enumType as string[];
}
if (typeof enumType !== 'object') {
return [];
}
// Enums with numeric values
// enum Size {
// SMALL = 1,
// BIG = 2
// }
// are transpiled to include a reverse mapping
// const Size = {
// "1": "SMALL",
// "2": "BIG",
// "SMALL": 1,
// "BIG": 2,
// }
const numericValues = Object.values(enumType)
.filter((value) => typeof value === 'number')
.map((value: any) => value.toString());
return Object.keys(enumType)
.filter((key) => !numericValues.includes(key))
.map((key) => enumType[key]);
}
export function getEnumType(
values: (string | number | boolean)[]
): 'string' | 'number' | 'boolean' {
const hasString = values.filter(isString).length > 0;
if (hasString) return 'string';
const hasBoolean = values.some((v) => typeof v === 'boolean');
if (hasBoolean) return 'boolean';
return 'number';
}
export function addEnumArraySchema(
paramDefinition: Partial<
Record<'schema' | 'isArray' | 'enumName' | 'enumSchema', any>
>,
decoratorOptions: Partial<Record<'enum' | 'enumName' | 'enumSchema', any>>
) {
const paramSchema: SchemaObject = paramDefinition.schema || {};
paramDefinition.schema = paramSchema;
paramSchema.type = 'array';
delete paramDefinition.isArray;
const enumValues = getEnumValues(decoratorOptions.enum);
paramSchema.items = {
type: getEnumType(enumValues),
enum: enumValues
};
if (decoratorOptions.enumName) {
paramDefinition.enumName = decoratorOptions.enumName;
}
if (decoratorOptions.enumSchema) {
paramDefinition.enumSchema = decoratorOptions.enumSchema;
}
}
export function addEnumSchema(
paramDefinition: Partial<Record<string, any>>,
decoratorOptions: Partial<Record<string, any>>
) {
const paramSchema: SchemaObject = paramDefinition.schema || {};
const enumValues = getEnumValues(decoratorOptions.enum);
paramDefinition.schema = paramSchema;
paramSchema.enum = enumValues;
paramSchema.type = getEnumType(enumValues);
if (decoratorOptions.enumName) {
paramDefinition.enumName = decoratorOptions.enumName;
}
if (decoratorOptions.enumSchema) {
paramDefinition.enumSchema = decoratorOptions.enumSchema;
}
}
export const isEnumArray = <T extends Partial<Record<'isArray' | 'enum', any>>>(
obj: Record<string, any>
): obj is T => obj.isArray && obj.enum;
export const isEnumDefined = <T extends Partial<Record<'enum', any>>>(
obj: Record<string, any>
): obj is T => obj.enum;
export const isEnumMetadata = (metadata: SchemaObjectMetadata) =>
metadata.enum || (metadata.isArray && metadata.items?.['enum']);