diff --git a/packages/typed-openapi/src/tanstack-query.generator.ts b/packages/typed-openapi/src/tanstack-query.generator.ts index 8c1436a..671f2df 100644 --- a/packages/typed-openapi/src/tanstack-query.generator.ts +++ b/packages/typed-openapi/src/tanstack-query.generator.ts @@ -9,7 +9,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati const endpointMethods = new Set(ctx.endpointList.map((endpoint) => endpoint.method.toLowerCase())); const file = ` - import { queryOptions } from "@tanstack/react-query" + import { queryOptions, mutationOptions } from "@tanstack/react-query" import type { EndpointByMethod, ApiClient } from "${ctx.relativeApiClientPath}" type EndpointQueryKey = [ @@ -93,7 +93,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati }, queryKey: queryKey }), - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: queryKey, mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters} ? Parameters: never) => { const res = await this.client.${method}(path, { @@ -103,7 +103,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati }); return res as TEndpoint["response"]; } - } + }) }; return query @@ -131,14 +131,14 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati /** type-only property if you need easy access to the endpoint params */ "~endpoint": {} as TEndpoint, mutationKey: mutationKey, - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: mutationKey, mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { - const response = await this.client.request(method, path, params); - const res = selectFn ? selectFn(response) : response - return res as unknown extends TSelection ? typeof response : Awaited + const response = await this.client.request(method, path, params); + const res = selectFn ? selectFn(response) : response + return res as unknown extends TSelection ? typeof response : Awaited }, - }, + }), }; } // diff --git a/packages/typed-openapi/tests/tanstack-query.generator.test.ts b/packages/typed-openapi/tests/tanstack-query.generator.test.ts index 460595d..723b35c 100644 --- a/packages/typed-openapi/tests/tanstack-query.generator.test.ts +++ b/packages/typed-openapi/tests/tanstack-query.generator.test.ts @@ -11,7 +11,7 @@ describe("generator", () => { ...mapOpenApiEndpoints(openApiDoc), relativeApiClientPath: "./api.client.ts" })).toMatchInlineSnapshot(` - "import { queryOptions } from "@tanstack/react-query"; + "import { queryOptions, mutationOptions } from "@tanstack/react-query"; import type { EndpointByMethod, ApiClient } from "./api.client.ts"; type EndpointQueryKey = [ @@ -93,7 +93,7 @@ describe("generator", () => { }, queryKey: queryKey, }), - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: queryKey, mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { const res = await this.client.put(path, { @@ -103,7 +103,7 @@ describe("generator", () => { }); return res as TEndpoint["response"]; }, - }, + }), }; return query; @@ -131,7 +131,7 @@ describe("generator", () => { }, queryKey: queryKey, }), - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: queryKey, mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { const res = await this.client.post(path, { @@ -141,7 +141,7 @@ describe("generator", () => { }); return res as TEndpoint["response"]; }, - }, + }), }; return query; @@ -169,7 +169,7 @@ describe("generator", () => { }, queryKey: queryKey, }), - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: queryKey, mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { const res = await this.client.get(path, { @@ -179,7 +179,7 @@ describe("generator", () => { }); return res as TEndpoint["response"]; }, - }, + }), }; return query; @@ -207,7 +207,7 @@ describe("generator", () => { }, queryKey: queryKey, }), - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: queryKey, mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { const res = await this.client.delete(path, { @@ -217,7 +217,7 @@ describe("generator", () => { }); return res as TEndpoint["response"]; }, - }, + }), }; return query; @@ -248,14 +248,14 @@ describe("generator", () => { /** type-only property if you need easy access to the endpoint params */ "~endpoint": {} as TEndpoint, mutationKey: mutationKey, - mutationOptions: { + mutationOptions: mutationOptions({ mutationKey: mutationKey, mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => { const response = await this.client.request(method, path, params); const res = selectFn ? selectFn(response) : response; return res as unknown extends TSelection ? typeof response : Awaited; }, - }, + }), }; } //