Skip to content

Commit 9dccab8

Browse files
committed
Add response typing to delete
1 parent 2dc04b7 commit 9dccab8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/FetchClient.test.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,17 @@ Deno.test("can putJSON with client middleware", async () => {
277277
assertEquals(r.data!.completed, false);
278278
});
279279

280-
Deno.test("can delete with client middleware", async () => {
280+
Deno.test("can deleteJSON with client middleware", async () => {
281281
const provider = new FetchClientProvider();
282282
const fakeFetch = (): Promise<Response> =>
283283
new Promise((resolve) => {
284-
resolve(new Response());
284+
const data = JSON.stringify({
285+
userId: 1,
286+
id: 1,
287+
title: "A random title",
288+
completed: false,
289+
});
290+
resolve(new Response(data));
285291
});
286292

287293
provider.fetch = fakeFetch;
@@ -297,10 +303,17 @@ Deno.test("can delete with client middleware", async () => {
297303
});
298304
assert(client);
299305

300-
const r = await client.delete("https://jsonplaceholder.typicode.com/todos/1");
306+
const r = await client.deleteJSON<Todo>(
307+
"https://jsonplaceholder.typicode.com/todos/1",
308+
);
301309
assert(r.ok);
302310
assertEquals(r.status, 200);
311+
assert(r.data);
303312
assert(called);
313+
assertEquals(r.data!.userId, 1);
314+
assertEquals(r.data!.id, 1);
315+
assertEquals(r.data!.title, "A random title");
316+
assertEquals(r.data!.completed, false);
304317
});
305318

306319
Deno.test("can abort getJSON", () => {

src/FetchClient.ts

+15
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ export class FetchClient {
375375
);
376376
}
377377

378+
/**
379+
* Sends a DELETE request with JSON payload to the specified URL.
380+
*
381+
* @template T - The type of the response data.
382+
* @param {string} url - The URL to send the request to.
383+
* @param {RequestOptions} [options] - Additional options for the request.
384+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
385+
*/
386+
deleteJSON<T>(
387+
url: string,
388+
options?: RequestOptions,
389+
): Promise<FetchClientResponse<T>> {
390+
return this.delete(url, options) as Promise<FetchClientResponse<T>>;
391+
}
392+
378393
private async validate(
379394
data: unknown,
380395
options?: RequestOptions,

0 commit comments

Comments
 (0)