-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathresolvers.ts
70 lines (60 loc) · 2.37 KB
/
resolvers.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
import * as path from 'path';
import { RequestHandler } from 'express';
import { RouteMetadata } from './framework/openapi.spec.loader';
import { OpenAPIV3 } from './framework/types';
const cache = {};
export function defaultResolver(
handlersPath: string,
route: RouteMetadata,
apiDoc: OpenAPIV3.Document,
): RequestHandler {
const tmpModules = {};
const { basePath, expressRoute, openApiRoute, method } = route;
const pathKey = openApiRoute.substring(basePath.length);
const schema = apiDoc.paths[pathKey][method.toLowerCase()];
const oId = schema['x-eov-operation-id'] || schema['operationId'];
const baseName = schema['x-eov-operation-handler'];
const cacheKey = `${expressRoute}-${method}-${oId}-${baseName}`;
if (cache[cacheKey]) return cache[cacheKey];
if (oId && !baseName) {
throw Error(
`found x-eov-operation-id for route ${method} - ${expressRoute}]. x-eov-operation-handler required.`,
);
}
if (!oId && baseName) {
throw Error(
`found x-eov-operation-handler for route [${method} - ${expressRoute}]. operationId or x-eov-operation-id required.`,
);
}
if (oId && baseName && typeof handlersPath === 'string') {
const modulePath = path.join(handlersPath, baseName);
if (!tmpModules[modulePath]) {
tmpModules[modulePath] = require(modulePath);
}
const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default;
if (!handler) {
throw Error(
`Could not find 'x-eov-operation-handler' with id ${oId} in module '${modulePath}'. Make sure operation '${oId}' defined in your API spec exists as a handler function (or module has a default export) in '${modulePath}'.`,
);
}
cache[cacheKey] = handler;
return handler;
}
}
export function modulePathResolver(
handlersPath: string,
route: RouteMetadata,
apiDoc: OpenAPIV3.Document,
): RequestHandler {
const pathKey = route.openApiRoute.substring(route.basePath.length);
const schema = apiDoc.paths[pathKey][route.method.toLowerCase()];
const [controller, method] = schema['operationId'].split('.');
const modulePath = path.join(handlersPath, controller);
const handler = require(modulePath);
if (handler[method] === undefined) {
throw new Error(
`Could not find a [${method}] function in ${modulePath} when trying to route [${route.method} ${route.expressRoute}].`,
);
}
return handler[method];
}