-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
mod.ts
130 lines (111 loc) · 4.2 KB
/
mod.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
import { getMetadataArgsStorage, ObjectKeyAny, AppSettings } from '../src/mod.ts';
import { writeJsonSync } from 'https://deno.land/std/fs/mod.ts';
import { MetaRoute } from '../src/models/meta-route.ts';
import { MetadataArgsStorage } from '../src/metadata/metadata.ts';
import { registerControllers } from '../src/utils/register-controllers.ts';
import { registerAreas } from '../src/utils/register-areas.ts';
import { OpenApiBuilder } from './builder/openapi-builder.ts';
import * as oa from './builder/openapi-models.ts';
/**
* For testing this builder use this editor:
* https://editor.swagger.io/
*/
// Builder OpenAPI v3.0.0
export class AlosaurOpenApiBuilder {
private classes: ObjectKeyAny[] = [];
private metadata: MetadataArgsStorage;
private routes: MetaRoute[] = [];
private builder = new OpenApiBuilder();
static create(settings: AppSettings): AlosaurOpenApiBuilder {
return new AlosaurOpenApiBuilder(settings);
}
constructor(settings: AppSettings) {
this.metadata = getMetadataArgsStorage();
registerAreas(this.metadata);
registerControllers(
this.metadata.controllers,
this.classes,
(route: MetaRoute) => {
// '/app/home/test/:id/:name/detail' => '/app/home/test/{id}/{name}/detail'
const openApiRoute: string = route.route.replace(/:[A-Za-z1-9]+/g, (m) => `{${m.substr(1)}}`);
this.builder.addPath(openApiRoute, this.getPathItem(route));
},
false,
);
}
public getSpec(): oa.OpenAPIObject {
return this.builder.getSpec();
}
public saveToFile(path: string = './openapi.json'): AlosaurOpenApiBuilder {
writeJsonSync(path, this.getSpec());
return this;
}
public print(): void {
console.log(this.builder.getSpec());
}
private getPathItem(route: MetaRoute): oa.PathItemObject {
const operation: oa.OperationObject = {
tags: [route.baseRoute],
responses: {
'200': {
description: '',
},
},
};
// @ts-ignore: Object is possibly 'null'.
operation.parameters = [] as oa.ParameterObject[];
// TODO Add all paramentrs
route.params.forEach((param) => {
// TODO reference object
switch (param.type) {
case 'query':
// @ts-ignore: Object is possibly 'null'.
operation.parameters.push({
// @ts-ignore: Object is possibly 'null'.
name: param.name,
in: 'query',
schema: { type: 'string' },
});
break;
case 'route-param':
// @ts-ignore: Object is possibly 'null'.
operation.parameters.push({
// @ts-ignore: Object is possibly 'null'.
name: param.name,
required: true,
in: 'path',
schema: { type: 'string' },
});
break;
case 'cookie':
// @ts-ignore: Object is possibly 'null'.
operation.parameters.push({
// @ts-ignore: Object is possibly 'null'.
name: param.name,
in: 'cookie',
schema: { type: 'string' },
});
break;
}
});
return {
[route.method.toLowerCase()]: operation,
};
}
public addTitle(title: string): AlosaurOpenApiBuilder {
this.builder.addTitle(title);
return this;
}
public addVersion(version: string): AlosaurOpenApiBuilder {
this.builder.addVersion(version);
return this;
}
public addDescription(description: string): AlosaurOpenApiBuilder {
this.builder.addDescription(description);
return this;
}
public addServer(server: oa.ServerObject): AlosaurOpenApiBuilder {
this.builder.addServer(server);
return this;
}
}