Skip to content

Commit b2b3358

Browse files
authored
Merge pull request #22629 from github/repo-sync
repo sync
2 parents 92f44d7 + af79c96 commit b2b3358

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

.github/workflows/code-lint.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ on:
1818
- .eslintrc.cjs
1919
# In case something like eslint or tsc or prettier upgrades
2020
- 'package-lock.json'
21+
# In case one of the script definitions changed
22+
- 'package.json'
2123
# Ultimately, for debugging this workflow itself
2224
- .github/workflows/code-lint.yml
2325

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

42+
- name: Cache node_modules
43+
uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
44+
with:
45+
path: node_modules
46+
key: ${{ runner.os }}-node_modules-${{ hashFiles('package*.json') }}
47+
4048
- name: Setup node
4149
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516
4250
with:
4351
node-version: '16.17.0'
4452
cache: npm
4553

4654
- name: Install dependencies
47-
run: npm ci
55+
run: npm install --prefer-offline --no-audit --ignore-scripts
4856

4957
- name: Run linter
5058
run: npm run lint

.github/workflows/main-preview-docker-cache.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030

3131
steps:
3232
- name: 'Az CLI login'
33-
uses: azure/login@66d2e78565ab7af265d2b627085bc34c73ce6abb
33+
uses: azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf
3434
with:
3535
creds: ${{ secrets.NONPROD_AZURE_CREDENTIALS }}
3636

components/lib/get-rest-code-samples.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useMainContext } from 'components/context/MainContext'
88
type CodeExamples = Record<string, any>
99
/*
1010
Generates a curl example
11-
1211
For example:
1312
curl \
1413
-X POST \
@@ -24,6 +23,11 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
2423
? codeSample.response.contentType
2524
: 'application/vnd.github+json'
2625

26+
const contentTypeHeader =
27+
codeSample?.request?.contentType === 'application/octet-stream'
28+
? '-H "Content-Type: application/octet-stream"'
29+
: ''
30+
2731
let requestPath = codeSample?.request?.parameters
2832
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
2933
: operation.requestPath
@@ -46,14 +50,22 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
4650
const CURL_CONTENT_TYPE_MAPPING: { [key: string]: string } = {
4751
'application/x-www-form-urlencoded': '--data-urlencode',
4852
'multipart/form-data': '--form',
53+
'application/octet-stream': '--data-binary',
4954
}
5055
const contentType = codeSample.request.contentType
5156
if (codeSample.request.contentType in CURL_CONTENT_TYPE_MAPPING) {
5257
requestBodyParams = ''
53-
const paramNames = Object.keys(codeSample.request.bodyParameters)
54-
paramNames.forEach((elem) => {
55-
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
56-
})
58+
// Most of the time the example body parameters have a name and value
59+
// and are included in an object. But, some cases are a single value
60+
// and the type is a string.
61+
if (typeof codeSample.request.bodyParameters === 'object') {
62+
const paramNames = Object.keys(codeSample.request.bodyParameters)
63+
paramNames.forEach((elem) => {
64+
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
65+
})
66+
} else {
67+
requestBodyParams = `${CURL_CONTENT_TYPE_MAPPING[contentType]} "${codeSample.request.bodyParameters}"`
68+
}
5769
}
5870
}
5971

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

103116
let requestBodyParams = ''
104-
if (codeSample?.request?.bodyParameters) {
117+
// Most of the time the example body parameters have a name and value
118+
// and are included in an object. But, some cases are a single value
119+
// and the type is a string.
120+
if (typeof codeSample?.request?.bodyParameters === 'object') {
105121
const bodyParamValues = Object.values(codeSample.request.bodyParameters)
106122
// GitHub CLI does not support sending Objects and arrays using the -F or
107123
// -f flags. That support may be added in the future. It is possible to
@@ -120,6 +136,8 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
120136
}
121137
})
122138
.join('\\\n ')
139+
} else {
140+
requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
123141
}
124142
const args = [
125143
operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`,
@@ -147,9 +165,15 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
147165
148166
*/
149167
export function getJSExample(operation: Operation, codeSample: CodeSample) {
150-
const parameters = codeSample.request
151-
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
152-
: {}
168+
const parameters =
169+
// Most of the time the example body parameters have a name and value
170+
// and are included in an object. But, some cases are a single value
171+
// and the type is a string.
172+
typeof codeSample.request.bodyParameters === 'object'
173+
? codeSample.request
174+
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
175+
: {}
176+
: { ...codeSample.request.parameters, data: codeSample.request.bodyParameters }
153177

154178
let queryParameters = ''
155179

0 commit comments

Comments
 (0)