From c1862f42caeaa4653a40db1223b8678ff6c2bf64 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 10 Apr 2021 13:48:20 +0200 Subject: [PATCH] config-schema: add StaticSchemaLoader Signed-off-by: Patrik Oldsberg --- plugins/config-schema/package.json | 1 + .../src/api/StaticSchemaLoader.ts | 57 +++++++++++++++++++ plugins/config-schema/src/api/index.ts | 1 + plugins/config-schema/src/index.ts | 2 + 4 files changed, 61 insertions(+) create mode 100644 plugins/config-schema/src/api/StaticSchemaLoader.ts diff --git a/plugins/config-schema/package.json b/plugins/config-schema/package.json index 67dcfa5976..83d04e1334 100644 --- a/plugins/config-schema/package.json +++ b/plugins/config-schema/package.json @@ -22,6 +22,7 @@ "dependencies": { "@backstage/config": "^0.1.4", "@backstage/core": "^0.7.3", + "@backstage/errors": "^0.1.1", "@backstage/theme": "^0.2.5", "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", diff --git a/plugins/config-schema/src/api/StaticSchemaLoader.ts b/plugins/config-schema/src/api/StaticSchemaLoader.ts new file mode 100644 index 0000000000..ee61370468 --- /dev/null +++ b/plugins/config-schema/src/api/StaticSchemaLoader.ts @@ -0,0 +1,57 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Observable } from '@backstage/core'; +import ObservableImpl from 'zen-observable'; +import { ResponseError } from '@backstage/errors'; +import { Schema } from 'jsonschema'; +import { ConfigSchemaApi, ConfigSchemaResult } from './types'; + +const DEFAULT_URL = 'config-schema.json'; + +/** + * A ConfigSchemaApi implementation that loads the configuration from a URL. + */ +export class StaticSchemaLoader implements ConfigSchemaApi { + private readonly url: string; + + constructor({ url = DEFAULT_URL }: { url?: string } = {}) { + this.url = url; + } + + schema$(): Observable { + return new ObservableImpl(subscriber => { + this.fetchSchema().then( + schema => subscriber.next({ schema }), + error => subscriber.error(error), + ); + }); + } + + private async fetchSchema(): Promise { + const res = await fetch(this.url); + + if (!res.ok) { + if (res.status === 404) { + return undefined; + } + + throw ResponseError.fromResponse(res); + } + + return await res.json(); + } +} diff --git a/plugins/config-schema/src/api/index.ts b/plugins/config-schema/src/api/index.ts index ed5e40c268..eb705b3fa3 100644 --- a/plugins/config-schema/src/api/index.ts +++ b/plugins/config-schema/src/api/index.ts @@ -16,3 +16,4 @@ export { configSchemaApiRef } from './types'; export type { ConfigSchemaApi } from './types'; +export { StaticSchemaLoader } from './StaticSchemaLoader'; diff --git a/plugins/config-schema/src/index.ts b/plugins/config-schema/src/index.ts index 0254d6a36c..009b093d50 100644 --- a/plugins/config-schema/src/index.ts +++ b/plugins/config-schema/src/index.ts @@ -13,4 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +export * from './api'; export { configSchemaPlugin, ConfigSchemaPage } from './plugin';