UNPKG

@adizen/events-hub-sdk

Version:

Official client SDK for triggering Events Hub workflows from any app

128 lines (126 loc) 4.02 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { EventsHubClient: () => EventsHubClient }); module.exports = __toCommonJS(index_exports); var import_axios = __toESM(require("axios")); var DEFAULT_TIMEOUT_MS = 1e4; var EventsHubClient = class { constructor(config) { if (!config?.baseUrl) { throw new Error("EventsHubClient requires a baseUrl"); } const baseURL = config.baseUrl.replace(/\/+$/, ""); this.apiKey = config.apiKey; this.apiKeyHeader = config.apiKeyHeader ?? "x-api-key"; this.defaultHeaders = config.defaultHeaders ?? {}; this.http = import_axios.default.create({ baseURL, timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS }); } /** * Trigger a single event immediately (or with optional delay/schedule). */ async trigger(eventName, data, options) { const payload = { name: eventName, data, userId: options?.userId, metadata: options?.metadata, delay: options?.delay, schedule: options?.schedule }; const response = await this.http.post( "/api/events", payload, this.buildRequestConfig() ); return response.data; } /** * Trigger multiple events in a single request. */ async triggerBatch(events) { const response = await this.http.post( "/api/events/batch", events, this.buildRequestConfig() ); return response.data; } /** * Convenience helper to trigger an event after a delay (ms). */ async triggerDelayed(eventName, data, delayMs, options) { return this.trigger(eventName, data, { ...options, delay: delayMs }); } /** * Convenience helper to schedule an event using a cron expression. */ async triggerScheduled(eventName, data, cronExpression, options) { return this.trigger(eventName, data, { ...options, schedule: cronExpression }); } /** * Retrieve handler metadata (useful for schema introspection in apps). */ async getHandler(handlerId) { const response = await this.http.get( `/api/admin/handlers/${handlerId}`, this.buildRequestConfig() ); return response.data; } /** * Private helper to build HTTP headers. */ buildRequestConfig(overrides) { const headers = { ...this.defaultHeaders, ...overrides?.headers }; if (this.apiKey && !headers[this.apiKeyHeader]) { headers[this.apiKeyHeader] = this.apiKey; } return { ...overrides, headers }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EventsHubClient });