diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d64f0..877de7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog +## v5.2.0 + +- fix a small issue when no header was given +- add support for baseURL option +- export ExtendedRequest type for validator typing + ## v5.1.0 - add support for initial headers when creating wrapper diff --git a/README.md b/README.md index 123eddb..089ee84 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This library offers a fetch wrapper that can: - accept a timeout option and abort when timeout is reached - add `Accept` header with value `application/json, text/plain, */*` if not already set by you - set global headers when creating the wrapper +- set a `baseURL` when creating the wrapper Version v5.0.0+ is the recommended version now (abort controller is used now). please don't use v4 of fetch goody anymore. @@ -19,7 +20,7 @@ Version v5.0.0+ is the recommended version now (abort controller is used now). p you can import `wrapFetch` from `mod.ts` file. ```js -export { wrapFetch } from 'https://deno.land/x/fetch_goody@v5.0.0/mod.ts'; +export { wrapFetch } from 'https://deno.land/x/fetch_goody@v5.2.0/mod.ts'; ``` ### wrapFetch @@ -70,7 +71,7 @@ const resp3 = await wrappedfetch("url",{ // adding a response validator where you can throw errors const resp4 = await wrappedfetch("url",{ - validator(response: Response, init: ExtendedRequestInit) { + validator(response: Response, init: ExtendedRequest) { if (response.status > 200) { throw new Error('yada'); } diff --git a/fetch_wrapper.ts b/fetch_wrapper.ts index 999493a..de757ef 100644 --- a/fetch_wrapper.ts +++ b/fetch_wrapper.ts @@ -75,6 +75,8 @@ export type WrapFetchOptions = { timeout?: number; /** if set, will be used as default headers. new added headers will be added on top of these. */ headers?: Headers; + /** if set, will be prepended to the target url using URL api. */ + baseURL?: string | URL; }; export function wrapFetch(options?: WrapFetchOptions) { @@ -84,8 +86,8 @@ export function wrapFetch(options?: WrapFetchOptions) { validator, timeout = 99999999, headers, + baseURL, } = options || {}; - return async function wrappedFetch( input: string | Request | URL, init?: ExtendedRequestInit | RequestInit | undefined, @@ -211,7 +213,18 @@ export function wrapFetch(options?: WrapFetchOptions) { interceptedInit.signal = abortController.signal; } - const response = await fetch(input, interceptedInit as RequestInit); + let newInput; + if (input instanceof Request) { + newInput = input.url; + } else { + newInput = input.toString(); + } + + if (baseURL) { + newInput = new URL(newInput, baseURL); + } + + const response = await fetch(newInput, interceptedInit as RequestInit); clearTimeout(timeoutId); if (typeof validator === "function") { diff --git a/fetch_wrapper_test.ts b/fetch_wrapper_test.ts index 2193513..18ba32f 100644 --- a/fetch_wrapper_test.ts +++ b/fetch_wrapper_test.ts @@ -370,7 +370,7 @@ Deno.test("interaction with a server", { assertStrictEquals( response, - "foo=bar&foo=baz", // test fails, but data is sent: https://github.com/denoland/deno_std/issues/716 + "foo=bar&foo=baz", ); }); @@ -558,6 +558,18 @@ Deno.test("interaction with a server", { true, ); }); + + await t.step("WrappedFetch uses the given baseURL", async () => { + const wrappedFetch = wrapFetch({ + baseURL: serverOneUrl, + }); + + const headerString = await wrappedFetch("/accept").then(( + r, + ) => r.text()); + + assertStrictEquals(headerString, "application/json, text/plain, */*"); + }); } finally { server1.close(); } diff --git a/mod.ts b/mod.ts index e4e06e7..7322f4f 100644 --- a/mod.ts +++ b/mod.ts @@ -1,2 +1,5 @@ export * from "./fetch_wrapper.ts"; -export type { ExtendedRequestInit } from "./extended_request_init.ts"; +export type { + ExtendedRequest, + ExtendedRequestInit, +} from "./extended_request_init.ts";