UNPKG

@unito/integration-debugger

Version:

The Unito Integration Debugger

201 lines (200 loc) 6.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setEnvironment = setEnvironment; exports.getApiKey = getApiKey; exports.setApiKey = setApiKey; exports.getProfile = getProfile; exports.getIntegration = getIntegration; exports.getIntegrations = getIntegrations; exports.getCredential = getCredential; exports.getCredentials = getCredentials; exports.getPlatformAlive = getPlatformAlive; exports.getApiKeyAlive = getApiKeyAlive; exports.getIntegrationAlive = getIntegrationAlive; exports.getNoneIntegrationAlive = getNoneIntegrationAlive; exports.getCredentialAlive = getCredentialAlive; exports.getNoneCredentialAlive = getNoneCredentialAlive; const IntegrationsPlatform = __importStar(require("@unito/integrations-platform-client")); const headers_1 = __importDefault(require("../resources/headers")); const configuration_1 = require("../resources/configuration"); const urls_1 = require("../resources/urls"); const Servers = { [configuration_1.Environment.None]: '', [configuration_1.Environment.Local]: IntegrationsPlatform.servers.local, [configuration_1.Environment.Staging]: IntegrationsPlatform.servers.staging, [configuration_1.Environment.Production]: IntegrationsPlatform.servers.production, }; function setEnvironment(environment) { IntegrationsPlatform.defaults.baseUrl = Servers[environment]; } function getApiKey() { const header = IntegrationsPlatform.defaults.headers?.Authorization?.toString(); return header?.split(' ')?.at(1); } function setApiKey(apiKey) { if (apiKey) { IntegrationsPlatform.defaults.headers = { Authorization: `Bearer ${apiKey}` }; } else { IntegrationsPlatform.defaults.headers = {}; } } async function getProfile() { return IntegrationsPlatform.optimistic(IntegrationsPlatform.getProfile()); } async function getIntegration(integrationId) { return IntegrationsPlatform.optimistic(IntegrationsPlatform.getIntegrationById(integrationId)); } async function getIntegrations() { const integrations = []; let page; const limit = 100; let nextPageOffset = 0; do { page = await IntegrationsPlatform.optimistic(IntegrationsPlatform.getIntegrations({ pagination: { offset: nextPageOffset, limit: limit + 1 }, })); nextPageOffset += limit; integrations.push(...page.data); } while (page.data.length > limit); return integrations; } async function getCredential(credentialId) { return IntegrationsPlatform.optimistic(IntegrationsPlatform.getCredentialById(credentialId)); } async function getCredentials(integrationId) { const credentials = []; let page; const limit = 100; let nextPageOffset = 0; do { page = await IntegrationsPlatform.optimistic(IntegrationsPlatform.getCredentials({ pagination: { offset: nextPageOffset, limit: limit + 1 }, })); nextPageOffset += limit; credentials.push(...page.data); } while (page.data.length > limit); // TODO: add filter on integration. return credentials.filter(credential => credential.integrationId === integrationId); } async function getPlatformAlive(environment) { if (environment === configuration_1.Environment.None) { return true; } let alive = true; try { await fetch(`${IntegrationsPlatform.defaults.baseUrl}/health/alive`); } catch { alive = false; } return alive; } async function getApiKeyAlive(environment) { if (environment === configuration_1.Environment.None) { return true; } let alive = true; try { await IntegrationsPlatform.optimistic(IntegrationsPlatform.getProfile()); } catch { alive = false; } return alive; } async function getIntegrationAlive(integration, credential) { if (!integration || !credential) { return false; } let alive = true; try { await IntegrationsPlatform.optimistic(IntegrationsPlatform.getProxyGraph((credential?.id ?? -1).toString(), '/')); } catch { // TODO: catch less errors. alive = false; } return alive; } async function getNoneIntegrationAlive(integrationUrl) { if (!integrationUrl) { return false; } let alive = true; try { const response = await fetch((0, urls_1.toSafeUrl)(integrationUrl, 'health')); alive = response.status < 500; } catch (e) { // TODO: catch less errors. alive = false; } return alive; } async function getCredentialAlive(credential) { if (!credential) { return false; } let alive = true; try { await IntegrationsPlatform.optimistic(IntegrationsPlatform.getProxyGraph((credential?.id ?? -1).toString(), '/')); } catch { // TODO: catch less errors. alive = false; } return alive; } async function getNoneCredentialAlive(integrationUrl, credentialPayload) { if (!integrationUrl || !credentialPayload) { return false; } let alive = true; try { await fetch((0, urls_1.toSafeUrl)(integrationUrl, 'health'), { headers: { [headers_1.default.CREDENTIALS]: Buffer.from(JSON.stringify(credentialPayload)).toString('base64') }, }); } catch { // TODO: catch less errors. alive = false; } return alive; }