Format, require ETag, fix imports

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-09-29 10:56:26 +02:00
parent db920df053
commit 9c8142cdac
@@ -174,16 +174,16 @@ registered location and you'll see your entities start to appear in Backstage.
## Caching processing results
The catalog periodically refreshes entities in the catalog, and in doing so it calls
out to external systems to fetch changes. This can be taxing for upstream
The catalog periodically refreshes entities in the catalog, and in doing so it
calls out to external systems to fetch changes. This can be taxing for upstream
services and large deployments may get rate limited if too many requests are
sent. Luckily many external systems provide ETag support to check for changes
which usually doesn't count towards the quota and saves resources both internally
and externally.
which usually doesn't count towards the quota and saves resources both
internally and externally.
The catalog has built in support for leveraging ETags when refreshing external locations
in GitHub. This example aims to demonstrate how to add the same behavior for `system-x`
that we implemented earlier.
The catalog has built in support for leveraging ETags when refreshing external
locations in GitHub. This example aims to demonstrate how to add the same
behavior for `system-x` that we implemented earlier.
```ts
import { UrlReader } from '@backstage/backend-common';
@@ -193,8 +193,8 @@ import {
CatalogProcessor,
CatalogProcessorEmit,
CatalogProcessorCache,
CatalogProcessorParser,
} from '@backstage/plugin-catalog-backend';
import { CatalogProcessorParser } from './CatalogProcessorParser';
// It's recommended to always bump the CACHE_KEY version if you make
// changes to the processor implementation or CacheItem.
@@ -204,7 +204,7 @@ const CACHE_KEY = 'v1';
// as well as the processing result used when the Etag matches.
// Bump the CACHE_KEY version if you make any changes to this type.
type CacheItem = {
etag?: string;
etag: string;
entity: Entity;
};
@@ -244,6 +244,13 @@ export class SystemXReaderProcessor implements CatalogProcessor {
);
}
// ETag is optional in the response but we need it to cache the result.
if (!response.etag) {
throw new Error(
'No ETag returned from system-x, cannot use response for caching',
);
}
// For this example the JSON payload is a single entity.
const entity: Entity = JSON.parse(response.buffer.toString());
emit(results.entity(location, entity));