Skip to content

Commit

Permalink
Merge pull request #4591 from bayasdev/include-default
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 31, 2024
2 parents 8b32304 + fa5b012 commit 4ccd524
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
9 changes: 6 additions & 3 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { factory } from './utils/factory';
const generatedApiName = 'injectedRtkApi';
const v3DocCache: Record<string, OpenAPIV3.Document> = {};

function defaultIsDataResponse(code: string) {
if (code === 'default') {
function defaultIsDataResponse(code: string, includeDefault: boolean) {
if (includeDefault && code === 'default') {
return true;
}
const parsedCode = Number(code);
Expand Down Expand Up @@ -112,6 +112,7 @@ export async function generateApi(
unionUndefined,
encodeParams = false,
flattenArg = false,
includeDefault = false,
useEnumType = false,
mergeReadWriteOnly = false,
httpResolverOptions,
Expand Down Expand Up @@ -253,7 +254,9 @@ export async function generateApi(
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
] as const
)
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
.filter(([status, response]) =>
isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
)
.filter(([_1, _2, type]) => type !== keywordType.void)
.map(([code, response, type]) =>
ts.addSyntheticLeadingComment(
Expand Down
7 changes: 7 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type GenerationOptions = Id<
Optional<OutputFileOptions, 'outputFile'> & {
isDataResponse?(
code: string,
includeDefault: boolean,
response: OpenAPIV3.ResponseObject,
allResponses: OpenAPIV3.ResponsesObject
): boolean;
Expand Down Expand Up @@ -82,6 +83,12 @@ export interface CommonOptions {
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
*/
flattenArg?: boolean;
/**
* default to false
* If set to `true`, the default response type will be included in the generated code for all endpoints.
* @see https://swagger.io/docs/specification/describing-responses/#default
*/
includeDefault?: boolean;
/**
* default to false
* `true` will not generate separate types for read-only and write-only properties.
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 = /** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
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 = /** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
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 = /** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
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 = /** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -714,8 +714,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1125,8 +1124,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1404,8 +1402,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1702,8 +1699,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -1986,8 +1982,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -2262,8 +2257,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -2757,8 +2751,7 @@ export type DeleteOrderV2ApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserV2ApiResponse =
/** status default successful operation */ User;
export type CreateUserV2ApiResponse = unknown;
export type CreateUserV2ApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -3385,8 +3378,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down Expand Up @@ -3787,8 +3779,7 @@ export type DeleteOrderApiArg = {
/** ID of the order that needs to be deleted */
orderId: number;
};
export type CreateUserApiResponse =
/** status default successful operation */ User;
export type CreateUserApiResponse = unknown;
export type CreateUserApiArg = {
/** Created user object */
user: User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ test('calling without `outputFile` returns the generated api', async () => {
expect(api).toMatchSnapshot();
});

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

0 comments on commit 4ccd524

Please sign in to comment.