-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-vars-to-x-ms-parameterized-host.js
247 lines (212 loc) · 6.33 KB
/
server-vars-to-x-ms-parameterized-host.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* @copyright Copyright 2019-2020 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/server-vars-to-x-ms-parameterized-host.js"
*/
import OpenApiTransformerBase from 'openapi-transformer-base';
import visit from 'openapi-transformer-base/visit.js';
/** Gets the scheme and authority portions of a URL template.
*
* @private
*/
function getSchemeAuthority(url) {
// Note: parameterized host may contain host and/or schema parameters.
// A single parameter may contain both schema and host.
// No way to disambiguate url like {schema}{host}{pathPrefix}.
// Assume the scheme+authority parts end at first slash following //
// (if present). (Note: protocol-relative url may start with //)
const match =
/^(?:(?:\{[^{}]*\}|[^/{}])*\/\/)?(?:\{[^{}]*\}|[^/{}])+/.exec(url);
return match && match[0];
}
function parseServerUrl(url) {
if (typeof url !== 'string') {
this.warn('Ignoring Server with non-string url', url);
return undefined;
}
const schemeAuth = getSchemeAuthority(url);
if (!schemeAuth) {
this.warn('Unable to determine scheme and authority for url', url);
return undefined;
}
// Remove non-templated scheme, if present
const hostTemplate = schemeAuth.replace(/^[a-zA-Z_+.-]+:\/\//, '');
if (!hostTemplate) {
this.warn('Unable to determine host for url', url);
return undefined;
}
const hostTemplateExprs = hostTemplate.match(/\{[^{}]*\}/g);
if (!hostTemplateExprs) {
this.warn('No template expressions in scheme and authority for url', url);
return undefined;
}
return {
hostTemplate,
// If non-templated scheme was removed, use the declared schemes
useSchemePrefix: hostTemplate !== schemeAuth,
hostTemplateVarNames: hostTemplateExprs
.map((templateExpr) => templateExpr.slice(1, -1)),
};
}
/**
* Transformer to convert Server Variables in host portion to
* x-ms-parameterized-host for use with OpenAPI 2.
*
* https://github.com/Azure/autorest/tree/main/docs/extensions#x-ms-parameterized-host
*
* TODO: Roll this into OpenAPI 3 -> 2 conversion?
*
* Note: Currently expects OAS3 input (required for Server Objects) but
* produces OAS2 parameters as specified for x-ms-parameterized-host.
*/
export default class ServerVarsToParamHostTransformer
extends OpenApiTransformerBase {
constructor({ omitDefault, parameter, xMsParameterizedHost } = {}) {
super();
if (omitDefault !== undefined && !Array.isArray(omitDefault)) {
throw new TypeError('options.omitDefault must be an Array');
}
if (parameter !== undefined
&& (parameter === null
|| typeof parameter !== 'object')) {
throw new TypeError('options.parameter must be a object');
}
if (xMsParameterizedHost !== undefined
&& (xMsParameterizedHost === null
|| typeof xMsParameterizedHost !== 'object')) {
throw new TypeError('options.xMsParameterizedHost must be a object');
}
this.options = {
omitDefault: omitDefault || [],
parameter,
xMsParameterizedHost,
};
}
transformServerVariableToParameter(serverVariable) {
if (serverVariable === null
|| typeof serverVariable !== 'object'
|| Array.isArray(serverVariable)) {
this.warn('Ignoring non-object Server Variable', serverVariable);
return undefined;
}
return {
name: undefined,
type: 'string',
...serverVariable,
in: 'path',
required: true,
...this.options.parameter,
};
}
transformServerVariablesToParameters(variables, varNames) {
if (variables === null
|| typeof variables !== 'object'
|| Array.isArray(variables)) {
this.warn('Ignoring non-object Map', variables);
return undefined;
}
const parameters = [];
for (const varName of varNames) {
const variable =
Object.hasOwn(variables, varName) ? variables[varName]
: undefined;
if (variable === undefined) {
this.warn('Unable to convert Server missing variable %j', varName);
return undefined;
}
const parameter = visit(
this,
this.transformServerVariableToParameter,
varName,
variable,
);
if (!parameter) {
return undefined;
}
parameter.name = varName;
if (this.options.omitDefault.includes(varName)
&& Object.hasOwn(parameter, 'default')) {
delete parameter.default;
}
parameters.push(parameter);
}
return parameters;
}
transformServerToParameterizedHost(server) {
if (server === null
|| typeof server !== 'object'
|| Array.isArray(server)) {
this.warn('Ignoring non-object Server', server);
return undefined;
}
const { url, variables = {} } = server;
const urlParts = visit(this, parseServerUrl, 'url', url);
if (!urlParts) {
return undefined;
}
const {
hostTemplate,
useSchemePrefix,
hostTemplateVarNames,
} = urlParts;
const parameters = visit(
this,
this.transformServerVariablesToParameters,
'variables',
variables,
hostTemplateVarNames,
);
if (!parameters) {
return undefined;
}
return {
hostTemplate,
useSchemePrefix,
parameters,
...this.options.xMsParameterizedHost,
};
}
transformServersToParameterizedHost(servers) {
if (!Array.isArray(servers)) {
this.warn('Ignoring non-Array servers', servers);
return undefined;
}
if (servers.length === 0) {
this.warn('Ignoring empty servers', servers);
return undefined;
}
if (servers.length > 1) {
this.warn('Ignoring all but first server', servers);
}
return visit(
this,
this.transformServerToParameterizedHost,
'0',
servers[0],
);
}
transformOpenApi(openApi) {
if (openApi === null
|| typeof openApi !== 'object'
|| Array.isArray(openApi)) {
this.warn('Ignoring non-object OpenAPI', openApi);
return openApi;
}
if (openApi.servers === undefined) {
return openApi;
}
const parameterizedHost = visit(
this,
this.transformServersToParameterizedHost,
'servers',
openApi.servers,
);
if (parameterizedHost === undefined) {
return openApi;
}
return {
...openApi,
'x-ms-parameterized-host': parameterizedHost,
};
}
}