-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
metadata.ts
457 lines (401 loc) · 11.9 KB
/
metadata.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/core
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import * as assert from 'assert';
import {Reflector} from '@loopback/context';
import {
OperationObject,
ParameterLocation,
ParameterObject,
SchemaObject,
ParameterTypeValue,
createEmptyApiSpec,
PathsObject,
} from '@loopback/openapi-spec';
const debug = require('debug')('loopback:core:router:metadata');
// tslint:disable:no-any
export interface ControllerSpec {
/**
* The base path on which the Controller API is served.
* If it is not included, the API is served directly under the host.
* The value MUST start with a leading slash (/).
*/
basePath?: string;
/**
* The available paths and operations for the API.
*/
paths: PathsObject;
}
/**
* Decorate the given Controller constructor with metadata describing
* the HTTP/REST API the Controller implements/provides.
*
* @param spec OpenAPI specification describing the endpoints
* handled by this controller
*
* @decorator
*/
export function api(spec: ControllerSpec) {
return function(constructor: Function) {
assert(
typeof constructor === 'function',
'The @api decorator can be applied to constructors only.',
);
Reflector.defineMetadata('loopback:api-spec', spec, constructor);
};
}
interface RestEndpoint {
verb: string;
path: string;
}
export function getControllerSpec(constructor: Function): ControllerSpec {
debug(`Retrieving OpenAPI specification for controller ${constructor.name}`);
let spec: ControllerSpec = Reflector.getMetadata(
'loopback:api-spec',
constructor,
);
if (spec) {
debug(' using class-level spec defined via @api()', spec);
return spec;
}
spec = {paths: {}};
for (
let proto = constructor.prototype;
proto && proto !== Object.prototype;
proto = Object.getPrototypeOf(proto)
) {
addPrototypeMethodsToSpec(spec, proto);
}
return spec;
}
function addPrototypeMethodsToSpec(spec: ControllerSpec, proto: any) {
const controllerMethods = Object.getOwnPropertyNames(proto).filter(
key => key !== 'constructor' && typeof proto[key] === 'function',
);
for (const methodName of controllerMethods) {
addControllerMethodToSpec(spec, proto, methodName);
}
}
function addControllerMethodToSpec(
spec: ControllerSpec,
proto: any,
methodName: string,
) {
const className = proto.constructor.name || '<UnknownClass>';
const fullMethodName = `${className}.${methodName}`;
const endpoint: RestEndpoint = Reflector.getMetadata(
'loopback:operation-endpoint',
proto,
methodName,
);
if (!endpoint) {
debug(` skipping ${fullMethodName} - no endpoint is defined`);
return;
}
const {verb, path} = endpoint;
const endpointName = `${fullMethodName} (${verb} ${path})`;
let operationSpec = Reflector.getMetadata(
'loopback:operation-spec',
proto,
methodName,
);
if (!operationSpec) {
// The operation was defined via @operation(verb, path) with no spec
operationSpec = {
responses: {},
};
}
if (!spec.paths[path]) {
spec.paths[path] = {};
}
if (spec.paths[path][verb]) {
debug(` skipping ${endpointName} - endpoint was already defined`);
return;
}
debug(` adding ${endpointName}`, operationSpec);
spec.paths[path][verb] = Object.assign({}, operationSpec, {
'x-operation-name': methodName,
});
}
/**
* Expose a Controller method as a REST API operation
* mapped to `GET` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function get(path: string, spec?: OperationObject) {
return operation('get', path, spec);
}
/**
* Expose a Controller method as a REST API operation
* mapped to `POST` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function post(path: string, spec?: OperationObject) {
return operation('post', path, spec);
}
/**
* Expose a Controller method as a REST API operation
* mapped to `PUT` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function put(path: string, spec?: OperationObject) {
return operation('put', path, spec);
}
/**
* Expose a Controller method as a REST API operation
* mapped to `PATCH` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function patch(path: string, spec?: OperationObject) {
return operation('patch', path, spec);
}
/**
* Expose a Controller method as a REST API operation
* mapped to `DELETE` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function del(path: string, spec?: OperationObject) {
return operation('delete', path, spec);
}
/**
* Expose a Controller method as a REST API operation.
*
* @param verb HTTP verb, e.g. `GET` or `POST`.
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function operation(verb: string, path: string, spec?: OperationObject) {
// tslint:disable-next-line:no-any
return function(
target: object,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
const endpoint: RestEndpoint = {verb, path};
Reflector.defineMetadata(
'loopback:operation-endpoint',
endpoint,
target,
propertyKey,
);
if (!spec) {
// Users can define parameters and responses using decorators
return;
}
// Decorator are invoked in reverse order of their definition.
// For example, a method decorated with @operation() @param()
// will invoke param() decorator first and operation() second.
// As a result, we need to preserve any partial definitions
// already provided by other decorators.
editOperationSpec(target, propertyKey, overrides => {
const mergedSpec = Object.assign({}, spec, overrides);
// Merge "responses" definitions
mergedSpec.responses = Object.assign(
{},
spec.responses,
overrides.responses,
);
return mergedSpec;
});
};
}
/**
* Describe an input parameter of a Controller method.
*
* @param paramSpec Parameter specification.
*/
export function param(paramSpec: ParameterObject) {
return function(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
assert(
typeof target[propertyKey] === 'function',
'@param decorator can be applied to methods only',
);
editOperationSpec(target, propertyKey, operationSpec => {
if (!operationSpec.parameters) {
operationSpec.parameters = [];
}
operationSpec.parameters.unshift(paramSpec);
return operationSpec;
});
};
}
function editOperationSpec(
target: any,
propertyKey: string,
updateFn: (spec: OperationObject) => OperationObject,
) {
let spec: OperationObject = Reflector.getMetadata(
'loopback:operation-spec',
target,
propertyKey,
);
if (!spec) {
spec = {
responses: {},
};
}
spec = updateFn(spec);
Reflector.defineMetadata(
'loopback:operation-spec',
spec,
target,
propertyKey,
);
}
export namespace param {
export const query = {
/**
* Define a parameter of "string" type that's read from the query string.
*
* @param name Parameter name.
*/
string: createParamShortcut('query', 'string'),
/**
* Define a parameter of "number" type that's read from the query string.
*
* @param name Parameter name.
*/
number: createParamShortcut('query', 'number'),
/**
* Define a parameter of "integer" type that's read from the query string.
*
* @param name Parameter name.
*/
integer: createParamShortcut('query', 'integer'),
/**
* Define a parameter of "boolean" type that's read from the query string.
*
* @param name Parameter name.
*/
boolean: createParamShortcut('query', 'boolean'),
};
export const header = {
/**
* Define a parameter of "string" type that's read from a request header.
*
* @param name Parameter name, it must match the header name
* (e.g. `Content-Type`).
*/
string: createParamShortcut('header', 'string'),
/**
* Define a parameter of "number" type that's read from a request header.
*
* @param name Parameter name, it must match the header name
* (e.g. `Content-Length`).
*/
number: createParamShortcut('header', 'number'),
/**
* Define a parameter of "integer" type that's read from a request header.
*
* @param name Parameter name, it must match the header name
* (e.g. `Content-Length`).
*/
integer: createParamShortcut('header', 'integer'),
/**
* Define a parameter of "boolean" type that's read from a request header.
*
* @param name Parameter name, it must match the header name,
* (e.g. `DNT` or `X-Do-Not-Track`).
*/
boolean: createParamShortcut('header', 'boolean'),
};
export const path = {
/**
* Define a parameter of "string" type that's read from request path.
*
* @param name Parameter name matching one of the placeholders in the path
* string.
*/
string: createParamShortcut('path', 'string'),
/**
* Define a parameter of "number" type that's read from request path.
*
* @param name Parameter name matching one of the placeholders in the path
* string.
*/
number: createParamShortcut('path', 'number'),
/**
* Define a parameter of "integer" type that's read from request path.
*
* @param name Parameter name matching one of the placeholders in the path
* string.
*/
integer: createParamShortcut('path', 'integer'),
/**
* Define a parameter of "boolean" type that's read from request path.
*
* @param name Parameter name matching one of the placeholders in the path
* string.
*/
boolean: createParamShortcut('path', 'boolean'),
};
export const formData = {
/**
* Define a parameter of "string" type that's read
* from a field in the request body.
*
* @param name Parameter name.
*/
string: createParamShortcut('formData', 'string'),
/**
* Define a parameter of "number" type that's read
* from a field in the request body.
*
* @param name Parameter name.
*/
number: createParamShortcut('formData', 'number'),
/**
* Define a parameter of "integer" type that's read
* from a field in the request body.
*
* @param name Parameter name.
*/
integer: createParamShortcut('formData', 'integer'),
/**
* Define a parameter of "boolean" type that's read
* from a field in the request body.
*
* @param name Parameter name.
*/
boolean: createParamShortcut('formData', 'boolean'),
};
/**
* Define a parameter that's set to the full request body.
*
* @param name Parameter name
* @param schema The schema defining the type used for the body parameter.
*/
export const body = function(name: string, schema: SchemaObject) {
return param({name, in: 'body', schema});
};
}
function createParamShortcut(
source: ParameterLocation,
type: ParameterTypeValue,
) {
// TODO(bajtos) @param.IN.TYPE('foo', {required: true})
return (name: string) => {
return param({name, in: source, type});
};
}