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

Function getRequestExamples

src/rest/scripts/utils/create-rest-examples.js:129–218  ·  view source on GitHub ↗
(operation)

Source from the content-addressed store, hash-verified

127 }
128*/
129export function getRequestExamples(operation) {
130 const requestExamples = []
131 const parameterExamples = getParameterExamples(operation)
132
133 // When no request body or parameters are defined, we create a generic
134 // request example. Not all operations have request bodies or parameters,
135 // but we always want to show at least an example with the path.
136 if (!operation.requestBody && Object.keys(parameterExamples).length === 0) {
137 return [
138 {
139 key: DEFAULT_EXAMPLE_KEY,
140 request: {
141 description: DEFAULT_EXAMPLE_DESCRIPTION,
142 acceptHeader: DEFAULT_ACCEPT_HEADER,
143 },
144 },
145 ]
146 }
147
148 // When no request body exists, we create an example from the parameters
149 if (!operation.requestBody) {
150 return Object.keys(parameterExamples).map((key) => {
151 return {
152 key,
153 request: {
154 description: DEFAULT_EXAMPLE_DESCRIPTION,
155 acceptHeader: DEFAULT_ACCEPT_HEADER,
156 parameters: parameterExamples[key] || parameterExamples.default,
157 },
158 }
159 })
160 }
161
162 // Requests can have multiple content types each with their own set of
163 // examples.
164 Object.keys(operation.requestBody.content).forEach((contentType) => {
165 let examples = {}
166 // This is a fallback to allow using the `example` property in
167 // the schema. If we start to enforce using examples vs. example using
168 // a linter, we can remove the check for `example`.
169 // For now, we'll use the key default, which is a common default
170 // example name in the OpenAPI schema.
171 if (operation.requestBody.content[contentType].example) {
172 examples = {
173 default: {
174 value: operation.requestBody.content[contentType].example,
175 },
176 }
177 } else if (operation.requestBody.content[contentType].examples) {
178 examples = operation.requestBody.content[contentType].examples
179 } else {
180 // Example for this content type doesn't exist so we'll try and create one
181 requestExamples.push({
182 key: DEFAULT_EXAMPLE_KEY,
183 request: {
184 contentType,
185 description: DEFAULT_EXAMPLE_DESCRIPTION,
186 acceptHeader: DEFAULT_ACCEPT_HEADER,

Callers 1

getCodeSamplesFunction · 0.85

Calls 1

getParameterExamplesFunction · 0.85

Tested by

no test coverage detected