/
openapi.spec.loader.ts
124 lines (114 loc) 路 3.62 KB
/
openapi.spec.loader.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
import * as _ from 'lodash';
import { OpenAPIFramework } from './index';
import {
OpenAPIFrameworkAPIContext,
OpenAPIV3,
OpenAPIFrameworkArgs,
} from './types';
export interface Spec {
apiDoc: OpenAPIV3.Document;
basePaths: string[];
routes: RouteMetadata[];
}
export interface RouteMetadata {
expressRoute: string;
openApiRoute: string;
method: string;
pathParams: string[];
schema: OpenAPIV3.OperationObject;
}
interface DiscoveredRoutes {
apiDoc: OpenAPIV3.Document;
basePaths: string[];
routes: RouteMetadata[];
}
export class OpenApiSpecLoader {
private readonly framework: OpenAPIFramework;
constructor(opts: OpenAPIFrameworkArgs) {
this.framework = new OpenAPIFramework(opts);
}
public async load(): Promise<Spec> {
return this.discoverRoutes();
}
public loadSync(): Spec {
const discoverRoutesSync = () => {
let savedError,
savedResult: Spec,
done = false;
const discoverRoutes = require('util').callbackify(
this.discoverRoutes.bind(this),
);
// const discoverRoutes: any = this.discoverRoutes.bind(this);
discoverRoutes((error, result) => {
savedError = error;
savedResult = result;
done = true;
});
// Deasync should be used here any nowhere else!
// it is an optional peer dep
// Only necessary for those looking to use a blocking
// intial openapi parse to resolve json-schema-refs
require('deasync').loopWhile(() => !done);
if (savedError) throw savedError;
return savedResult;
};
return discoverRoutesSync();
}
private async discoverRoutes(): Promise<DiscoveredRoutes> {
const routes: RouteMetadata[] = [];
const toExpressParams = this.toExpressParams;
// const basePaths = this.framework.basePaths;
// let apiDoc: OpenAPIV3.Document = null;
// let basePaths: string[] = null;
const { apiDoc, basePaths } = await this.framework.initialize({
visitApi(ctx: OpenAPIFrameworkAPIContext) {
const apiDoc = ctx.getApiDoc();
const basePaths = ctx.basePaths;
for (const bpa of basePaths) {
const bp = bpa.replace(/\/$/, '');
for (const [path, methods] of Object.entries(apiDoc.paths)) {
for (const [method, schema] of Object.entries(methods)) {
if (['parameters', 'summary', 'description'].includes(method)) {
continue;
}
const schemaParameters = new Set();
(schema.parameters || []).forEach(parameter =>
schemaParameters.add(parameter),
);
(methods.parameters || []).forEach(parameter =>
schemaParameters.add(parameter),
);
schema.parameters = Array.from(schemaParameters);
const pathParams = new Set<string>();
for (const param of schema.parameters) {
if (param.in === 'path') {
pathParams.add(param.name);
}
}
const openApiRoute = `${bp}${path}`;
const expressRoute = `${openApiRoute}`
.split('/')
.map(toExpressParams)
.join('/');
routes.push({
expressRoute,
openApiRoute,
method: method.toUpperCase(),
pathParams: Array.from(pathParams),
schema,
});
}
}
}
},
});
return {
apiDoc,
basePaths,
routes,
};
}
private toExpressParams(part: string): string {
return part.replace(/\{([^}]+)}/g, ':$1');
}
}