Skip to content

Commit

Permalink
support default return schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Bar Perach committed Feb 3, 2024
1 parent ea31b84 commit fd24979
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import ts from 'typescript';
const generatedApiName = 'injectedRtkApi';

function defaultIsDataResponse(code: string) {
if (code === "default") {
return true;
}
const parsedCode = Number(code);
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
}
Expand Down Expand Up @@ -234,15 +237,15 @@ export async function generateApi(
] as const
)
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
.filter(([_1, _2, type]) => type !== keywordType.void)
.map(([code, response, type]) =>
ts.addSyntheticLeadingComment(
{ ...type },
ts.SyntaxKind.MultiLineCommentTrivia,
`* status ${code} ${response.description} `,
false
)
)
.filter((type) => type !== keywordType.void);
);
if (returnTypes.length > 0) {
ResponseType = factory.createUnionTypeNode(returnTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse = unknown;
export type CreateUserApiResponse = /** status default successful operation */ User;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -363,7 +363,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse = unknown;
export type CreateUserApiResponse = /** status default successful operation */ User;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse = unknown;
export type CreateUserApiResponse = /** status default successful operation */ User;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1031,7 +1031,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse = unknown;
export type CreateUserApiResponse = /** status default successful operation */ User;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1389,7 +1389,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse = unknown;
export type CreateUserApiResponse = /** status default successful operation */ User;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ test('calling without `outputFile` returns the generated api', async () => {
expect(api).toMatchSnapshot();
});

test('should set response type for request with default response type', async () => {
const api = await generateEndpoints({
apiFile: './fixtures/emptyApi.ts',
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
});
// eslint-disable-next-line no-template-curly-in-string
expect(api).toMatch(/export type CreateUserApiResponse =[\s\S/*]+status default successful operation[\s/*]+User;/);
});

test('endpoint filtering', async () => {
const api = await generateEndpoints({
unionUndefined: true,
Expand Down

0 comments on commit fd24979

Please sign in to comment.