Skip to content

Commit

Permalink
Updated docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fveracoechea committed Jul 3, 2023
1 parent 2ac5907 commit 9b38a94
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 591 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ indent_style = space
indent_size = 2
max_line_length = 100

[*.{css,scss,sass,graphql,gql,md,mdx}]
[*.{css,scss,sass,graphql,gql}]
indent_size = 4
12 changes: 6 additions & 6 deletions docs/components/features.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Features

- **Small** - Less than 3KB
- **Composable** - Safely reuse previous configurations
- **Intuitive** - Clean and easy to use API
- **Type safe** - Strongly typed, written in TypeScript
- **Isomorphic** - Compatible with modern browsers, Node.js and Deno
- **Well Tested** - Fully covered by unit tests
- **Small** - Less than 3KB
- **Composable** - Safely reuse previous configurations
- **Intuitive** - Clean and easy to use API
- **Type safe** - Strongly typed, written in TypeScript
- **Isomorphic** - Compatible with modern browsers, Node.js and Deno
- **Well Tested** - Fully covered by unit tests
76 changes: 76 additions & 0 deletions docs/pages/error-handling.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
# Error Handling

Sicen [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) doest not reject on http error status, manually checking and handling every request error code can be very tedious:

```typescript

fetch("anything")
.then(response => {
if(!response.ok) {
if(response.status === 404) throw new Error("Not found");
else if(response.status === 401) throw new Error("Unauthorized");
else if(response.status === 418) throw new Error("I'm a teapot !");
/* ... */
else throw new Error("Other error")
} else {
/* ... */
}
})
.then(data => /* ... */)
.catch(error => { /* ... */ })
```

Fetchtastic throws an [HttpError](/reference/http-error) when the response is not successful and contains helper methods to handle common codes.

We call those helper methods [Error Catchers](/reference/fetchtastic#error-catchers):

```typescript
const api = new Fetchtastic('https://jsonplaceholder.typicode.com/posts');

await api
.badRequest(error => /* 400 bad-request */)
.unauthorized(error => /* 401 unauthorized */)
.forbidden(error => /* 403 forbidden */)
.notFound(error => /* 404 not-found */)
.timeout(error => /* 408 request-timeout */)
.serverError(error => /* 500 internal-server-error */)
.onError(418, error => /* I'm a Teapot */)
.resolve()
.then(doSomethingWithResponse)
.catch((error: Error) => {
/* Uncaught errors */

if (error instanceof HttpError) {
console.log(error.url) // https://jsonplaceholder.typicode.com/posts
console.log(error.status) // 429
console.log(error.message) // Too Many Requests
}

});

```

The original config is passed along the error and can be used in order to perform an additional request, and modify the initial response:

```typescript
let authToken = '';

const api = new Fetchtastic('https://my.backend-api.com');

function getResource(callback) {
api
.get('/resource')
.headers({ Authorization: authToken })
.unauthorized(async (error, config) => {
// Renew credentials
const credentials = await api.get('/renew-token').json();
authToken = credentials.token;
// Replay the original request with new credentials
return config.headers({ Authorization: authToken }).resolve();
})
.json()
.then(callback);
}
```

In the above example, you can notice that the execution flow is preserved as expected,
`.then` will be called with the result of the original request or the replayed one.
8 changes: 4 additions & 4 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ Simplifies code structure, enhances readability, and improves the overall develo
const config = new Fetchtastic('https://jsonplaceholder.typicode.com');
const posts = await config
.header({ Accept: 'application/json', 'Content-Type': 'application/json' })
.searchParams({ page: 1, first: 12 })
.get('/posts')
.json();
.header({ Accept: 'application/json', 'Content-Type': 'application/json' })
.searchParams({ page: 1, first: 12 })
.get('/posts')
.json();
```
7 changes: 3 additions & 4 deletions docs/pages/reference/_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"core": "Core",
"resolver": "Resolver",
"error": "Error",
"fetchtastic": "Fetchtastic",
"http-error": "HttpError",
"types": "Types",
"utility": "Utility"
"utilities": "Utilities"
}
241 changes: 0 additions & 241 deletions docs/pages/reference/core.mdx

This file was deleted.

31 changes: 0 additions & 31 deletions docs/pages/reference/error.mdx

This file was deleted.

Loading

0 comments on commit 9b38a94

Please sign in to comment.