-
Notifications
You must be signed in to change notification settings - Fork 2
/
converter.ts
702 lines (630 loc) · 19.2 KB
/
converter.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
import { OpenAPIV3 } from 'openapi-types'
import toJsonSchema from 'to-json-schema'
import qs from 'query-string'
import Paw from 'types/paw'
import { PawURL } from 'utils'
type PawToOAS3 = OpenAPIV3.OperationObject & {
path: string
method: string
}
const OAUTH2_DEFAULT_LABEL = 'oauth2_auth'
/**
* @private
* @function extendToJsonSchema
* @summary
* a function that includes missing key+value that makes up a requestObject schema and
* also includes exampes based.
*
* @todo:
* - add proper typings please
* - add mechanism to detect date-time
*
* @param {Object} refSchema - a schema data
* @param {Object} reference - an object where refSchema is built from
* @returns {Object}
*/
function extendToJsonSchema(refSchema: any, reference: any): any {
const schema = { ...refSchema }
if (refSchema.type === 'object') {
const props = schema.properties
if (!props) return { ...schema }
Object.keys(props).forEach((i: string) => {
if (props[i].type === 'null' || props[i].type === 'undefined') {
props[i].nullable = true
delete props[i].type
}
if (props[i].type === 'array') {
props[i].type =
reference[i].length > 0 ? typeof reference[i][0] : 'string'
if (props[i].type === 'array') {
props[i].items = {
type: reference[i].length > 0 ? typeof reference[i][0] : 'string',
}
}
}
if (props[i]['type'] === 'integer') {
props[i].format = 'int64'
props[i].example = reference[i]
}
if (props[i].type === 'string') {
props[i].example = reference[i] || ''
delete props[i].format
}
if (props[i].type === 'object') {
extendToJsonSchema(props[i], reference[i])
}
})
}
return schema
}
/**
* @private
* @function jsonParseCheck
* @summary
* safely parse json string and or query string into objects. this utility
* fallsback to string, in which should match a `text/plain` content type.
* @param {String} str - a valid json string or not?
* @returns {Object<unknown>|String}
*/
function jsonParseCheck(str: string): any {
const isQueryString = new RegExp(
/^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$/,
'g',
)
try {
return JSON.parse(str)
} catch (error) {
if (/^(\?.*)/g.test(str) && isQueryString.test(str)) {
return qs.parse(str)
}
if (isQueryString.test(`?${str}`)) {
return qs.parse(str)
}
return str
}
}
/**
* @private
* @function isVariableInString
* @summary
*
* @param {Object<DynamicString>} dynamicString
* @param variable
* @returns
*/
function isVariableInString(
dynamicString: DynamicString,
variable: Paw.RequestVariable,
) {
return !!dynamicString.components.find(
(component: DynamicStringComponent) =>
typeof component === 'object' &&
component.type === 'com.luckymarmot.RequestVariableDynamicValue' &&
component.variableUUID === variable.id,
)
}
/**
* @function buildDocumentInfoObject
* @summary a function that builds openapi document info object
* @param {Object<Paw.Context>} context - an instance of Paw Context object.
* @returns {Object<OpenAPIV3.InfoObject>}
*/
export function buildDocumentInfoObject(
context: Paw.Context,
): OpenAPIV3.InfoObject {
const { user, document } = context
const { name } = document
return {
title: name || '',
description: '',
version: '1.0.0',
contact: {
name: user!.username || '',
email: user!.email || '',
},
}
}
/**
* @function getServers
* @summary
*
* @param {Array<Paw.Request>} requests - an array of Paw Requests.
* @param {Object<Paw.Context>} context - an instance of Paw Context object.
*
* @returns {Array<OpenAPIV3.ServerObject>}
*/
export function buildServerObject(
requests: Paw.Request[],
context: Paw.Context,
): OpenAPIV3.ServerObject[] {
/**
* @function mapServers
* - an array map callback function to map servers used in the requests.
*/
function mapServers(item: Paw.Request): OpenAPIV3.ServerObject {
const parameters = buildParameterObjectArray(item)
const requestURL = new PawURL(item, context, parameters)
const getBasePath = new RegExp(/(\/api\/v\d+)/, 'g')
const getBaseURL = new URL(requestURL.fullUrl)
let baseURL = new URL(getBaseURL.origin)
let basePathStr = ''
let variables = null
if (getBasePath.test(getBaseURL.pathname)) {
const hasBasePath = getBaseURL.pathname.match(getBasePath) || []
if (hasBasePath.length > 0) {
basePathStr = '{basePath}'
variables = {
['basePath']: {
default: hasBasePath[0] || '',
},
}
}
}
return {
url: baseURL.href + basePathStr,
description: '',
variables: variables || undefined,
}
}
/**
* @function filterDuplicates
* - an array filter callback function to remove recurring server objects.
*/
function filterDuplicates(
item: OpenAPIV3.ServerObject,
index: number,
arr: OpenAPIV3.ServerObject[],
) {
return (
index ===
arr.findIndex(
(currentObject) =>
JSON.stringify(currentObject) === JSON.stringify(item),
)
)
}
return [...requests].map(mapServers).filter(filterDuplicates)
}
/**
* @function buildRequestObject
* @summary
* @returns {Object<OpenAPIV3.RequestBodyObject>}
*/
export function buildRequestBodyObject(
request: Paw.Request,
): OpenAPIV3.RequestBodyObject | undefined {
let output: OpenAPIV3.RequestBodyObject = {
description: request.description,
content: {},
required: ['post', 'put', 'patch'].includes(
request.method.toString().toLowerCase(),
),
}
const getContentType = Object.keys(request.headers)
.filter((header) => header.toLowerCase() === 'content-type')
.map((header) => (request.headers[header] as string).toLowerCase())
const type = getContentType
.map((header) => header.match(/((\w+)\/\'?\w+([-']\w+)*\'?)/g))
.join()
.toString()
const body = request.getBody(true) as DynamicString
switch (type) {
case 'multipart/form-data':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(request.getMultipartBody()),
request.getMultipartBody(),
) as OpenAPIV3.SchemaObject,
}
: {}
break
case 'application/x-www-form-urlencoded':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(body.getEvaluatedString()),
body.getEvaluatedString(),
) as OpenAPIV3.SchemaObject,
}
: {}
break
case 'application/json':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(JSON.parse(body.getEvaluatedString())),
JSON.parse(body.getEvaluatedString()),
) as OpenAPIV3.SchemaObject,
}
: {}
break
case 'application/octet-stream':
const content = body.getOnlyDynamicValue() as DynamicValue
output.content[type as string] =
content.type === 'com.luckymarmot.FileContentDynamicValue'
? {
schema: {
type: 'string',
format: 'base64',
example: content.bookmarkData ? content.bookmarkData : '',
},
}
: {}
break
case 'text/plain':
output.content['text/plain'] = body
? {
schema: {
type: 'string',
example: request.getBody() || '',
},
}
: {}
break
}
return output
}
/**
* @function buildResponseObject
* @summary
* a function that builds a singline response object based on
* httpexchange's response body content type.
*
* @param {String} contentType -
* @param {Object|String} getResponseType -
*
* @returns {Object<OpenAPIV3.RequestBodyObject>}
*/
function buildResponseObject(
media: string,
content: any,
): OpenAPIV3.ResponseObject {
// If the response is an array, build a schema from the first item in the collection.
const responseSchema =
Array.isArray(content) && content.length > 0
? toJsonSchema(content[0])
: toJsonSchema(content)
// utilize the built schema, extend it to comply with oas3 schema ref
const schema =
Array.isArray(content) && content.length > 0
? extendToJsonSchema(responseSchema, content[0])
: extendToJsonSchema(responseSchema, content)
return {
description: '',
content: {
[media]: {
schema: Array.isArray(content)
? { type: 'array', items: { ...schema } }
: { ...schema },
},
},
}
}
/**
* @function buildResponseObject
* @summary
* @param {Object<Paw.Request>} request - an instance of Paw request.
* @returns {Object<OpenAPIV3.ResponsesObject>}
*/
export function buildResponsesObject(
request: Paw.Request,
): OpenAPIV3.ResponsesObject {
const getHttpExchange = request.getLastExchange()
if (!getHttpExchange) {
return { 200: { description: '' } }
}
const getContentType = Object.keys(getHttpExchange.responseHeaders)
.filter((header) => header.toLowerCase() === 'content-type')
.map((header) => getHttpExchange.responseHeaders[header])
const getHeaders = Object.keys(getHttpExchange.responseHeaders)
.map((header) => ({
key: header,
value: {
description: '',
schema: {
type: typeof getHttpExchange.responseHeaders[header],
example: getHttpExchange.responseHeaders[header],
},
},
}))
.reduce(
(acc, curr) => ({ ...acc, [curr.key]: curr.value }),
Object.create({}) as OpenAPIV3.HeaderObject,
)
const statusCode =
getHttpExchange.responseStatusCode === 0
? 200
: getHttpExchange.responseStatusCode
const contentType =
getContentType.length > 0
? getContentType[0].replace(/(; .*)$/g, '')
: 'text/plain'
const responses: OpenAPIV3.ResponsesObject = {
[statusCode]: { description: '' },
}
const getResponseType = jsonParseCheck(getHttpExchange.responseBody)
const isJSON = new RegExp('(application/json)', 'g')
const isURLEncoded = new RegExp('(application/x-www-form-urlencoded)', 'g')
switch (true) {
case isJSON.test(contentType) && typeof getResponseType === 'object':
case isURLEncoded.test(contentType) && typeof getResponseType === 'object':
responses[statusCode] = {
...buildResponseObject(contentType, getResponseType),
headers: getHeaders as Record<string, OpenAPIV3.HeaderObject>,
}
break
default:
responses[statusCode] = {
description: '',
content: {
[contentType]: {
example: {
value: getResponseType,
},
},
},
}
break
}
return responses
}
/**
* @function buildParameterObjectArray
* @summary
* @returns {Array<OpenAPIV3.ParameterObject>}
*/
export function buildParameterObjectArray(
request: Paw.Request,
): OpenAPIV3.ParameterObject[] {
type RequestParameter = { [key: string]: string | DynamicString }
/**
* @function fromHeaderParams
* a helper function that converts Paw.Request headers to
* an openapi parameter object.
*
* @todo - find a way to access request variable type to avoid using conditional checks.
*/
function fromHeaderParams(
headers: RequestParameter,
): OpenAPIV3.ParameterObject[] {
if (Object.keys(headers).length === 0) return []
return Object.keys(headers).map((name) => {
const getType = toJsonSchema(headers[name])
return {
name,
in: 'header',
schema: {
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: headers[name].toString() || '',
description: '',
},
}
}) as OpenAPIV3.ParameterObject[]
}
/**
* @function fromPathParams
* a helper function that converts Paw.Request path variables to
* an openapi parameter object.
*
* @todo - find a way to access request variable type to avoid using conditional checks.
*/
function fromPathParams(req: Paw.Request): OpenAPIV3.ParameterObject[] {
const variables = req.getVariablesNames() || []
if (variables.length === 0) return []
const createObject = variables
.map((name: string) => {
const variable = req.getVariableByName(name) as Paw.RequestVariable
const isTruthy = isVariableInString(
req.getUrlBase(true) as DynamicString,
variable,
)
if (!isTruthy) return null
const currentValue = variable.getCurrentValue()
const getType = toJsonSchema(currentValue)
return {
name,
in: 'path',
required: variable.required,
schema: {
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: currentValue || '',
description: variable.description || '',
},
}
})
.filter((item) => item !== null)
return createObject as OpenAPIV3.ParameterObject[]
}
/**
* @function fromQueryParams
* a helper function that converts Paw.Request query params to
* an openapi parameter object.
*
* @todo - find a way to access request variable type to avoid using conditional checks.
*/
function fromQueryParams(queryString: string): OpenAPIV3.ParameterObject[] {
if (queryString.trim() === '') return []
const createQsObject = qs.parse(queryString)
return Object.keys(createQsObject).map((name) => {
const getType = toJsonSchema(createQsObject[name])
return {
name,
in: 'query',
schema: {
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: createQsObject[name]?.toString() || '',
description: '',
},
}
}) as OpenAPIV3.ParameterObject[]
}
return ([] as OpenAPIV3.ParameterObject[]).concat(
fromQueryParams(request.urlQuery),
fromHeaderParams(request.headers),
fromPathParams(request),
)
}
/**
* @function buildPathItemObject
* @summary
* - is a function where `pathItemObject` and `operationObject` are extracted from paw request.
* - it maps request path and methods which are utilized to build the operationObject.
*
* @param {Array<Paw.Request>} requests - an array of Paw Requests
* @param {Object<Paw.Context>} context - an instance of Paw Context object
*
* @returns {Array<OpenAPIV3.ServerObject>}
*/
export function buildPathItemObject(
request: Paw.Request[],
context: Paw.Context,
): OpenAPIV3.PathItemObject {
/**
* @function mapRequestData
* - an array map callback that maps paw request data
*/
function mapRequestData(item: Paw.Request): PawToOAS3 {
const { method, description, id, name } = item
const parameters = buildParameterObjectArray(item)
const requestURL = new PawURL(item, context, parameters)
const getRequestPath = requestURL.pathname
const getPathPrefix = new RegExp(/(\/api\/v\d+)/, 'g')
const requestBody = buildRequestBodyObject(item)
const responses = buildResponsesObject(item)
const security: OpenAPIV3.SecurityRequirementObject[] = []
/**
* @todo:
* - oauth2: placing this security requirement here for now because it's the only
* security type that is identifiable in Paw.Request instance.
*
* - `apiKey` and `openIdConnect` types doesn't seem to be supported in `Paw.Request`
* which doesn't allow the extension to evaluate a request's security scheme.
*
* - `http` type is evaluated from the request parameters.
*/
if (item.oauth2) {
const getOauth2Scopes =
(item.oauth2.scope as string).replace(/\'/gi, '').split(',') || []
security.push({ [OAUTH2_DEFAULT_LABEL]: [...getOauth2Scopes] })
}
const output = {
method: method as string,
path: getPathPrefix.test(getRequestPath)
? getRequestPath.replace(getPathPrefix, '').replace(/\/$/, '')
: getRequestPath,
summary: name,
description,
operationId: id,
parameters,
requestBody,
responses,
security,
}
if (!requestBody) {
delete output.requestBody
}
return output
}
/**
* @function mapRequestMethods
* - an array reduce callback that groups requests with the same endpoints.
* - removes null, undefined or empty value keys
*/
function mapRequestMethods(
accumulator: any,
requestItem: PawToOAS3,
): OpenAPIV3.PathItemObject {
accumulator[requestItem.path] = accumulator[requestItem.path] || {}
const key = (requestItem.method as string).toLowerCase()
accumulator[requestItem.path][key] = {
...requestItem,
} as OpenAPIV3.OperationObject
delete accumulator[requestItem.path][key].path
delete accumulator[requestItem.path][key].method
return accumulator
}
return [...request]
.map(mapRequestData)
.reduce(mapRequestMethods, Object.create({}) as OpenAPIV3.PathItemObject)
}
/**
* @function buildSecuritySchemeAndRequirementsObject
* @summary
* @returns {Object<OpenAPIv3.SecurityDefinitionsObject>}
*/
export function buildSecurityShemeObject(requests: Paw.Request[]): any {
type SecuritySchemeMapping = {
label: string
value: OpenAPIV3.SecuritySchemeObject | null
}
/**
* @function getOauth2Schema
* a helper function that extracts oauth2 `SecurityDefinitionsObject` from Paw.Request.
* @todo:
* - shouldn't we be able to set different types of oauth flows?
* see `OpenAPIV3.SecurityDefinitionsObject`, Paw.Request doesn't change/update
* `grant_type` value.
*/
function getOauth2Schema(auth: OAuth2) {
const scopes = (auth.scope as string).replace(/\'/gi, '').split(',') || []
const oauth2Object = {
type: 'oauth2',
flows: {
implicit: {
authorizationUrl: auth.authorization_uri,
scopes: scopes.reduce(
(acc, curr) => ({
...acc,
[curr]: 'scope description',
}),
{},
),
},
},
}
return {
label: OAUTH2_DEFAULT_LABEL,
value: oauth2Object as OpenAPIV3.SecuritySchemeObject,
}
}
/**
* @function mapRequestSecurityData
* an array callback that builds security scheme object based off paw request object.
*/
function mapRequestSecurityData(
request: Paw.Request,
): SecuritySchemeMapping | null {
let builtSchema = null
if (request.oauth2) {
builtSchema = getOauth2Schema(request.oauth2)
}
return builtSchema
}
/**
* @function filterDuplicates
* an array callback that removes null objects from an array
*/
function filterDuplicates(
item: SecuritySchemeMapping,
index: number,
array: SecuritySchemeMapping[],
): boolean {
return array.findIndex((i) => i.label === item.label) === index
}
/**
* @function mapSecuritySchema
* - an array reduce callback to produce a proper SecuritySchemeObject
*/
function mapSecuritySchema(acc: any, curr: any) {
return { ...acc, [curr.label]: { ...curr.value } }
}
return [...requests]
.map(mapRequestSecurityData)
.filter((item: SecuritySchemeMapping) => item !== null)
.filter(filterDuplicates)
.reduce(
mapSecuritySchema,
{} as { [key: string]: OpenAPIV3.SecuritySchemeObject },
)
}