Skip to content

Commit 7d5fa42

Browse files
author
Bar Perach
committed
support default return schemas
1 parent 7928634 commit 7d5fa42

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import ts from 'typescript';
2626
const generatedApiName = 'injectedRtkApi';
2727

2828
function defaultIsDataResponse(code: string) {
29+
if (code === "default") {
30+
return true;
31+
}
2932
const parsedCode = Number(code);
3033
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
3134
}
@@ -234,15 +237,15 @@ export async function generateApi(
234237
] as const
235238
)
236239
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
240+
.filter(([_1, _2, type]) => type !== keywordType.void)
237241
.map(([code, response, type]) =>
238242
ts.addSyntheticLeadingComment(
239243
{ ...type },
240244
ts.SyntaxKind.MultiLineCommentTrivia,
241245
`* status ${code} ${response.description} `,
242246
false
243247
)
244-
)
245-
.filter((type) => type !== keywordType.void);
248+
);
246249
if (returnTypes.length > 0) {
247250
ResponseType = factory.createUnionTypeNode(returnTypes);
248251
}

packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export type DeleteOrderApiArg = {
143143
/** ID of the order that needs to be deleted */
144144
orderId: number;
145145
};
146-
export type CreateUserApiResponse = unknown;
146+
export type CreateUserApiResponse = /** status default successful operation */ User;
147147
export type CreateUserApiArg = {
148148
/** Created user object */
149149
user: User;
@@ -363,7 +363,7 @@ export type DeleteOrderApiArg = {
363363
/** ID of the order that needs to be deleted */
364364
orderId: number;
365365
};
366-
export type CreateUserApiResponse = unknown;
366+
export type CreateUserApiResponse = /** status default successful operation */ User;
367367
export type CreateUserApiArg = {
368368
/** Created user object */
369369
user: User;

packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export type DeleteOrderApiArg = {
186186
/** ID of the order that needs to be deleted */
187187
orderId: number;
188188
};
189-
export type CreateUserApiResponse = unknown;
189+
export type CreateUserApiResponse = /** status default successful operation */ User;
190190
export type CreateUserApiArg = {
191191
/** Created user object */
192192
user: User;
@@ -1031,7 +1031,7 @@ export type DeleteOrderApiArg = {
10311031
/** ID of the order that needs to be deleted */
10321032
orderId: number;
10331033
};
1034-
export type CreateUserApiResponse = unknown;
1034+
export type CreateUserApiResponse = /** status default successful operation */ User;
10351035
export type CreateUserApiArg = {
10361036
/** Created user object */
10371037
user: User;
@@ -1389,7 +1389,7 @@ export type DeleteOrderApiArg = {
13891389
/** ID of the order that needs to be deleted */
13901390
orderId: number;
13911391
};
1392-
export type CreateUserApiResponse = unknown;
1392+
export type CreateUserApiResponse = /** status default successful operation */ User;
13931393
export type CreateUserApiArg = {
13941394
/** Created user object */
13951395
user: User;

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ test('calling without `outputFile` returns the generated api', async () => {
2222
expect(api).toMatchSnapshot();
2323
});
2424

25+
test('should set response type for request with default response type', async () => {
26+
const api = await generateEndpoints({
27+
apiFile: './fixtures/emptyApi.ts',
28+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
29+
});
30+
// eslint-disable-next-line no-template-curly-in-string
31+
expect(api).toMatch(/export type CreateUserApiResponse =[\s\S/*]+status default successful operation[\s/*]+User;/);
32+
});
33+
2534
test('endpoint filtering', async () => {
2635
const api = await generateEndpoints({
2736
unionUndefined: true,

0 commit comments

Comments
 (0)