Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

operation.js 13 KB

You have to be logged in to leave a comment. Sign In
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
  1. const { get, flatten, isPlainObject } = require('lodash')
  2. const { sentenceCase } = require('change-case')
  3. const slugger = new (require('github-slugger'))()
  4. const httpStatusCodes = require('http-status-code')
  5. const renderContent = require('../../../lib/render-content')
  6. const createCodeSamples = require('./create-code-samples')
  7. const Ajv = require('ajv')
  8. // titles that can't be derived by sentence-casing the ID
  9. const categoryTitles = { scim: 'SCIM' }
  10. module.exports = class Operation {
  11. constructor (verb, requestPath, props, serverUrl) {
  12. const defaultProps = {
  13. parameters: [],
  14. 'x-codeSamples': [],
  15. responses: {}
  16. }
  17. Object.assign(this, { verb, requestPath, serverUrl }, defaultProps, props)
  18. slugger.reset()
  19. this.slug = slugger.slug(this.summary)
  20. // Add category
  21. // workaround for misnamed `code-scanning.` category bug
  22. // https://github.com/github/rest-api-description/issues/38
  23. this['x-github'].category = this['x-github'].category.replace('.', '')
  24. this.category = this['x-github'].category
  25. this.categoryLabel = categoryTitles[this.category] || sentenceCase(this.category)
  26. // Add subcategory
  27. if (this['x-github'].subcategory) {
  28. this.subcategory = this['x-github'].subcategory
  29. this.subcategoryLabel = sentenceCase(this.subcategory)
  30. }
  31. return this
  32. }
  33. get schema () {
  34. return require('./operation-schema')
  35. }
  36. async process () {
  37. this['x-codeSamples'] = createCodeSamples(this)
  38. await Promise.all([
  39. this.renderDescription(),
  40. this.renderCodeSamples(),
  41. this.renderResponses(),
  42. this.renderParameterDescriptions(),
  43. this.renderBodyParameterDescriptions(),
  44. this.renderPreviewNotes(),
  45. this.renderNotes()
  46. ])
  47. const ajv = new Ajv()
  48. const valid = ajv.validate(this.schema, this)
  49. if (!valid) {
  50. console.error(JSON.stringify(ajv.errors, null, 2))
  51. throw new Error('Invalid operation found')
  52. }
  53. }
  54. async renderDescription () {
  55. this.descriptionHTML = await renderContent(this.description)
  56. return this
  57. }
  58. async renderCodeSamples () {
  59. return Promise.all(this['x-codeSamples'].map(async (sample) => {
  60. const markdown = createCodeBlock(sample.source, sample.lang.toLowerCase())
  61. sample.html = await renderContent(markdown)
  62. return sample
  63. }))
  64. }
  65. async renderResponses () {
  66. // clone and delete this.responses so we can turn it into a clean array of objects
  67. const rawResponses = JSON.parse(JSON.stringify(this.responses))
  68. delete this.responses
  69. this.responses = await Promise.all(Object.keys(rawResponses).map(async (responseCode) => {
  70. const rawResponse = rawResponses[responseCode]
  71. const httpStatusCode = responseCode
  72. const httpStatusMessage = httpStatusCodes.getMessage(Number(responseCode))
  73. const responseDescription = rawResponse.description
  74. const cleanResponses = []
  75. /* Responses can have zero, one, or multiple examples. The `examples`
  76. * property often only contains one example object. Both the `example`
  77. * and `examples` properties can be used in the OpenAPI but `example`
  78. * doesn't work with `$ref`.
  79. * This works:
  80. * schema:
  81. * '$ref': '../../components/schemas/foo.yaml'
  82. * example:
  83. * id: 10
  84. * description: This is a summary
  85. * foo: bar
  86. *
  87. * This doesn't
  88. * schema:
  89. * '$ref': '../../components/schemas/foo.yaml'
  90. * example:
  91. * '$ref': '../../components/examples/bar.yaml'
  92. */
  93. const examplesProperty = get(rawResponse, 'content.application/json.examples')
  94. const exampleProperty = get(rawResponse, 'content.application/json.example')
  95. // Return early if the response doesn't have an example payload
  96. if (!exampleProperty && !examplesProperty) {
  97. return [{
  98. httpStatusCode,
  99. httpStatusMessage,
  100. description: responseDescription
  101. }]
  102. }
  103. // Use the same format for `example` as `examples` property so that all
  104. // examples can be handled the same way.
  105. const normalizedExampleProperty = {
  106. default: {
  107. value: exampleProperty
  108. }
  109. }
  110. const rawExamples = examplesProperty || normalizedExampleProperty
  111. const rawExampleKeys = Object.keys(rawExamples)
  112. for (const exampleKey of rawExampleKeys) {
  113. const exampleValue = rawExamples[exampleKey].value
  114. const exampleSummary = rawExamples[exampleKey].summary
  115. const cleanResponse = {
  116. httpStatusCode,
  117. httpStatusMessage
  118. }
  119. // If there is only one example, use the response description
  120. // property. For cases with more than one example, some don't have
  121. // summary properties with a description, so we can sentence case
  122. // the property name as a fallback
  123. cleanResponse.description = rawExampleKeys.length === 1
  124. ? exampleSummary || responseDescription
  125. : exampleSummary || sentenceCase(exampleKey)
  126. const payloadMarkdown = createCodeBlock(exampleValue, 'json')
  127. cleanResponse.payload = await renderContent(payloadMarkdown)
  128. cleanResponses.push(cleanResponse)
  129. }
  130. return cleanResponses
  131. }))
  132. // flatten child arrays
  133. this.responses = flatten(this.responses)
  134. }
  135. async renderParameterDescriptions () {
  136. return Promise.all(this.parameters.map(async (param) => {
  137. param.descriptionHTML = await renderContent(param.description)
  138. return param
  139. }))
  140. }
  141. async renderBodyParameterDescriptions () {
  142. let bodyParamsObject = get(this, 'requestBody.content.application/json.schema.properties', {})
  143. let requiredParams = get(this, 'requestBody.content.application/json.schema.required', [])
  144. const oneOfObject = get(this, 'requestBody.content.application/json.schema.oneOf', undefined)
  145. // oneOf is an array of input parameter options, so we need to either
  146. // use the first option or munge the options together.
  147. if (oneOfObject) {
  148. const firstOneOfObject = oneOfObject[0]
  149. const allOneOfAreObjects = oneOfObject
  150. .filter(elem => elem.type === 'object')
  151. .length === oneOfObject.length
  152. // TODO: Remove this check
  153. // This operation shouldn't have a oneOf in this case, it needs to be
  154. // removed from the schema in the github/github repo.
  155. if (this.operationId === 'checks/create') {
  156. delete bodyParamsObject.oneOf
  157. } else if (allOneOfAreObjects) {
  158. // When all of the oneOf objects have the `type: object` we
  159. // need to display all of the parameters.
  160. // This merges all of the properties and required values into the
  161. // first requestBody object.
  162. for (let i = 1; i < oneOfObject.length; i++) {
  163. Object.assign(firstOneOfObject.properties, oneOfObject[i].properties)
  164. requiredParams = firstOneOfObject.required
  165. .concat(oneOfObject[i].required)
  166. }
  167. bodyParamsObject = firstOneOfObject.properties
  168. } else if (oneOfObject) {
  169. // When a oneOf exists but the `type` differs, the case has historically
  170. // been that the alternate option is an array, where the first option
  171. // is the array as a property of the object. We need to ensure that the
  172. // first option listed is the most comprehensive and preferred option.
  173. bodyParamsObject = firstOneOfObject.properties
  174. requiredParams = firstOneOfObject.required
  175. }
  176. }
  177. this.bodyParameters = await getBodyParams(bodyParamsObject, requiredParams)
  178. }
  179. async renderPreviewNotes () {
  180. const previews = get(this, 'x-github.previews', [])
  181. .filter(preview => preview.note)
  182. return Promise.all(previews.map(async (preview) => {
  183. const note = preview.note
  184. // remove extra leading and trailing newlines
  185. .replace(/```\n\n\n/mg, '```\n')
  186. .replace(/```\n\n/mg, '```\n')
  187. .replace(/\n\n\n```/mg, '\n```')
  188. .replace(/\n\n```/mg, '\n```')
  189. // convert single-backtick code snippets to fully fenced triple-backtick blocks
  190. // example: This is the description.\n\n`application/vnd.github.machine-man-preview+json`
  191. .replace(/\n`application/, '\n```\napplication')
  192. .replace(/json`$/, 'json\n```')
  193. preview.html = await renderContent(note)
  194. }))
  195. }
  196. // add additional notes to this array whenever we want
  197. async renderNotes () {
  198. this.notes = []
  199. return Promise.all(this.notes.map(async (note) => renderContent(note)))
  200. }
  201. }
  202. // need to use this function recursively to get child and grandchild params
  203. async function getBodyParams (paramsObject, requiredParams) {
  204. if (!isPlainObject(paramsObject)) return []
  205. return Promise.all(Object.keys(paramsObject).map(async (paramKey) => {
  206. const param = paramsObject[paramKey]
  207. param.name = paramKey
  208. param.in = 'body'
  209. param.rawType = param.type
  210. param.rawDescription = param.description
  211. // Stores the types listed under the `Type` column in the `Parameters`
  212. // table in the REST API docs. When the parameter contains oneOf
  213. // there are multiple acceptable parameters that we should list.
  214. const paramArray = []
  215. const oneOfArray = param.oneOf
  216. const isOneOfObjectOrArray = oneOfArray
  217. ? oneOfArray.filter(elem => elem.type !== 'object' || elem.type !== 'array')
  218. : false
  219. // When oneOf has the type array or object, the type is defined
  220. // in a child object
  221. if (oneOfArray && isOneOfObjectOrArray.length > 0) {
  222. // Store the defined types
  223. paramArray.push(oneOfArray
  224. .filter(elem => elem.type)
  225. .map(elem => elem.type)
  226. )
  227. // If an object doesn't have a description, it is invalid
  228. const oneOfArrayWithDescription = oneOfArray.filter(elem => elem.description)
  229. // Use the parent description when set, otherwise enumerate each
  230. // description in the `Description` column of the `Parameters` table.
  231. if (!param.description && oneOfArrayWithDescription.length > 1) {
  232. param.description = oneOfArray
  233. .filter(elem => elem.description)
  234. .map(elem => `**Type ${elem.type}** - ${elem.description}`)
  235. .join('\n\n')
  236. } else if (!param.description && oneOfArrayWithDescription.length === 1) {
  237. // When there is only on valid description, use that one.
  238. param.description = oneOfArrayWithDescription[0].description
  239. }
  240. }
  241. // Arrays require modifying the displayed type (e.g., array of strings)
  242. if (param.type === 'array') {
  243. if (param.items.type) paramArray.push(`array of ${param.items.type}s`)
  244. if (param.items.oneOf) {
  245. paramArray.push(param.items.oneOf
  246. .map(elem => `array of ${elem.type}s`)
  247. )
  248. }
  249. } else if (param.type) {
  250. paramArray.push(param.type)
  251. }
  252. if (param.nullable) paramArray.push('nullable')
  253. param.type = paramArray.flat().join(' or ')
  254. param.description = param.description || ''
  255. const isRequired = requiredParams && requiredParams.includes(param.name)
  256. const requiredString = isRequired ? '**Required**. ' : ''
  257. param.description = await renderContent(requiredString + param.description)
  258. // there may be zero, one, or multiple object parameters that have children parameters
  259. param.childParamsGroups = []
  260. const childParamsGroup = await getChildParamsGroup(param)
  261. if (childParamsGroup && childParamsGroup.params.length) {
  262. param.childParamsGroups.push(childParamsGroup)
  263. }
  264. // if the param is an object, it may have child object params that have child params :/
  265. if (param.rawType === 'object') {
  266. param.childParamsGroups.push(...flatten(childParamsGroup.params
  267. .filter(param => param.childParamsGroups.length)
  268. .map(param => param.childParamsGroups)))
  269. }
  270. return param
  271. }))
  272. }
  273. async function getChildParamsGroup (param) {
  274. // only objects, arrays of objects, anyOf, allOf, and oneOf have child params
  275. if (!(param.rawType === 'array' || param.rawType === 'object' || param.oneOf)) return
  276. if (param.oneOf && !param.oneOf.filter(param => param.type === 'object' || param.type === 'array')) return
  277. if (param.items && param.items.type !== 'object') return
  278. const childParamsObject = param.rawType === 'array' ? param.items.properties : param.properties
  279. const requiredParams = param.rawType === 'array' ? param.items.required : param.required
  280. const childParams = await getBodyParams(childParamsObject, requiredParams)
  281. // adjust the type for easier readability in the child table
  282. const parentType = param.rawType === 'array' ? 'items' : param.rawType
  283. // add an ID to the child table so they can be linked to
  284. slugger.reset()
  285. const id = slugger.slug(`${param.name}-${parentType}`)
  286. return {
  287. parentName: param.name,
  288. parentType,
  289. id,
  290. params: childParams
  291. }
  292. }
  293. function createCodeBlock (input, language) {
  294. // stringify JSON if needed
  295. if (language === 'json' && typeof input !== 'string') {
  296. input = JSON.stringify(input, null, 2)
  297. }
  298. return ['```' + language, input, '```'].join('\n')
  299. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...