Skip to content

Commit e6cdf21

Browse files
rajzikjan.silhan
andauthored
fix: Issue with generated utils. (#291)
Co-authored-by: jan.silhan <[email protected]>
1 parent f795e7c commit e6cdf21

File tree

2 files changed

+61
-156
lines changed

2 files changed

+61
-156
lines changed

plugins/typescript/src/generators/generateReactQueryComponents.test.ts

Lines changed: 58 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ describe("generateReactQueryComponents", () => {
8484
config
8585
);
8686

87-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
88-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
87+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
88+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
8989
"/**
9090
* Generated by @openapi-codegen
9191
*
@@ -250,8 +250,8 @@ describe("generateReactQueryComponents", () => {
250250
{ ...config }
251251
);
252252

253-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
254-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
253+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
254+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
255255
"/**
256256
* Generated by @openapi-codegen
257257
*
@@ -441,8 +441,8 @@ describe("generateReactQueryComponents", () => {
441441
config
442442
);
443443

444-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
445-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
444+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
445+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
446446
"/**
447447
* Generated by @openapi-codegen
448448
*
@@ -647,8 +647,8 @@ describe("generateReactQueryComponents", () => {
647647
{ ...config }
648648
);
649649

650-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
651-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
650+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
651+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
652652
"/**
653653
* Generated by @openapi-codegen
654654
*
@@ -839,8 +839,8 @@ describe("generateReactQueryComponents", () => {
839839
config
840840
);
841841

842-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
843-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
842+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
843+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
844844
"/**
845845
* Generated by @openapi-codegen
846846
*
@@ -1032,8 +1032,8 @@ describe("generateReactQueryComponents", () => {
10321032
{ ...config }
10331033
);
10341034

1035-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
1036-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
1035+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1036+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
10371037
"/**
10381038
* Generated by @openapi-codegen
10391039
*
@@ -1239,8 +1239,8 @@ describe("generateReactQueryComponents", () => {
12391239
{ ...config, injectedHeaders: ["breed"] }
12401240
);
12411241

1242-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
1243-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
1242+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1243+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
12441244
"/**
12451245
* Generated by @openapi-codegen
12461246
*
@@ -1438,8 +1438,8 @@ describe("generateReactQueryComponents", () => {
14381438
config
14391439
);
14401440

1441-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
1442-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
1441+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1442+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
14431443
"/**
14441444
* Generated by @openapi-codegen
14451445
*
@@ -1645,8 +1645,8 @@ describe("generateReactQueryComponents", () => {
16451645
config
16461646
);
16471647

1648-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
1649-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
1648+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1649+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
16501650
"/**
16511651
* Generated by @openapi-codegen
16521652
*
@@ -1800,8 +1800,8 @@ describe("generateReactQueryComponents", () => {
18001800
config
18011801
);
18021802

1803-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
1804-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
1803+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1804+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
18051805
"/**
18061806
* Generated by @openapi-codegen
18071807
*
@@ -2003,8 +2003,8 @@ describe("generateReactQueryComponents", () => {
20032003
config
20042004
);
20052005

2006-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
2007-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2006+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
2007+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
20082008
"/**
20092009
* Generated by @openapi-codegen
20102010
*
@@ -2164,8 +2164,8 @@ describe("generateReactQueryComponents", () => {
21642164
config
21652165
);
21662166

2167-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
2168-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2167+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
2168+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
21692169
"/**
21702170
* Generated by @openapi-codegen
21712171
*
@@ -2298,8 +2298,8 @@ describe("generateReactQueryComponents", () => {
22982298
config
22992299
);
23002300

2301-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
2302-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2301+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
2302+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
23032303
"/**
23042304
* Generated by @openapi-codegen
23052305
*
@@ -2436,8 +2436,8 @@ describe("generateReactQueryComponents", () => {
24362436
config
24372437
);
24382438

2439-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
2440-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2439+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
2440+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
24412441
"/**
24422442
* Generated by @openapi-codegen
24432443
*
@@ -2574,8 +2574,8 @@ describe("generateReactQueryComponents", () => {
25742574
config
25752575
);
25762576

2577-
expect(writeFile.mock.calls[1][0]).toBe("petstoreComponents.ts");
2578-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2577+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
2578+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
25792579
"/**
25802580
* Generated by @openapi-codegen
25812581
*
@@ -2712,8 +2712,8 @@ describe("generateReactQueryComponents", () => {
27122712
{ ...config, filenamePrefix: "" }
27132713
);
27142714

2715-
expect(writeFile.mock.calls[1][0]).toBe("components.ts");
2716-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2715+
expect(writeFile.mock.calls[0][0]).toBe("components.ts");
2716+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
27172717
"/**
27182718
* Generated by @openapi-codegen
27192719
*
@@ -2824,136 +2824,39 @@ describe("generateReactQueryComponents", () => {
28242824
{
28252825
openAPIDocument,
28262826
writeFile,
2827-
existsFile: () => true,
2827+
existsFile: (file) => !file.includes("Utils"),
28282828
readFile: async () => "",
28292829
},
28302830
config
28312831
);
28322832

28332833
expect(writeFile.mock.calls[0][0]).toBe("petstoreUtils.ts");
2834-
expect(writeFile.mock.calls[1][1]).toMatchInlineSnapshot(`
2835-
"/**
2836-
* Generated by @openapi-codegen
2837-
*
2838-
* @version 1.0.0
2839-
*/
2840-
import * as reactQuery from "@tanstack/react-query";
2841-
import {
2842-
usePetstoreContext,
2843-
PetstoreContext,
2844-
queryKeyFn,
2845-
} from "./petstoreContext";
2846-
import { deepMerge } from "./petstoreUtils";
2847-
import type * as Fetcher from "./petstoreFetcher";
2848-
import { petstoreFetch } from "./petstoreFetcher";
2849-
import type * as Schemas from "./petstoreSchemas";
2850-
import type { ServerErrorStatus } from "./petstoreUtils";
2851-
2852-
type QueryFnOptions = {
2853-
signal?: AbortController["signal"];
2854-
};
2855-
2856-
export type ListPetsError = Fetcher.ErrorWrapper<{
2857-
status: ServerErrorStatus;
2858-
payload: string;
2859-
}>;
2860-
2861-
export type ListPetsResponse = Schemas.Pet[];
2862-
2863-
export type ListPetsVariables = PetstoreContext["fetcherOptions"];
2864-
2865-
/**
2866-
* Get all the pets
2867-
*/
2868-
export const fetchListPets = (
2869-
variables: ListPetsVariables,
2870-
signal?: AbortSignal,
2871-
) =>
2872-
petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
2873-
url: "/pets",
2874-
method: "get",
2875-
...variables,
2876-
signal,
2877-
});
2878-
2879-
/**
2880-
* Get all the pets
2881-
*/
2882-
export function listPetsQuery(variables: ListPetsVariables): {
2883-
queryKey: reactQuery.QueryKey;
2884-
queryFn: (options: QueryFnOptions) => Promise<ListPetsResponse>;
2885-
};
2886-
2887-
export function listPetsQuery(
2888-
variables: ListPetsVariables | reactQuery.SkipToken,
2889-
): {
2890-
queryKey: reactQuery.QueryKey;
2891-
queryFn:
2892-
| ((options: QueryFnOptions) => Promise<ListPetsResponse>)
2893-
| reactQuery.SkipToken;
2894-
};
2895-
2896-
export function listPetsQuery(
2897-
variables: ListPetsVariables | reactQuery.SkipToken,
2898-
) {
2899-
return {
2900-
queryKey: queryKeyFn({
2901-
path: "/pets",
2902-
operationId: "listPets",
2903-
variables,
2904-
}),
2905-
queryFn:
2906-
variables === reactQuery.skipToken
2907-
? reactQuery.skipToken
2908-
: ({ signal }: QueryFnOptions) => fetchListPets(variables, signal),
2909-
};
2834+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
2835+
"type ComputeRange<
2836+
N extends number,
2837+
Result extends Array<unknown> = [],
2838+
> = Result["length"] extends N
2839+
? Result
2840+
: ComputeRange<N, [...Result, Result["length"]]>;
2841+
2842+
export type ClientErrorStatus = Exclude<
2843+
ComputeRange<500>[number],
2844+
ComputeRange<400>[number]
2845+
>;
2846+
export type ServerErrorStatus = Exclude<
2847+
ComputeRange<600>[number],
2848+
ComputeRange<500>[number]
2849+
>;
2850+
2851+
export function deepMerge<T, U extends T>(target: T, source: U): U {
2852+
const returnType = (target || {}) as U;
2853+
for (const key in source) {
2854+
if (source[key] instanceof Object)
2855+
Object.assign(source[key], deepMerge(returnType[key], source[key]));
2856+
}
2857+
Object.assign(returnType || {}, source);
2858+
return returnType;
29102859
}
2911-
2912-
/**
2913-
* Get all the pets
2914-
*/
2915-
export const useSuspenseListPets = <TData = ListPetsResponse,>(
2916-
variables: ListPetsVariables,
2917-
options?: Omit<
2918-
reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>,
2919-
"queryKey" | "queryFn" | "initialData"
2920-
>,
2921-
) => {
2922-
const { queryOptions, fetcherOptions } = usePetstoreContext(options);
2923-
return reactQuery.useSuspenseQuery<ListPetsResponse, ListPetsError, TData>({
2924-
...listPetsQuery(deepMerge(fetcherOptions, variables)),
2925-
...options,
2926-
...queryOptions,
2927-
});
2928-
};
2929-
2930-
/**
2931-
* Get all the pets
2932-
*/
2933-
export const useListPets = <TData = ListPetsResponse,>(
2934-
variables: ListPetsVariables | reactQuery.SkipToken,
2935-
options?: Omit<
2936-
reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>,
2937-
"queryKey" | "queryFn" | "initialData"
2938-
>,
2939-
) => {
2940-
const { queryOptions, fetcherOptions } = usePetstoreContext(options);
2941-
return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>({
2942-
...listPetsQuery(
2943-
variables === reactQuery.skipToken
2944-
? variables
2945-
: deepMerge(fetcherOptions, variables),
2946-
),
2947-
...options,
2948-
...queryOptions,
2949-
});
2950-
};
2951-
2952-
export type QueryOperation = {
2953-
path: "/pets";
2954-
operationId: "listPets";
2955-
variables: ListPetsVariables | reactQuery.SkipToken;
2956-
};
29572860
"
29582861
`);
29592862
});

plugins/typescript/src/generators/generateReactQueryComponents.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ export const generateReactQueryComponents = async (
331331
utils: utilsFilename,
332332
});
333333

334-
await context.writeFile(`${utilsFilename}.ts`, getUtils());
334+
if (!context.existsFile(`${utilsFilename}.ts`)) {
335+
await context.writeFile(`${utilsFilename}.ts`, getUtils());
336+
}
335337

336338
await context.writeFile(
337339
filename + ".ts",

0 commit comments

Comments
 (0)