@whitewall/blip-warehouse 0.0.3 → 0.0.5

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';
@@ -25,6 +25,9 @@ export interface HistoricalIngestRequest {
25
25
  accessKey: string;
26
26
  tenantId: string;
27
27
  }
28
+ export interface HistoricalIngestProgressEvent {
29
+ contactsProcessed: number;
30
+ }
28
31
  export interface HistoricalIngestResponse {
29
32
  success: boolean;
30
33
  }
@@ -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,108 @@
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(request),
55
+ dispatcher
56
+ });
57
+ if (!response.ok) {
58
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
59
+ throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
60
+ }
61
+ const reader = response.body?.getReader();
62
+ if (!reader) throw new Error("Response body is not readable");
63
+ const decoder = new TextDecoder();
64
+ let buffer = "";
65
+ let result = { success: false };
66
+ try {
67
+ while (true) {
68
+ const { done, value } = await reader.read();
69
+ if (done) break;
70
+ buffer += decoder.decode(value, { stream: true });
71
+ const lines = buffer.split("\n");
72
+ buffer = lines.pop() ?? "";
73
+ for (const line of lines) {
74
+ if (line.startsWith("event:")) continue;
75
+ if (line.startsWith("data:")) {
76
+ const data = JSON.parse(line.slice(5).trim());
77
+ if ("contactsProcessed" in data) onProgress?.(data);
78
+ else if ("success" in data) result = data;
79
+ }
80
+ }
81
+ }
82
+ return result;
83
+ } catch (error) {
84
+ throw new Error(`SSE stream failed: ${error instanceof Error ? error.message : "Unknown streaming error"}`);
85
+ } finally {
86
+ reader.releaseLock();
87
+ }
88
+ }
89
+ async request(path, options = {}) {
90
+ const url = `${this.baseUrl}${path}`;
91
+ const response = await fetch(url, {
92
+ ...options,
93
+ headers: {
94
+ "Content-Type": "application/json",
95
+ Authorization: `Bearer ${this.token}`,
96
+ ...options.headers
97
+ }
98
+ });
99
+ if (!response.ok) {
100
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
101
+ throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
102
+ }
103
+ return response.json();
104
+ }
105
+ };
106
+
107
+ //#endregion
108
+ exports.BlipWarehouseClient = BlipWarehouseClient;