Pretty format (#20352)
* Update prettier flow to include JS * Run prettier * ...run prettier
This commit is contained in:
@@ -4,30 +4,29 @@ import { stringify } from 'javascript-stringify'
|
||||
import { get, mapValues, snakeCase } from 'lodash-es'
|
||||
export default createCodeSamples
|
||||
|
||||
|
||||
const PARAMETER_EXAMPLES = {
|
||||
owner: 'octocat',
|
||||
repo: 'hello-world',
|
||||
email: 'octocat@github.com',
|
||||
emails: ['octocat@github.com']
|
||||
emails: ['octocat@github.com'],
|
||||
}
|
||||
|
||||
function createCodeSamples (operation) {
|
||||
function createCodeSamples(operation) {
|
||||
const route = {
|
||||
method: operation.verb.toUpperCase(),
|
||||
path: operation.requestPath,
|
||||
operation
|
||||
operation,
|
||||
}
|
||||
const serverUrl = operation.serverUrl
|
||||
|
||||
const codeSampleParams = { route, serverUrl }
|
||||
return [
|
||||
{ lang: 'Shell', source: toShellExample(codeSampleParams) },
|
||||
{ lang: 'JavaScript', source: toJsExample(codeSampleParams) }
|
||||
{ lang: 'JavaScript', source: toJsExample(codeSampleParams) },
|
||||
]
|
||||
}
|
||||
|
||||
function toShellExample ({ route, serverUrl }) {
|
||||
function toShellExample({ route, serverUrl }) {
|
||||
const pathParams = mapValues(getExamplePathParams(route), (value, paramName) =>
|
||||
PARAMETER_EXAMPLES[paramName] ? value : snakeCase(value).toUpperCase()
|
||||
)
|
||||
@@ -35,8 +34,9 @@ function toShellExample ({ route, serverUrl }) {
|
||||
const params = getExampleBodyParams(route)
|
||||
const { method } = route
|
||||
|
||||
const requiredPreview = get(route, 'operation.x-github.previews', [])
|
||||
.find(preview => preview.required)
|
||||
const requiredPreview = get(route, 'operation.x-github.previews', []).find(
|
||||
(preview) => preview.required
|
||||
)
|
||||
|
||||
const defaultAcceptHeader = requiredPreview
|
||||
? `application/vnd.github.${requiredPreview.name}-preview+json`
|
||||
@@ -48,7 +48,7 @@ function toShellExample ({ route, serverUrl }) {
|
||||
if (route.operation.contentType === 'application/x-www-form-urlencoded') {
|
||||
requestBodyParams = ''
|
||||
const paramNames = Object.keys(params)
|
||||
paramNames.forEach(elem => {
|
||||
paramNames.forEach((elem) => {
|
||||
requestBodyParams = `${requestBodyParams} --data-urlencode ${elem}=${params[elem]}`
|
||||
})
|
||||
requestBodyParams = requestBodyParams.trim()
|
||||
@@ -58,20 +58,20 @@ function toShellExample ({ route, serverUrl }) {
|
||||
method !== 'GET' && `-X ${method}`,
|
||||
defaultAcceptHeader ? `-H "Accept: ${defaultAcceptHeader}"` : '',
|
||||
`${serverUrl}${path}`,
|
||||
Object.keys(params).length && requestBodyParams
|
||||
Object.keys(params).length && requestBodyParams,
|
||||
].filter(Boolean)
|
||||
return `curl \\\n ${args.join(' \\\n ')}`
|
||||
}
|
||||
|
||||
function toJsExample ({ route }) {
|
||||
function toJsExample({ route }) {
|
||||
const params = route.operation.parameters
|
||||
.filter(param => !param.deprecated)
|
||||
.filter(param => param.in !== 'header')
|
||||
.filter(param => param.required)
|
||||
.filter((param) => !param.deprecated)
|
||||
.filter((param) => param.in !== 'header')
|
||||
.filter((param) => param.required)
|
||||
.reduce(
|
||||
(_params, param) =>
|
||||
Object.assign(_params, {
|
||||
[param.name]: getExampleParamValue(param.name, param.schema)
|
||||
[param.name]: getExampleParamValue(param.name, param.schema),
|
||||
}),
|
||||
{}
|
||||
)
|
||||
@@ -79,23 +79,23 @@ function toJsExample ({ route }) {
|
||||
|
||||
// add any required preview headers to the params object
|
||||
const requiredPreviewNames = get(route.operation, 'x-github.previews', [])
|
||||
.filter(preview => preview.required)
|
||||
.map(preview => preview.name)
|
||||
.filter((preview) => preview.required)
|
||||
.map((preview) => preview.name)
|
||||
|
||||
if (requiredPreviewNames.length) {
|
||||
Object.assign(params, {
|
||||
mediaType: { previews: requiredPreviewNames }
|
||||
mediaType: { previews: requiredPreviewNames },
|
||||
})
|
||||
}
|
||||
|
||||
// add required content type header (presently only for `POST /markdown/raw`)
|
||||
const contentTypeHeader = route.operation.parameters.find(param => {
|
||||
const contentTypeHeader = route.operation.parameters.find((param) => {
|
||||
return param.name.toLowerCase() === 'content-type' && get(param, 'schema.enum')
|
||||
})
|
||||
|
||||
if (contentTypeHeader) {
|
||||
Object.assign(params, {
|
||||
headers: { 'content-type': contentTypeHeader.schema.enum[0] }
|
||||
headers: { 'content-type': contentTypeHeader.schema.enum[0] },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -103,8 +103,8 @@ function toJsExample ({ route }) {
|
||||
return `await octokit.request('${route.method} ${route.path}'${args})`
|
||||
}
|
||||
|
||||
function getExamplePathParams ({ operation }) {
|
||||
const pathParams = operation.parameters.filter(param => param.in === 'path')
|
||||
function getExamplePathParams({ operation }) {
|
||||
const pathParams = operation.parameters.filter((param) => param.in === 'path')
|
||||
if (pathParams.length === 0) {
|
||||
return {}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ function getExamplePathParams ({ operation }) {
|
||||
}, {})
|
||||
}
|
||||
|
||||
function getExampleBodyParams ({ operation }) {
|
||||
function getExampleBodyParams({ operation }) {
|
||||
const contentType = Object.keys(get(operation, 'requestBody.content', []))[0]
|
||||
let schema
|
||||
try {
|
||||
@@ -149,7 +149,7 @@ function getExampleBodyParams ({ operation }) {
|
||||
}, {})
|
||||
}
|
||||
|
||||
function getExampleParamValue (name, schema) {
|
||||
function getExampleParamValue(name, schema) {
|
||||
const value = PARAMETER_EXAMPLES[name]
|
||||
if (value) {
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user