config-schema: add StaticSchemaLoader
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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<ConfigSchemaResult> {
|
||||
return new ObservableImpl(subscriber => {
|
||||
this.fetchSchema().then(
|
||||
schema => subscriber.next({ schema }),
|
||||
error => subscriber.error(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private async fetchSchema(): Promise<undefined | Schema> {
|
||||
const res = await fetch(this.url);
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 404) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
throw ResponseError.fromResponse(res);
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
}
|
||||
}
|
||||
@@ -16,3 +16,4 @@
|
||||
|
||||
export { configSchemaApiRef } from './types';
|
||||
export type { ConfigSchemaApi } from './types';
|
||||
export { StaticSchemaLoader } from './StaticSchemaLoader';
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user