Skip to content

Commit 86a384f

Browse files
committed
feat: add mutation selectFn + endpoint type-only property in .mutation
1 parent fa574bc commit 86a384f

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

.changeset/spicy-seas-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
add mutation selectFn + endpoint type-only property in .mutation

packages/typed-openapi/src/tanstack-query.generator.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/typed-openapi/tests/tanstack-query.generator.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,29 @@ describe("generator", () => {
232232
TMethod extends keyof EndpointByMethod,
233233
TPath extends keyof EndpointByMethod[TMethod],
234234
TEndpoint extends EndpointByMethod[TMethod][TPath],
235-
>(method: TMethod, path: TPath) {
235+
TSelection,
236+
>(
237+
method: TMethod,
238+
path: TPath,
239+
selectFn?: (
240+
res: Omit<Response, "json"> & {
241+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
242+
json: () => Promise<TEndpoint extends { response: infer Res } ? Res : never>;
243+
},
244+
) => TSelection,
245+
) {
236246
const mutationKey = [{ method, path }] as const;
237247
return {
248+
/** type-only property if you need easy access to the endpoint params */
249+
"~endpoint": {} as TEndpoint,
238250
mutationKey: mutationKey,
239251
mutationOptions: {
240252
mutationKey: mutationKey,
241-
mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) =>
242-
this.client.request(method, path, params),
253+
mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
254+
const response = await this.client.request(method, path, params);
255+
const res = selectFn ? selectFn(response) : response;
256+
return res as unknown extends TSelection ? typeof response : Awaited<TSelection>;
257+
},
243258
},
244259
};
245260
}

0 commit comments

Comments
 (0)