UNPKG

@strathberry/klaviyo-client

Version:

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

239 lines 9.25 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.ListsKlaviyoApi = void 0; const __1 = require(".."); const wait_for_retry_1 = require("../utils/wait-for-retry"); class ListsKlaviyoApi { constructor(apiKey, token) { this.apiKey = apiKey; this.token = token; } // See https://www.klaviyo.com/docs/api/v2/lists#get-members-all for details getGroupProfiles(groupId, marker = null) { return __awaiter(this, void 0, void 0, function* () { const profiles = []; let url = `https://a.klaviyo.com/api/v2/group/${groupId}/members/all?api_key=${encodeURI(this.apiKey)}`; if (marker != null) { url = `${url}&marker=${encodeURI('' + marker)}`; } const res = yield fetch(url); if (res.ok) { const data = (yield res.json()); profiles.push(...data.records); if (data.marker != null) { const nextProfiles = yield this.getGroupProfiles(groupId, data.marker); profiles.push(...nextProfiles); } } else if (res.status === 404) { return profiles; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); const nextProfiles = yield this.getGroupProfiles(groupId, marker); profiles.push(...nextProfiles); return profiles; } else { throw new __1.KlaviyoError(res); } return profiles; }); } // See https://www.klaviyo.com/docs/api/v2/lists#post-lists for details createList(name) { return __awaiter(this, void 0, void 0, function* () { const url = 'https://a.klaviyo.com/api/v2/lists'; const res = yield fetch(url, { method: 'post', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ api_key: this.apiKey, list_name: name }), }); if (res.ok) { const data = (yield res.json()); return data; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.createList(name); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/v2/lists#get-lists for details getLists() { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/lists?api_key=${encodeURI(this.apiKey)}`; const res = yield fetch(url); if (res.ok) { const data = (yield res.json()); return data; } else if (res.status === 404) { return []; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.getLists(); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/v2/lists#get-list for details getListDetails(id) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/list/${id}?api_key=${encodeURI(this.apiKey)}`; const res = yield fetch(url); if (res.ok) { const data = (yield res.json()); return data; } else if (res.status === 404) { return null; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.getListDetails(id); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/v2/lists#put-list for details updateList(id, name) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/list/${id}`; const res = yield fetch(url, { method: 'put', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ api_key: this.apiKey, list_name: name }), }); if (res.ok) { return; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.updateList(id, name); } else { throw new __1.KlaviyoError(res); } }); } // See https://www.klaviyo.com/docs/api/v2/lists#delete-list for details deleteList(id) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/list/${id}`; const res = yield fetch(url, { method: 'delete', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ api_key: this.apiKey }), }); if (res.ok) { return; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.deleteList(id); } else { throw new __1.KlaviyoError(res); } }); } // See https://a.klaviyo.com/api/v2/list/{list_id}/members for details addMembersToList(listId, profiles) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/list/${listId}/members`; const res = yield fetch(url, { method: 'post', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ api_key: this.apiKey, profiles }), }); if (res.ok) { return; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.addMembersToList(listId, profiles); } else { throw new __1.KlaviyoError(res); } }); } // See https://a.klaviyo.com/api/v2/list/{list_id}/subscribe for details subscribe(listId, profiles) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/api/v2/list/${listId}/subscribe`; const res = yield fetch(url, { method: 'post', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ api_key: this.apiKey, profiles }), }); if (res.ok) { return; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.subscribe(listId, profiles); } else { throw new __1.KlaviyoError(res); } }); } // See https://developers.klaviyo.com/en/v1-2/docs/how-to-set-up-custom-back-in-stock for details subscribeToBackInStock(variantId, profile) { return __awaiter(this, void 0, void 0, function* () { const url = `https://a.klaviyo.com/onsite/components/back-in-stock/subscribe`; const res = yield fetch(url, { method: 'post', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ email: profile.email, variant: variantId, platform: 'shopify', }), }); if (res.ok) { return; } else if (res.status === 429) { yield wait_for_retry_1.waitForRetry(res); return yield this.subscribeToBackInStock(variantId, profile); } else { throw new __1.KlaviyoError(res); } }); } } exports.ListsKlaviyoApi = ListsKlaviyoApi; //# sourceMappingURL=lists.js.map