Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo sync #22629

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/code-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
- .eslintrc.cjs
# In case something like eslint or tsc or prettier upgrades
- 'package-lock.json'
# In case one of the script definitions changed
- 'package.json'
# Ultimately, for debugging this workflow itself
- .github/workflows/code-lint.yml

Expand All @@ -37,14 +39,20 @@ jobs:
- name: Check out repo
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8

- name: Cache node_modules
uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
with:
path: node_modules
key: ${{ runner.os }}-node_modules-${{ hashFiles('package*.json') }}

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516
with:
node-version: '16.17.0'
cache: npm

- name: Install dependencies
run: npm ci
run: npm install --prefer-offline --no-audit --ignore-scripts

- name: Run linter
run: npm run lint
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main-preview-docker-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

steps:
- name: 'Az CLI login'
uses: azure/login@66d2e78565ab7af265d2b627085bc34c73ce6abb
uses: azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf
with:
creds: ${{ secrets.NONPROD_AZURE_CREDENTIALS }}

Expand Down
42 changes: 33 additions & 9 deletions components/lib/get-rest-code-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useMainContext } from 'components/context/MainContext'
type CodeExamples = Record<string, any>
/*
Generates a curl example

For example:
curl \
-X POST \
Expand All @@ -24,6 +23,11 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
? codeSample.response.contentType
: 'application/vnd.github+json'

const contentTypeHeader =
codeSample?.request?.contentType === 'application/octet-stream'
? '-H "Content-Type: application/octet-stream"'
: ''

let requestPath = codeSample?.request?.parameters
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
: operation.requestPath
Expand All @@ -46,14 +50,22 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
const CURL_CONTENT_TYPE_MAPPING: { [key: string]: string } = {
'application/x-www-form-urlencoded': '--data-urlencode',
'multipart/form-data': '--form',
'application/octet-stream': '--data-binary',
}
const contentType = codeSample.request.contentType
if (codeSample.request.contentType in CURL_CONTENT_TYPE_MAPPING) {
requestBodyParams = ''
const paramNames = Object.keys(codeSample.request.bodyParameters)
paramNames.forEach((elem) => {
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
})
// Most of the time the example body parameters have a name and value
// and are included in an object. But, some cases are a single value
// and the type is a string.
if (typeof codeSample.request.bodyParameters === 'object') {
const paramNames = Object.keys(codeSample.request.bodyParameters)
paramNames.forEach((elem) => {
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
})
} else {
requestBodyParams = `${CURL_CONTENT_TYPE_MAPPING[contentType]} "${codeSample.request.bodyParameters}"`
}
}
}

Expand All @@ -71,6 +83,7 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
const args = [
operation.verb !== 'get' && `-X ${operation.verb.toUpperCase()}`,
`-H "Accept: ${defaultAcceptHeader}" \\\n ${authHeader}${apiVersionHeader}`,
contentTypeHeader,
`${operation.serverUrl}${requestPath}`,
requestBodyParams,
].filter(Boolean)
Expand Down Expand Up @@ -101,7 +114,10 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''

let requestBodyParams = ''
if (codeSample?.request?.bodyParameters) {
// Most of the time the example body parameters have a name and value
// and are included in an object. But, some cases are a single value
// and the type is a string.
if (typeof codeSample?.request?.bodyParameters === 'object') {
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
Expand All @@ -120,6 +136,8 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
}
})
.join('\\\n ')
} else {
requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
}
const args = [
operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`,
Expand Down Expand Up @@ -147,9 +165,15 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {

*/
export function getJSExample(operation: Operation, codeSample: CodeSample) {
const parameters = codeSample.request
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
: {}
const parameters =
// Most of the time the example body parameters have a name and value
// and are included in an object. But, some cases are a single value
// and the type is a string.
typeof codeSample.request.bodyParameters === 'object'
? codeSample.request
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
: {}
: { ...codeSample.request.parameters, data: codeSample.request.bodyParameters }

let queryParameters = ''

Expand Down