@digital-ai/plugin-dai-deploy
Version:
Frontend functionalities for the dai-deploy backstage plugin
91 lines (88 loc) • 3.64 kB
JavaScript
import { parseErrorResponseBody, AuthenticationError, NotAllowedError, NotFoundError, ServiceUnavailableError } from '@backstage/errors';
import { beginDateFormat, endDateFormat } from './utils.esm.js';
import moment from 'moment';
import { parseEntityRef } from '@backstage/catalog-model';
class DaiDeployApiClient {
discoveryApi;
identityApi;
constructor(options) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
}
async getToken() {
const { token } = await this.identityApi.getCredentials();
return token;
}
async getCurrentDeployments(ciId, page, rowsPerPage, orderBy, orderDirection, entity) {
const queryString = new URLSearchParams();
const now = /* @__PURE__ */ new Date();
const order = `${orderBy}:${orderDirection}`;
const entityRef = parseEntityRef(entity);
queryString.append("appName", ciId);
queryString.append(
"beginDate",
moment(now).subtract(7, "days").format(beginDateFormat)
);
queryString.append("endDate", moment(now).format(endDateFormat));
queryString.append("order", order);
queryString.append("pageNumber", (page + 1).toString());
queryString.append("resultsPerPage", rowsPerPage.toString());
queryString.append("taskSet", "ALL");
const urlSegment = `deployment-status/${encodeURIComponent(entityRef.namespace)}/${encodeURIComponent(
entityRef.kind
)}/${encodeURIComponent(entityRef.name)}?${queryString}`;
const items = await this.get(urlSegment);
return { items };
}
async getDeploymentsReports(ciId, page, rowsPerPage, orderBy, orderDirection, entity) {
const queryString = new URLSearchParams();
const order = `${orderBy}:${orderDirection}`;
const now = /* @__PURE__ */ new Date();
const entityRef = parseEntityRef(entity);
queryString.append("appName", ciId);
queryString.append(
"beginDate",
moment(now).subtract(7, "days").format(beginDateFormat)
);
queryString.append("endDate", moment(now).format(endDateFormat));
queryString.append("order", order);
queryString.append("pageNumber", (page + 1).toString());
queryString.append("resultsPerPage", rowsPerPage.toString());
const urlSegment = `deployment-history/${encodeURIComponent(entityRef.namespace)}/${encodeURIComponent(
entityRef.kind
)}/${encodeURIComponent(entityRef.name)}?${queryString}`;
const items = await this.get(urlSegment);
return { items };
}
async get(path) {
const baseUrl = `${await this.discoveryApi.getBaseUrl("dai-deploy")}/`;
const url = new URL(path, baseUrl);
const idToken = await this.getToken();
const response = await fetch(url.toString(), {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${idToken}`
}
});
if (!response.ok) {
const data = await parseErrorResponseBody(response);
if (response.status === 401) {
throw new AuthenticationError(data.error.message);
} else if (response.status === 403) {
throw new NotAllowedError(data.error.message);
} else if (response.status === 404) {
throw new NotFoundError(data.error.message);
} else if (response.status === 500) {
throw new ServiceUnavailableError(`Deploy Service Unavailable`);
}
throw new Error(
`Unexpected error: failed to fetch data, status ${response.status}: ${response.statusText}`
);
}
return await response.json();
}
}
export { DaiDeployApiClient };
//# sourceMappingURL=DaiDeployApiClient.esm.js.map