/
openapi.metadata.ts
56 lines (50 loc) 路 1.8 KB
/
openapi.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
const pathToRegexp = require('path-to-regexp');
const _ = require('lodash');
import { OpenApiContext } from '../openapi.context';
export function applyOpenApiMetadata(openApiContext: OpenApiContext) {
return (req, res, next) => {
req.openapi = {};
console.log('applyOpenApiMetadata: applying metadata to request', req.path);
const matched = matchRoute(req);
if (matched) {
const { expressRoute, openApiRoute, pathParams, schema } = matched;
console.log('applyOpenApiMetadata', expressRoute, openApiRoute);
req.openapi.expressRoute = expressRoute;
req.openapi.openApiRoute = openApiRoute;
req.openapi.pathParams = pathParams;
req.openapi.schema = schema;
req.params = pathParams;
}
next();
};
function matchRoute(req) {
const path = req.path;
const method = req.method;
for (const [expressRoute, methods] of Object.entries(
openApiContext.expressRouteMap
)) {
const schema = methods[method];
const routePair = openApiContext.routePair(expressRoute);
const openApiRoute = routePair.openApiRoute;
const keys = [];
const regexp = pathToRegexp(expressRoute, keys);
const matchedRoute = regexp.exec(path);
if (matchedRoute) {
console.log('core_mw: matchRoute', matchedRoute);
console.log('core_mw: matchRoute:keys', keys);
const paramKeys = keys.map(k => k.name);
const paramsVals = matchedRoute.slice(1);
const pathParams = _.zipObject(paramKeys, paramsVals);
console.log('core_mw: create params', pathParams);
return {
schema,
// schema may or may not contain express and openApi routes, so include them here
expressRoute,
openApiRoute,
pathParams,
};
}
}
return null;
}
}