Skip to content

Commit d206121

Browse files
authored
Merge pull request #72 from jordancarney1/generate-void-type
Generates void type when Request QueryArgs are empty.
2 parents 37db9e7 + 451a2c2 commit d206121

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

src/generate.ts

+25-16
Original file line numberDiff line numberDiff line change
@@ -288,29 +288,38 @@ export async function generateApi(
288288
return name;
289289
};
290290

291+
const queryArgValues = Object.values(queryArg);
292+
291293
const QueryArg = factory.createTypeReferenceNode(
292294
registerInterface(
293295
factory.createTypeAliasDeclaration(
294296
undefined,
295297
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
296298
capitalize(getOperationName(verb, path, operation.operationId) + argSuffix),
297299
undefined,
298-
factory.createTypeLiteralNode(
299-
Object.values(queryArg).map((def) => {
300-
const comment = def.origin === 'param' ? def.param.description : def.body.description;
301-
const node = factory.createPropertySignature(
302-
undefined,
303-
propertyName(def.name),
304-
createQuestionToken(!def.required),
305-
def.type
306-
);
307-
308-
if (comment) {
309-
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `* ${comment} `, true);
310-
}
311-
return node;
312-
})
313-
)
300+
queryArgValues.length > 0
301+
? factory.createTypeLiteralNode(
302+
queryArgValues.map((def) => {
303+
const comment = def.origin === 'param' ? def.param.description : def.body.description;
304+
const node = factory.createPropertySignature(
305+
undefined,
306+
propertyName(def.name),
307+
createQuestionToken(!def.required),
308+
def.type
309+
);
310+
311+
if (comment) {
312+
return ts.addSyntheticLeadingComment(
313+
node,
314+
ts.SyntaxKind.MultiLineCommentTrivia,
315+
`* ${comment} `,
316+
true
317+
);
318+
}
319+
return node;
320+
})
321+
)
322+
: factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)
314323
)
315324
).name
316325
);

test/__snapshots__/cli.test.ts.snap

+15-15
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export type UploadFileApiArg = {
176176
export type GetInventoryApiResponse = /** status 200 successful operation */ {
177177
[key: string]: number;
178178
};
179-
export type GetInventoryApiArg = {};
179+
export type GetInventoryApiArg = void;
180180
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
181181
export type PlaceOrderApiArg = {
182182
order: Order;
@@ -208,7 +208,7 @@ export type LoginUserApiArg = {
208208
password?: string;
209209
};
210210
export type LogoutUserApiResponse = unknown;
211-
export type LogoutUserApiArg = {};
211+
export type LogoutUserApiArg = void;
212212
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
213213
export type GetUserByNameApiArg = {
214214
/** The name that needs to be fetched. Use user1 for testing. */
@@ -352,7 +352,7 @@ export const api = createApi({
352352
export type GetHealthcheckApiResponse = /** status 200 OK */ {
353353
message: string;
354354
};
355-
export type GetHealthcheckApiArg = {};
355+
export type GetHealthcheckApiArg = void;
356356
export type UpdatePetApiResponse = /** status 200 Successful operation */ Pet;
357357
export type UpdatePetApiArg = {
358358
/** Update an existent pet in the store */
@@ -404,7 +404,7 @@ export type UploadFileApiArg = {
404404
export type GetInventoryApiResponse = /** status 200 successful operation */ {
405405
[key: string]: number;
406406
};
407-
export type GetInventoryApiArg = {};
407+
export type GetInventoryApiArg = void;
408408
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
409409
export type PlaceOrderApiArg = {
410410
order: Order;
@@ -436,7 +436,7 @@ export type LoginUserApiArg = {
436436
password?: string;
437437
};
438438
export type LogoutUserApiResponse = unknown;
439-
export type LogoutUserApiArg = {};
439+
export type LogoutUserApiArg = void;
440440
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
441441
export type GetUserByNameApiArg = {
442442
/** The name that needs to be fetched. Use user1 for testing. */
@@ -631,7 +631,7 @@ export const api = createApi({
631631
export type GetHealthcheckApiResponse = /** status 200 OK */ {
632632
message: string;
633633
};
634-
export type GetHealthcheckApiArg = {};
634+
export type GetHealthcheckApiArg = void;
635635
export type UpdatePetApiResponse = /** status 200 Successful operation */ Pet;
636636
export type UpdatePetApiArg = {
637637
/** Update an existent pet in the store */
@@ -683,7 +683,7 @@ export type UploadFileApiArg = {
683683
export type GetInventoryApiResponse = /** status 200 successful operation */ {
684684
[key: string]: number;
685685
};
686-
export type GetInventoryApiArg = {};
686+
export type GetInventoryApiArg = void;
687687
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
688688
export type PlaceOrderApiArg = {
689689
order: Order;
@@ -715,7 +715,7 @@ export type LoginUserApiArg = {
715715
password?: string;
716716
};
717717
export type LogoutUserApiResponse = unknown;
718-
export type LogoutUserApiArg = {};
718+
export type LogoutUserApiArg = void;
719719
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
720720
export type GetUserByNameApiArg = {
721721
/** The name that needs to be fetched. Use user1 for testing. */
@@ -932,7 +932,7 @@ export const api = createApi({
932932
export type GetHealthcheckApiResponse = /** status 200 OK */ {
933933
message: string;
934934
};
935-
export type GetHealthcheckApiArg = {};
935+
export type GetHealthcheckApiArg = void;
936936
export type UpdatePetApiResponse = /** status 200 Successful operation */ Pet;
937937
export type UpdatePetApiArg = {
938938
/** Update an existent pet in the store */
@@ -984,7 +984,7 @@ export type UploadFileApiArg = {
984984
export type GetInventoryApiResponse = /** status 200 successful operation */ {
985985
[key: string]: number;
986986
};
987-
export type GetInventoryApiArg = {};
987+
export type GetInventoryApiArg = void;
988988
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
989989
export type PlaceOrderApiArg = {
990990
order: Order;
@@ -1016,7 +1016,7 @@ export type LoginUserApiArg = {
10161016
password?: string;
10171017
};
10181018
export type LogoutUserApiResponse = unknown;
1019-
export type LogoutUserApiArg = {};
1019+
export type LogoutUserApiArg = void;
10201020
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
10211021
export type GetUserByNameApiArg = {
10221022
/** The name that needs to be fetched. Use user1 for testing. */
@@ -1205,7 +1205,7 @@ export type UploadFileApiArg = {
12051205
export type GetInventoryApiResponse = /** status 200 successful operation */ {
12061206
[key: string]: number;
12071207
};
1208-
export type GetInventoryApiArg = {};
1208+
export type GetInventoryApiArg = void;
12091209
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
12101210
export type PlaceOrderApiArg = {
12111211
order: Order;
@@ -1237,7 +1237,7 @@ export type LoginUserApiArg = {
12371237
password?: string;
12381238
};
12391239
export type LogoutUserApiResponse = unknown;
1240-
export type LogoutUserApiArg = {};
1240+
export type LogoutUserApiArg = void;
12411241
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
12421242
export type GetUserByNameApiArg = {
12431243
/** The name that needs to be fetched. Use user1 for testing. */
@@ -1522,7 +1522,7 @@ export type UploadFileApiArg = {
15221522
export type GetInventoryApiResponse = /** status 200 successful operation */ {
15231523
[key: string]: number;
15241524
};
1525-
export type GetInventoryApiArg = {};
1525+
export type GetInventoryApiArg = void;
15261526
export type PlaceOrderApiResponse = /** status 200 successful operation */ Order;
15271527
export type PlaceOrderApiArg = {
15281528
order: Order;
@@ -1554,7 +1554,7 @@ export type LoginUserApiArg = {
15541554
password?: string;
15551555
};
15561556
export type LogoutUserApiResponse = unknown;
1557-
export type LogoutUserApiArg = {};
1557+
export type LogoutUserApiArg = void;
15581558
export type GetUserByNameApiResponse = /** status 200 successful operation */ User;
15591559
export type GetUserByNameApiArg = {
15601560
/** The name that needs to be fetched. Use user1 for testing. */

0 commit comments

Comments
 (0)