1
0
mirror of synced 2025-12-19 18:10:59 -05:00

Fix GH CLI examples that contain arrays of objects (#49180)

This commit is contained in:
Evan Bonsignori
2024-02-28 10:36:59 -08:00
committed by GitHub
parent 8e09c3bd55
commit 10e50e7a3f
2 changed files with 19 additions and 11 deletions

View File

@@ -139,7 +139,7 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
const { bodyParameters } = codeSample.request const { bodyParameters } = codeSample.request
if (bodyParameters) { if (bodyParameters) {
if (typeof bodyParameters === 'object') { if (typeof bodyParameters === 'object') {
const bodyParamValues = Object.values(codeSample.request.bodyParameters) const bodyParamValues = Object.values(bodyParameters)
// GitHub CLI does not support sending Objects using the -F or // GitHub CLI does not support sending Objects using the -F or
// -f flags. That support may be added in the future. It is possible to // -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 // use gh api --input to take a JSON object from standard input
@@ -148,23 +148,31 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
if (bodyParamValues.some((elem) => typeof elem === 'object' && !Array.isArray(elem))) { if (bodyParamValues.some((elem) => typeof elem === 'object' && !Array.isArray(elem))) {
return undefined return undefined
} }
requestBodyParams = Object.keys(codeSample.request.bodyParameters) requestBodyParams = Object.entries(bodyParameters)
.map((key) => { .map(([key, params]) => {
if (typeof codeSample.request.bodyParameters[key] === 'string') { if (typeof params === 'string') {
return `-f ${key}='${codeSample.request.bodyParameters[key]}' ` return `-f ${key}='${params}' `
} else if (Array.isArray(codeSample.request.bodyParameters[key])) { } else if (Array.isArray(params)) {
let cliLine = '' let cliLine = ''
for (const value of codeSample.request.bodyParameters[key]) { for (const param of params) {
cliLine += `${typeof value === 'string' ? '-f' : '-F'} "${key}[]=${value}" ` if (typeof param === 'string') {
cliLine += `-f "${key}[]=${param}" `
} else {
// When an array of objects is sent, the CLI takes the key and value as two separate arguments
// E.g. -F "properties[][property_name]=repo" -F "properties[][value]=docs-internal"
for (const [k, v] of Object.entries(param)) {
cliLine += `-F "${key}[][${k}]=${v}" `
}
}
} }
return cliLine return cliLine
} else { } else {
return `-F ${key}=${codeSample.request.bodyParameters[key]} ` return `-F ${key}=${params} `
} }
}) })
.join('\\\n ') .join('\\\n ')
} else { } else {
requestBodyParams = `-f '${codeSample.request.bodyParameters}'` requestBodyParams = `-f '${bodyParameters}'`
} }
} }

View File

@@ -54,7 +54,7 @@ export interface CodeSample {
request: { request: {
contentType: string contentType: string
acceptHeader: string acceptHeader: string
bodyParameters: Record<string, string | Array<string>> bodyParameters: Record<string, string | Array<string | { [key: string]: string }>>
parameters: Record<string, string> parameters: Record<string, string>
description: string description: string
} }