UNPKG

@careevolution/orchestrate

Version:

A TypeScript client for the Orchestrate API

140 lines (139 loc) 5.75 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpHandler = void 0; const exceptions_js_1 = require("./exceptions.js"); class OperationalOutcomeIssue { constructor(severity, code, diagnostics, details) { this.severity = severity; this.code = code; this.diagnostics = diagnostics; this.details = details; } toString() { let s = `${this.severity}: ${this.code}`; const message = [this.details, this.diagnostics] .filter(msg => msg) .join("; "); if (message) { s += ` - ${message}`; } return s; } } function getIssueDetailString(detail) { if (detail === null || detail === void 0 ? void 0 : detail.text) { return detail.text; } if ((detail === null || detail === void 0 ? void 0 : detail.coding) && Array.isArray(detail.coding)) { for (const coding of detail.coding) { const codingString = getDetailCodingString(coding); if (codingString) { return codingString; } } } return ""; } function getDetailCodingString(coding) { const parts = [coding === null || coding === void 0 ? void 0 : coding.code, coding === null || coding === void 0 ? void 0 : coding.display].filter(s => s); return parts.join(": "); } function readJsonOutcomes(responseText) { return __awaiter(this, void 0, void 0, function* () { try { const json = JSON.parse(responseText); if (json.issue) { return json.issue.map((issue) => // eslint-disable-line @typescript-eslint/no-explicit-any new OperationalOutcomeIssue(issue.severity || "", issue.code || "", issue.diagnostics || "", getIssueDetailString(issue.details || {}))); } if (json.type === "https://tools.ietf.org/html/rfc9110#section-15.5.1") { return [new OperationalOutcomeIssue("error", json.title || "", json.detail || "", "")]; } return []; } catch (e) { return []; } }); } function readOperationalOutcomes(responseText) { return __awaiter(this, void 0, void 0, function* () { const outcomes = yield readJsonOutcomes(responseText); if (outcomes.length > 0) { return outcomes.map((o) => o.toString()); } return [responseText]; }); } function errorFromResponse(response) { return __awaiter(this, void 0, void 0, function* () { const responseText = yield response.text(); const operationalOutcomes = yield readOperationalOutcomes(responseText); if (response.status >= 400 && response.status < 600) { throw new exceptions_js_1.OrchestrateClientError(responseText, operationalOutcomes); } throw new exceptions_js_1.OrchestrateHttpError(responseText); }); } class HttpHandler { constructor(baseUrl, defaultHeaders, timeoutMs) { this.baseUrl = baseUrl; this.defaultHeaders = defaultHeaders; this.timeoutMs = timeoutMs; } mergeHeaders(headers) { return Object.assign(Object.assign({}, this.defaultHeaders), (headers !== null && headers !== void 0 ? headers : {})); } toString() { return this.baseUrl; } post(path, body, // eslint-disable-line @typescript-eslint/no-explicit-any headers) { return __awaiter(this, void 0, void 0, function* () { const requestHeaders = this.mergeHeaders(headers); const preparedBody = requestHeaders["Content-Type"] === "application/json" ? JSON.stringify(body) : body; const url = this.baseUrl + path; const response = yield fetch(url, { method: "POST", headers: requestHeaders, body: preparedBody, signal: this.timeoutMs ? AbortSignal.timeout(this.timeoutMs) : undefined, }); if (!response.ok) { yield errorFromResponse(response); } if (requestHeaders["Accept"] === "application/zip" || requestHeaders["Accept"] === "application/pdf") { return yield response.arrayBuffer(); } if (requestHeaders["Accept"] === "application/json") { return yield response.json(); } return yield response.text(); }); } get(path, headers) { return __awaiter(this, void 0, void 0, function* () { const requestHeaders = this.mergeHeaders(headers); const url = this.baseUrl + path; const response = yield fetch(url, { method: "GET", headers: requestHeaders, signal: this.timeoutMs ? AbortSignal.timeout(this.timeoutMs) : undefined, }); if (!response.ok) { yield errorFromResponse(response); } return yield response.json(); }); } } exports.HttpHandler = HttpHandler;