Skip to content

Commit

Permalink
Document middlewares.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelroemer committed Dec 13, 2023
1 parent 8b7ccac commit 79bd91d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/plugins/piral-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,48 @@ const instance = createInstance({

**Note**: `piral-fetch` plays nicely together with authentication providers such as `piral-adal`. As such authentication tokens are automatically inserted on requests to the base URL.

### Middlewares

`piral-fetch` allows you to configure middleware functions which are executed on each `fetch` call. Middleware functions receive the same parameters as `fetch`, plus a `next` function which calls either the next middleware or the actual `fetch` function. The following code shows an exemplary middleware which logs when requests start and finish:

```ts
const logRequests: FetchMiddleware = async (
path: string,
options: FetchOptions,
next: PiletFetchApiFetch,
): Promise<FetchResponse<any>> => {
try {
console.log(`Making request to ${path}...`);
const response = await next(path, options);
console.log(`Request to ${path} returned status code ${response.code}.`);
return response;
} catch (e) {
console.error(`Request to ${path} threw an error: `, e);
throw e;
}
};
```

Middlewares must be configured in the Piral instance:

```ts
const instance = createInstance({
plugins: [createFetchApi({
// important part
middlewares: [
firstMiddleware,
secondMiddleware,
thirdMiddleware,
logRequests,
],
// ...other options...
})],
// ...
});
```

Middlewares are invoked in a top-down order. In the above example, this means that `firstMiddleware` is invoked first, then `secondMiddleware`, then `thirdMiddleware`, then `logRequests` and finally the actual `fetch` function.

:::

## License
Expand Down

0 comments on commit 79bd91d

Please sign in to comment.