Comparing version 1.32.0 to 2.0.0
@@ -55,3 +55,3 @@ import * as campaigns from './campaigns'; | ||
list: () => Promise<catalogs.synchronous.CatalogListResponse>; | ||
items: <T extends catalogs.synchronous.CatalogListItem<Record<string, unknown>>>(body: catalogs.synchronous.CatalogListItemsBody) => Promise<catalogs.synchronous.CatalogListItemsResponse<T>>; | ||
items: <T extends catalogs.synchronous.CatalogListItem<Record<string, unknown>>>(body: catalogs.synchronous.CatalogListItemsBody) => AsyncGenerator<T, any, unknown>; | ||
item: <T_1 extends catalogs.synchronous.CatalogListItem<Record<string, unknown>>>(body: catalogs.synchronous.CatalogListItemBody) => Promise<catalogs.synchronous.CatalogListItemResponse<T_1>>; | ||
@@ -58,0 +58,0 @@ }; |
@@ -44,3 +44,3 @@ "use strict"; | ||
list: () => catalogs.synchronous.listCatalogs(this.apiUrl, this.apiKey), | ||
items: (body) => catalogs.synchronous.listCatalogItems(this.apiUrl, this.apiKey, body), | ||
items: (body) => catalogs.synchronous.getListCatalogItemsIterator(this.apiUrl, this.apiKey, body), | ||
item: (body) => catalogs.synchronous.getCatalogItem(this.apiUrl, this.apiKey, body), | ||
@@ -47,0 +47,0 @@ }, |
@@ -1,2 +0,12 @@ | ||
import { CatalogListItem, CatalogListItemsBody, CatalogListItemsResponse } from './types'; | ||
import { CatalogListItem, CatalogListItemsBody } from './types'; | ||
declare class CatalogListItems<T extends CatalogListItem> { | ||
private readonly apiUrl; | ||
private readonly apiKey; | ||
readonly items: T[]; | ||
private readonly nextPageLink?; | ||
constructor(apiUrl: string, apiKey: string, items: T[], nextPageLink?: string | undefined); | ||
hasNextPage(): boolean; | ||
next(): Promise<CatalogListItems<T>>; | ||
static queryItems<T extends CatalogListItem>(apiUrl: string, apiKey: string, url: string): Promise<CatalogListItems<T>>; | ||
} | ||
/** | ||
@@ -7,3 +17,10 @@ * Request catalog items. | ||
*/ | ||
export declare function listCatalogItems<T extends CatalogListItem>(apiUrl: string, apiKey: string, body: CatalogListItemsBody): Promise<CatalogListItemsResponse<T>>; | ||
export declare function getListCatalogItems<T extends CatalogListItem>(apiUrl: string, apiKey: string, { catalog_name }: CatalogListItemsBody): Promise<CatalogListItems<T>>; | ||
/** | ||
* Request catalog items. | ||
* | ||
* {@link https://www.braze.com/docs/api/endpoints/catalogs/catalog_items/synchronous/get_catalog_items_details_bulk/} | ||
*/ | ||
export declare function getListCatalogItemsIterator<T extends CatalogListItem>(apiUrl: string, apiKey: string, body: CatalogListItemsBody): AsyncGenerator<T>; | ||
export {}; | ||
//# sourceMappingURL=list_items.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.listCatalogItems = void 0; | ||
exports.getListCatalogItemsIterator = exports.getListCatalogItems = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
const request_1 = require("../../common/request"); | ||
const params_1 = require("../../common/request/params"); | ||
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 !!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, { | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
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'))); | ||
} | ||
} | ||
/** | ||
@@ -11,13 +51,24 @@ * Request catalog items. | ||
*/ | ||
function listCatalogItems(apiUrl, apiKey, body) { | ||
const options = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}; | ||
const { catalog_name, ...params } = body; | ||
return (0, request_1.get)(`${apiUrl}/catalogs/${catalog_name}/items?${(0, params_1.buildParams)(params)}`, undefined, options); | ||
function getListCatalogItems(apiUrl, apiKey, { catalog_name }) { | ||
return CatalogListItems.queryItems(apiUrl, apiKey, `/catalogs/${catalog_name}/items`); | ||
} | ||
exports.listCatalogItems = listCatalogItems; | ||
exports.getListCatalogItems = getListCatalogItems; | ||
/** | ||
* 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); | ||
} | ||
exports.getListCatalogItemsIterator = getListCatalogItemsIterator; | ||
//# sourceMappingURL=list_items.js.map |
@@ -23,3 +23,3 @@ import { ServerResponse } from '../../common/request'; | ||
catalog_name: string; | ||
cursor?: string; | ||
max_pages?: number; | ||
} | ||
@@ -26,0 +26,0 @@ export type CatalogListItem<T extends Record<string, any> = Record<string, unknown>> = { |
@@ -13,2 +13,7 @@ import type { RequestInit } from 'node-fetch'; | ||
} | ||
export declare class ResponseError extends Error { | ||
status: number; | ||
errors?: ServerResponse['errors']; | ||
constructor(message: string, status: number, errors?: ServerResponse['errors']); | ||
} | ||
/** | ||
@@ -15,0 +20,0 @@ * Makes a request. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.request = exports.RequestMethod = void 0; | ||
exports.request = exports.ResponseError = exports.RequestMethod = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
@@ -17,2 +17,3 @@ var RequestMethod; | ||
} | ||
exports.ResponseError = ResponseError; | ||
/** | ||
@@ -19,0 +20,0 @@ * Makes a request. |
{ | ||
"name": "braze-api", | ||
"version": "1.32.0", | ||
"version": "2.0.0", | ||
"description": "Track users, send messages, export data, and more with Braze API.", | ||
@@ -14,3 +14,3 @@ "author": "Mark <mark@remarkablemark.org>", | ||
"lint:fix": "npm run lint -- --fix", | ||
"lint:tsc": "tsc", | ||
"lint:tsc": "tsc && tsc --project tsconfig.test.json", | ||
"_postinstall": "husky install", | ||
@@ -40,12 +40,13 @@ "postpublish": "pinst --enable", | ||
"devDependencies": { | ||
"@commitlint/cli": "17.5.0", | ||
"@commitlint/cli": "17.5.1", | ||
"@commitlint/config-conventional": "17.4.4", | ||
"@types/jest": "29.5.0", | ||
"@types/node": "18.15.10", | ||
"@typescript-eslint/eslint-plugin": "5.56.0", | ||
"@typescript-eslint/parser": "5.56.0", | ||
"eslint": "8.36.0", | ||
"@types/node": "18.15.11", | ||
"@typescript-eslint/eslint-plugin": "5.57.0", | ||
"@typescript-eslint/parser": "5.57.0", | ||
"eslint": "8.37.0", | ||
"eslint-plugin-prettier": "4.2.1", | ||
"eslint-plugin-simple-import-sort": "10.0.0", | ||
"eslint-plugin-tsdoc": "0.2.17", | ||
"fetch-mock-jest": "1.5.1", | ||
"husky": "8.0.3", | ||
@@ -52,0 +53,0 @@ "jest": "29.5.0", |
@@ -12,2 +12,3 @@ <!-- readme-start --> | ||
[![codecov](https://codecov.io/gh/remarkablemark/braze-api/branch/master/graph/badge.svg?token=QHPI1I0XI3)](https://codecov.io/gh/remarkablemark/braze-api) | ||
[![NPM downloads](https://badgen.net/npm/dm/braze-api)](https://www.npmjs.com/package/braze-api) | ||
@@ -222,5 +223,5 @@ Node.js library for [Braze](https://www.braze.com/) (see [demo](https://replit.com/@remarkablemark/braze-api)). The types are inspired by [Braze's Postman collection](https://documenter.getpostman.com/view/4689407/SVYrsdsG). | ||
## Release | ||
## Contributing | ||
Release is automated with [Release Please](https://github.com/googleapis/release-please). | ||
We encourage contributions! Please check out the [contributing guide](https://github.com/remarkablemark/braze-api/blob/master/.github/CONTRIBUTING.md) on how to proceed. | ||
@@ -227,0 +228,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
218151
3426
235
19