UNPKG

@gotamedia/oc

Version:

Set of OC helper for NodeJs Runtime.

48 lines (47 loc) 2.2 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.LOG_TYPES = exports.getLogs = void 0; const errors_1 = require("../errors"); const configure_js_1 = require("../configure.js"); const LOG_TYPES = { Event: "eventlog", Content: "contentlog" }; exports.LOG_TYPES = LOG_TYPES; const getLogs = (id, type) => __awaiter(void 0, void 0, void 0, function* () { const { baseUrl, username, password } = configure_js_1.OC_SERVICE_CONFIG; if (!baseUrl || !username || !password) { throw new errors_1.LogsError("Missing required params for OC service, make sure to pass valid: 'baseUrl', 'username' and 'password'"); } if (!type) { throw new errors_1.LogsError("Missing required type for OC service, make sure to pass valid type: 'eventlog' OR 'contentlog'"); } try { const requestUrl = `${baseUrl}/${type}?event=${id}`; const credentials = Buffer.from(`${username}:${password}`).toString("base64"); const requestOptions = { headers: { Authorization: `Basic ${credentials}` } }; const response = yield fetch(requestUrl, requestOptions); const { events } = yield response.json(); if (!Array.isArray(events) || !events.length) { return; } return events; } catch (error) { throw new errors_1.LogsError("Something went wrong while fetching logs from OC", { cause: error }); } }); exports.getLogs = getLogs;