-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
swagger-controller.ts
150 lines (131 loc) 路 4.19 KB
/
swagger-controller.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
// std
import { readFile } from 'fs';
import { join } from 'path';
import { promisify } from 'util';
// 3p
import {
Class, Context, createHttpResponseFile, createOpenApiDocument,
dependency, Get, HttpResponseBadRequest,
HttpResponseMovedPermanently, HttpResponseNotFound, HttpResponseOK, ServiceManager
} from '@foal/core';
import { getAbsoluteFSPath } from 'swagger-ui-dist';
function isUrlOption(option): option is { url: string } {
return option.hasOwnProperty('url');
}
/**
* Serve Swagger UI to visualize and interact with API resources.
*
* @export
* @abstract
* @class SwaggerController
*/
export abstract class SwaggerController {
@dependency
controllers: ServiceManager;
/**
* Specify the OpenAPI Specification(s) and their location(s).
*
* If a controller class is provided, then an OpenAPI Specification is generated
* from its definition.
*
* @abstract
* @type {({ url: string } |
* { controllerClass: Class } |
* (
* { name: string, url: string, primary?: boolean } |
* { name: string, controllerClass: Class, primary?: boolean }
* )[])}
* @memberof SwaggerController
*/
abstract options: { url: string } |
{ controllerClass: Class } |
(
{ name: string, url: string, primary?: boolean } |
{ name: string, controllerClass: Class, primary?: boolean }
)[];
/**
* Extend Swagger UI options.
*
* See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/.
*
* @type {object}
* @example
* uiOptions = { docExpansion: 'none' };
* @memberof SwaggerController
*/
uiOptions: object = {};
/* Spec file(s) */
@Get('/openapi.json')
getOpenApiDefinition(ctx: Context) {
if (isUrlOption(this.options)) {
return new HttpResponseNotFound();
}
if (!Array.isArray(this.options)) {
const document = createOpenApiDocument(this.options.controllerClass, this.controllers);
return new HttpResponseOK(document);
}
const name = ctx.request.query.name;
if (typeof name !== 'string') {
return new HttpResponseBadRequest('Missing URL parameter "name".');
}
const option = this.options.find(option => option.name === name);
if (!option || isUrlOption(option)) {
return new HttpResponseNotFound();
}
return new HttpResponseOK(createOpenApiDocument(option.controllerClass, this.controllers));
}
/* UI */
@Get('/')
async index(ctx: Context) {
if (!ctx.request.path.endsWith('/')) {
return new HttpResponseMovedPermanently(ctx.request.path + '/');
}
const template = await promisify(readFile)(join(__dirname, 'index.tpl.html'), 'utf8');
let body = '';
if (!Array.isArray(this.options)) {
const url = isUrlOption(this.options) ? this.options.url : 'openapi.json';
body = template
.replace('{{ urls }}', `url: "${url}"`)
.replace('{{ primaryName }}', '');
} else {
let primaryName: string = '';
const options = this.options
.map(option => {
if (option.primary) {
primaryName = `\n \'urls.primaryName\': "${option.name}",`;
}
return {
name: option.name,
url: isUrlOption(option) ? option.url : `openapi.json?name=${option.name}`
};
});
body = template
.replace('{{ urls }}', `urls: ${JSON.stringify(options)}`)
.replace('{{ primaryName }}', primaryName);
}
body = body.replace('{{ uiOptions }}', JSON.stringify(this.uiOptions));
return new HttpResponseOK(body)
.setHeader('Content-Type', 'text/html; charset=utf-8');
}
@Get('/swagger-ui.css')
swaggerUi() {
return createHttpResponseFile({
directory: getAbsoluteFSPath(),
file: 'swagger-ui.css'
});
}
@Get('/swagger-ui-bundle.js')
swaggerUiBundle() {
return createHttpResponseFile({
directory: getAbsoluteFSPath(),
file: 'swagger-ui-bundle.js'
});
}
@Get('/swagger-ui-standalone-preset.js')
swaggerUiStandalonePreset() {
return createHttpResponseFile({
directory: getAbsoluteFSPath(),
file: 'swagger-ui-standalone-preset.js'
});
}
}