Add fetch examples

Co-authored-by: Fredrik Adelöw <freben@users.noreply.github.com>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-12-21 11:21:04 +01:00
parent fbc20dc18e
commit e5703a3482
@@ -1,7 +1,8 @@
---
id: adrs-adr013
title: 'ADR013: Use node-fetch for data fetching'
description: Architecture Decision Record (ADR) for HTTP data fetching packages
title: 'ADR013: Proper use of HTTP fetching libraries'
# prettier-ignore
description: Architecture Decision Record (ADR) for the proper use of fetchApiRef, node-fetch, and cross-fetch for data fetching.
---
## Context
@@ -11,9 +12,57 @@ support burden of keeping said package up to date.
## Decision
Node packages should use the `node-fetch` package for HTTP data fetching.
Isomorphic packages should use the `cross-fetch` as a development dependency and
rely on the built-in `fetch` API.
Backend (node) packages should use the `node-fetch` package for HTTP data
fetching. Example:
```ts
import fetch from 'node-fetch';
import { ResponseError } from '@backstage/errors';
const response = await fetch('https://example.com/api/v1/users.json');
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
const users = await response.json();
```
Frontend plugins and packages should prefer to use the
[`fetchApiRef`](https://backstage.io/docs/reference/core-plugin-api.fetchapiref).
It uses `cross-fetch` internally. Example:
```ts
import { useApi } from '@backstage/core-plugin-api';
const { fetch } = useApi(fetchApiRef);
const response = await fetch('https://example.com/api/v1/users.json');
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
const users = await response.json();
```
Isomorphic packages should have a dependency on the `cross-fetch` package for
mocking and type definitions. Preferably, classes and functions in isomorphic
packages should accept an argument of type `typeof fetch` to let callers supply
their preferred implementation of `fetch`. This lets them adorn the calls with
auth or other information, and track metrics etc, in a cross-platform way.
Example:
```ts
import crossFetch from 'cross-fetch';
export class MyClient {
private readonly fetch: typeof crossFetch;
constructor(options: { fetch?: typeof crossFetch }) {
this.fetch = options.fetch || crossFetch;
}
async users() {
return await this.fetch('https://example.com/api/v1/users.json');
}
}
```
## Consequences