UNPKG

onetriggr

Version:

Node.js client library for OneTriggr event triggering

45 lines (44 loc) 1.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneTriggr = void 0; const axios_1 = __importDefault(require("axios")); class OneTriggr { constructor(config = {}) { this.apiKey = config.apiKey || process.env.ONETRIGGR_API_KEY || ''; if (!this.apiKey) { throw new Error('API key is required. Provide it in config.apiKey or set ONETRIGGR_API_KEY environment variable.'); } // Use baseUrl from config, then ONETRIGGR_ENDPOINT env var, then default const baseUrl = config.baseUrl || process.env.ONETRIGGR_ENDPOINT || 'https://api.onetriggr.com'; this.httpClient = axios_1.default.create({ baseURL: baseUrl, headers: { 'Content-Type': 'application/json', 'x-api-key': this.apiKey, }, }); } async triggr(eventName, recipient, payload) { try { const request = { eventName, recipient, params: payload }; const response = await this.httpClient.post('/triggr', request); return response.data; } catch (error) { if (error.response?.data) { return error.response.data; } throw new Error(`Failed to trigger event: ${error.message}`); } } } exports.OneTriggr = OneTriggr;