forked from ErikWittern/openapi-snippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi-to-har.js
394 lines (362 loc) · 11.3 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
/**
* 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 {Object} HAR Request object
*/
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)
const har = {
method: method.toUpperCase(),
url: baseUrl + path,
headers: getHeadersArray(openApi, path, method),
queryString: getQueryStrings(openApi, path, method, queryParamValues),
httpVersion: 'HTTP/1.1',
cookies: [],
headersSize: 0,
bodySize: 0
}
// get payload data, if available:
const postData = getPayload(openApi, path, method)
if (postData) har.postData = postData
return har
}
/**
* 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 {object}
*/
const getPayload = 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.content &&
openApi.paths[path][method].requestBody.content['application/json'] &&
openApi.paths[path][method].requestBody.content['application/json'].schema) {
const sample = OpenAPISampler.sample(openApi.paths[path][method].requestBody.content['application/json'].schema, {skipReadOnly: true}, openApi)
return {
mimeType: 'application/json',
text: JSON.stringify(sample)
}
}
return null
}
/**
* Gets the base URL constructed from the given openApi.
*
* @param {Object} openApi OpenAPI document
* @return {string} Base URL
*/
const getBaseUrl = function (openApi) {
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
}
/**
* 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 = {}
}
const queryStrings = []
if (typeof openApi.paths[path][method].parameters !== 'undefined') {
for (let i in openApi.paths[path][method].parameters) {
let param = openApi.paths[path][method].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') {
let value = 'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE'
if (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 + ''
}
queryStrings.push({
name: param.name,
value: value
})
}
}
}
return queryStrings
}
/**
* 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
})
}
}
// 'content-type' header:
if (typeof pathObj.produces !== 'undefined') {
for (let j in pathObj.produces) {
const type2 = pathObj.produces[j]
headers.push({
name: 'content-type',
value: type2
})
}
}
// v3 'content-type' header:
if (pathObj.requestBody && pathObj.requestBody.content) {
for (const type3 of Object.keys(pathObj.requestBody.content)) {
headers.push({
name: 'content-type',
value: type3
});
}
}
// 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({
name: param.name,
value: 'SOME_' + (param.type||param.schema.type).toUpperCase() + '_VALUE'
})
}
}
}
// 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'){
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 {
// determine basePath:
const baseUrl = getBaseUrl(openApi)
// iterate openApi and create har objects:
const harList = []
for (let path in openApi.paths) {
for (let method in openApi.paths[path]) {
const url = baseUrl + path
const har = createHar(openApi, path, method)
harList.push({
method: method.toUpperCase(),
url: url,
description: openApi.paths[path][method].description || 'No description available',
har: har
})
}
}
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
}