From 4fce4abddae03c02b8938981cdd5c7c3212e3bcb Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Fri, 17 May 2024 17:28:37 -0400 Subject: [PATCH] Update README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 2f51171..88832ef 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ const users = await response.json(usersSchema); - [Transformers](#transformers) - [Request transformers](#request-transformers) - [Response transformers](#response-transformers) + - [Request timeout](#request-timeout) - [Body](#body) - [Query](#query) - [Params](#params) @@ -258,6 +259,24 @@ const response = await service.get("/users") // response.statusText will be 'It worked!' ``` +### Request timeout + +A single timeout based on the service can be very useful since the default fetch timeout is quite long. +The `timeout` parameter accepts a number of milliseconds and abort any request that takes longer than that limit. + +Using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) for individual requests is still possible for more fine grained control. + +The example below will abort the request after 30 seconds rejecting the promise. + +```ts +const service = makeService('https://example.com/api', { + timeout: 30000, +}) + +const response = await service.get("/users") + +``` + ### Body The function can also receive a `body` object that will be stringified and sent as the request body: