-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ This library offers a fetch wrapper that can: | |
- accept a `timeout` option and abort when timeout is reached | ||
- accept a `retry` option and retry the request when it throws | ||
- accept a `retryDelay` option to wait before retrying. it can be a function. | ||
- add `Accept` header with value `application/json, text/plain, */*` if not already set by you | ||
- 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 | ||
- add `interceptors` when creating the wrapper or for individual requests | ||
|
@@ -20,7 +21,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/[email protected].0/mod.ts'; | ||
export { wrapFetch } from "https://deno.land/x/[email protected].1/mod.ts"; | ||
``` | ||
|
||
### wrapFetch | ||
|
@@ -40,59 +41,56 @@ const wrappedfetch = wrapFetch({ fetch: yourFetch }); | |
You can also add global interceptors: | ||
|
||
```ts | ||
const wrappedfetch = wrapFetch({ | ||
interceptors: { | ||
request(init: ExtendedRequest) { | ||
// add some header before each request is sent | ||
// for example add some headers from your cookie-jar | ||
}, | ||
} | ||
const wrappedfetch = wrapFetch({ | ||
interceptors: { | ||
request(init: ExtendedRequest) { | ||
// add some header before each request is sent | ||
// for example add some headers from your cookie-jar | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
#### using the new wrappedfetch | ||
|
||
```ts | ||
// for sending a multipart/form-data body now you should use `formData`. | ||
const resp1 = await wrappedfetch("url",{ | ||
const resp1 = await wrappedfetch("url", { | ||
form: { | ||
'foo': 'bar' | ||
} | ||
"foo": "bar", | ||
}, | ||
}); // sets method to POST by default and converts object to application/x-www-form-urlencoded. | ||
|
||
// or | ||
|
||
|
||
// or | ||
|
||
const resp2 = await wrappedfetch("url",{ | ||
const resp2 = await wrappedfetch("url", { | ||
body: { | ||
'foo': 'bar' | ||
} | ||
"foo": "bar", | ||
}, | ||
}); // is sent as json and corresponding header is set | ||
// also if method is not defined for this, it will be set as POST | ||
|
||
// other features: | ||
|
||
// adding query string | ||
|
||
const resp3 = await wrappedfetch("url",{ | ||
const resp3 = await wrappedfetch("url", { | ||
qs: { | ||
'foo': 'bar' | ||
} | ||
"foo": "bar", | ||
}, | ||
}); // results to url being sent to be "url?foo=bar" | ||
|
||
// adding interceptors where you can throw errors and other stuff | ||
|
||
const resp4 = await wrappedfetch("url",{ | ||
const resp4 = await wrappedfetch("url", { | ||
interceptors: { | ||
response(init: ExtendedRequest, response: Response) { | ||
if (response.status > 200) { | ||
throw new Error('yada'); | ||
throw new Error("yada"); | ||
} | ||
}, | ||
} | ||
}, | ||
}); | ||
|
||
``` | ||
|
||
## test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { | |
assertStrictEquals, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { Server } from "https://deno.land/[email protected]/http/server.ts"; | ||
import { wrapFetch } from "./mod.ts"; | ||
import { ExtendedRequestInit, wrapFetch } from "./mod.ts"; | ||
import { delay } from "https://deno.land/[email protected]/async/delay.ts"; | ||
const serverOneUrl = "http://localhost:54933"; | ||
|
||
|
@@ -739,12 +739,14 @@ Deno.test("Retry option", { | |
); | ||
|
||
await t.step( | ||
"if WrappedFetch retryDelay is a function, it will be called with attempt number and request init", | ||
"if WrappedFetch retryDelay is a function, it will be called with attempt number, input url and request init", | ||
async () => { | ||
const wrappedFetch = wrapFetch(); | ||
|
||
let count = 0; | ||
let lastAttempt = 0; | ||
let lastInput: URL | undefined; | ||
let lastInit: ExtendedRequestInit | undefined; | ||
// see if it retries the connection by retry times: | ||
try { | ||
await wrappedFetch(serverOneUrl + "/count", { | ||
|
@@ -756,24 +758,36 @@ Deno.test("Retry option", { | |
}, | ||
}, | ||
retry: 3, | ||
retryDelay: (attempt, init) => { | ||
retryDelay: (attempt, input, init) => { | ||
lastAttempt = attempt; | ||
lastInput = input; | ||
lastInit = init; | ||
assertStrictEquals(count, attempt); | ||
assertStrictEquals(!!init, true); | ||
assertStrictEquals(init.body, "foo"); | ||
return 0; | ||
}, | ||
}); | ||
} catch { | ||
} | ||
|
||
assertStrictEquals( | ||
lastInput?.toString(), | ||
serverOneUrl + "/count", | ||
"didnt received the input in retry function", | ||
); | ||
assertStrictEquals( | ||
lastInit?.body, | ||
"foo", | ||
"didnt receive the init body in retryDelay", | ||
); | ||
assertStrictEquals( | ||
count, | ||
4, | ||
"retry count is incorrect", | ||
); | ||
assertStrictEquals( | ||
lastAttempt, | ||
4, | ||
"attempt count is incorrect", | ||
); | ||
}, | ||
); | ||
|