From c74a6e43e3ea3c7e331ff9a8b2537d282e12f6cb Mon Sep 17 00:00:00 2001 From: Arunanshu Biswas Date: Mon, 5 Jun 2023 16:37:36 +0530 Subject: [PATCH 1/3] fix(fetch): use try-catch block to capture fetching errors --- src/fetch.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index cd9ebf5..f7cc9de 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -170,16 +170,18 @@ export function createFetch(globalOptions: CreateFetchOptions): $Fetch { } } - context.response = await fetch( - context.request, - context.options as RequestInit - ).catch(async (error) => { - context.error = error; + try { + context.response = await fetch( + context.request, + context.options as RequestInit + ); + } catch (error) { + context.error = error as Error; if (context.options.onRequestError) { await context.options.onRequestError(context as any); } - return onError(context); - }); + return await onError(context); + } const responseType = (context.options.parseResponse ? "json" : context.options.responseType) || From 0f4249d5edf9d2518bb9c97bc41b629eca0dd050 Mon Sep 17 00:00:00 2001 From: Arunanshu Biswas Date: Mon, 5 Jun 2023 19:01:03 +0530 Subject: [PATCH 2/3] test(index): keep the server alive during the entire test session Also, remove use of deprecated code. --- test/index.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index a64cddd..f0ff0af 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -9,14 +9,14 @@ import { } from "h3"; import { Blob } from "fetch-blob"; import { FormData } from "formdata-polyfill/esm.min.js"; -import { describe, beforeEach, afterEach, it, expect } from "vitest"; +import { describe, beforeAll, afterAll, it, expect } from "vitest"; import { Headers, $fetch } from "../src/node"; describe("ofetch", () => { let listener; const getURL = (url) => joinURL(listener.url, url); - beforeEach(async () => { + beforeAll(async () => { const app = createApp() .use( "/ok", @@ -24,23 +24,23 @@ describe("ofetch", () => { ) .use( "/params", - eventHandler((event) => getQuery(event.req.url || "")) + eventHandler((event) => getQuery(event.node.req.url || "")) ) .use( "/url", - eventHandler((event) => event.req.url) + eventHandler((event) => event.node.req.url) ) .use( "/post", eventHandler(async (event) => ({ body: await readBody(event), - headers: event.req.headers, + headers: event.node.req.headers, })) ) .use( "/binary", eventHandler((event) => { - event.res.setHeader("Content-Type", "application/octet-stream"); + event.node.res.setHeader("Content-Type", "application/octet-stream"); return new Blob(["binary"]); }) ) @@ -51,7 +51,7 @@ describe("ofetch", () => { listener = await listen(toNodeListener(app)); }); - afterEach(async () => { + afterAll(async () => { await listener.close(); }); From 22c30be7a7d559434f8b490827047d521dcd480e Mon Sep 17 00:00:00 2001 From: Arunanshu Biswas <48434243+arunanshub@users.noreply.github.com> Date: Tue, 6 Jun 2023 07:29:59 +0000 Subject: [PATCH 3/3] fix(fetch): await `onError` and use asnyc function syntax --- src/fetch.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index f7cc9de..d3f30c3 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -207,14 +207,15 @@ export function createFetch(globalOptions: CreateFetchOptions): $Fetch { await context.options.onResponseError(context as any); } - return onError(context); + return await onError(context); } return context.response; }; - const $fetch = function $fetch(request, options) { - return $fetchRaw(request, options).then((r) => r._data); + const $fetch = async function $fetch(request, options) { + const r = await $fetchRaw(request, options); + return r._data; } as $Fetch; $fetch.raw = $fetchRaw;