MCPcopy Index your code
hub / github.com/github/docs / getJSExample

Function getJSExample

components/lib/get-rest-code-samples.ts:169–216  ·  view source on GitHub ↗
(operation: Operation, codeSample: CodeSample)

Source from the content-addressed store, hash-verified

167
168*/
169export function getJSExample(operation: Operation, codeSample: CodeSample) {
170 const parameters: { [key: string]: string } = {}
171 if (codeSample.request) {
172 Object.assign(parameters, codeSample.request.parameters)
173 // Most of the time the example body parameters have a name and value
174 // and are included in an object. But, some cases are a single value
175 // and the type is a string.
176 if (
177 codeSample.request.bodyParameters &&
178 typeof codeSample.request.bodyParameters !== 'object'
179 ) {
180 parameters.data = codeSample.request.bodyParameters
181 } else {
182 Object.assign(parameters, codeSample.request.bodyParameters)
183 }
184 }
185
186 let queryParameters = ''
187
188 // Query parameters are set automatically for GET and HEAD requests, we
189 // otherwise have to handle it ourselves for other request methods by adding
190 // the parameters to the request path in URL template format e.g.:
191 //
192 // 'POST /repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}'
193 if (
194 operation.verb === 'delete' ||
195 operation.verb === 'patch' ||
196 operation.verb === 'post' ||
197 operation.verb === 'put'
198 ) {
199 const queryParams = operation.parameters
200 .filter((param) => {
201 return param.in === 'query'
202 })
203 .map((param) => {
204 return param.name
205 })
206 if (queryParams.length > 0) {
207 queryParameters = `{?${queryParams.join(',')}}`
208 }
209 }
210 const comment = `// Octokit.js\n// https://github.com/octokit/core.js#readme\n`
211 const require = `const octokit = new Octokit(${stringify({ auth: 'YOUR-TOKEN' }, null, 2)})\n\n`
212
213 return `${comment}${require}await octokit.request('${operation.verb.toUpperCase()} ${
214 operation.requestPath
215 }${queryParameters}', ${stringify(parameters, null, 2)})`
216}
217
218// Every code example parameter object can be slightly different depending on the operation. For e.g. for Packages it's something like this:
219// [

Callers 1

RestCodeSamplesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected