@@ -7,10 +7,12 @@ import { generateTanstackQueryFile } from "../src/tanstack-query.generator.ts";
7
7
describe ( "generator" , ( ) => {
8
8
test ( "petstore" , async ( { expect } ) => {
9
9
const openApiDoc = ( await SwaggerParser . parse ( "./tests/samples/petstore.yaml" ) ) as OpenAPIObject ;
10
- expect ( await generateTanstackQueryFile ( {
11
- ...mapOpenApiEndpoints ( openApiDoc ) ,
12
- relativeApiClientPath : "./api.client.ts"
13
- } ) ) . toMatchInlineSnapshot ( `
10
+ expect (
11
+ await generateTanstackQueryFile ( {
12
+ ...mapOpenApiEndpoints ( openApiDoc ) ,
13
+ relativeApiClientPath : "./api.client.ts" ,
14
+ } ) ,
15
+ ) . toMatchInlineSnapshot ( `
14
16
"import { queryOptions } from "@tanstack/react-query";
15
17
import type { EndpointByMethod, ApiClient, SafeApiResponse } from "./api.client.ts";
16
18
@@ -302,12 +304,12 @@ describe("generator", () => {
302
304
[K in keyof TResponses]: K extends string
303
305
? K extends \`\${infer TStatusCode extends number}\`
304
306
? TStatusCode extends ErrorStatusCode
305
- ? { status: TStatusCode; data: TResponses[K] }
307
+ ? Omit<Response, "status"> & { status: TStatusCode; data: TResponses[K] }
306
308
: never
307
309
: never
308
310
: K extends number
309
311
? K extends ErrorStatusCode
310
- ? { status: K; data: TResponses[K] }
312
+ ? Omit<Response, "status"> & { status: K; data: TResponses[K] }
311
313
: never
312
314
: never;
313
315
}[keyof TResponses]
@@ -344,16 +346,32 @@ describe("generator", () => {
344
346
// Type assertion is safe because we're handling the method dynamically
345
347
const response = await (this.client as any)[method](path, { ...(params as any), withResponse: true });
346
348
if (!response.ok) {
347
- const error = { status: response.status, data: response.data } as TError;
349
+ // Create a Response-like error object with additional data property
350
+ const error = Object.assign(Object.create(Response.prototype), {
351
+ ...response,
352
+ data: response.data,
353
+ }) as TError;
348
354
throw error;
349
355
}
350
356
const res = selectFn ? selectFn(response as any) : response;
351
357
return res as TSelection;
352
358
}
353
359
354
360
// Type assertion is safe because we're handling the method dynamically
355
- const response = await (this.client as any)[method](path, { ...(params as any), withResponse: false });
356
- const res = selectFn ? selectFn(response as any) : response;
361
+ // Always get the full response for error handling, even when withResponse is false
362
+ const response = await (this.client as any)[method](path, { ...(params as any), withResponse: true });
363
+ if (!response.ok) {
364
+ // Create a Response-like error object with additional data property
365
+ const error = Object.assign(Object.create(Response.prototype), {
366
+ ...response,
367
+ data: response.data,
368
+ }) as TError;
369
+ throw error;
370
+ }
371
+
372
+ // Return just the data if withResponse is false, otherwise return the full response
373
+ const finalResponse = withResponse ? response : response.data;
374
+ const res = selectFn ? selectFn(finalResponse as any) : finalResponse;
357
375
return res as TSelection;
358
376
},
359
377
} as import("@tanstack/react-query").UseMutationOptions<
0 commit comments