@whitewall/blip-warehouse 0.0.4 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { CreateTokenRequest, CreateTokenResponse, GetMessagesOptions, HistoricalIngestRequest, HistoricalIngestResponse, MessagesByIdentityResult, SearchRequest, SearchResult } from './types';
1
+ import type { CreateTokenRequest, CreateTokenResponse, GetMessagesOptions, HistoricalIngestProgressEvent, HistoricalIngestRequest, HistoricalIngestResponse, MessagesByIdentityResult, SearchRequest, SearchResult } from './types';
2
2
  export declare class BlipWarehouseClient {
3
3
  private readonly baseUrl;
4
4
  private readonly token;
@@ -10,6 +10,6 @@ export declare class BlipWarehouseClient {
10
10
  search(request: SearchRequest): Promise<SearchResult>;
11
11
  getMessagesByIdentity(identity: string, options?: GetMessagesOptions): Promise<MessagesByIdentityResult>;
12
12
  createToken(request: CreateTokenRequest): Promise<CreateTokenResponse>;
13
- historicalIngest(request: HistoricalIngestRequest): Promise<HistoricalIngestResponse>;
13
+ historicalIngest(request: HistoricalIngestRequest, onProgress?: (event: HistoricalIngestProgressEvent) => void): Promise<HistoricalIngestResponse>;
14
14
  private request;
15
15
  }
@@ -1,2 +1,2 @@
1
1
  export { BlipWarehouseClient } from './client';
2
- export type { ContactsCountResponse, GetMessagesOptions, IndexedContact, IndexedMessage, MessageRow, MessagesByIdentityResult, PaginationCursor, SearchRequest, SearchResult, } from './types';
2
+ export type { ContactsCountResponse, GetMessagesOptions, HistoricalIngestProgressEvent, HistoricalIngestRequest, HistoricalIngestResponse, IndexedContact, IndexedMessage, MessageRow, MessagesByIdentityResult, PaginationCursor, SearchRequest, SearchResult, } from './types';
@@ -24,6 +24,11 @@ export interface CreateTokenResponse {
24
24
  export interface HistoricalIngestRequest {
25
25
  accessKey: string;
26
26
  tenantId: string;
27
+ startDate: Date | string;
28
+ endDate: Date | string;
29
+ }
30
+ export interface HistoricalIngestProgressEvent {
31
+ contactsProcessed: number;
27
32
  }
28
33
  export interface HistoricalIngestResponse {
29
34
  success: boolean;
@@ -0,0 +1,42 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+ var __toDynamicImportESM = (isNodeMode) => (mod) => __toESM(mod.default, isNodeMode);
28
+
29
+ //#endregion
30
+
31
+ Object.defineProperty(exports, '__commonJSMin', {
32
+ enumerable: true,
33
+ get: function () {
34
+ return __commonJSMin;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, '__toDynamicImportESM', {
38
+ enumerable: true,
39
+ get: function () {
40
+ return __toDynamicImportESM;
41
+ }
42
+ });
@@ -0,0 +1,112 @@
1
+ const require_chunk = require('./chunk-DYk-Zboy.js');
2
+
3
+ //#region src/client.ts
4
+ var BlipWarehouseClient = class {
5
+ baseUrl;
6
+ token;
7
+ constructor(config) {
8
+ this.baseUrl = config.baseUrl?.replace(/\/$/, "") ?? "https://api.warehouse.whitewall.dev";
9
+ this.token = config.token;
10
+ }
11
+ async getContactsCount() {
12
+ return (await this.request("/warehouse/contacts")).count;
13
+ }
14
+ async search(request) {
15
+ return this.request("/warehouse/search", {
16
+ method: "POST",
17
+ body: JSON.stringify({
18
+ query: request.query,
19
+ limit: request.limit ?? 10,
20
+ cursor: request.cursor
21
+ })
22
+ });
23
+ }
24
+ async getMessagesByIdentity(identity, options = {}) {
25
+ const params = new URLSearchParams();
26
+ if (options.limit !== void 0) params.append("limit", options.limit.toString());
27
+ if (options.cursor) params.append("cursor", options.cursor);
28
+ const queryString = params.toString();
29
+ const path = `/warehouse/contacts/${encodeURIComponent(identity)}/messages${queryString ? `?${queryString}` : ""}`;
30
+ return this.request(path);
31
+ }
32
+ async createToken(request) {
33
+ return this.request("/auth/tokens", {
34
+ method: "POST",
35
+ body: JSON.stringify(request)
36
+ });
37
+ }
38
+ async historicalIngest(request, onProgress) {
39
+ const url = `${this.baseUrl}/blip/historical-ingest`;
40
+ let dispatcher;
41
+ if (typeof process !== "undefined" && process.versions && process.versions.node) {
42
+ const { Agent } = await Promise.resolve().then(() => require_chunk.__toDynamicImportESM()(require("./undici-CNAV_IlA.js")));
43
+ dispatcher = new Agent({
44
+ connectTimeout: 6e4 * 30,
45
+ bodyTimeout: 6e4 * 30
46
+ });
47
+ }
48
+ const response = await fetch(url, {
49
+ method: "POST",
50
+ headers: {
51
+ "Content-Type": "application/json",
52
+ Authorization: `Bearer ${this.token}`
53
+ },
54
+ body: JSON.stringify({
55
+ ...request,
56
+ startDate: request.startDate instanceof Date ? request.startDate.toISOString() : request.startDate,
57
+ endDate: request.endDate instanceof Date ? request.endDate.toISOString() : request.endDate
58
+ }),
59
+ dispatcher
60
+ });
61
+ if (!response.ok) {
62
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
63
+ throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
64
+ }
65
+ const reader = response.body?.getReader();
66
+ if (!reader) throw new Error("Response body is not readable");
67
+ const decoder = new TextDecoder();
68
+ let buffer = "";
69
+ let result = { success: false };
70
+ try {
71
+ while (true) {
72
+ const { done, value } = await reader.read();
73
+ if (done) break;
74
+ buffer += decoder.decode(value, { stream: true });
75
+ const lines = buffer.split("\n");
76
+ buffer = lines.pop() ?? "";
77
+ for (const line of lines) {
78
+ if (line.startsWith("event:")) continue;
79
+ if (line.startsWith("data:")) {
80
+ const data = JSON.parse(line.slice(5).trim());
81
+ if ("contactsProcessed" in data) onProgress?.(data);
82
+ else if ("success" in data) result = data;
83
+ }
84
+ }
85
+ }
86
+ return result;
87
+ } catch (error) {
88
+ throw new Error(`SSE stream failed: ${error instanceof Error ? error.message : "Unknown streaming error"}`);
89
+ } finally {
90
+ reader.releaseLock();
91
+ }
92
+ }
93
+ async request(path, options = {}) {
94
+ const url = `${this.baseUrl}${path}`;
95
+ const response = await fetch(url, {
96
+ ...options,
97
+ headers: {
98
+ "Content-Type": "application/json",
99
+ Authorization: `Bearer ${this.token}`,
100
+ ...options.headers
101
+ }
102
+ });
103
+ if (!response.ok) {
104
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
105
+ throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
106
+ }
107
+ return response.json();
108
+ }
109
+ };
110
+
111
+ //#endregion
112
+ exports.BlipWarehouseClient = BlipWarehouseClient;