-
Notifications
You must be signed in to change notification settings - Fork 69
/
openapi-to-har.js
555 lines (512 loc) · 15.9 KB
/
openapi-to-har.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* Translates given OpenAPI document to an array of HTTP Archive (HAR) 1.2 Request Object.
* See more:
* - http://swagger.io/specification/
* - http://www.softwareishard.com/blog/har-12-spec/#request
*
* Example HAR Request Object:
* "request": {
* "method": "GET",
* "url": "http://www.example.com/path/?param=value",
* "httpVersion": "HTTP/1.1",
* "cookies": [],
* "headers": [],
* "queryString" : [],
* "postData" : {},
* "headersSize" : 150,
* "bodySize" : 0,
* "comment" : ""
* }
*/
const OpenAPISampler = require('openapi-sampler');
/**
* Create HAR Request object for path and method pair described in given OpenAPI
* document.
*
* @param {Object} openApi OpenAPI document
* @param {string} path Key of the path
* @param {string} method Key of the method
* @param {Object} queryParamValues Optional: Values for the query parameters if present
* @return {array} List of HAR Request objects for the endpoint
*/
const createHar = function (openApi, path, method, queryParamValues) {
// if the operational parameter is not provided, set it to empty object
if (typeof queryParamValues === 'undefined') {
queryParamValues = {};
}
const baseUrl = getBaseUrl(openApi, path, method);
const baseHar = {
method: method.toUpperCase(),
url: baseUrl + getFullPath(openApi, path, method),
headers: getHeadersArray(openApi, path, method),
queryString: getQueryStrings(openApi, path, method, queryParamValues),
httpVersion: 'HTTP/1.1',
cookies: [],
headersSize: 0,
bodySize: 0,
};
let hars = [];
// get payload data, if available:
const postDatas = getPayloads(openApi, path, method);
// For each postData create a snippet
if (postDatas.length > 0) {
for (let i in postDatas) {
const postData = postDatas[i];
const copiedHar = JSON.parse(JSON.stringify(baseHar));
copiedHar.postData = postData;
copiedHar.comment = postData.mimeType;
copiedHar.headers.push({
name: 'content-type',
value: postData.mimeType,
});
hars.push(copiedHar);
}
} else {
hars = [baseHar];
}
return hars;
};
/**
* Get the payload definition for the given endpoint (path + method) from the
* given OAI specification. References within the payload definition are
* resolved.
*
* @param {object} openApi
* @param {string} path
* @param {string} method
* @return {array} A list of payload objects
*/
const getPayloads = function (openApi, path, method) {
if (typeof openApi.paths[path][method].parameters !== 'undefined') {
for (let i in openApi.paths[path][method].parameters) {
const param = openApi.paths[path][method].parameters[i];
if (
typeof param.in !== 'undefined' &&
param.in.toLowerCase() === 'body' &&
typeof param.schema !== 'undefined'
) {
try {
const sample = OpenAPISampler.sample(
param.schema,
{ skipReadOnly: true },
openApi
);
return [
{
mimeType: 'application/json',
text: JSON.stringify(sample),
},
];
} catch (err) {
console.log(err);
return null;
}
}
}
}
if (
openApi.paths[path][method].requestBody &&
openApi.paths[path][method].requestBody['$ref']
) {
openApi.paths[path][method].requestBody = resolveRef(
openApi,
openApi.paths[path][method].requestBody['$ref']
);
}
const payloads = [];
if (
openApi.paths[path][method].requestBody &&
openApi.paths[path][method].requestBody.content
) {
[
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data',
].forEach((type) => {
const content = openApi.paths[path][method].requestBody.content[type];
if (content && content.schema) {
const sample = OpenAPISampler.sample(
content.schema,
{ skipReadOnly: true },
openApi
);
if (type === 'application/json') {
payloads.push({
mimeType: type,
text: JSON.stringify(sample),
});
} else if (type === 'multipart/form-data') {
if (sample !== undefined) {
const params = [];
Object.keys(sample).forEach((key) => {
let value = sample[key];
if (typeof sample[key] !== 'string') {
value = JSON.stringify(sample[key]);
}
params.push({ name: key, value: value });
});
payloads.push({
mimeType: type,
params: params,
});
}
} else if (type == 'application/x-www-form-urlencoded') {
if (sample === undefined) return null;
const params = [];
Object.keys(sample).map((key) =>
params.push({
name: encodeURIComponent(key).replace(/\%20/g, '+'),
value: encodeURIComponent(sample[key]).replace(/\%20/g, '+'),
})
);
payloads.push({
mimeType: 'application/x-www-form-urlencoded',
params: params,
text: Object.keys(params)
.map((key) => key + '=' + sample[key])
.join('&'),
});
}
}
});
}
return payloads;
};
/**
* Gets the base URL constructed from the given openApi.
*
* @param {Object} openApi OpenAPI document
* @return {string} Base URL
*/
const getBaseUrl = function (openApi, path, method) {
if (openApi.paths[path][method].servers)
return openApi.paths[path][method].servers[0].url;
if (openApi.paths[path].servers) return openApi.paths[path].servers[0].url;
if (openApi.servers) return openApi.servers[0].url;
let baseUrl = '';
if (typeof openApi.schemes !== 'undefined') {
baseUrl += openApi.schemes[0];
} else {
baseUrl += 'http';
}
if (openApi.basePath === '/') {
baseUrl += '://' + openApi.host;
} else {
baseUrl += '://' + openApi.host + openApi.basePath;
}
return baseUrl;
};
/**
* Gets an object describing the the paremeters (header or query) in a given OpenAPI method
* @param {Object} param parameter values to use in snippet
* @param {Object} values Optional: query parameter values to use in the snippet if present
* @return {Object} Object describing the parameters in a given OpenAPI method or path
*/
const getParameterValues = function (param, values) {
let value =
'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE';
if (values && typeof values[param.name] !== 'undefined') {
value =
values[param.name] + ''; /* adding a empty string to convert to string */
} else if (typeof param.default !== 'undefined') {
value = param.default + '';
} else if (
typeof param.schema !== 'undefined' &&
typeof param.schema.example !== 'undefined'
) {
value = param.schema.example + '';
} else if (typeof param.example !== 'undefined') {
value = param.example + '';
}
return {
name: param.name,
value: value,
};
};
/**
* Parse parameter object into query string objects
*
* @param {Object} openApi OpenApi document
* @param {Object} parameters Objects described in the document to parse into the query string
* @param {Object} values Optional: query parameter values to use in the snippet if present
* @return {Object} Object describing the parameters for a method or path
*/
const parseParametersToQuery = function (openApi, parameters, values) {
const queryStrings = {};
for (let i in parameters) {
let param = parameters[i];
if (typeof param['$ref'] === 'string' && /^#/.test(param['$ref'])) {
param = resolveRef(openApi, param['$ref']);
}
if (typeof param.schema !== 'undefined') {
if (
typeof param.schema['$ref'] === 'string' &&
/^#/.test(param.schema['$ref'])
) {
param.schema = resolveRef(openApi, param.schema['$ref']);
if (typeof param.schema.type === 'undefined') {
// many schemas don't have an explicit type
param.schema.type = 'object';
}
}
}
if (typeof param.in !== 'undefined' && param.in.toLowerCase() === 'query') {
// param.name is a safe key, because the spec defines
// that name MUST be unique
queryStrings[param.name] = getParameterValues(param, values);
}
}
return queryStrings;
};
/**
* Get array of objects describing the query parameters for a path and method
* pair described in the given OpenAPI document.
*
* @param {Object} openApi OpenApi document
* @param {string} path Key of the path
* @param {string} method Key of the method
* @param {Object} values Optional: query parameter values to use in the snippet if present
* @return {array} List of objects describing the query strings
*/
const getQueryStrings = function (openApi, path, method, values) {
// Set the optional parameter if it's not provided
if (typeof values === 'undefined') {
values = {};
}
let pathQueryStrings = {};
let methodQueryStrings = {};
// First get any parameters from the path
if (typeof openApi.paths[path].parameters !== 'undefined') {
pathQueryStrings = parseParametersToQuery(
openApi,
openApi.paths[path].parameters,
values
);
}
if (typeof openApi.paths[path][method].parameters !== 'undefined') {
methodQueryStrings = parseParametersToQuery(
openApi,
openApi.paths[path][method].parameters,
values
);
}
// Merge query strings, with method overriding path
// from the spec:
// If a parameter is already defined at the Path Item, the new definition will override
// it but can never remove it.
// https://swagger.io/specification/
const queryStrings = Object.assign(pathQueryStrings, methodQueryStrings);
return Object.values(queryStrings);
};
/**
* Return the path with the parameters example values used if specified.
*
* @param {Object} openApi OpenApi document
* @param {string} path Key of the path
* @param {string} method Key of the method
* @return {string} Full path including example values
*/
const getFullPath = function (openApi, path, method) {
let fullPath = path;
const parameters =
openApi.paths[path].parameters || openApi.paths[path][method].parameters;
if (typeof parameters !== 'undefined') {
for (let i in parameters) {
let param = parameters[i];
if (typeof param['$ref'] === 'string' && /^#/.test(param['$ref'])) {
param = resolveRef(openApi, param['$ref']);
}
if (
typeof param.in !== 'undefined' &&
param.in.toLowerCase() === 'path'
) {
if (typeof param.example !== 'undefined') {
// only if the schema has an example value
fullPath = fullPath.replace('{' + param.name + '}', param.example);
}
}
}
}
return fullPath;
};
/**
* Get an array of objects describing the header for a path and method pair
* described in the given OpenAPI document.
*
* @param {Object} openApi OpenAPI document
* @param {string} path Key of the path
* @param {string} method Key of the method
* @return {array} List of objects describing the header
*/
const getHeadersArray = function (openApi, path, method) {
const headers = [];
const pathObj = openApi.paths[path][method];
// 'accept' header:
if (typeof pathObj.consumes !== 'undefined') {
for (let i in pathObj.consumes) {
const type = pathObj.consumes[i];
headers.push({
name: 'accept',
value: type,
});
}
}
// headers defined in path object:
if (typeof pathObj.parameters !== 'undefined') {
for (let k in pathObj.parameters) {
const param = pathObj.parameters[k];
if (
typeof param.in !== 'undefined' &&
param.in.toLowerCase() === 'header'
) {
headers.push(getParameterValues(param));
}
}
}
// security:
let basicAuthDef;
let apiKeyAuthDef;
let oauthDef;
if (typeof pathObj.security !== 'undefined') {
for (var l in pathObj.security) {
const secScheme = Object.keys(pathObj.security[l])[0];
const secDefinition = openApi.securityDefinitions
? openApi.securityDefinitions[secScheme]
: openApi.components.securitySchemes[secScheme];
const authType = secDefinition.type.toLowerCase();
let authScheme = null;
if (authType !== 'apikey' && secDefinition.scheme != null) {
authScheme = secDefinition.scheme.toLowerCase();
}
switch (authType) {
case 'basic':
basicAuthDef = secScheme;
break;
case 'apikey':
if (secDefinition.in === 'header') {
apiKeyAuthDef = secDefinition;
}
break;
case 'oauth2':
oauthDef = secScheme;
break;
case 'http':
switch (authScheme) {
case 'bearer':
oauthDef = secScheme;
break;
case 'basic':
basicAuthDef = secScheme;
break;
}
break;
}
}
} else if (typeof openApi.security !== 'undefined') {
// Need to check OAS 3.0 spec about type http and scheme
for (let m in openApi.security) {
const secScheme = Object.keys(openApi.security[m])[0];
const secDefinition = openApi.components.securitySchemes[secScheme];
const authType = secDefinition.type.toLowerCase();
let authScheme = null;
if (authType !== 'apikey' && authType !== 'oauth2') {
authScheme = secDefinition.scheme.toLowerCase();
}
switch (authType) {
case 'http':
switch (authScheme) {
case 'bearer':
oauthDef = secScheme;
break;
case 'basic':
basicAuthDef = secScheme;
break;
}
break;
case 'basic':
basicAuthDef = secScheme;
break;
case 'apikey':
if (secDefinition.in === 'header') {
apiKeyAuthDef = secDefinition;
}
break;
case 'oauth2':
oauthDef = secScheme;
break;
}
}
}
if (basicAuthDef) {
headers.push({
name: 'Authorization',
value: 'Basic ' + 'REPLACE_BASIC_AUTH',
});
} else if (apiKeyAuthDef) {
headers.push({
name: apiKeyAuthDef.name,
value: 'REPLACE_KEY_VALUE',
});
} else if (oauthDef) {
headers.push({
name: 'Authorization',
value: 'Bearer ' + 'REPLACE_BEARER_TOKEN',
});
}
return headers;
};
/**
* Produces array of HAR files for given OpenAPI document
*
* @param {object} openApi OpenAPI document
* @param {Function} callback
*/
const openApiToHarList = function (openApi) {
try {
// iterate openApi and create har objects:
const harList = [];
for (let path in openApi.paths) {
for (let method in openApi.paths[path]) {
const url = getBaseUrl(openApi, path, method) + path;
const hars = createHar(openApi, path, method);
// need to push multiple here
harList.push({
method: method.toUpperCase(),
url: url,
description:
openApi.paths[path][method].description ||
'No description available',
hars: hars,
});
}
}
return harList;
} catch (e) {
console.log(e);
}
};
/**
* Returns the value referenced in the given reference string
*
* @param {object} openApi OpenAPI document
* @param {string} ref A reference string
* @return {any}
*/
const resolveRef = function (openApi, ref) {
const parts = ref.split('/');
if (parts.length <= 1) return {}; // = 3
const recursive = function (obj, index) {
if (index + 1 < parts.length) {
// index = 1
let newCount = index + 1;
return recursive(obj[parts[index]], newCount);
} else {
return obj[parts[index]];
}
};
return recursive(openApi, 1);
};
module.exports = {
getAll: openApiToHarList,
getEndpoint: createHar,
};