Skip to content

Commit e80be05

Browse files
authored
Documentation - SDK Middleware Http (#1697)
chore(doc): documentation - add info for abortController | getAbortController - add usage examples for abortController and getAbortController
1 parent 523fd3e commit e80be05

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

docs/sdk/api/sdkMiddlewareHttp.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The HTTP middleware can run in either a browser or Node.js environment. For Node
4040
11. `maxDelay` _(Number)_: The maximum duration (milliseconds) to wait before retrying, useful if the delay time grew exponentially more than reasonable
4141
12. `fetch` _(Function)_: A `fetch` implementation which can be e.g. `node-fetch` or `unfetch` but also the native browser `fetch` function
4242
13. `timeout` _(Number)_: Request/response timeout in ms. Must have globally available or passed in `AbortController`
43-
14. `AbortController` (_AbortController_): An `AbortController` instance. Could be [abort-controller](https://www.npmjs.com/package/abort-controller) or globally available one.
43+
14. `abortController` or `getAbortController` depending on you chose to handle the timeout (_abortController_): This property accepts the `AbortController` instance. Could be [abort-controller](https://www.npmjs.com/package/abort-controller) or globally available one.
4444

4545
#### Retrying requests
4646

@@ -79,6 +79,36 @@ const client = createClient({
7979
})
8080
```
8181

82+
## `abortController` | `getAbortController`
83+
84+
This is used to signal the retry module to retry the request in an event of a request timeout or service outage.
85+
86+
#### Usage example
87+
88+
```js
89+
// Use default options
90+
const httpMiddleware = createHttpMiddleware({
91+
host: testHost,
92+
timeout: 1000, // time out after 1s
93+
fetch,
94+
abortController: new AbortController(),
95+
})
96+
```
97+
98+
Note however the slight difference in usage of the `getAbortController` property of the http middleware.
99+
100+
```js
101+
// Use default options
102+
const httpMiddleware = createHttpMiddleware({
103+
host: testHost,
104+
timeout: 1000, // time out after 1s
105+
fetch,
106+
getAbortController: () => new AbortController(),
107+
})
108+
```
109+
110+
This is to ensure that a new instance of the AbortController is always created and is independent of each other. Unlike the former (abortController) which only creates a single abortController instance for the middleware, in this very case, if a single request times out, it will propagate to all other http requests that is using the `AbortController` instance. This is useful when a bunch of sent out requests needs to timeout if at least one within the bunch times out.
111+
82112
## `getErrorByCode(code)`
83113

84114
Returns a [custom error type](/sdk/Glossary.md#httperrortype) given its status _code_.

0 commit comments

Comments
 (0)