-
Notifications
You must be signed in to change notification settings - Fork 568
/
api-header.decorator.ts
64 lines (58 loc) 路 1.57 KB
/
api-header.decorator.ts
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
import { DECORATORS } from '../constants';
import { SwaggerEnumType, ParameterObject, Type } from '../interfaces';
import { getEnumType, getEnumValues } from '../common/enum.utils';
import { createMixedDecorator } from './helpers';
export interface ApiHeaderOptions extends Omit<ParameterObject, 'in'> {
enum?: SwaggerEnumType;
}
const defaultHeaderOptions: Partial<ApiHeaderOptions> = {
name: '',
};
export function ApiHeader(options: ApiHeaderOptions): any {
const param = {
name: !options.name ? defaultHeaderOptions.name : options.name,
in: 'header',
description: options.description,
required: options.required,
schema: {
...(options.schema || {}),
type: 'string',
},
};
if (options.enum) {
const enumValues = getEnumValues(options.enum);
param.schema = {
enum: enumValues,
type: getEnumType(enumValues),
};
}
return (
target: object | Type,
key?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>
): any => {
if (descriptor) {
return createMixedDecorator(DECORATORS.API_HEADERS, param)(
target,
key,
descriptor
);
}
return createMixedDecorator(DECORATORS.API_HEADERS, param)(
target,
undefined,
undefined
);
};
}
export const ApiHeaders = (
headers: ApiHeaderOptions[]
): MethodDecorator & ClassDecorator => {
return (
target: object | Type,
key?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>
): any => {
headers.forEach(options => ApiHeader(options)(target, key, descriptor));
};
};