UNPKG

@thecodersmx/overseer-client

Version:

Client library to send logs, network events, and GraphQL traces to Overseer backend for debugging React Native apps.

101 lines (100 loc) 4.18 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.reportEvent = exports.initOverseer = void 0; let overseerURL = "http://localhost:8000/api/event"; let axiosInstance = null; const initOverseer = (options) => { if (options === null || options === void 0 ? void 0 : options.url) overseerURL = options.url; if (options === null || options === void 0 ? void 0 : options.axios) axiosInstance = options.axios; interceptFetch(); if (axiosInstance) interceptAxios(axiosInstance); }; exports.initOverseer = initOverseer; const interceptFetch = () => { const originalFetch = globalThis.fetch; globalThis.fetch = (...args) => __awaiter(void 0, void 0, void 0, function* () { const [resource, config] = args; const start = Date.now(); try { const res = yield originalFetch(...args); const end = Date.now(); const cloned = res.clone(); const body = yield cloned.text(); let type = "fetch"; const isGraphQL = typeof (config === null || config === void 0 ? void 0 : config.body) === "string" && (config.body.includes("query") || config.body.includes("mutation")); if (isGraphQL) { type = "graphql"; } send({ method: (config === null || config === void 0 ? void 0 : config.method) || "GET", url: resource, request_body: config === null || config === void 0 ? void 0 : config.body, status: cloned.status, response_body: body, duration_ms: end - start, }, type); return res; } catch (err) { send({ error: String(err), url: resource }, "fetch-error"); throw err; } }); }; const interceptAxios = (axios) => { axios.interceptors.request.use((config) => { config._overseer_start = Date.now(); return config; }); axios.interceptors.response.use((response) => { const duration = Date.now() - (response.config._overseer_start || Date.now()); const type = isGraphQL(response.config) ? "graphql" : "axios"; send({ method: response.config.method, url: response.config.url, request_body: response.config.data, status: response.status, response_body: response.data, duration_ms: duration, }, type); return response; }, (error) => { var _a; send({ error: String(error), url: (_a = error === null || error === void 0 ? void 0 : error.config) === null || _a === void 0 ? void 0 : _a.url, }, "axios-error"); return Promise.reject(error); }); }; const isGraphQL = (config) => { return typeof (config === null || config === void 0 ? void 0 : config.data) === "string" && (config.data.includes("query") || config.data.includes("mutation")); }; const reportEvent = (payload, type = "custom") => { send(payload, type); }; exports.reportEvent = reportEvent; const send = (payload, type) => __awaiter(void 0, void 0, void 0, function* () { try { yield fetch(overseerURL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type, payload }), }); } catch (err) { console.log("[Overseer] Failed to report", err); } });