1
0
mirror of synced 2025-12-23 11:54:18 -05:00

Adding x-github-api-version header to js and cli examples (#34935)

This commit is contained in:
Grace Park
2023-02-21 13:56:50 -08:00
committed by GitHub
parent 5b424cba87
commit 320ffe6d01

View File

@@ -104,11 +104,19 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
export function getGHExample(operation: Operation, codeSample: CodeSample) {
const defaultAcceptHeader = getAcceptHeader(codeSample)
const hostname = operation.serverUrl !== 'https://api.github.com' ? '--hostname HOSTNAME' : ''
const { currentVersion } = useVersion()
const { allVersions } = useMainContext()
let requestPath = codeSample?.request?.parameters
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
: operation.requestPath
const apiVersionHeader =
allVersions[currentVersion].apiVersions.length > 0 &&
allVersions[currentVersion].latestApiVersion
? `-H "X-GitHub-Api-Version: ${allVersions[currentVersion].latestApiVersion}"`
: ''
const requiredQueryParams = getRequiredQueryParamsPath(operation, codeSample)
requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''
@@ -141,9 +149,11 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
}
}
const args = [
operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`,
`-H "Accept: ${defaultAcceptHeader}"`,
apiVersionHeader,
hostname,
requestPath,
requestBodyParams,
@@ -167,7 +177,10 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
*/
export function getJSExample(operation: Operation, codeSample: CodeSample) {
const parameters: { [key: string]: string } = {}
const { currentVersion } = useVersion()
const { allVersions } = useMainContext()
const parameters: { [key: string]: string | object } = {}
if (codeSample.request) {
Object.assign(parameters, codeSample.request.parameters)
// Most of the time the example body parameters have a name and value
@@ -207,6 +220,16 @@ export function getJSExample(operation: Operation, codeSample: CodeSample) {
queryParameters = `{?${queryParams.join(',')}}`
}
}
if (
allVersions[currentVersion].apiVersions.length > 0 &&
allVersions[currentVersion].latestApiVersion
) {
parameters.headers = {
'X-GitHub-Api-Version': `${allVersions[currentVersion].latestApiVersion}`,
}
}
const comment = `// Octokit.js\n// https://github.com/octokit/core.js#readme\n`
const require = `const octokit = new Octokit(${stringify({ auth: 'YOUR-TOKEN' }, null, 2)})\n\n`