1
0
mirror of synced 2025-12-23 03:44:00 -05:00

Add required query params to examples (#32248)

This commit is contained in:
Grace Park
2022-11-10 13:17:34 -08:00
committed by GitHub
parent fc85041a08
commit a0536f8063
2 changed files with 71 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import { stringify } from 'javascript-stringify'
import type { CodeSample, Operation } from '../rest/types'
type CodeExamples = Record<string, any>
/*
Generates a curl example
@@ -19,10 +20,13 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
? codeSample.response.contentType
: 'application/vnd.github+json'
const requestPath = codeSample?.request?.parameters
let requestPath = codeSample?.request?.parameters
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
: operation.requestPath
const requiredQueryParams = getRequiredQueryParamsPath(operation, codeSample)
requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''
let requestBodyParams = ''
if (codeSample?.request?.bodyParameters) {
requestBodyParams = `-d '${JSON.stringify(codeSample.request.bodyParameters).replace(
@@ -73,10 +77,13 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
: 'application/vnd.github+json'
const hostname = operation.serverUrl !== 'https://api.github.com' ? '--hostname HOSTNAME' : ''
const requestPath = codeSample?.request?.parameters
let requestPath = codeSample?.request?.parameters
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
: operation.requestPath
const requiredQueryParams = getRequiredQueryParamsPath(operation, codeSample)
requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''
let requestBodyParams = ''
if (codeSample?.request?.bodyParameters) {
const bodyParamValues = Object.values(codeSample.request.bodyParameters)
@@ -130,19 +137,23 @@ export function getJSExample(operation: Operation, codeSample: CodeSample) {
let queryParameters = ''
// Add query parameters to the request path for POST and PUT operations in
// Add query parameters to the request path for POST, PUT, DELETE, GET, operations in
// URL template format e.g. 'POST /repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}'
if (operation.verb === 'post' || operation.verb === 'put') {
const queryParms = operation.parameters
if (
operation.verb === 'post' ||
operation.verb === 'put' ||
operation.verb === 'delete' ||
operation.verb === 'get'
) {
const queryParams = operation.parameters
.filter((param) => {
return param.in === 'query'
})
.map((param) => {
return param.name
})
if (queryParms.length > 0) {
queryParameters = `{?${queryParms.join(',')}}`
if (queryParams.length > 0) {
queryParameters = `{?${queryParams.join(',')}}`
}
}
const comment = `// Octokit.js\n// https://github.com/octokit/core.js#readme\n`
@@ -152,3 +163,54 @@ export function getJSExample(operation: Operation, codeSample: CodeSample) {
operation.requestPath
}${queryParameters}', ${stringify(parameters, null, 2)})`
}
// Every code example parameter object can be slightly different depending on the operation. For e.g. for Packages it's something like this:
// [
// {
// "id": 197,
// "name": "hello_docker",
// "package_type": "container",
// },
// {
// "id": 198,
// "name": "goodbye_docker",
// "package_type": "container",
// }
// ]
// But for Actions cache it's something like this:
// {
// "total_count": 1,
// "actions_caches": [
// {
// "id": 505,
// "ref": "refs/heads/main",
// "key": "Linux-node-958aff96db2d75d67787d1e634ae70b659de937b",
// }
// ]
// }
// We need to find the matching key so this is using JSON.stringify to handle the "recursion" to search for the matching key.
function findMatchingQueryKey(exampleObj: CodeExamples | CodeExamples[], matchKey: string) {
let match: string | null = null
JSON.stringify(exampleObj, (_, nestedValue) => {
if (nestedValue && Object.prototype.hasOwnProperty.call(nestedValue, matchKey)) {
match = nestedValue[matchKey]
}
return nestedValue
})
return match
}
function getRequiredQueryParamsPath(operation: Operation, codeSample: CodeSample) {
const requiredQueryParams = new URLSearchParams()
for (const param of operation.parameters) {
if (param.in === 'query' && param.required === true) {
const codeExamples = codeSample.response?.example
const match = findMatchingQueryKey(codeExamples, param.name)
requiredQueryParams.append(param.name, match || param.name.toUpperCase())
}
}
return requiredQueryParams.toString()
}