This repository was archived by the owner on Mar 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerateExamples.js
More file actions
85 lines (79 loc) · 2.8 KB
/
Copy pathgenerateExamples.js
File metadata and controls
85 lines (79 loc) · 2.8 KB
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
require('isomorphic-fetch')
const fs = require('fs').promises
const path = require('path')
const { convert } = require('widdershins/lib/index')
const { convertPathToFilename } = require('./helpers')
const OUTPUT_DIR = path.resolve(__dirname, 'examples/')
const setupData = (data) => {
data.operationUniqueSlug = data.method.slug
data.operation = data.method.operation
data.methodUpper = data.method.verb.toUpperCase()
data.url = data.utils.slashes(data.baseUrl + data.method.path)
data.parameters = data.operation.parameters
data.enums = []
data.utils.fakeProdCons(data)
data.utils.fakeBodyParameter(data)
data.utils.mergePathParameters(data)
data.utils.getParameters(data)
}
const getContentType = (operation, statusCode) => {
let content = operation.responses[`${statusCode}`]
content = content ? content.content : null
content = Object.keys(content || {})
content = content.length > 0 ? content[0] : null
return content
}
const fetchAndSaveExample = async (urlToFetch, filename, operation) => {
const res = await fetch(urlToFetch)
let content = await res.text()
const contentType = getContentType(operation, res.status)
if (!contentType || contentType === 'application/json') {
let json
try {
json = JSON.parse(content)
} catch (_) {
console.warn('Skipping output for', urlToFetch, "as it's not JSON")
}
if (json) {
console.log('Parsing as JSON', urlToFetch)
if (Array.isArray(json.data)) {
// Shorten lists
json.data = json.data.slice(0, 1)
}
content = JSON.stringify(json, null, 2)
const filePath = path.resolve(OUTPUT_DIR, filename)
console.log('Writing to', filePath)
await fs.writeFile(filePath, content, 'utf-8')
}
} else {
console.warn('Skipping output for', urlToFetch, "as it's not JSON")
}
}
const generateExamples = async (swagger, baseUrl) => {
await fs.mkdir(OUTPUT_DIR, { recursive: true })
const toFetch = []
await convert(swagger, {
templateCallback: (templateName, stage, data) => {
data.baseUrl = data.baseUrl.replace('AUDIUS_API_HOST', baseUrl)
if (templateName === 'main' && stage === 'post') {
for (const resource in data.resources) {
data.resource = data.resources[resource]
for (const method in data.resource.methods) {
data.method = data.resource.methods[method]
setupData(data)
const operation = data.operation
const urlToFetch = data.baseUrl + data.requiredUriExample
const filename = convertPathToFilename(data.method.path)
console.log('Fetchidang', urlToFetch)
toFetch.push(fetchAndSaveExample(urlToFetch, filename, operation))
}
}
}
return data
}
})
await Promise.all(toFetch)
}
module.exports = {
generateExamples
}