Skip to content

Commit 73c1ef1

Browse files
committed
feat: mutationOptions + .mutation (if input is not available before)
1 parent 0142cc5 commit 73c1ef1

File tree

3 files changed

+107
-9
lines changed

3 files changed

+107
-9
lines changed

.changeset/quick-ends-listen.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+
feat: mutationOptions + .mutation (if input is not available before)

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,50 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
8484
...params,
8585
...queryKey[0],
8686
signal,
87-
throwOnError: true
8887
});
8988
return res as TEndpoint["response"];
9089
},
9190
queryKey: queryKey
9291
}),
92+
mutationOptions: {
93+
mutationKey: queryKey,
94+
mutationFn: async (localOptions) => {
95+
const res = await this.client.${method}(path, {
96+
...params,
97+
...queryKey[0],
98+
...localOptions,
99+
});
100+
return res as TEndpoint["response"];
101+
}
102+
}
93103
};
94104
95105
return query
96106
}
97-
// </ApiClient.get>
107+
// </ApiClient.${method}>
98108
`).join("\n")}
109+
110+
// <ApiClient.request>
111+
/**
112+
* Generic mutation method with full type-safety for any endpoint that doesnt require parameters to be passed initially
113+
*/
114+
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) {
121+
const mutationKey = [{ method, path }] as const;
122+
return {
123+
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+
}
129+
}
130+
// </ApiClient.request>
99131
}
100132
`;
101133

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

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,27 @@ describe("generator", () => {
8888
...params,
8989
...queryKey[0],
9090
signal,
91-
throwOnError: true,
9291
});
9392
return res as TEndpoint["response"];
9493
},
9594
queryKey: queryKey,
9695
}),
96+
mutationOptions: {
97+
mutationKey: queryKey,
98+
mutationFn: async (localOptions) => {
99+
const res = await this.client.put(path, {
100+
...params,
101+
...queryKey[0],
102+
...localOptions,
103+
});
104+
return res as TEndpoint["response"];
105+
},
106+
},
97107
};
98108
99109
return query;
100110
}
101-
// </ApiClient.get>
111+
// </ApiClient.put>
102112
103113
// <ApiClient.post>
104114
post<Path extends keyof PostEndpoints, TEndpoint extends PostEndpoints[Path]>(
@@ -116,17 +126,27 @@ describe("generator", () => {
116126
...params,
117127
...queryKey[0],
118128
signal,
119-
throwOnError: true,
120129
});
121130
return res as TEndpoint["response"];
122131
},
123132
queryKey: queryKey,
124133
}),
134+
mutationOptions: {
135+
mutationKey: queryKey,
136+
mutationFn: async (localOptions) => {
137+
const res = await this.client.post(path, {
138+
...params,
139+
...queryKey[0],
140+
...localOptions,
141+
});
142+
return res as TEndpoint["response"];
143+
},
144+
},
125145
};
126146
127147
return query;
128148
}
129-
// </ApiClient.get>
149+
// </ApiClient.post>
130150
131151
// <ApiClient.get>
132152
get<Path extends keyof GetEndpoints, TEndpoint extends GetEndpoints[Path]>(
@@ -144,12 +164,22 @@ describe("generator", () => {
144164
...params,
145165
...queryKey[0],
146166
signal,
147-
throwOnError: true,
148167
});
149168
return res as TEndpoint["response"];
150169
},
151170
queryKey: queryKey,
152171
}),
172+
mutationOptions: {
173+
mutationKey: queryKey,
174+
mutationFn: async (localOptions) => {
175+
const res = await this.client.get(path, {
176+
...params,
177+
...queryKey[0],
178+
...localOptions,
179+
});
180+
return res as TEndpoint["response"];
181+
},
182+
},
153183
};
154184
155185
return query;
@@ -172,17 +202,48 @@ describe("generator", () => {
172202
...params,
173203
...queryKey[0],
174204
signal,
175-
throwOnError: true,
176205
});
177206
return res as TEndpoint["response"];
178207
},
179208
queryKey: queryKey,
180209
}),
210+
mutationOptions: {
211+
mutationKey: queryKey,
212+
mutationFn: async (localOptions) => {
213+
const res = await this.client.delete(path, {
214+
...params,
215+
...queryKey[0],
216+
...localOptions,
217+
});
218+
return res as TEndpoint["response"];
219+
},
220+
},
181221
};
182222
183223
return query;
184224
}
185-
// </ApiClient.get>
225+
// </ApiClient.delete>
226+
227+
// <ApiClient.request>
228+
/**
229+
* Generic mutation method with full type-safety for any endpoint that doesnt require parameters to be passed initially
230+
*/
231+
mutation<
232+
TMethod extends keyof EndpointByMethod,
233+
TPath extends keyof EndpointByMethod[TMethod],
234+
TEndpoint extends EndpointByMethod[TMethod][TPath],
235+
>(method: TMethod, path: TPath) {
236+
const mutationKey = [{ method, path }] as const;
237+
return {
238+
mutationKey: mutationKey,
239+
mutationOptions: {
240+
mutationKey: mutationKey,
241+
mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) =>
242+
this.client.request(method, path, params),
243+
},
244+
};
245+
}
246+
// </ApiClient.request>
186247
}
187248
"
188249
`);

0 commit comments

Comments
 (0)