Skip to content

feat: use mutationOptions #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/typed-openapi/src/tanstack-query.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TOptions extends EndpointParameters> = [
Expand Down Expand Up @@ -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, {
Expand All @@ -103,7 +103,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
});
return res as TEndpoint["response"];
}
}
})
};

return query
Expand Down Expand Up @@ -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<TSelection>
const response = await this.client.request(method, path, params);
const res = selectFn ? selectFn(response) : response
return res as unknown extends TSelection ? typeof response : Awaited<TSelection>
},
},
}),
};
}
// </ApiClient.request>
Expand Down
22 changes: 11 additions & 11 deletions packages/typed-openapi/tests/tanstack-query.generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TOptions extends EndpointParameters> = [
Expand Down Expand Up @@ -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, {
Expand All @@ -103,7 +103,7 @@ describe("generator", () => {
});
return res as TEndpoint["response"];
},
},
}),
};

return query;
Expand Down Expand Up @@ -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, {
Expand All @@ -141,7 +141,7 @@ describe("generator", () => {
});
return res as TEndpoint["response"];
},
},
}),
};

return query;
Expand Down Expand Up @@ -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, {
Expand All @@ -179,7 +179,7 @@ describe("generator", () => {
});
return res as TEndpoint["response"];
},
},
}),
};

return query;
Expand Down Expand Up @@ -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, {
Expand All @@ -217,7 +217,7 @@ describe("generator", () => {
});
return res as TEndpoint["response"];
},
},
}),
};

return query;
Expand Down Expand Up @@ -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<TSelection>;
},
},
}),
};
}
// </ApiClient.request>
Expand Down