-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecorators.ts
265 lines (243 loc) · 6.58 KB
/
decorators.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @module
* Provides decorators to define metadata to generate your openAPI/Swagger documentation.
*/
import { BODY_TYPE_KEY, MetadataHelper, QUERY_TYPE_KEY } from './deps.ts';
import { Constructor } from './mod.ts';
import { Swagger } from "./swagger.ts";
type DecoratorFunction = (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => void;
/**
* Metadata key to store the property schema
*/
export const API_PROPERTY = 'api-property';
/**
* Decorator to define API property metadata for a class property.
*
* @param property - Optional Swagger schema to define the property metadata.
* @returns A decorator function that sets the API property metadata.
*/
export function ApiProperty(property?: Swagger.Schema): DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(API_PROPERTY, property ?? null, target, propertyKey);
};
}
/**
* Metadata key to mark a property as optional
*/
export const OPTIONAL_KEY = 'optional';
/**
* Decorator that marks a property or method as optional.
*
* @example
* ```typescript
* class Example {
* @Optional()
* optionalProperty?: string;
* }
* ```
*/
export function Optional(): DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(OPTIONAL_KEY, true, target, propertyKey);
};
}
/**
* Metadata key to store the returned type of a method
*/
export const RETURNED_TYPE_KEY = 'returntype';
/**
* Decorator to set metadata for the returned type of a method.
*
* @param returnedType - The type of the value that the method or property returns.
* @param isArray - Optional boolean indicating if the returned type is an array.
*/
export function ReturnedType(returnedType: unknown, isArray?: boolean): DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: any,
) => {
MetadataHelper.setMetadata(
RETURNED_TYPE_KEY,
{
returnedType,
isArray,
},
target,
propertyKey,
);
};
}
/**
* Decorator to indicate the body type of a request.
*
* @param type - The constructor of the type to be used as the body type.
*/
export function BodyType(type: Constructor): DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(BODY_TYPE_KEY, type, target, propertyKey);
};
}
/**
* Decorator to indicate the query type.
*
* @param type - The constructor function of the type to be set as metadata.
*/
export function QueryType(type: Constructor) : DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
MetadataHelper.setMetadata(QUERY_TYPE_KEY, type, target, propertyKey);
};
}
/**
* A constant key used to store or retrieve tags metadata.
* This key is typically used in decorators to annotate
* classes or methods with specific tags for documentation.
*/
export const TAGS_KEY = 'tags';
/**
* A decorator function to add an openAPI tag to a class or a method.
*
* @param tagName - The name of the tag to be added.
*
* @example
* ```typescript
* @Tag('exampleTag')
* class ExampleClass {
* @Tag('methodTag')
* exampleMethod() {
* // method implementation
* }
* }
* ```
*/
export function Tag(tagName: string) : DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
if (propertyKey) {
MetadataHelper.setMetadata(TAGS_KEY, tagName, target, propertyKey);
} else {
MetadataHelper.setMetadata(TAGS_KEY, tagName, target);
}
};
}
/**
* A constant key used to store or retrieve description metadata.
* This key is typically used in decorators to annotate
* classes or methods with specific description for documentation.
*/
export const DESCRIPTION_KEY = 'description';
/**
* A decorator function to add an openAPI description to a class or a method.
*
* @param description - The description.
*
* @example
* ```typescript
* class ExampleClass {
* @Description('My very cool description')
* exampleMethod() {
* // method implementation
* }
* }
* ```
*/
export function Description(description: string) : DecoratorFunction {
return (
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
if (propertyKey) {
MetadataHelper.setMetadata(DESCRIPTION_KEY, description, target, propertyKey);
} else {
MetadataHelper.setMetadata(DESCRIPTION_KEY, description, target);
}
};
}
/**
* A constant key used to store or retrieve api-security metadata.
*/
export const API_SECURITY = 'api-security';
/**
* A constant key used to store or retrieve api-security data metadata.
*/
export const API_SECURITY_DATA = 'api-security-data';
/**
* Indicate that the endpoint use basic authentication security.
*
* This function is a shorthand for applying the 'basic' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiBasicAuth(): DecoratorFunction { return ApiSecurity('basic') }
/**
* Indicate that the endpoint use api bearer auth security.
*
* This function is a shorthand for applying the 'bearer' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiBearerAuth(): DecoratorFunction { return ApiSecurity('bearer') }
/**
* Indicate that the endpoint use cookie security.
*
* This function is a shorthand for applying the 'cookie' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiCookieAuth(): DecoratorFunction { return ApiSecurity('cookie') }
/**
* Indicate that the endpoint use oauth2 security.
*
* This function is a shorthand for applying the 'oauth2' security scheme using the `ApiSecurity` decorator.
*
*/
export function ApiOAuth2(data: string[]): DecoratorFunction { return ApiSecurity('oauth2', data) }
/**
* Decorator that indicate that an endpoint use a security mechanism.
*
* @param name - The name of the security scheme.
* @param data - An optional array of strings representing the security requirements.
*
*/
export function ApiSecurity(name: string, data: string[] = []) : DecoratorFunction {
return (
// deno-lint-ignore ban-types
target: Object,
propertyKey?: string | symbol,
descriptor?: PropertyDescriptor,
) => {
if (propertyKey) {
MetadataHelper.setMetadata(API_SECURITY, {
[name]: data
}, target, propertyKey);
} else {
MetadataHelper.setMetadata(API_SECURITY, {
[name]: data
}, target);
}
};
}