UNPKG

@retracedhq/retraced

Version:

The official NodeJS client for interacting with the Retraced audit logging API.

127 lines (126 loc) 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const _ = require("lodash"); const event_1 = require("./event"); const graphql_1 = require("./graphql"); const axios_1 = require("axios"); const defaultEndpoint = "http://localhost:3000/auditlog"; class Client { constructor(config) { if (!config.endpoint) { config.endpoint = defaultEndpoint; } this.config = config; } mapping(event) { return { action: event.action, group: event.group, crud: event.crud, created: event.created, actor: event.actor, target: event.target, source_ip: event.source_ip, description: event.description, is_failure: event.is_failure, is_anonymous: event.is_anonymous, fields: event.fields, component: this.config.component, version: this.config.version, external_id: event.external_id, metadata: event.metadata, }; } async reportEvent(event) { const { endpoint, apiKey, projectId } = this.config; const requestBody = this.mapping(event); let newEvent; try { const { data } = await axios_1.default.post(`${endpoint}/publisher/v1/project/${projectId}/event`, requestBody, { headers: { Accept: "application/json", Authorization: `token=${apiKey}`, }, }); newEvent = data; } catch (err) { const status = err.response ? err.response.status : 500; const statusText = err.response ? err.response.statusText : "Unknown"; throw new Error(`Unexpected HTTP response: ${status} ${statusText}`); } try { (0, event_1.verifyHash)(event, newEvent); } catch (err) { throw new Error(`Local event hash calculation did not match the server's: ${err}`); } return newEvent.id; } async reportEvents(events) { const { endpoint, apiKey, projectId } = this.config; const requestBody = _.map(events, (event) => { return this.mapping(event); }); let newEvents; try { const { data } = await axios_1.default.post(`${endpoint}/publisher/v1/project/${projectId}/event/bulk`, { events: requestBody }, { headers: { Accept: "application/json", Authorization: `token=${apiKey}`, }, }); newEvents = data; } catch (err) { const status = err.response ? err.response.status : 500; const statusText = err.response ? err.response.statusText : "Unknown"; throw new Error(`Unexpected HTTP response: ${status} ${statusText}`); } try { _.forEach(newEvents, (newEvent, index) => { (0, event_1.verifyHash)(events[index], newEvent); }); } catch (err) { throw new Error(`Local event hash calculation did not match the server's: ${err}`); } return newEvents.map((newEvent) => newEvent.id); } async getViewerToken(groupId, actorId, isAdmin) { const { endpoint, apiKey, projectId, viewLogAction } = this.config; const params = { group_id: groupId, actor_id: actorId, is_admin: "" + !!isAdmin, }; if (viewLogAction) { params.view_log_action = viewLogAction; } const q = new URLSearchParams(params); const urlWithQuery = `${endpoint}/publisher/v1/project/${projectId}/viewertoken?${q.toString()}`; let token; try { const { data } = await axios_1.default.get(urlWithQuery, { headers: { Authorization: `Token token=${apiKey}`, }, }); token = data.token; } catch (err) { const status = err.response ? err.response.status : 500; const statusText = err.response ? err.response.statusText : "Unknown"; throw new Error(`Unexpected HTTP response: ${status} ${statusText}`); } return token; } async query(q, mask, pageSize) { const { endpoint, apiKey, projectId } = this.config; const conn = new graphql_1.EventsConnection(`${endpoint}/publisher/v1/project/${projectId}/graphql`, `Token token=${apiKey}`, q, mask, pageSize); await conn.init(); return conn; } } exports.Client = Client;