UNPKG

@strathberry/klaviyo-client

Version:

(Fork) Klaviyo API client for Node.js and browser

69 lines 2.85 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.KlaviyoClient = void 0; const _1 = require("."); const js_base64_1 = require("js-base64"); const wait_for_retry_1 = require("./utils/wait-for-retry"); // import fetch from 'cross-fetch'; class KlaviyoClient { constructor(token) { this.token = token; } // See https://www.klaviyo.com/docs/http-api#identify for details identify(profile) { return __awaiter(this, void 0, void 0, function* () { const params = { token: this.token, properties: profile, }; const payload = js_base64_1.encode(JSON.stringify(params)); const url = `https://a.klaviyo.com/api/identify?data=${payload}`; const res = yield fetch(url); if (res.ok) { return (yield res.json()); } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.identify(profile); } else { throw new _1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/http-api#track for details track(eventName, profile, event) { return __awaiter(this, void 0, void 0, function* () { const params = { token: this.token, event: eventName, customer_properties: profile, properties: event, }; const payload = js_base64_1.encode(JSON.stringify(params)); const url = `https://a.klaviyo.com/api/track?data=${payload}`; const res = yield fetch(url); if (res.ok) { return (yield res.json()); } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.track(eventName, profile, event); } else { throw new _1.KlaviyoError(res); } }); } } exports.KlaviyoClient = KlaviyoClient; //# sourceMappingURL=klaviyo.client.js.map