-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathredoc.ts
169 lines (153 loc) · 5 KB
/
redoc.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Command, Flags, Args } from '@oclif/core';
import { URL } from 'url';
import * as fs from 'fs';
import * as path from 'path';
import * as proxy from 'koa-proxy';
import * as mount from 'koa-mount';
import * as commonFlags from '../common/flags';
import { parseDefinition, resolveDefinition } from '../common/definition';
import { startServer, createServer } from '../common/koa';
import { Document } from '@apidevtools/swagger-parser';
import { parseHeaderFlag } from '../common/utils';
import { RedocOpts, getRedocIndexHTML, serveRedoc } from '../common/redoc';
export class Redoc extends Command {
public static description = 'Start or bundle a ReDoc instance';
public static examples = [
'$ openapi redoc',
'$ openapi redoc ./openapi.yml',
'$ openapi redoc ./openapi.yml --bundle outDir',
];
public static flags = {
...commonFlags.help(),
...commonFlags.serverOpts(),
...commonFlags.servers(),
...commonFlags.inject(),
...commonFlags.excludeExt(),
...commonFlags.strip(),
...commonFlags.header(),
...commonFlags.apiRoot(),
bundle: Flags.string({
char: 'B',
description: 'bundle a static site to directory',
helpValue: 'outDir',
}),
};
public static args = {
definition: Args.string({
description: 'input definition file'
})
}
public async run() {
const { args, flags } = await this.parse(Redoc);
const { port, logger, bundle, header, root } = flags;
const definition = resolveDefinition(args.definition);
const app = createServer({ logger });
let proxyPath: string;
let documentPath: string;
let document: Document;
const openApiFile = 'openapi.json';
if (definition) {
if (definition.match('://') && !flags.server && !flags.proxy) {
// use remote definition
documentPath = definition;
} else {
// parse definition
document = await parseDefinition({
definition,
servers: flags.server,
inject: flags.inject,
excludeExt: flags?.['exclude-ext'],
strip: flags.strip,
header,
root,
});
documentPath = `./${openApiFile}`;
}
}
const redocOpts: RedocOpts = {
specUrl: documentPath,
title: document?.info?.title,
}
if (bundle) {
// bundle files to directory
const bundleDir = path.resolve(bundle);
// create a directory if one does not exist
if (!fs.existsSync(bundleDir)) {
fs.mkdirSync(bundleDir);
}
// copy openapi definition file
if (document) {
const openApiPath = path.join(bundleDir, openApiFile);
fs.writeFileSync(openApiPath, JSON.stringify(document));
this.log(`${openApiPath}`);
}
// copy redoc index.html file
const redocPath = path.join(bundleDir, 'index.html');
const redocHtml = getRedocIndexHTML(redocOpts)
fs.writeFileSync(redocPath, redocHtml);
this.log(path.join(redocPath));
} else {
if (flags.proxy) {
// set up a proxy for the api
let serverURL = null;
if (document.servers && document.servers[0]) {
serverURL = document.servers[0].url;
}
if (flags.server && typeof flags.server === 'object') {
serverURL = flags.server[0];
}
if (flags.server && typeof flags.server === 'string') {
serverURL = flags.server;
}
if (!serverURL) {
this.error('Unable to find server URL from definition, please provide a --server parameter');
}
const apiUrl = new URL(serverURL);
const proxyOpts = {
host: `${apiUrl.protocol}//${apiUrl.host}`,
map: (path: string) => {
if (flags.root) {
return `${flags.root}${path}`;
}
if (apiUrl.pathname === '/') {
return path;
}
return `${apiUrl.pathname}${path}`;
},
jar: flags.withcredentials,
};
proxyPath = '/proxy';
app.use(
mount(proxyPath, (ctx, next) => {
ctx.request.header = {
...ctx.request.header,
...parseHeaderFlag(header),
};
return proxy(proxyOpts)(ctx, next);
}),
);
document.servers = [{ url: proxyPath }, ...document.servers];
}
if (document) {
// serve the openapi file
app.use(
mount(`/${openApiFile}`, (ctx) => {
ctx.body = JSON.stringify(document);
}),
);
}
// serve swagger ui
app.use(mount('/', serveRedoc(redocOpts)));
// start server
const { port: portRunning } = await startServer({ app, port });
this.log(`Redoc running at http://localhost:${portRunning}`);
if (document) {
this.log(`OpenAPI definition at http://localhost:${portRunning}/${openApiFile}`);
}
if (proxyPath) {
this.log(`Proxy running at http://localhost:${portRunning}${proxyPath}`);
}
this.log();
}
}
}