From 198affd2599f93c6491eeba1ef1dfe5f78b3384a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Sun, 4 Feb 2024 14:45:06 +0100 Subject: [PATCH] just keep the updated docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../help-im-behind-a-corporate-proxy.md | 2 +- .../adr013-use-node-fetch.md | 30 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/contrib/docs/tutorials/help-im-behind-a-corporate-proxy.md b/contrib/docs/tutorials/help-im-behind-a-corporate-proxy.md index 6ae6d10fac..deef356d84 100644 --- a/contrib/docs/tutorials/help-im-behind-a-corporate-proxy.md +++ b/contrib/docs/tutorials/help-im-behind-a-corporate-proxy.md @@ -84,7 +84,7 @@ The app port must proxy web socket connections in order to make hot reloading wo ## Alternatives to `global-agent` -The `proxy-agent` package can be used as an alternative to `global-agent` (do not install both!), and also ensures that the `node-fetch` library correctly respects proxy settings, but [does NOT work](https://github.com/TooTallNate/proxy-agents/issues/239) for modern `undici` based native Node.js `fetch`, so you'll have to also do the `undici` steps in the section above in addition to this. +The `proxy-agent` package can be used as an alternative to `global-agent` (do not install both!), and also ensures that the `node-fetch` library correctly respects proxy settings, but [does NOT work](https://github.com/TooTallNate/proxy-agents/issues/239) for modern `undici` based native Node.js `fetch`, so you'll still have to also do the `undici` steps in the section above in addition to this. `proxy-agent` is a library that you can use to override the `globalAgents` of `node` land with a tunnel to use for each request. diff --git a/docs/architecture-decisions/adr013-use-node-fetch.md b/docs/architecture-decisions/adr013-use-node-fetch.md index e7d1494b0b..d802988ae4 100644 --- a/docs/architecture-decisions/adr013-use-node-fetch.md +++ b/docs/architecture-decisions/adr013-use-node-fetch.md @@ -2,7 +2,7 @@ id: adrs-adr013 title: 'ADR013: Proper use of HTTP fetching libraries' # prettier-ignore -description: Architecture Decision Record (ADR) for the proper use of fetch libraries for data fetching. +description: Architecture Decision Record (ADR) for the proper use of fetchApiRef, node-fetch, and cross-fetch for data fetching. --- ## Context @@ -12,13 +12,13 @@ support burden of keeping said package up to date. ## Decision -Newly written backend (Node.js) packages should use the native `fetch` call for HTTP data -fetching. Additionally, in legacy code, using the `node-fetch` library continues to be allowed. Other third party fetching libraries (for example `axios`, `got` etc) are not allowed. Example: +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'; -// note that this is the global fetch, not imported from anywhere const response = await fetch('https://example.com/api/v1/users.json'); if (!response.ok) { throw await ResponseError.fromResponse(response); @@ -26,19 +26,19 @@ if (!response.ok) { const users = await response.json(); ``` -Frontend plugins and packages should use the [`fetchApiRef`](https://backstage.io/docs/reference/core-plugin-api.fetchapiref). Example: +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, fetchApiRef } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; +const { fetch } = useApi(fetchApiRef); -const MyComponent = () => { - 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(); - // ... +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 @@ -67,5 +67,5 @@ export class MyClient { ## Consequences We will gradually transition away from third party packages such as `axios`, -`got` and others. Once we have transitioned to native `fetch` and `node-fetch` we will add lint +`got` and others. Once we have transitioned to `node-fetch` we will add lint rules to enforce this decision.