better-zap 0.1.0 → 0.2.1

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.
@@ -203,8 +203,9 @@ interface WebhookEntry {
203
203
  }
204
204
  interface WebhookChange {
205
205
  value: WebhookValue;
206
- field: "messages";
206
+ field: WhatsAppWebhookField;
207
207
  }
208
+ type WhatsAppWebhookField = "messages" | "message_template_status_update" | "message_template_quality_update" | "phone_number_name_update" | "phone_number_quality_update" | "account_update" | "security" | "history" | "smb_app_state_sync" | "smb_message_echoes" | "account_offboarded" | "account_reconnected" | (string & {});
208
209
  interface WebhookValue {
209
210
  messaging_product: "whatsapp";
210
211
  metadata: {
@@ -226,7 +227,7 @@ interface IncomingMessage {
226
227
  from: string;
227
228
  id: string;
228
229
  timestamp: string;
229
- type: "text" | "image" | "audio" | "video" | "document" | "location" | "contacts" | "interactive" | "button" | "reaction" | "sticker";
230
+ type: "text" | "image" | "audio" | "video" | "document" | "location" | "contacts" | "interactive" | "button" | "reaction" | "sticker" | "unsupported" | (string & {});
230
231
  text?: {
231
232
  body: string;
232
233
  };
@@ -270,6 +271,11 @@ interface IncomingMessage {
270
271
  body: string;
271
272
  ctwa_clid?: string;
272
273
  };
274
+ errors?: MessageError[];
275
+ edited?: boolean;
276
+ revoked?: boolean;
277
+ unsupported?: true;
278
+ [key: string]: unknown;
273
279
  }
274
280
  interface MediaMessage {
275
281
  id: string;
@@ -434,25 +440,29 @@ interface WhatsAppLogRecord {
434
440
  deliveredAt?: string | null;
435
441
  readAt?: string | null;
436
442
  }
443
+ type CreateWhatsAppLogParams = {
444
+ phone: string;
445
+ userId?: string;
446
+ contactName?: string;
447
+ direction: WhatsAppDirection;
448
+ messageType: WhatsAppMessageType;
449
+ content: string;
450
+ templateName?: string;
451
+ waMessageId?: string;
452
+ status: WhatsAppStatus;
453
+ errorMessage?: string;
454
+ metadata?: any;
455
+ sentAt: string;
456
+ };
437
457
  /**
438
458
  * Interface for database persistence of WhatsApp logs.
439
459
  * Decouples Better Zap from any specific application database package.
440
460
  */
441
461
  interface WhatsAppLogStore {
442
- createWhatsAppLog(params: {
443
- phone: string;
444
- userId?: string;
445
- contactName?: string;
446
- direction: WhatsAppDirection;
447
- messageType: WhatsAppMessageType;
448
- content: string;
449
- templateName?: string;
450
- waMessageId?: string;
451
- status: WhatsAppStatus;
452
- errorMessage?: string;
453
- metadata?: any;
454
- sentAt: string;
455
- }): Promise<WhatsAppLogRecord>;
462
+ createWhatsAppLog(params: CreateWhatsAppLogParams): Promise<{
463
+ record: WhatsAppLogRecord;
464
+ created: boolean;
465
+ }>;
456
466
  getMessageByWaId(waMessageId: string): Promise<WhatsAppLogRecord | null>;
457
467
  updateWhatsAppLogByWaMessageId(waMessageId: string, updates: Partial<WhatsAppLogRecord>): Promise<void>;
458
468
  /**
@@ -536,7 +546,22 @@ declare class MessageLoggerService {
536
546
  sentAt: string;
537
547
  senderName?: string;
538
548
  metadata?: Record<string, unknown>;
539
- }): Promise<void>;
549
+ }): Promise<boolean>;
550
+ /**
551
+ * Log an imported WhatsApp message with an explicit direction and timestamp.
552
+ * Used by coexistence history imports and app echo webhooks where the message
553
+ * did not originate from the local send API call.
554
+ */
555
+ logImportedMessage(params: {
556
+ phone: string;
557
+ waMessageId: string;
558
+ direction: WhatsAppDirection;
559
+ content: string;
560
+ sentAt: string;
561
+ senderName?: string;
562
+ messageType?: WhatsAppMessageType;
563
+ metadata?: Record<string, unknown>;
564
+ }): Promise<boolean>;
540
565
  }
541
566
  //#endregion
542
567
  //#region src/services/whatsapp.service.d.ts
@@ -695,12 +720,47 @@ interface ZapClientOptions<TTemplates extends TemplateRegistry = {}> {
695
720
  * Custom fetch implementation. Defaults to the global `fetch`.
696
721
  */
697
722
  fetch?: typeof fetch;
723
+ /**
724
+ * How requests authenticate to the Better Zap HTTP routes. Defaults to
725
+ * sending requests as-is (ambient same-origin cookies). See {@link ZapTransport}
726
+ * and the {@link sessionTransport} / {@link apiKeyTransport} built-ins.
727
+ */
728
+ transport?: ZapTransport;
698
729
  /**
699
730
  * Optional template registry used only for type inference.
700
731
  * The HTTP server still owns runtime serialization and validation.
701
732
  */
702
733
  templates?: TTemplates;
703
734
  }
735
+ /**
736
+ * Controls how the client authenticates its requests to the Better Zap HTTP
737
+ * routes. This is the seam that lets a browser client talk to the API without
738
+ * embedding an API key: the app proxies Better Zap behind its own
739
+ * session-authenticated routes and the client just forwards the session.
740
+ *
741
+ * Two built-ins cover the common cases:
742
+ * - {@link sessionTransport} — session/proxy auth: sends same-origin
743
+ * credentials (cookies) and no API key. Use when a trusted server owns the
744
+ * session and proxies Better Zap (e.g. a dashboard's own authed routes).
745
+ * - {@link apiKeyTransport} — sends `Authorization: Bearer <key>`. Use for
746
+ * server-to-server calls in a trusted environment.
747
+ *
748
+ * Implement the interface directly for anything else (e.g. a rotating token).
749
+ */
750
+ interface ZapTransport {
751
+ /** Extra headers merged onto every request. Called per request, may be async. */
752
+ headers?(): Record<string, string> | Promise<Record<string, string>>;
753
+ /** Fetch credentials mode; `sessionTransport` sets `"include"`. */
754
+ credentials?: RequestCredentials;
755
+ }
756
+ /**
757
+ * Session/proxy transport: send same-origin credentials (cookies), no API key.
758
+ * The recommended browser default when the app proxies Better Zap behind its
759
+ * own authenticated routes.
760
+ */
761
+ declare function sessionTransport(): ZapTransport;
762
+ /** API-key transport: send `Authorization: Bearer <apiKey>` on every request. */
763
+ declare function apiKeyTransport(apiKey: string): ZapTransport;
704
764
  declare class BetterZapClientError extends Error {
705
765
  status: number;
706
766
  code?: string;
@@ -787,4 +847,4 @@ interface ZapClient<TTemplates extends TemplateRegistry = {}> {
787
847
  }
788
848
  declare function createZapClient<TTemplates extends TemplateRegistry = {}>(options?: ZapClientOptions<TTemplates>): ZapClient<TTemplates>;
789
849
  //#endregion
790
- export { UIMessage as $, createLogger as A, ConversationRecord as B, WhatsAppLogRecord as C, LogLevel as D, WhatsAppStatus as E, NewMessageEvent as F, MessageError as G, IncomingMessage as H, StatusUpdateEvent as I, SendMessageError as J, MessageStatus as K, SyncEvent as L, serializeError as M, ConversationSummary as N, Logger as O, ConversationUpdateEvent as P, TemplateParameter as Q, WhatsAppConfig as R, WhatsAppDirection as S, WhatsAppMessageType as T, InteractiveMediaCarouselCardInput as U, FreeformMessageWindow as V, MediaMessage as W, SendResult as X, SendMessageResponse as Y, TemplateComponent as Z, OutgoingLoggingMetadata as _, SupportedTemplateParameterType as a, WebhookPayload as at, MessageLoggerService as b, TemplateName as c, WhatsAppInteractiveButtonsMessage as ct, TemplateParams as d, WhatsAppLocationMessage as dt, UIMessageStatus as et, TemplateRegistry as f, WhatsAppTemplateMessage as ft, serializeTemplateFromRegistry as g, hasConfiguredTemplates as h, EMPTY_TEMPLATE_REGISTRY as i, WebhookError as it, noopLogger as j, LoggerConfig as k, TemplateParameterDefinition as l, WhatsAppInteractiveListMessage as lt, getTemplateNames as m, ZapClient as n, WebhookContact as nt, TemplateComponentDefinition as o, WebhookValue as ot, defineTemplates as p, WhatsAppTextMessage as pt, SendInteractiveMediaCarouselData as q, createZapClient as r, WebhookEntry as rt, TemplateDefinition as s, WhatsAppCarouselCard as st, BetterZapClientError as t, WebhookChange as tt, TemplateParameterInputMap as u, WhatsAppInteractiveMediaCarouselMessage as ut, WhatsAppService as v, WhatsAppLogStore as w, WHATSAPP_MESSAGE_TYPES as x, MessageLoggerNotifier as y, Conversation as z };
850
+ export { SendResult as $, LogLevel as A, SyncEvent as B, MessageLoggerService as C, WhatsAppLogStore as D, WhatsAppLogRecord as E, serializeError as F, IncomingMessage as G, Conversation as H, ConversationSummary as I, MessageError as J, InteractiveMediaCarouselCardInput as K, ConversationUpdateEvent as L, LoggerConfig as M, createLogger as N, WhatsAppMessageType as O, noopLogger as P, SendMessageResponse as Q, NewMessageEvent as R, MessageLoggerNotifier as S, WhatsAppDirection as T, ConversationRecord as U, WhatsAppConfig as V, FreeformMessageWindow as W, SendInteractiveMediaCarouselData as X, MessageStatus as Y, SendMessageError as Z, getTemplateNames as _, WhatsAppWebhookField as _t, createZapClient as a, WebhookContact as at, OutgoingLoggingMetadata as b, SupportedTemplateParameterType as c, WebhookPayload as ct, TemplateName as d, WhatsAppInteractiveButtonsMessage as dt, TemplateComponent as et, TemplateParameterDefinition as f, WhatsAppInteractiveListMessage as ft, defineTemplates as g, WhatsAppTextMessage as gt, TemplateRegistry as h, WhatsAppTemplateMessage as ht, apiKeyTransport as i, WebhookChange as it, Logger as j, WhatsAppStatus as k, TemplateComponentDefinition as l, WebhookValue as lt, TemplateParams as m, WhatsAppLocationMessage as mt, ZapClient as n, UIMessage as nt, sessionTransport as o, WebhookEntry as ot, TemplateParameterInputMap as p, WhatsAppInteractiveMediaCarouselMessage as pt, MediaMessage as q, ZapTransport as r, UIMessageStatus as rt, EMPTY_TEMPLATE_REGISTRY as s, WebhookError as st, BetterZapClientError as t, TemplateParameter as tt, TemplateDefinition as u, WhatsAppCarouselCard as ut, hasConfiguredTemplates as v, WHATSAPP_MESSAGE_TYPES as w, WhatsAppService as x, serializeTemplateFromRegistry as y, StatusUpdateEvent as z };
@@ -203,8 +203,9 @@ interface WebhookEntry {
203
203
  }
204
204
  interface WebhookChange {
205
205
  value: WebhookValue;
206
- field: "messages";
206
+ field: WhatsAppWebhookField;
207
207
  }
208
+ type WhatsAppWebhookField = "messages" | "message_template_status_update" | "message_template_quality_update" | "phone_number_name_update" | "phone_number_quality_update" | "account_update" | "security" | "history" | "smb_app_state_sync" | "smb_message_echoes" | "account_offboarded" | "account_reconnected" | (string & {});
208
209
  interface WebhookValue {
209
210
  messaging_product: "whatsapp";
210
211
  metadata: {
@@ -226,7 +227,7 @@ interface IncomingMessage {
226
227
  from: string;
227
228
  id: string;
228
229
  timestamp: string;
229
- type: "text" | "image" | "audio" | "video" | "document" | "location" | "contacts" | "interactive" | "button" | "reaction" | "sticker";
230
+ type: "text" | "image" | "audio" | "video" | "document" | "location" | "contacts" | "interactive" | "button" | "reaction" | "sticker" | "unsupported" | (string & {});
230
231
  text?: {
231
232
  body: string;
232
233
  };
@@ -270,6 +271,11 @@ interface IncomingMessage {
270
271
  body: string;
271
272
  ctwa_clid?: string;
272
273
  };
274
+ errors?: MessageError[];
275
+ edited?: boolean;
276
+ revoked?: boolean;
277
+ unsupported?: true;
278
+ [key: string]: unknown;
273
279
  }
274
280
  interface MediaMessage {
275
281
  id: string;
@@ -434,25 +440,29 @@ interface WhatsAppLogRecord {
434
440
  deliveredAt?: string | null;
435
441
  readAt?: string | null;
436
442
  }
443
+ type CreateWhatsAppLogParams = {
444
+ phone: string;
445
+ userId?: string;
446
+ contactName?: string;
447
+ direction: WhatsAppDirection;
448
+ messageType: WhatsAppMessageType;
449
+ content: string;
450
+ templateName?: string;
451
+ waMessageId?: string;
452
+ status: WhatsAppStatus;
453
+ errorMessage?: string;
454
+ metadata?: any;
455
+ sentAt: string;
456
+ };
437
457
  /**
438
458
  * Interface for database persistence of WhatsApp logs.
439
459
  * Decouples Better Zap from any specific application database package.
440
460
  */
441
461
  interface WhatsAppLogStore {
442
- createWhatsAppLog(params: {
443
- phone: string;
444
- userId?: string;
445
- contactName?: string;
446
- direction: WhatsAppDirection;
447
- messageType: WhatsAppMessageType;
448
- content: string;
449
- templateName?: string;
450
- waMessageId?: string;
451
- status: WhatsAppStatus;
452
- errorMessage?: string;
453
- metadata?: any;
454
- sentAt: string;
455
- }): Promise<WhatsAppLogRecord>;
462
+ createWhatsAppLog(params: CreateWhatsAppLogParams): Promise<{
463
+ record: WhatsAppLogRecord;
464
+ created: boolean;
465
+ }>;
456
466
  getMessageByWaId(waMessageId: string): Promise<WhatsAppLogRecord | null>;
457
467
  updateWhatsAppLogByWaMessageId(waMessageId: string, updates: Partial<WhatsAppLogRecord>): Promise<void>;
458
468
  /**
@@ -536,7 +546,22 @@ declare class MessageLoggerService {
536
546
  sentAt: string;
537
547
  senderName?: string;
538
548
  metadata?: Record<string, unknown>;
539
- }): Promise<void>;
549
+ }): Promise<boolean>;
550
+ /**
551
+ * Log an imported WhatsApp message with an explicit direction and timestamp.
552
+ * Used by coexistence history imports and app echo webhooks where the message
553
+ * did not originate from the local send API call.
554
+ */
555
+ logImportedMessage(params: {
556
+ phone: string;
557
+ waMessageId: string;
558
+ direction: WhatsAppDirection;
559
+ content: string;
560
+ sentAt: string;
561
+ senderName?: string;
562
+ messageType?: WhatsAppMessageType;
563
+ metadata?: Record<string, unknown>;
564
+ }): Promise<boolean>;
540
565
  }
541
566
  //#endregion
542
567
  //#region src/services/whatsapp.service.d.ts
@@ -695,12 +720,47 @@ interface ZapClientOptions<TTemplates extends TemplateRegistry = {}> {
695
720
  * Custom fetch implementation. Defaults to the global `fetch`.
696
721
  */
697
722
  fetch?: typeof fetch;
723
+ /**
724
+ * How requests authenticate to the Better Zap HTTP routes. Defaults to
725
+ * sending requests as-is (ambient same-origin cookies). See {@link ZapTransport}
726
+ * and the {@link sessionTransport} / {@link apiKeyTransport} built-ins.
727
+ */
728
+ transport?: ZapTransport;
698
729
  /**
699
730
  * Optional template registry used only for type inference.
700
731
  * The HTTP server still owns runtime serialization and validation.
701
732
  */
702
733
  templates?: TTemplates;
703
734
  }
735
+ /**
736
+ * Controls how the client authenticates its requests to the Better Zap HTTP
737
+ * routes. This is the seam that lets a browser client talk to the API without
738
+ * embedding an API key: the app proxies Better Zap behind its own
739
+ * session-authenticated routes and the client just forwards the session.
740
+ *
741
+ * Two built-ins cover the common cases:
742
+ * - {@link sessionTransport} — session/proxy auth: sends same-origin
743
+ * credentials (cookies) and no API key. Use when a trusted server owns the
744
+ * session and proxies Better Zap (e.g. a dashboard's own authed routes).
745
+ * - {@link apiKeyTransport} — sends `Authorization: Bearer <key>`. Use for
746
+ * server-to-server calls in a trusted environment.
747
+ *
748
+ * Implement the interface directly for anything else (e.g. a rotating token).
749
+ */
750
+ interface ZapTransport {
751
+ /** Extra headers merged onto every request. Called per request, may be async. */
752
+ headers?(): Record<string, string> | Promise<Record<string, string>>;
753
+ /** Fetch credentials mode; `sessionTransport` sets `"include"`. */
754
+ credentials?: RequestCredentials;
755
+ }
756
+ /**
757
+ * Session/proxy transport: send same-origin credentials (cookies), no API key.
758
+ * The recommended browser default when the app proxies Better Zap behind its
759
+ * own authenticated routes.
760
+ */
761
+ declare function sessionTransport(): ZapTransport;
762
+ /** API-key transport: send `Authorization: Bearer <apiKey>` on every request. */
763
+ declare function apiKeyTransport(apiKey: string): ZapTransport;
704
764
  declare class BetterZapClientError extends Error {
705
765
  status: number;
706
766
  code?: string;
@@ -787,4 +847,4 @@ interface ZapClient<TTemplates extends TemplateRegistry = {}> {
787
847
  }
788
848
  declare function createZapClient<TTemplates extends TemplateRegistry = {}>(options?: ZapClientOptions<TTemplates>): ZapClient<TTemplates>;
789
849
  //#endregion
790
- export { UIMessage as $, createLogger as A, ConversationRecord as B, WhatsAppLogRecord as C, LogLevel as D, WhatsAppStatus as E, NewMessageEvent as F, MessageError as G, IncomingMessage as H, StatusUpdateEvent as I, SendMessageError as J, MessageStatus as K, SyncEvent as L, serializeError as M, ConversationSummary as N, Logger as O, ConversationUpdateEvent as P, TemplateParameter as Q, WhatsAppConfig as R, WhatsAppDirection as S, WhatsAppMessageType as T, InteractiveMediaCarouselCardInput as U, FreeformMessageWindow as V, MediaMessage as W, SendResult as X, SendMessageResponse as Y, TemplateComponent as Z, OutgoingLoggingMetadata as _, SupportedTemplateParameterType as a, WebhookPayload as at, MessageLoggerService as b, TemplateName as c, WhatsAppInteractiveButtonsMessage as ct, TemplateParams as d, WhatsAppLocationMessage as dt, UIMessageStatus as et, TemplateRegistry as f, WhatsAppTemplateMessage as ft, serializeTemplateFromRegistry as g, hasConfiguredTemplates as h, EMPTY_TEMPLATE_REGISTRY as i, WebhookError as it, noopLogger as j, LoggerConfig as k, TemplateParameterDefinition as l, WhatsAppInteractiveListMessage as lt, getTemplateNames as m, ZapClient as n, WebhookContact as nt, TemplateComponentDefinition as o, WebhookValue as ot, defineTemplates as p, WhatsAppTextMessage as pt, SendInteractiveMediaCarouselData as q, createZapClient as r, WebhookEntry as rt, TemplateDefinition as s, WhatsAppCarouselCard as st, BetterZapClientError as t, WebhookChange as tt, TemplateParameterInputMap as u, WhatsAppInteractiveMediaCarouselMessage as ut, WhatsAppService as v, WhatsAppLogStore as w, WHATSAPP_MESSAGE_TYPES as x, MessageLoggerNotifier as y, Conversation as z };
850
+ export { SendResult as $, LogLevel as A, SyncEvent as B, MessageLoggerService as C, WhatsAppLogStore as D, WhatsAppLogRecord as E, serializeError as F, IncomingMessage as G, Conversation as H, ConversationSummary as I, MessageError as J, InteractiveMediaCarouselCardInput as K, ConversationUpdateEvent as L, LoggerConfig as M, createLogger as N, WhatsAppMessageType as O, noopLogger as P, SendMessageResponse as Q, NewMessageEvent as R, MessageLoggerNotifier as S, WhatsAppDirection as T, ConversationRecord as U, WhatsAppConfig as V, FreeformMessageWindow as W, SendInteractiveMediaCarouselData as X, MessageStatus as Y, SendMessageError as Z, getTemplateNames as _, WhatsAppWebhookField as _t, createZapClient as a, WebhookContact as at, OutgoingLoggingMetadata as b, SupportedTemplateParameterType as c, WebhookPayload as ct, TemplateName as d, WhatsAppInteractiveButtonsMessage as dt, TemplateComponent as et, TemplateParameterDefinition as f, WhatsAppInteractiveListMessage as ft, defineTemplates as g, WhatsAppTextMessage as gt, TemplateRegistry as h, WhatsAppTemplateMessage as ht, apiKeyTransport as i, WebhookChange as it, Logger as j, WhatsAppStatus as k, TemplateComponentDefinition as l, WebhookValue as lt, TemplateParams as m, WhatsAppLocationMessage as mt, ZapClient as n, UIMessage as nt, sessionTransport as o, WebhookEntry as ot, TemplateParameterInputMap as p, WhatsAppInteractiveMediaCarouselMessage as pt, MediaMessage as q, ZapTransport as r, UIMessageStatus as rt, EMPTY_TEMPLATE_REGISTRY as s, WebhookError as st, BetterZapClientError as t, TemplateParameter as tt, TemplateDefinition as u, WhatsAppCarouselCard as ut, hasConfiguredTemplates as v, WHATSAPP_MESSAGE_TYPES as w, WhatsAppService as x, serializeTemplateFromRegistry as y, StatusUpdateEvent as z };
package/dist/client.cjs CHANGED
@@ -1,5 +1,17 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#region src/client.ts
3
+ /**
4
+ * Session/proxy transport: send same-origin credentials (cookies), no API key.
5
+ * The recommended browser default when the app proxies Better Zap behind its
6
+ * own authenticated routes.
7
+ */
8
+ function sessionTransport() {
9
+ return { credentials: "include" };
10
+ }
11
+ /** API-key transport: send `Authorization: Bearer <apiKey>` on every request. */
12
+ function apiKeyTransport(apiKey) {
13
+ return { headers: () => ({ Authorization: `Bearer ${apiKey}` }) };
14
+ }
3
15
  var BetterZapClientError = class extends Error {
4
16
  status;
5
17
  code;
@@ -18,8 +30,18 @@ function createZapClient(options) {
18
30
  const baseURL = options?.baseURL ?? (typeof window !== "undefined" ? window.location.origin : "");
19
31
  const basePath = options?.basePath ?? "/api/whatsapp";
20
32
  const fetchFn = options?.fetch ?? fetch;
33
+ const transport = options?.transport;
21
34
  async function request(path, init) {
22
- const response = await fetchFn(`${baseURL}${basePath}${path}`, init);
35
+ const url = `${baseURL}${basePath}${path}`;
36
+ const transportHeaders = transport?.headers ? await transport.headers() : void 0;
37
+ const response = await fetchFn(url, transportHeaders || transport?.credentials ? {
38
+ ...init,
39
+ ...transport?.credentials ? { credentials: transport.credentials } : {},
40
+ headers: {
41
+ ...init?.headers,
42
+ ...transportHeaders
43
+ }
44
+ } : init ?? {});
23
45
  const payload = await response.json().catch(() => null);
24
46
  if (!response.ok) {
25
47
  const error = payload;
@@ -70,4 +92,6 @@ function createZapClient(options) {
70
92
  }
71
93
  //#endregion
72
94
  exports.BetterZapClientError = BetterZapClientError;
95
+ exports.apiKeyTransport = apiKeyTransport;
73
96
  exports.createZapClient = createZapClient;
97
+ exports.sessionTransport = sessionTransport;
package/dist/client.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { n as ZapClient, r as createZapClient, t as BetterZapClientError } from "./client-CRwYOHIG.cjs";
2
- export { BetterZapClientError, ZapClient, createZapClient };
1
+ import { a as createZapClient, i as apiKeyTransport, n as ZapClient, o as sessionTransport, r as ZapTransport, t as BetterZapClientError } from "./client-DNlBPLBd.cjs";
2
+ export { BetterZapClientError, ZapClient, ZapTransport, apiKeyTransport, createZapClient, sessionTransport };
package/dist/client.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { n as ZapClient, r as createZapClient, t as BetterZapClientError } from "./client-s6x3lYea.mjs";
2
- export { BetterZapClientError, ZapClient, createZapClient };
1
+ import { a as createZapClient, i as apiKeyTransport, n as ZapClient, o as sessionTransport, r as ZapTransport, t as BetterZapClientError } from "./client-DiHHMG8t.mjs";
2
+ export { BetterZapClientError, ZapClient, ZapTransport, apiKeyTransport, createZapClient, sessionTransport };
package/dist/client.mjs CHANGED
@@ -1,4 +1,16 @@
1
1
  //#region src/client.ts
2
+ /**
3
+ * Session/proxy transport: send same-origin credentials (cookies), no API key.
4
+ * The recommended browser default when the app proxies Better Zap behind its
5
+ * own authenticated routes.
6
+ */
7
+ function sessionTransport() {
8
+ return { credentials: "include" };
9
+ }
10
+ /** API-key transport: send `Authorization: Bearer <apiKey>` on every request. */
11
+ function apiKeyTransport(apiKey) {
12
+ return { headers: () => ({ Authorization: `Bearer ${apiKey}` }) };
13
+ }
2
14
  var BetterZapClientError = class extends Error {
3
15
  status;
4
16
  code;
@@ -17,8 +29,18 @@ function createZapClient(options) {
17
29
  const baseURL = options?.baseURL ?? (typeof window !== "undefined" ? window.location.origin : "");
18
30
  const basePath = options?.basePath ?? "/api/whatsapp";
19
31
  const fetchFn = options?.fetch ?? fetch;
32
+ const transport = options?.transport;
20
33
  async function request(path, init) {
21
- const response = await fetchFn(`${baseURL}${basePath}${path}`, init);
34
+ const url = `${baseURL}${basePath}${path}`;
35
+ const transportHeaders = transport?.headers ? await transport.headers() : void 0;
36
+ const response = await fetchFn(url, transportHeaders || transport?.credentials ? {
37
+ ...init,
38
+ ...transport?.credentials ? { credentials: transport.credentials } : {},
39
+ headers: {
40
+ ...init?.headers,
41
+ ...transportHeaders
42
+ }
43
+ } : init ?? {});
22
44
  const payload = await response.json().catch(() => null);
23
45
  if (!response.ok) {
24
46
  const error = payload;
@@ -68,4 +90,4 @@ function createZapClient(options) {
68
90
  };
69
91
  }
70
92
  //#endregion
71
- export { BetterZapClientError, createZapClient };
93
+ export { BetterZapClientError, apiKeyTransport, createZapClient, sessionTransport };