-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-components.js
285 lines (241 loc) · 6.78 KB
/
rename-components.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @copyright Copyright 2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/rename-components.js"
*/
// Note: Undocumented functions which are part of the public API:
// https://github.com/flitbit/json-ptr/issues/29
import {
decodeUriFragmentIdentifier,
encodeUriFragmentIdentifier,
} from 'json-ptr';
import OpenApiTransformerBase from 'openapi-transformer-base';
const renameFuncsSymbol = Symbol('renameFuncs');
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#fixed-fields-6
const componentFieldNames = [
'schemas',
'responses',
'parameters',
'examples',
'requestBodies',
'headers',
'securitySchemes',
'links',
'callbacks',
'pathItems',
];
function renameProps(obj, renameFunc) {
if (!renameFunc
|| typeof obj !== 'object'
|| obj === null
|| Array.isArray(obj)) {
return obj;
}
const newObj = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = renameFunc(key);
if (Object.hasOwn(newObj, newKey)) {
throw new Error(
`New name ${newKey} for ${key} conflicts with existing name`,
);
}
newObj[newKey] = value;
}
return newObj;
}
function isJsonRef(obj) {
return obj && typeof obj.$ref === 'string';
}
function renameRefObj(obj, getPathIndex, renameFunc) {
if (!renameFunc) {
return obj;
}
const { $ref } = obj;
if ($ref[0] !== '#') {
return obj;
}
let tokens;
try {
tokens = decodeUriFragmentIdentifier($ref);
} catch {
return obj;
}
const renameInd = getPathIndex(tokens);
if (renameInd < 0 || renameInd >= tokens.length) {
return obj;
}
const name = tokens[renameInd];
const newName = renameFunc(name);
if (newName !== name) {
tokens[renameInd] = newName;
return {
...obj,
$ref: encodeUriFragmentIdentifier(tokens),
};
}
return obj;
}
/**
* Transformer to rename components.
*/
export default class RenameComponentsTransformer
extends OpenApiTransformerBase {
constructor(options) {
super();
if (typeof options !== 'object' || options === null) {
throw new TypeError('options must be an object');
}
const renameFuncs = {};
for (const componentFieldName of componentFieldNames) {
const renameFuncOrMap = options[componentFieldName];
if (typeof renameFuncOrMap === 'function') {
renameFuncs[componentFieldName] = renameFuncOrMap;
} else if (typeof renameFuncOrMap === 'object') {
const patterns = Object.entries(renameFuncOrMap)
.map(([k, v]) => [new RegExp(k, 'g'), v]);
renameFuncs[componentFieldName] = (name) => {
for (const [pat, repl] of patterns) {
if (pat.test(name)) {
return name.replace(pat, repl);
}
}
return name;
};
} else if (renameFuncOrMap !== undefined) {
throw new TypeError(`options.${componentFieldName} must be a function`);
}
}
this[renameFuncsSymbol] = renameFuncs;
}
transformExample3(example) {
if (isJsonRef(example)) {
return renameRefObj(
example,
(pp) => (pp[0] === 'components' && pp[1] === 'examples' ? 2 : -1),
this[renameFuncsSymbol].examples,
);
}
return super.transformExample3(example);
}
transformSchema(schema) {
if (isJsonRef(schema)) {
return renameRefObj(
schema,
(pp) => (pp[0] === 'components' && pp[1] === 'schemas' ? 2
: pp[0] === 'definitions' ? 1 : -1),
this[renameFuncsSymbol].schemas,
);
}
return super.transformSchema(schema);
}
transformHeader(header) {
if (isJsonRef(header)) {
return renameRefObj(
header,
(pp) => (pp[0] === 'components' && pp[1] === 'headers' ? 2 : -1),
this[renameFuncsSymbol].headers,
);
}
return super.transformHeader(header);
}
transformLink(link) {
if (isJsonRef(link)) {
return renameRefObj(
link,
(pp) => (pp[0] === 'components' && pp[1] === 'links' ? 2 : -1),
this[renameFuncsSymbol].links,
);
}
return super.transformLink(link);
}
transformResponse(response) {
if (isJsonRef(response)) {
return renameRefObj(
response,
(pp) => (pp[0] === 'components' && pp[1] === 'responses' ? 2 : -1),
this[renameFuncsSymbol].responses,
);
}
return super.transformResponse(response);
}
transformParameter(parameter) {
if (isJsonRef(parameter)) {
return renameRefObj(
parameter,
(pp) => (pp[0] === 'components' && pp[1] === 'parameters' ? 2 : -1),
this[renameFuncsSymbol].parameters,
);
}
return super.transformParameter(parameter);
}
transformCallback(callback) {
if (isJsonRef(callback)) {
return renameRefObj(
callback,
(pp) => (pp[0] === 'components' && pp[1] === 'callbacks' ? 2 : -1),
this[renameFuncsSymbol].callbacks,
);
}
return super.transformCallback(callback);
}
transformRequestBody(requestBody) {
if (isJsonRef(requestBody)) {
return renameRefObj(
requestBody,
(pp) => (pp[0] === 'components' && pp[1] === 'requestBodies' ? 2 : -1),
this[renameFuncsSymbol].requestBodies,
);
}
return super.transformRequestBody(requestBody);
}
transformSecurityRequirement(securityRequirement) {
return renameProps(
super.transformSecurityRequirement(securityRequirement),
this[renameFuncsSymbol].securitySchemes,
);
}
transformPathItem(pathItem) {
if (isJsonRef(pathItem)) {
return renameRefObj(
pathItem,
(pp) => (pp[0] === 'components' && pp[1] === 'pathItems' ? 2 : -1),
this[renameFuncsSymbol].pathItems,
);
}
return super.transformPathItem(pathItem);
}
transformComponents(components) {
components = super.transformComponents(components);
if (typeof components !== 'object'
|| components === null
|| Array.isArray(components)) {
return components;
}
const newComponents = { ...components };
for (const [propName, renameFunc]
of Object.entries(this[renameFuncsSymbol])) {
const propValue = components[propName];
if (propValue) {
newComponents[propName] = renameProps(propValue, renameFunc);
}
}
return newComponents;
}
transformOpenApi(openApi) {
openApi = super.transformOpenApi(openApi);
if (typeof openApi !== 'object'
|| openApi === null
|| Array.isArray(openApi)) {
return openApi;
}
const { definitions } = openApi;
const renameSchemas = this[renameFuncsSymbol].schemas;
if (definitions && renameSchemas) {
openApi = {
...openApi,
definitions: renameProps(definitions, renameSchemas),
};
}
return openApi;
}
}