forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sophietheking-personalaccount
- Loading branch information
Showing
110 changed files
with
317,916 additions
and
241,636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { parseTemplate } from 'url-template' | ||
import { stringify } from 'javascript-stringify' | ||
|
||
import type { CodeSample, Operation } from '../rest/types' | ||
|
||
/* | ||
Generates a curl example | ||
For example: | ||
curl \ | ||
-X POST \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
https://{hostname}/api/v3/repos/OWNER/REPO/deployments \ | ||
-d '{"ref":"topic-branch","payload":"{ \"deploy\": \"migrate\" }","description":"Deploy request from hubot"}' | ||
*/ | ||
export function getShellExample(operation: Operation, codeSample: CodeSample) { | ||
// This allows us to display custom media types like application/sarif+json | ||
const defaultAcceptHeader = codeSample?.response?.contentType?.includes('+json') | ||
? codeSample.response.contentType | ||
: 'application/vnd.github.v3+json' | ||
|
||
const requestPath = codeSample?.request?.parameters | ||
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters) | ||
: operation.requestPath | ||
|
||
let requestBodyParams = '' | ||
if (codeSample?.request?.bodyParameters) { | ||
requestBodyParams = `-d '${JSON.stringify(codeSample.request.bodyParameters)}'` | ||
|
||
// If the content type is application/x-www-form-urlencoded the format of | ||
// the shell example is --data-urlencode param1=value1 --data-urlencode param2=value2 | ||
// For example, this operation: | ||
// https://docs.github.com/en/enterprise/rest/reference/enterprise-admin#enable-or-disable-maintenance-mode | ||
if (codeSample.request.contentType === 'application/x-www-form-urlencoded') { | ||
requestBodyParams = '' | ||
const paramNames = Object.keys(codeSample.request.bodyParameters) | ||
paramNames.forEach((elem) => { | ||
requestBodyParams = `${requestBodyParams} --data-urlencode ${elem}=${codeSample.request.bodyParameters[elem]}` | ||
}) | ||
} | ||
} | ||
|
||
const args = [ | ||
operation.verb !== 'get' && `-X ${operation.verb.toUpperCase()}`, | ||
`-H "Accept: ${defaultAcceptHeader}"`, | ||
`${operation.serverUrl}${requestPath}`, | ||
requestBodyParams, | ||
].filter(Boolean) | ||
return `curl \\\n ${args.join(' \\\n ')}` | ||
} | ||
|
||
/* | ||
Generates a GitHub CLI example | ||
For example: | ||
gh api \ | ||
-X POST \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
/repos/OWNER/REPO/deployments \ | ||
-fref,topic-branch=0,payload,{ "deploy": "migrate" }=1,description,Deploy request from hubot=2 | ||
*/ | ||
export function getGHExample(operation: Operation, codeSample: CodeSample) { | ||
const defaultAcceptHeader = codeSample?.response?.contentType?.includes('+json') | ||
? codeSample.response.contentType | ||
: 'application/vnd.github.v3+json' | ||
const hostname = operation.serverUrl !== 'https://api.github.com' ? '--hostname HOSTNAME' : '' | ||
|
||
const requestPath = codeSample?.request?.parameters | ||
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters) | ||
: operation.requestPath | ||
|
||
let requestBodyParams = '' | ||
if (codeSample?.request?.bodyParameters) { | ||
const bodyParamValues = Object.values(codeSample.request.bodyParameters) | ||
// GitHub CLI does not support sending Objects and arrays using the -F or | ||
// -f flags. That support may be added in the future. It is possible to | ||
// use gh api --input to take a JSON object from standard input | ||
// constructed by jq and piped to gh api. However, we'll hold off on adding | ||
// that complexity for now. | ||
if (bodyParamValues.some((elem) => typeof elem === 'object')) { | ||
return undefined | ||
} | ||
requestBodyParams = Object.keys(codeSample.request.bodyParameters) | ||
.map((key) => { | ||
if (typeof codeSample.request.bodyParameters[key] === 'string') { | ||
return `-f ${key}='${codeSample.request.bodyParameters[key]}'` | ||
} else { | ||
return `-F ${key}=${codeSample.request.bodyParameters[key]}` | ||
} | ||
}) | ||
.join(' ') | ||
} | ||
const args = [ | ||
operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`, | ||
`-H "Accept: ${defaultAcceptHeader}"`, | ||
hostname, | ||
requestPath, | ||
requestBodyParams, | ||
].filter(Boolean) | ||
return `gh api \\\n ${args.join(' \\\n ')}` | ||
} | ||
|
||
/* | ||
Generates an octokit.js example | ||
For example: | ||
await octokit.request('POST /repos/{owner}/{repo}/deployments'{ | ||
"owner": "OWNER", | ||
"repo": "REPO", | ||
"ref": "topic-branch", | ||
"payload": "{ \"deploy\": \"migrate\" }", | ||
"description": "Deploy request from hubot" | ||
}) | ||
*/ | ||
export function getJSExample(operation: Operation, codeSample: CodeSample) { | ||
const parameters = codeSample.request | ||
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters } | ||
: {} | ||
return `await octokit.request('${operation.verb.toUpperCase()} ${ | ||
operation.requestPath | ||
}', ${stringify(parameters, null, 2)})` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,92 @@ | ||
import type { xCodeSample } from './types' | ||
import type { Operation } from './types' | ||
import { useTranslation } from 'components/hooks/useTranslation' | ||
import { CodeBlock } from './CodeBlock' | ||
import { Fragment } from 'react' | ||
import { getShellExample, getGHExample, getJSExample } from '../lib/get-rest-code-samples' | ||
|
||
type Props = { | ||
slug: string | ||
xCodeSamples: Array<xCodeSample> | ||
operation: Operation | ||
} | ||
|
||
export function RestCodeSamples({ slug, xCodeSamples }: Props) { | ||
export function RestCodeSamples({ operation, slug }: Props) { | ||
const { t } = useTranslation('products') | ||
|
||
const JAVASCRIPT_HEADING = ( | ||
<span> | ||
JavaScript{' '} | ||
<a className="text-underline" href="https://github.com/octokit/core.js#readme"> | ||
@octokit/core.js | ||
</a> | ||
</span> | ||
) | ||
|
||
const GH_CLI_HEADING = ( | ||
<span> | ||
GitHub CLI{' '} | ||
<a className="text-underline" href="https://cli.github.com/manual/gh_api"> | ||
gh api | ||
</a> | ||
</span> | ||
) | ||
|
||
// Format the example properties into different language examples | ||
const languageExamples = operation.codeExamples.map((sample) => { | ||
const languageExamples = { | ||
curl: getShellExample(operation, sample), | ||
javascript: getJSExample(operation, sample), | ||
ghcli: getGHExample(operation, sample), | ||
} | ||
return Object.assign({}, sample, languageExamples) | ||
}) | ||
|
||
return ( | ||
<Fragment key={xCodeSamples + slug}> | ||
<> | ||
<h4 id={`${slug}--code-samples`}> | ||
<a href={`#${slug}--code-samples`}>{`${t('rest.reference.code_samples')}`}</a> | ||
</h4> | ||
{xCodeSamples.map((sample, index) => { | ||
const sampleElements: JSX.Element[] = [] | ||
if (sample.lang !== 'Ruby') { | ||
sampleElements.push( | ||
<CodeBlock | ||
key={sample.lang + index} | ||
headingLang={sample.lang} | ||
codeBlock={sample.source} | ||
highlight={sample.lang === 'JavaScript' ? 'javascript' : 'curl'} | ||
></CodeBlock> | ||
) | ||
} | ||
return sampleElements | ||
})} | ||
</Fragment> | ||
{languageExamples.map((sample, index) => ( | ||
<div key={`${JSON.stringify(sample)}-${index}`}> | ||
{/* Example requests */} | ||
{sample.request && ( | ||
<> | ||
{/* Title of the code sample block */} | ||
<h5 dangerouslySetInnerHTML={{ __html: sample.request.description }} /> | ||
{sample.curl && ( | ||
<CodeBlock headingLang="Shell" codeBlock={sample.curl} highlight="curl" /> | ||
)} | ||
{sample.javascript && ( | ||
<CodeBlock | ||
headingLang={JAVASCRIPT_HEADING} | ||
codeBlock={sample.javascript} | ||
highlight="javascript" | ||
/> | ||
)} | ||
{sample.ghcli && ( | ||
<CodeBlock headingLang={GH_CLI_HEADING} codeBlock={sample.ghcli} highlight="curl" /> | ||
)} | ||
</> | ||
)} | ||
|
||
{/* Title of the response */} | ||
{sample.response && ( | ||
<> | ||
<h5 dangerouslySetInnerHTML={{ __html: sample.response.description }} /> | ||
{/* Status code */} | ||
{sample.response.statusCode && ( | ||
<CodeBlock codeBlock={`Status: ${sample.response.statusCode}`} /> | ||
)} | ||
|
||
{/* Example response */} | ||
{sample.response.example && ( | ||
<CodeBlock | ||
codeBlock={JSON.stringify(sample.response.example, null, 2)} | ||
highlight="json" | ||
/> | ||
)} | ||
</> | ||
)} | ||
</div> | ||
))} | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.