UNPKG

@daml/hub-react

Version:

Daml React functions for Daml Hub

199 lines 10.3 kB
"use strict"; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.undeployAutomation = exports.deleteInstance = exports.deployAutomation = exports.listAutomationInstances = exports.listPublishedAutomations = void 0; var log_1 = __importDefault(require("../log")); var schemas_1 = require("./schemas"); /** ======================== List Public Automations ======================== */ var listPublishedAutomations = function (publicToken) { return __awaiter(void 0, void 0, void 0, function () { var headers, result, json, automations, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: headers = { Authorization: "Bearer ".concat(publicToken), 'Content-Type': 'application/json', }; _a.label = 1; case 1: _a.trys.push([1, 4, , 5]); return [4 /*yield*/, fetch("/.hub/v1/published", { headers: headers, method: 'GET' })]; case 2: result = _a.sent(); return [4 /*yield*/, result.json()]; case 3: json = _a.sent(); automations = schemas_1.publicAutomationListDecoder.runWithException(json); return [2 /*return*/, automations]; case 4: error_1 = _a.sent(); (0, log_1.default)('automation').error("Error fetching automation list: ".concat(JSON.stringify(error_1))); throw error_1; case 5: return [2 /*return*/]; } }); }); }; exports.listPublishedAutomations = listPublishedAutomations; /** ======================== List Automation Instances ======================== */ var listAutomationInstances = function (token) { return __awaiter(void 0, void 0, void 0, function () { var headers, result, json, instances, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: headers = { Authorization: "Bearer ".concat(token), 'Content-Type': 'application/json', }; _a.label = 1; case 1: _a.trys.push([1, 4, , 5]); return [4 /*yield*/, fetch("/.hub/v1/published/instance", { headers: headers, method: 'GET' })]; case 2: result = _a.sent(); return [4 /*yield*/, result.json()]; case 3: json = _a.sent(); instances = schemas_1.instanceListDecoder.runWithException(json); return [2 /*return*/, instances]; case 4: error_2 = _a.sent(); (0, log_1.default)('automation').error("Error fetching automation instances: ".concat(JSON.stringify(error_2))); throw error_2; case 5: return [2 /*return*/]; } }); }); }; exports.listAutomationInstances = listAutomationInstances; /** ==================== Deploying New Automation Instances ==================== */ var deployAutomation = function (token, automations, artifactHash, trigger) { return __awaiter(void 0, void 0, void 0, function () { var artifact, headers, body, response, json, instance, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 5, , 6]); artifact = automations === null || automations === void 0 ? void 0 : automations.find(function (a) { return a.artifactHash === artifactHash; }); if (!artifact) return [3 /*break*/, 3]; headers = { Authorization: "Bearer ".concat(token), 'Content-Type': 'application/json', }; body = JSON.stringify({ artifactHash: artifact.artifactHash, owner: artifact.owner, name: trigger, }); return [4 /*yield*/, fetch("/.hub/v1/published/deploy", { method: 'POST', headers: headers, body: body })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: json = _a.sent(); instance = schemas_1.instanceDecoder.runWithException(json); return [2 /*return*/, instance]; case 3: throw new Error('Artifact hash does not exist in list of automations'); case 4: return [3 /*break*/, 6]; case 5: error_3 = _a.sent(); (0, log_1.default)('automation').error("Error attempting to deploy an automation instance: ".concat(JSON.stringify(error_3))); throw error_3; case 6: return [2 /*return*/]; } }); }); }; exports.deployAutomation = deployAutomation; /** ==================== Deleting an Automation Instances ==================== */ var deleteInstance = function (token, instanceId, owner) { return __awaiter(void 0, void 0, void 0, function () { var headers, body, url, response, json, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); headers = { Authorization: "Bearer ".concat(token), 'Content-Type': 'application/json', }; body = JSON.stringify({ instanceId: instanceId, owner: owner }); url = "/.hub/v1/published/instance/delete"; return [4 /*yield*/, fetch(url, { method: 'POST', headers: headers, body: body })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: json = _a.sent(); return [2 /*return*/, schemas_1.successResponseDecoder.runWithException(json)]; case 3: error_4 = _a.sent(); (0, log_1.default)('automation').error("Error attempting to delete an automation instance: ".concat(JSON.stringify(error_4))); throw error_4; case 4: return [2 /*return*/]; } }); }); }; exports.deleteInstance = deleteInstance; /** ==================== Undeploying all Automation Instances ==================== */ var undeployAutomation = function (token, artifactHash) { return __awaiter(void 0, void 0, void 0, function () { var headers, url, response, json, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); headers = { Authorization: "Bearer ".concat(token), 'Content-Type': 'application/json', }; url = "/.hub/v1/published/undeploy/".concat(artifactHash); return [4 /*yield*/, fetch(url, { method: 'POST', headers: headers })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: json = _a.sent(); return [2 /*return*/, schemas_1.successResponseDecoder.runWithException(json)]; case 3: error_5 = _a.sent(); (0, log_1.default)('automation').error("Error attempting to delete an automation instance: ".concat(JSON.stringify(error_5))); throw error_5; case 4: return [2 /*return*/]; } }); }); }; exports.undeployAutomation = undeployAutomation; //# sourceMappingURL=automation.js.map