@@ -88,17 +88,27 @@ describe("generator", () => {
88
88
...params,
89
89
...queryKey[0],
90
90
signal,
91
- throwOnError: true,
92
91
});
93
92
return res as TEndpoint["response"];
94
93
},
95
94
queryKey: queryKey,
96
95
}),
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
+ },
97
107
};
98
108
99
109
return query;
100
110
}
101
- // </ApiClient.get >
111
+ // </ApiClient.put >
102
112
103
113
// <ApiClient.post>
104
114
post<Path extends keyof PostEndpoints, TEndpoint extends PostEndpoints[Path]>(
@@ -116,17 +126,27 @@ describe("generator", () => {
116
126
...params,
117
127
...queryKey[0],
118
128
signal,
119
- throwOnError: true,
120
129
});
121
130
return res as TEndpoint["response"];
122
131
},
123
132
queryKey: queryKey,
124
133
}),
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
+ },
125
145
};
126
146
127
147
return query;
128
148
}
129
- // </ApiClient.get >
149
+ // </ApiClient.post >
130
150
131
151
// <ApiClient.get>
132
152
get<Path extends keyof GetEndpoints, TEndpoint extends GetEndpoints[Path]>(
@@ -144,12 +164,22 @@ describe("generator", () => {
144
164
...params,
145
165
...queryKey[0],
146
166
signal,
147
- throwOnError: true,
148
167
});
149
168
return res as TEndpoint["response"];
150
169
},
151
170
queryKey: queryKey,
152
171
}),
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
+ },
153
183
};
154
184
155
185
return query;
@@ -172,17 +202,48 @@ describe("generator", () => {
172
202
...params,
173
203
...queryKey[0],
174
204
signal,
175
- throwOnError: true,
176
205
});
177
206
return res as TEndpoint["response"];
178
207
},
179
208
queryKey: queryKey,
180
209
}),
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
+ },
181
221
};
182
222
183
223
return query;
184
224
}
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>
186
247
}
187
248
"
188
249
` ) ;
0 commit comments