Skip to content

Commit

Permalink
feat: add support for baseURL option
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 27, 2022
1 parent 6005f59 commit 380f013
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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');
}
Expand Down
17 changes: 15 additions & 2 deletions fetch_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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") {
Expand Down
14 changes: 13 additions & 1 deletion fetch_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});

Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 4 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit 380f013

Please sign in to comment.