UNPKG

@strathberry/klaviyo-client

Version:

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

146 lines 6.07 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.ProfilesKlaviyoApi = void 0; const __1 = require(".."); const wait_for_retry_1 = require("../utils/wait-for-retry"); class ProfilesKlaviyoApi { constructor(apiKey, token) { this.apiKey = apiKey; this.token = token; } // See https://www.klaviyo.com/docs/api/people#person for details getProfile(id) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v1/person/${id}?api_key=${encodeURI(this.apiKey)}`; const res = yield fetch(url); if (res.ok) { return (yield res.json()); } else if (res.status === 404) { return null; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.getProfile(id); } else { throw new __1.KlaviyoError(res); } }); } // See https://developers.klaviyo.com/en/reference/get-profile-id for details getProfileId(identifier, identifierType) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/people/search?${identifierType}=${identifier}&api_key=${encodeURI(this.apiKey)}`; const res = yield fetch(url); if (res.ok) { return (yield res.json()); } else if (res.status === 404) { return null; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.getProfileId(identifier, identifierType); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/people#person for details updateProfile(id, profile) { return __awaiter(this, void 0, void 0, function* () { let url = `https://a.klaviyo.com/api/v1/person/${id}?api_key=${encodeURI(this.apiKey)}`; for (const key in profile) { url += `&${key}=${profile[key]}`; } const res = yield fetch(url, { method: 'put' }); if (res.ok) { return (yield res.json()); } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.updateProfile(id, profile); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/people#metrics-timeline for details getProfileEvents(id, since = null) { return __awaiter(this, void 0, void 0, function* () { const events = []; let url = `https://a.klaviyo.com/api/v1/person/${id}/metrics/timeline?api_key=${encodeURI(this.apiKey)}`; if (since != null) { url = `${url}&since=${encodeURI(since)}`; } const res = yield fetch(url); if (res.ok) { const data = (yield res.json()); events.push(...data.data); if (data.next != null) { const nextEvents = yield this.getProfileEvents(id, data.next); events.push(...nextEvents); } } else if (res.status === 404) { return events; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); const nextEvents = yield this.getProfileEvents(id, since); events.push(...nextEvents); return events; } else { throw new __1.KlaviyoError(res); } return events; }); } // See https://www.klaviyo.com/docs/api/people#metric-timeline for details getProfileEventsByMetric(id, metricId, since = null) { return __awaiter(this, void 0, void 0, function* () { const events = []; let url = `https://a.klaviyo.com/api/v1/person/${id}/metric/${metricId}/timeline?api_key=${encodeURI(this.apiKey)}`; if (since != null) { url = `${url}&since=${encodeURI(since)}`; } const res = yield fetch(url); if (res.ok) { const data = (yield res.json()); events.push(...data.data); if (data.next != null) { const nextEvents = yield this.getProfileEventsByMetric(id, metricId, data.next); events.push(...nextEvents); } } else if (res.status === 404) { return events; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); const nextEvents = yield this.getProfileEventsByMetric(id, metricId, since); events.push(...nextEvents); return events; } else { throw new __1.KlaviyoError(res); } return events; }); } } exports.ProfilesKlaviyoApi = ProfilesKlaviyoApi; //# sourceMappingURL=profiles.js.map