Skip to content

Commit

Permalink
fix: update retryDelay signiture
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed May 23, 2023
1 parent 9d31723 commit c466a96
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 55 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"--allow-all",
"--allow-env"
],
"outputCapture": "std",
"outputCapture": "std"
},
{
"name": "Debug Run Deno",
Expand Down Expand Up @@ -67,7 +67,7 @@
"--allow-all",
"--allow-env"
],
"outputCapture": "std",
"outputCapture": "std"
}
]
}
}
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"deno.enable": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.defaultFormatter": "denoland.vscode-deno"
},
"deno.unstable": true,
"deno.lint": true,
"debug.javascript.unmapMissingSources": true,
"deno.import_intellisense_origins": {
"https://deno.land": true
},
}
}
}
37 changes: 26 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@

# Changelog

## v6.2.1

- update `retryDelay` signiture

## v6.2.0

- add `retry` and `retryDelay`

## v6.1.0

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

## v6.0.0

Expand All @@ -24,22 +32,27 @@

## v5.0.0

- fix timeout functionality as the abort signal is now supported as of deno v1.11
- fix timeout functionality as the abort signal is now supported as of deno
v1.11

Version v5.0.0+ is the recommended version now (abort controller is used now). please don't use v4 of fetch goody anymore.
Version v5.0.0+ is the recommended version now (abort controller is used now).
please don't use v4 of fetch goody anymore.

## v4.0.0

- due to adding `timeout` option and the way it works, it may cause issues. so I release this as breaking change.
- due to adding `timeout` option and the way it works, it may cause issues. so I
release this as breaking change.

## v3.0.1

- fix `form` keys not getting url encoded.

## v3.0.0

- BREAKING CHANGE: Changed `form` option to `formData` and used `form` option as a `application/x-www-form-urlencoded` value instead of `multipart/form-data`.
this means anything meant to be sent as a `multipart/form-data` now should be passed as `formData` option.
- BREAKING CHANGE: Changed `form` option to `formData` and used `form` option as
a `application/x-www-form-urlencoded` value instead of `multipart/form-data`.
this means anything meant to be sent as a `multipart/form-data` now should be
passed as `formData` option.

## v2.1.0

Expand All @@ -50,12 +63,13 @@ this means anything meant to be sent as a `multipart/form-data` now should be pa
- BREAKING CHANGE: rename `fetchFn` to `fetch` in WrapFetchOptions
- create WrapFetchOptions type
- add a validator option to WrapFetchOptions
- the validator that is passed to WrapFetchOptions will run before the validator of ExtendedRequestInit

- the validator that is passed to WrapFetchOptions will run before the validator
of ExtendedRequestInit

## v1.5.0

- pass init to validator

## v1.4.0

- add validator option
Expand Down Expand Up @@ -87,7 +101,8 @@ this means anything meant to be sent as a `multipart/form-data` now should be pa

# v1.1.0

- Very important fix for `ExtendedRequestInit` type. The type is also exported now.
- Very important fix for `ExtendedRequestInit` type. The type is also exported
now.
- Very important fix for FormData.
- automatically add method for object body (json).
- automatically build and use form data if `form` is defined inside `init`
Expand Down
48 changes: 23 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions extended_request_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ interface RequestInitDiff {
/** if set, all requests will be retried this much. it's in addition to first request. so a retry with value of 1 will send 2 requests in total. */
retry?: number;
/** retry delay in milliseconds. if you need non linear delays, you can do that by passing in a function instead of number. defaults to `500ms`. */
retryDelay?:
| number
| ((attempt: number, init: RequestInit | ExtendedRequestInit) => number);
retryDelay?: number | RetryDelayFunction;
}

export type RetryDelayFunction = (
/** current attempt (1 = it is going to retry for the first time and so on) */
attempt: number,
/** unified input */
input: URL,
/** current init object */
init: ExtendedRequestInit,
) => number;

export type Interceptors = {
/** function that is called just before a request is sent*/
request?: (
Expand Down
7 changes: 3 additions & 4 deletions fetch_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ExtendedRequest,
ExtendedRequestInit,
Interceptors,
RetryDelayFunction,
} from "./extended_request_init.ts";

/**
Expand Down Expand Up @@ -80,9 +81,7 @@ export type WrapFetchOptions = {
/** if set, all requests will be retried this much */
retry?: number;
/** retry delay in milliseconds. if you need non linear delays, you can do that by passing in a function instead of number. defaults to `500ms`. */
retryDelay?:
| number
| ((attempt: number, init: RequestInit | ExtendedRequestInit) => number);
retryDelay?: number | RetryDelayFunction;
};

export function wrapFetch(options?: WrapFetchOptions) {
Expand Down Expand Up @@ -289,7 +288,7 @@ export function wrapFetch(options?: WrapFetchOptions) {
retryDelay,
);
if (typeof delayVal === "function") {
await utils.delay(delayVal(attempt, interceptedInit));
await utils.delay(delayVal(attempt, input, interceptedInit));
} else {
await utils.delay(delayVal);
}
Expand Down
24 changes: 19 additions & 5 deletions fetch_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", {
Expand All @@ -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",
);
},
);
Expand Down

0 comments on commit c466a96

Please sign in to comment.