-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-route.js
240 lines (238 loc) · 9.1 KB
/
api-route.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
const beautify = require('js-beautify').html
const fs = require('fs')
const path = require('path')
const HTML = require('./html.js')
const template = fs.readFileSync('./api-route-template.html').toString()
module.exports = (rootPath, moduleName, documentationPath, api, urlPath) => {
const fileDescription = api[urlPath].fileDescription
const filePath = fileDescription + '.js'
let verb
if (fileDescription.indexOf('create-') > -1) {
verb = 'POST'
} else if (fileDescription.indexOf('delete-') > -1) {
verb = 'DELETE'
} else if (fileDescription.indexOf('update-') > -1 || fileDescription.indexOf('set-') > -1 || fileDescription.indexOf('reset-') > -1) {
verb = 'PATCH'
} else {
verb = 'GET'
}
let moduleTitle, moduleIndexPath
if (moduleName === '@userdashboard/dashboard') {
moduleTitle = 'Dashboard'
moduleIndexPath = '/dashboard-api'
} else if (moduleName === '@userdashboard/organizations') {
moduleTitle = 'Organizations module'
moduleIndexPath = '/organizations-api'
} else if (moduleName === '@userdashboard/maxmind-geoip') {
moduleTitle = 'MaxMind GeoIP module'
moduleIndexPath = '/maxmind-geoip-api'
} else if (moduleName === '@userdashboard/stripe-connect') {
moduleTitle = 'Stripe Connect module'
moduleIndexPath = '/stripe-connect-api'
} else if (moduleName === '@userdashboard/stripe-subscriptions') {
moduleTitle = 'Stripe Subscriptions module'
moduleIndexPath = '/stripe-subscriptions-api'
}
const doc = HTML.parse(template)
doc.getElementById('title').child = [{
node: 'text',
text: `${fileDescription} (${verb})`
}]
doc.getElementsByTagName('h1')[0].child = [{
node: 'text',
text: `${moduleTitle} API explorer`
}]
doc.getElementsByTagName('title')[0].child = [{
node: 'text',
text: `${fileDescription}#${verb} (${moduleName})`
}]
doc.getElementById('subtitle').attr.href = moduleIndexPath
doc.getElementById('subtitle').child = [{
node: 'text',
text: `${moduleTitle} API`
}]
const segments = ['exceptions', 'receives', 'redacts', 'configuration']
const structure = api[urlPath].structure
for (const segment of segments) {
if (segment === 'receives') {
const data = []
if (structure[segment] && structure[segment].length) {
for (const listItem of structure[segment]) {
if (segment === 'receives') {
let required
if (listItem.indexOf('optionally-required') > -1) {
required = 'configurable as required'
} else {
required = listItem.indexOf('optional') > -1 ? 'optional' : 'required'
}
const type = listItem.indexOf('posted') > -1 ? 'POST' : 'URL'
let variable = type === 'POST' ? listItem.split('posted ').pop() : listItem.split('querystring ').pop()
let value = variable.indexOf('(') > -1 ? variable.substring(variable.indexOf('(') + 1) : 'string'
if (value.indexOf(')') > -1) {
value = value.substring(0, value.indexOf(')'))
}
if (value.indexOf('|') > -1) {
value = value.split('|').join(', ')
}
if (variable.indexOf('(') > -1) {
variable = variable.substring(0, variable.indexOf('('))
}
data.push({
object: 'property',
variable,
value,
required,
type
})
}
}
data.sort((a, b) => {
return a.variable < b.variable ? -1 : 1
})
if (segments === 'exceptions' || segment === 'receives') {
HTML.renderTable(doc, data, 'property-row-template', `${segment}-table`)
} else {
HTML.renderList(doc, data, 'property-row-template', `${segment}-list`)
}
} else {
const receivesContainer = doc.getElementById('receives-container')
receivesContainer.parentNode.removeChild(receivesContainer)
}
} else if (segment === 'redacted') {
const data = []
for (const item in structure[segment]) {
for (const i in structure[segment][item]) {
data.push({
object: 'property',
name: structure[segment][item][i]
})
}
}
data.sort((a, b) => {
return a.variable < b.variable ? -1 : 1
})
HTML.renderList(doc, data, `${segment}-row-template`, segment)
} else if (segment === 'exceptions') {
let id = 0
for (const item in structure[segment]) {
const exceptions = []
for (const i in structure[segment][item]) {
if (i === '0') {
exceptions.push({
id: id++,
object: 'exception',
text: item,
note: structure[segment][item][0],
rowSpan: structure[segment][item].length
})
} else {
exceptions.push({
id: id++,
object: 'exception',
note: structure[segment][item][i]
})
}
}
if (exceptions && exceptions.length) {
exceptions.sort((a, b) => {
return a.note < b.note ? -1 : 1
})
HTML.renderTable(doc, exceptions, 'exception-template', 'exceptions-table')
for (const exception of exceptions) {
if (exception.text) {
continue
}
const description = doc.getElementById(`description-${exception.id}`)
description.parentNode.removeChild(description)
}
}
}
} else {
const container = doc.getElementById(`${segment}-container`)
if (container) {
container.parentNode.removeChild(container)
}
}
}
const isAdministrator = urlPath.indexOf('/administrator') > -1
let newFilePath = path.join(documentationPath, 'api')
newFilePath = path.join(documentationPath, 'api/' + (isAdministrator ? 'administrator' : 'user'))
if (moduleName === '@userdashboard/maxmind-geoip') {
newFilePath += '/maxmind'
} else if (moduleName === '@userdashboard/stripe-connect') {
newFilePath += '/connect'
} else if (moduleName === '@userdashboard/stripe-subscriptions') {
newFilePath += '/subscriptions'
} else if (moduleName === '@userdashboard/organizations') {
newFilePath += '/organizations'
}
let nodejs = 'global' + fileDescription.split('/').join('.')
const parts = nodejs.split('.').pop().split('-')
nodejs = nodejs.substring(0, nodejs.lastIndexOf('.') + 1)
for (const i in parts) {
nodejs += parts[i].charAt(0).toUpperCase() + parts[i].substring(1)
}
doc.getElementById('nodejs').classList.add(verb.toLowerCase())
doc.getElementById('nodejs').child = [{
node: 'text',
text: 'await ' + nodejs + '.' + verb.toLowerCase() + '(req)'
}]
const jsFilePath = path.join(`${rootPath}/node_modules/${moduleName}/src/www${filePath}`)
doc.getElementById('source-code').child = [{
node: 'text',
text: fs.readFileSync(jsFilePath).toString()
}]
doc.getElementById('returns').child = [{
node: 'text',
text: structure.returns
}]
const responseFileName = '/' + filePath.split('/').pop().replace('.js', '.test.json')
if (fs.existsSync(newFilePath + responseFileName)) {
const response = doc.getElementById('response-code')
let responseJSON = fs.readFileSync(newFilePath + responseFileName).toString()
if (responseJSON.indexOf('{') > -1) {
responseJSON = JSON.parse(responseJSON)
for (const field in responseJSON) {
if (responseJSON[field] + 1 > 1578154662) {
responseJSON[field] = '{timestamp}'
}
}
response.child = [{
node: 'text',
text: JSON.stringify(responseJSON, null, ' ')
}]
} else {
response.parentNode.parentNode.parentNode.removeChild(response.parentNode.parentNode)
}
}
doc.getElementById('test-code').child = [{
node: 'text',
text: fs.readFileSync(jsFilePath.replace('.js', '.test.js')).toString()
}]
doc.getElementById('github_source').attr.href = `https://github.com/userdashboard/${moduleName}/tree/master/src/www${fileDescription}.js`
doc.getElementById('github_tests').attr.href = `https://github.com/userdashboard/${moduleName}/tree/master/src/www${fileDescription}.test.js`
const navbarTemplate = doc.getElementById('navbar')
doc.getElementById('navigation').child = navbarTemplate.child
navbarTemplate.parentNode.removeChild(navbarTemplate)
const filename = filePath.split('/').pop().replace('.js', '.html')
createFolderSync(newFilePath, process.env.DOCUMENTATION_PATH)
const html = beautify(doc.toString(), { indent_size: 2, space_in_empty_paren: true })
fs.writeFileSync(`${newFilePath}/${filename}`, html)
console.log('writing API route file', filename)
}
function createFolderSync(path, documentationPath) {
if (fs.existsSync(path)) {
return
}
console.log('making path', path)
const nested = path.substring(documentationPath.length)
const nestedParts = nested.split('/')
let nestedPath = documentationPath
for (const part of nestedParts) {
nestedPath += `/${part}`
if (!fs.existsSync(nestedPath)) {
console.log('making path', nestedPath)
fs.mkdirSync(nestedPath)
}
}
}