@@ -112,20 +112,28 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
112112 * Generic mutation method with full type-safety for any endpoint that doesnt require parameters to be passed initially
113113 */
114114 mutation<
115- TMethod extends keyof EndpointByMethod,
116- TPath extends keyof EndpointByMethod[TMethod],
117- TEndpoint extends EndpointByMethod[TMethod][TPath]
118- >(
119- method: TMethod,
120- path: TPath) {
115+ TMethod extends keyof EndpointByMethod,
116+ TPath extends keyof EndpointByMethod[TMethod],
117+ TEndpoint extends EndpointByMethod[TMethod][TPath],
118+ TSelection,
119+ >(method: TMethod, path: TPath, selectFn?: (res: Omit<Response, "json"> & {
120+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
121+ json: () => Promise<TEndpoint extends { response: infer Res } ? Res : never>;
122+ }) => TSelection) {
121123 const mutationKey = [{ method, path }] as const;
122124 return {
125+ /** type-only property if you need easy access to the endpoint params */
126+ "~endpoint": {} as TEndpoint,
127+ mutationKey: mutationKey,
128+ mutationOptions: {
123129 mutationKey: mutationKey,
124- mutationOptions: {
125- mutationKey: mutationKey,
126- mutationFn: async (params: TEndpoint extends { parameters: infer Parameters} ? Parameters: never) => this.client.request(method, path, params)
127- }
128- }
130+ mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
131+ const response = await this.client.request(method, path, params);
132+ const res = selectFn ? selectFn(response) : response
133+ return res as unknown extends TSelection ? typeof response : Awaited<TSelection>
134+ },
135+ },
136+ };
129137 }
130138 // </ApiClient.request>
131139 }
0 commit comments