This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
add-code-samples.js
58 lines (49 loc) · 1.93 KB
/
add-code-samples.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
module.exports = addCodeSamples
const urlTemplate = require('url-template')
const { stringify } = require('javascript-stringify')
// TODO: find a better place to define parameter examples
const PARAMETER_EXAMPLES = {
owner: 'octocat',
repo: 'hello-world',
issue_number: 1,
title: 'title'
}
function addCodeSamples (state) {
const { scope, baseUrl } = state
state.routes.forEach(route => {
const codeSampleParams = { route, scope, baseUrl }
route.operation['x-code-samples'] = route.operation['x-code-samples'].concat(
{ lang: 'Shell', source: toShellExample(codeSampleParams) },
{ lang: 'JS', source: toJsExample(codeSampleParams) }
)
})
}
function toShellExample ({ route, scope, baseUrl }) {
const path = urlTemplate.parse(route.path.replace(/:(\w+)/, '{$1}')).expand(PARAMETER_EXAMPLES)
const params = route.operation.parameters
.filter(param => !param.deprecated)
.filter(param => param.location === 'body')
.filter(param => param.required)
.reduce((params, param) => Object.assign(params, {
[param.name]: PARAMETER_EXAMPLES[param.name] || param.name
}), {})
const method = route.method.toUpperCase()
const defaultAcceptHeader = route.operation.parameters[0].schema.default
const args = [
method !== 'GET' && `-X${method}`,
`-H"Accept: ${defaultAcceptHeader}"`,
new URL(path, baseUrl).href,
Object.keys(params).length && `-d '${JSON.stringify(params)}'`
].filter(Boolean)
return `curl \\\n ${args.join(' \\\n ')}`
}
function toJsExample ({ route, scope, baseUrl }) {
const params = route.operation.parameters
.filter(param => !param.deprecated)
.filter(param => param.in !== 'header')
.filter(param => param.required)
.reduce((params, param) => Object.assign(params, {
[param.name]: PARAMETER_EXAMPLES[param.name] || param.name
}), {})
return `octokit.${scope}.get(${Object.keys(params).length ? stringify(params, null, 2) : ''})`
}