Skip to content

Commit

Permalink
Merge pull request #4152 from barp/master
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 30, 2024
2 parents 586184e + 7d5fa42 commit c6371df
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 @@ -22,6 +22,9 @@ import { factory } from './utils/factory';
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 @@ -232,15 +235,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 @@ -167,7 +167,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 @@ -414,7 +414,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 @@ -167,7 +167,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 @@ -1647,7 +1647,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 @@ -2048,7 +2048,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 @@ -26,6 +26,15 @@ test('calling without `outputFile` returns the generated api', async () => {
expect(api).toMatchSnapshot();

Check failure on line 26 in packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

View workflow job for this annotation

GitHub Actions / Test final build artifact with Node.js 20.x on ubuntu-latest

test/generateEndpoints.test.ts > calling without `outputFile` returns the generated api

Error: Snapshot `calling without `outputFile` returns the generated api 1` mismatched - Expected + Received @@ -215,11 +215,12 @@ export type DeleteOrderApiResponse = unknown; 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; }; export type CreateUsersWithListInputApiResponse = ❯ test/generateEndpoints.test.ts:26:15
});

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 c6371df

Please sign in to comment.