Skip to content

Commit

Permalink
feat: add url prop to interceptors first arg
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 28, 2022
1 parent c7a8ffd commit 7303f7b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Changelog

## v6.1.0

- add `url` prop to `ExtendedRequest` that is passed as first arg to interceptors.

## v6.0.0

- remmove `validator`.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This library offers a fetch wrapper that can:
you can import `wrapFetch` from `mod.ts` file.

```ts
export { wrapFetch } from 'https://deno.land/x/fetch_goody@v6.0.0/mod.ts';
export { wrapFetch } from 'https://deno.land/x/fetch_goody@v6.1.0/mod.ts';
```

### wrapFetch
Expand Down
2 changes: 1 addition & 1 deletion extended_request_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ export type ExtendedRequestInit =
export type ExtendedRequest =
& RequestInitDiff
& Omit<RequestInit, keyof RequestInitDiff | "headers">
& { headers: Headers };
& { headers: Headers; url: URL };
59 changes: 38 additions & 21 deletions fetch_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ export function wrapFetch(options?: WrapFetchOptions) {
interceptedInit.headers = baseHeaders;
}

// Normalize input to URL. when reading the specs (https://fetch.spec.whatwg.org/#request and https://fetch.spec.whatwg.org/#fetch-method),
// I didn't see anything mentioned about fetch using anything from an input that is instance of Request except It's URL.
// So it is safe to discard the Request object and use It's url only.
// Normalizing the url simplifies any feature we want to add later.
{
if (typeof input !== "string") {
if (input instanceof Request) {
input = input.url;
} else {
input = input.toString();
}
}

// URL doesn't support relative urls
if (input.includes("://")) {
input = new URL(input);
} else {
if (baseURL) {
input = new URL(input, baseURL);
} else {
try {
input = new URL(input, location.href);
} catch {
throw new Error(
"Cannot parse the input url. Either provide `--location` parameter to Deno, or use complete url, or use baseURL when wrapping fetch.",
);
}
}
}
}

// add url to interceptedInit
(interceptedInit as ExtendedRequest).url = input;

// setup a default accept
if (!interceptedInit.headers.get("Accept")) {
interceptedInit.headers.set(
Expand Down Expand Up @@ -182,16 +216,10 @@ export function wrapFetch(options?: WrapFetchOptions) {
},
) as [string, string][];
const searchParams = new URLSearchParams(filteredQs);
// doesn't support relative urls
if (typeof input === "string" && input.includes("://")) {
input = new URL(input);
}

if (input instanceof URL) {
for (const [spKey, spValue] of searchParams.entries()) {
if (spValue !== undefined) {
input.searchParams.set(spKey, spValue);
}
for (const [spKey, spValue] of searchParams.entries()) {
if (spValue !== undefined) {
input.searchParams.set(spKey, spValue);
}
}
}
Expand All @@ -214,17 +242,6 @@ export function wrapFetch(options?: WrapFetchOptions) {
interceptedInit.signal = abortController.signal;
}

let newInput;
if (input instanceof Request) {
newInput = input.url;
} else {
newInput = input.toString();
}

if (baseURL) {
newInput = new URL(newInput, baseURL);
}

if (typeof interceptors?.request === "function") {
await interceptors.request(interceptedInit as ExtendedRequest);
}
Expand All @@ -238,7 +255,7 @@ export function wrapFetch(options?: WrapFetchOptions) {
);
}

const response = await fetch(newInput, interceptedInit as RequestInit);
const response = await fetch(input, interceptedInit as RequestInit);
clearTimeout(timeoutId);

if (typeof interceptors?.response === "function") {
Expand Down
38 changes: 38 additions & 0 deletions fetch_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,44 @@ Deno.test("interaction with a server", {
},
);

await t.step(
"WrappedFetch interceptors first arg contains `url` prop",
async () => {
const wrappedFetch = wrapFetch();
let isUrlInInitCorrect = false;
// for string
await wrappedFetch(serverOneUrl + "/accept", {
interceptors: {
request(init) {
isUrlInInitCorrect = init.url.pathname === "/accept";
},
},
}).then((r) => r.text());

assertEquals(
isUrlInInitCorrect,
true,
);
},
);

await t.step(
"WrappedFetch throws when a baseURL is missing",
async () => {
const wrappedFetch = wrapFetch();
let thrown = false;
// for string
await wrappedFetch("/test").then((r) => r.text()).catch(() => {
thrown = true;
});

assertEquals(
thrown,
true,
);
},
);

await t.step("WrappedFetch uses the given baseURL", async () => {
const wrappedFetch = wrapFetch({
baseURL: serverOneUrl,
Expand Down
2 changes: 1 addition & 1 deletion utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function isArrayBuffer(val: unknown): val is ArrayBuffer {
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
*/
export function isArrayBufferView(val: unknown): val is ArrayBufferView {
var result;
let result;
if ((typeof ArrayBuffer !== "undefined") && (ArrayBuffer.isView)) {
result = ArrayBuffer.isView(val);
} else {
Expand Down

0 comments on commit 7303f7b

Please sign in to comment.