Skip to content

Commit add87c3

Browse files
committed
feat: support for array bodies using refs
refactor: minor code refactor for readbility
1 parent 4e3aa19 commit add87c3

1 file changed

Lines changed: 60 additions & 52 deletions

File tree

lib/process-document.ts

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,6 @@ export async function processOpenApiDocument(
201201
? operationObject.requestBody
202202
: undefined;
203203

204-
const requestBodyObjectJson =
205-
requestBodyObject?.content['application/json'];
206-
207-
const requestBodyObjectJsonSchemaRef =
208-
requestBodyObjectJson?.schema &&
209-
'$ref' in requestBodyObjectJson.schema
210-
? requestBodyObjectJson?.schema
211-
: undefined;
212-
213204
const queryParameters: OpenAPIV3.ParameterObject[] = [];
214205

215206
const parameters = [
@@ -284,53 +275,72 @@ export async function processOpenApiDocument(
284275
})
285276
: undefined;
286277

278+
const hasRequiredQueryParam = queryParameters.some((p) => p.required);
279+
287280
ensureImport(queryType);
288281

289-
const hasRequiredQueryParam = queryParameters.some((p) => p.required);
282+
const requestBodyObjectJson =
283+
requestBodyObject?.content['application/json'];
284+
285+
const requestBodyIsArray =
286+
requestBodyObjectJson?.schema &&
287+
'type' in requestBodyObjectJson.schema &&
288+
requestBodyObjectJson.schema?.type === 'array';
289+
290+
// get the ref from the schema or the array items
291+
const requestBodyObjectJsonSchema =
292+
(requestBodyObjectJson?.schema &&
293+
(('$ref' in requestBodyObjectJson.schema &&
294+
requestBodyObjectJson?.schema) ||
295+
(requestBodyIsArray &&
296+
'items' in requestBodyObjectJson.schema &&
297+
'$ref' in requestBodyObjectJson.schema.items &&
298+
requestBodyObjectJson.schema.items))) ||
299+
undefined;
290300

291301
const bodyType =
292-
requestBodyObjectJsonSchemaRef &&
293-
typesAndInterfaces.get(requestBodyObjectJsonSchemaRef.$ref);
302+
requestBodyObjectJsonSchema &&
303+
typesAndInterfaces.get(requestBodyObjectJsonSchema.$ref);
294304

295305
ensureImport(bodyType);
296306

297-
const paramsParam =
298-
bodyType || queryType
299-
? func.addParameter({
300-
name: 'parameters',
301-
hasQuestionToken: !bodyType && !hasRequiredQueryParam,
302-
type: Writers.objectType({
303-
properties: [
304-
...(bodyType
305-
? [
306-
{
307-
name: 'body',
308-
type: `Simplify<${bodyType.getName()}>`,
309-
hasQuestionToken: false,
310-
},
311-
]
312-
: []),
313-
...(queryType
314-
? [
315-
{
316-
name: 'query',
317-
type: queryType.getName(),
318-
hasQuestionToken: !hasRequiredQueryParam,
319-
},
320-
]
321-
: []),
322-
],
323-
}),
324-
// type: `${commandParamsType.getName()}<${
325-
// bodyType?.getName() || 'never'
326-
// }, ${queryType?.getName() || 'never'}>`,
327-
})
328-
: undefined;
307+
const paramsParamName = 'parameters';
308+
309+
if (bodyType || queryType) {
310+
func.addParameter({
311+
name: paramsParamName,
312+
hasQuestionToken: !bodyType && !hasRequiredQueryParam,
313+
type: Writers.objectType({
314+
properties: [
315+
...(bodyType
316+
? [
317+
{
318+
name: 'body',
319+
type: `Simplify<${bodyType.getName()}>${
320+
requestBodyIsArray ? '[]' : ''
321+
}`,
322+
hasQuestionToken: false,
323+
},
324+
]
325+
: []),
326+
...(queryType
327+
? [
328+
{
329+
name: 'query',
330+
type: queryType.getName(),
331+
hasQuestionToken: !hasRequiredQueryParam,
332+
},
333+
]
334+
: []),
335+
],
336+
}),
337+
});
338+
}
329339

330340
if (bodyType) {
331341
jsdoc.addTag({
332342
tagName: 'param',
333-
text: `${paramsParam?.getName()}.body {${bodyType.getName()}} ${maybeJsDocDescription()}`.trim(),
343+
text: `${paramsParamName}.body {${bodyType.getName()}} ${maybeJsDocDescription()}`.trim(),
334344
});
335345
}
336346

@@ -339,7 +349,7 @@ export async function processOpenApiDocument(
339349

340350
jsdoc.addTag({
341351
tagName: 'param',
342-
text: `${paramsParam?.getName()}.query.${queryParameterName}${
352+
text: `${paramsParamName}.query.${queryParameterName}${
343353
queryParam.required ? '' : '?'
344354
} {String} ${maybeJsDocDescription(
345355
queryParam.deprecated && 'DEPRECATED',
@@ -428,12 +438,10 @@ export async function processOpenApiDocument(
428438
initializer: Writers.object({
429439
method: Writers.assertion((w) => w.quote(method), 'const'),
430440
pathname: `\`${path.replaceAll(/{/g, '${')}\``,
431-
...(queryType &&
432-
paramsParam && {
433-
query: `${paramsParam.getName()}?.query`,
434-
}),
435-
...(bodyType &&
436-
paramsParam && { body: `${paramsParam.getName()}.body` }),
441+
...(queryType && {
442+
query: `${paramsParamName}?.query`,
443+
}),
444+
...(bodyType && { body: `${paramsParamName}.body` }),
437445
}),
438446
},
439447
],

0 commit comments

Comments
 (0)