@fortress-js/node
Version:
The Fortress Backend SDK
215 lines (214 loc) • 8.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const crypto_1 = require("./crypto");
class InternalError extends Error {
constructor(message = "Internal Server Error") {
super(message);
this.name = "InternalError";
}
}
class ValidationError extends Error {
constructor(message = "Validation Error") {
super(message);
this.name = "ValidationError";
}
}
class Client {
constructor(orgId, apiKey) {
this.baseUrl = "https://api.dev.fortress.build";
this.orgId = orgId;
this.apiKey = apiKey;
}
get(uri) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(uri, {
headers: { "Api-Key": this.apiKey },
});
return response;
});
}
post(uri, data) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(uri, {
method: "POST",
headers: { "Api-Key": this.apiKey },
body: data !== undefined ? JSON.stringify(data) : undefined,
});
return response;
});
}
delete(uri) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(uri, {
method: "DELETE",
headers: { "Api-Key": this.apiKey },
});
return response;
});
}
parseDate(dateStr) {
return new Date(dateStr);
}
getUri(id, type) {
return __awaiter(this, void 0, void 0, function* () {
if (type !== "tenant" && type !== "database") {
throw new ValidationError("Type must be either 'tenant' or 'database'");
}
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/${type}/${id}/uri`;
const response = yield this.get(endpoint);
const jsonResponse = yield response.json();
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
if (response.status === 500) {
throw new InternalError("Internal Server Error");
}
}
let connectionDetailsStr = null;
try {
connectionDetailsStr = (0, crypto_1.decrypt)(this.apiKey, jsonResponse.connectionDetails || "");
}
catch (_a) {
throw new InternalError("An error occurred: Unable to decrypt connection details");
}
if (!connectionDetailsStr) {
throw new InternalError("An error occurred: Unable to decrypt connection details");
}
const cleanedConnectionDetailsStr = connectionDetailsStr.replace(/[^\x20-\x7E]/g, "");
const connectionDetails = JSON.parse(cleanedConnectionDetailsStr);
const url = connectionDetails.url || "";
const database = connectionDetails.database || "";
const port = Number.parseInt(connectionDetails.port || "0", 10);
const username = connectionDetails.username || "";
const password = connectionDetails.password || "";
if (!url || !port || !username || !password || !database) {
throw new InternalError("An error occurred: Invalid connection details");
}
return {
databaseId: jsonResponse.databaseId,
url,
database,
port,
username,
password,
};
});
}
createDatabase(platform, alias) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/database`;
const response = yield this.post(endpoint, { platform, alias });
const jsonResponse = yield response.json();
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
if (response.status === 500) {
throw new InternalError("Internal Server Error");
}
}
return jsonResponse.databaseId;
});
}
deleteDatabase(databaseId) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/database/${databaseId}`;
const response = yield this.delete(endpoint);
const jsonResponse = yield response.json();
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
if (response.status === 500) {
throw new InternalError("Internal Server Error");
}
}
});
}
listDatabases() {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/databases`;
const response = yield this.get(endpoint);
const jsonResponse = yield response.json();
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
throw new InternalError("Internal Server Error");
}
return jsonResponse.databases.map((data) => ({
id: data.databaseId || "",
alias: data.alias || "",
size: data.sizeBytes || 0,
averageReadIOPS: data.averageReadIOPS || 0,
averageWriteIOPS: data.averageWriteIOPS || 0,
createdDate: this.parseDate(data.createdDate || ""),
}));
});
}
createTenant(tenantId, isolationLevel, platform, alias, databaseId) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/tenant/${tenantId}`;
const payload = {
isolationLevel,
platform,
alias,
databaseId,
};
const response = yield this.post(endpoint, payload);
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
if (response.status === 500) {
throw new InternalError("Internal Server Error");
}
}
});
}
deleteTenant(tenantId) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/tenant/${tenantId}`;
const response = yield this.delete(endpoint);
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
if (response.status === 500) {
throw new InternalError("Internal Server Error");
}
}
});
}
listTenants() {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${this.baseUrl}/v1/organization/${this.orgId}/tenants`;
const response = yield this.get(endpoint);
const jsonResponse = yield response.json();
if (response.status !== 200) {
if (response.status === 400) {
throw new ValidationError("Validation Error");
}
throw new InternalError("Internal Server Error");
}
return jsonResponse.tenants.map((data) => ({
id: data.tenantId || "",
alias: data.alias || "",
databaseId: data.databaseId || "",
createdDate: this.parseDate(data.createdDate || ""),
}));
});
}
}
exports.Client = Client;