diff --git a/.github/workflows/code-lint.yml b/.github/workflows/code-lint.yml index a13aad23f51e..c4b0ef2563e9 100644 --- a/.github/workflows/code-lint.yml +++ b/.github/workflows/code-lint.yml @@ -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 @@ -37,6 +39,12 @@ 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: @@ -44,7 +52,7 @@ jobs: cache: npm - name: Install dependencies - run: npm ci + run: npm install --prefer-offline --no-audit --ignore-scripts - name: Run linter run: npm run lint diff --git a/.github/workflows/main-preview-docker-cache.yml b/.github/workflows/main-preview-docker-cache.yml index d942721aaae1..2f74edacd0d1 100644 --- a/.github/workflows/main-preview-docker-cache.yml +++ b/.github/workflows/main-preview-docker-cache.yml @@ -30,7 +30,7 @@ jobs: steps: - name: 'Az CLI login' - uses: azure/login@66d2e78565ab7af265d2b627085bc34c73ce6abb + uses: azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf with: creds: ${{ secrets.NONPROD_AZURE_CREDENTIALS }} diff --git a/components/lib/get-rest-code-samples.ts b/components/lib/get-rest-code-samples.ts index 26f4bd08d4d6..c00a3367aaf1 100644 --- a/components/lib/get-rest-code-samples.ts +++ b/components/lib/get-rest-code-samples.ts @@ -8,7 +8,6 @@ import { useMainContext } from 'components/context/MainContext' type CodeExamples = Record /* Generates a curl example - For example: curl \ -X POST \ @@ -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 @@ -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}"` + } } } @@ -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) @@ -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 @@ -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()}`, @@ -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 = ''