braze-api
Version:
Track users, send messages, export data, and more with Braze API.
67 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getListCatalogItems = getListCatalogItems;
exports.getListCatalogItemsIterator = getListCatalogItemsIterator;
const node_fetch_1 = require("node-fetch");
const request_1 = require("../../common/request");
function getNextPageLink(linkHeader) {
const linkMatches = linkHeader?.matchAll(/<([^>]+)>; rel="(prev|next)"/g);
if (linkMatches) {
for (const match of linkMatches) {
if (match[2] === 'next') {
return match[1];
}
}
}
}
class CatalogListItems {
constructor(apiUrl, apiKey, items, nextPageLink) {
this.apiUrl = apiUrl;
this.apiKey = apiKey;
this.items = items;
this.nextPageLink = nextPageLink;
}
hasNextPage() {
return Boolean(this.nextPageLink);
}
next() {
if (!this.nextPageLink) {
throw new request_1.ResponseError('There is no next page', 0);
}
return CatalogListItems.queryItems(this.apiUrl, this.apiKey, this.nextPageLink);
}
static async queryItems(apiUrl, apiKey, url) {
const response = await (0, node_fetch_1.default)(apiUrl + url, (0, request_1.buildOptions)({ apiKey }));
const data = await response.json();
if (!response.ok) {
throw new request_1.ResponseError(data.message, response.status, data.errors);
}
return new CatalogListItems(apiUrl, apiKey, data.items, getNextPageLink(response.headers.get('Link')));
}
}
/**
* Request catalog items.
*
* {@link https://www.braze.com/docs/api/endpoints/catalogs/catalog_items/synchronous/get_catalog_items_details_bulk/}
*/
function getListCatalogItems(apiUrl, apiKey, { catalog_name }) {
return CatalogListItems.queryItems(apiUrl, apiKey, `/catalogs/${catalog_name}/items`);
}
/**
* Request catalog items.
*
* {@link https://www.braze.com/docs/api/endpoints/catalogs/catalog_items/synchronous/get_catalog_items_details_bulk/}
*/
async function* getListCatalogItemsIterator(apiUrl, apiKey, body) {
let result = await getListCatalogItems(apiUrl, apiKey, body);
do {
for (const item of result.items) {
yield item;
}
if (!result.hasNextPage()) {
break;
}
result = await result.next();
} while (true);
}
//# sourceMappingURL=list_items.js.map