UNPKG

@backstage-community/plugin-gitops-profiles

Version:

A Backstage plugin that helps you manage GitOps profiles

85 lines (82 loc) 2.19 kB
import { createApiRef } from '@backstage/core-plugin-api'; class FetchError extends Error { get name() { return this.constructor.name; } static async forResponse(resp) { return new FetchError( `Request failed with status code ${resp.status}. Reason: ${await resp.text()}` ); } } const gitOpsApiRef = createApiRef({ id: "plugin.gitops.service" }); class GitOpsRestApi { constructor(url = "") { this.url = url; } async fetch(path, init) { const resp = await fetch(`${this.url}${path}`, init); if (!resp.ok) throw await FetchError.forResponse(resp); return await resp.json(); } async fetchUserInfo(req) { const resp = await fetch(`https://api.github.com/user`, { method: "get", headers: new Headers({ Authorization: `token ${req.accessToken}` }) }); if (!resp.ok) throw await FetchError.forResponse(resp); return await resp.json(); } async fetchLog(req) { return await this.fetch(`/api/cluster/run-status`, { method: "post", headers: new Headers({ "Content-Type": "application/json" }), body: JSON.stringify(req) }); } async changeClusterState(req) { return await this.fetch("/api/cluster/state", { method: "post", headers: new Headers({ "Content-Type": "application/json" }), body: JSON.stringify(req) }); } async cloneClusterFromTemplate(req) { return await this.fetch("/api/cluster/clone-from-template", { method: "post", headers: new Headers({ "Content-Type": "application/json" }), body: JSON.stringify(req) }); } async applyProfiles(req) { return await this.fetch("/api/cluster/profiles", { method: "post", headers: new Headers({ "Content-Type": "application/json" }), body: JSON.stringify(req) }); } async listClusters(req) { return await this.fetch("/api/clusters", { method: "post", headers: new Headers({ "Content-Type": "application/json" }), body: JSON.stringify(req) }); } } export { FetchError, GitOpsRestApi, gitOpsApiRef }; //# sourceMappingURL=api.esm.js.map