@zindua/sdk 1.2.8 → 1.2.9

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.
package/README.md CHANGED
@@ -68,20 +68,22 @@ await zindua.send({
68
68
 
69
69
  ### WhatsApp anti-ban robot (required)
70
70
 
71
- Zindua runs an **anti-ban robot** on WhatsApp sends. Burst / simultaneous OTP traffic looks like spam and can **ban the number you linked** in the dashboard.
71
+ Zindua runs an **anti-ban Guardian** on WhatsApp sends. Burst / simultaneous OTP traffic looks like spam and can **ban the number you linked** in the dashboard.
72
72
 
73
- - **Server:** after a WhatsApp send, wait **≥ 3 seconds** before the next one (same project), or you get `RATE_LIMIT_EXCEEDED` / `Wait Xs before sending another WhatsApp OTP`.
74
- - **SDK (default):** `autoPaceWhatsapp: true` waits on this client and prints a warning so you learn to space sends in your app (queue, debounce, resend countdown).
75
- - **Your app:** still use a queue or ≥3s gap for production UX the SDK cannot pace across multiple servers or cold starts.
73
+ - **Server:** unsafe WhatsApp traffic is paced or refused so your linked line stays protected.
74
+ - **SDK:** official clients cooperate with the Guardian automatically on WhatsApp `send()`.
75
+ - **Your app:** use a queue or resend cooldown in product UX; never fire parallel WhatsApp bursts.
76
76
 
77
- Guide with Node / PHP / Python / C#: [WhatsApp OTP anti-ban pacing](https://zindua.run/blog/whatsapp-otp-anti-ban-pacing)
77
+ Guide: [WhatsApp anti-ban Guardian](https://zindua.run/whatsapp/anti-ban)
78
+
79
+ ### Upgrade (already installed?)
80
+
81
+ ```bash
82
+ npm install @zindua/sdk@1.2.8
83
+ ```
78
84
 
79
85
  ```typescript
80
- // Default: SDK waits ≥3s between WhatsApp sends on this client
81
86
  const zindua = new Zindua({ apiKey: process.env.ZINDUA_API_KEY! });
82
-
83
- // Only if you already queue WhatsApp OTP yourself:
84
- new Zindua({ apiKey: process.env.ZINDUA_API_KEY!, autoPaceWhatsapp: false });
85
87
  ```
86
88
 
87
89
  ### Channel and `to` must match
package/dist/client.d.ts CHANGED
@@ -112,6 +112,152 @@ export declare class Zindua {
112
112
  private lastWhatsappSendAt;
113
113
  private whatsappPaceChain;
114
114
  constructor(options: ZinduaClientOptions);
115
+ /** PushMirror — 1-tap push challenges (emoji / approve / digit) */
116
+ readonly pushMirror: {
117
+ create: (options: {
118
+ /** Phone (E.164), email, or your user id — used to resolve devices / WhatsApp */
119
+ to?: string;
120
+ /** Alias of `to` (docs / older snippets) */
121
+ userId?: string;
122
+ userExternalId?: string;
123
+ type?: "emoji" | "approve" | "digit";
124
+ purpose?: "login" | "sensitive_action";
125
+ context?: Record<string, unknown>;
126
+ /** Delivery: auto | whatsapp | webpush | fcm | byo */
127
+ channel?: "auto" | "whatsapp" | "webpush" | "fcm" | "byo";
128
+ fallbackChannel?: "whatsapp" | "email" | "none";
129
+ /** Alias: `{ channel, to }` → fallbackChannel + recipient */
130
+ fallback?: {
131
+ channel?: "whatsapp" | "email" | "none";
132
+ to?: string;
133
+ };
134
+ ttlSeconds?: number;
135
+ }) => Promise<{
136
+ success: boolean;
137
+ challengeId: string;
138
+ id: string;
139
+ display: string;
140
+ choices: string[];
141
+ type: string;
142
+ status: string;
143
+ channel?: string;
144
+ expiresAt: string;
145
+ }>;
146
+ getStatus: (challengeId: string) => Promise<{
147
+ ok: boolean;
148
+ challenge: {
149
+ id: string;
150
+ status: string;
151
+ type: string;
152
+ choices: string[];
153
+ targetValue: string;
154
+ display?: string;
155
+ expiresAt: string;
156
+ };
157
+ }>;
158
+ respond: (challengeId: string, value: string) => Promise<{
159
+ ok: boolean;
160
+ status: string;
161
+ error?: string;
162
+ }>;
163
+ /**
164
+ * SSE listener for challenge status (requires API key — server-side or trusted runtime).
165
+ * Resolves when status is approved | denied | expired | failed.
166
+ */
167
+ listen: (challengeId: string, handlers?: {
168
+ onApproved?: () => void;
169
+ onDenied?: () => void;
170
+ onExpired?: () => void;
171
+ onEvent?: (event: {
172
+ challengeId: string;
173
+ status: string;
174
+ }) => void;
175
+ signal?: AbortSignal;
176
+ }) => Promise<{
177
+ challengeId: string;
178
+ status: string;
179
+ }>;
180
+ };
181
+ /** @deprecated Prefer `pushMirror` — same API */
182
+ readonly confirm: {
183
+ create: (options: {
184
+ /** Phone (E.164), email, or your user id — used to resolve devices / WhatsApp */
185
+ to?: string;
186
+ /** Alias of `to` (docs / older snippets) */
187
+ userId?: string;
188
+ userExternalId?: string;
189
+ type?: "emoji" | "approve" | "digit";
190
+ purpose?: "login" | "sensitive_action";
191
+ context?: Record<string, unknown>;
192
+ /** Delivery: auto | whatsapp | webpush | fcm | byo */
193
+ channel?: "auto" | "whatsapp" | "webpush" | "fcm" | "byo";
194
+ fallbackChannel?: "whatsapp" | "email" | "none";
195
+ /** Alias: `{ channel, to }` → fallbackChannel + recipient */
196
+ fallback?: {
197
+ channel?: "whatsapp" | "email" | "none";
198
+ to?: string;
199
+ };
200
+ ttlSeconds?: number;
201
+ }) => Promise<{
202
+ success: boolean;
203
+ challengeId: string;
204
+ id: string;
205
+ display: string;
206
+ choices: string[];
207
+ type: string;
208
+ status: string;
209
+ channel?: string;
210
+ expiresAt: string;
211
+ }>;
212
+ getStatus: (challengeId: string) => Promise<{
213
+ ok: boolean;
214
+ challenge: {
215
+ id: string;
216
+ status: string;
217
+ type: string;
218
+ choices: string[];
219
+ targetValue: string;
220
+ display?: string;
221
+ expiresAt: string;
222
+ };
223
+ }>;
224
+ respond: (challengeId: string, value: string) => Promise<{
225
+ ok: boolean;
226
+ status: string;
227
+ error?: string;
228
+ }>;
229
+ /**
230
+ * SSE listener for challenge status (requires API key — server-side or trusted runtime).
231
+ * Resolves when status is approved | denied | expired | failed.
232
+ */
233
+ listen: (challengeId: string, handlers?: {
234
+ onApproved?: () => void;
235
+ onDenied?: () => void;
236
+ onExpired?: () => void;
237
+ onEvent?: (event: {
238
+ challengeId: string;
239
+ status: string;
240
+ }) => void;
241
+ signal?: AbortSignal;
242
+ }) => Promise<{
243
+ challengeId: string;
244
+ status: string;
245
+ }>;
246
+ };
247
+ /** Zindua Devices (FCM / APNs / WebPush Token Registration) API */
248
+ readonly devices: {
249
+ register: (options: {
250
+ userExternalId: string;
251
+ platform?: "web" | "android" | "ios";
252
+ pushToken?: string;
253
+ keys?: Record<string, string>;
254
+ }) => Promise<{
255
+ success: boolean;
256
+ deviceId: string;
257
+ platform: string;
258
+ userExternalId: string;
259
+ }>;
260
+ };
115
261
  /** Returns true when using a znd_test_ key (sandbox / no real delivery). */
116
262
  isTestMode(): boolean;
117
263
  /**
package/dist/client.js CHANGED
@@ -4,7 +4,7 @@ exports.Zindua = void 0;
4
4
  const errors_1 = require("./errors");
5
5
  const validate_1 = require("./validate");
6
6
  const whatsapp_anti_ban_1 = require("./whatsapp-anti-ban");
7
- const SDK_VERSION = "1.2.8";
7
+ const SDK_VERSION = "1.2.9";
8
8
  const USER_AGENT = `Zindua-JS/${SDK_VERSION}`;
9
9
  function buildPayload(options, channel) {
10
10
  const to = (0, validate_1.validateRecipient)(options.to, channel);
@@ -98,6 +98,119 @@ class Zindua {
98
98
  : USER_AGENT;
99
99
  this.autoPaceWhatsapp = options.autoPaceWhatsapp !== false;
100
100
  }
101
+ /** PushMirror — 1-tap push challenges (emoji / approve / digit) */
102
+ pushMirror = {
103
+ create: async (options) => {
104
+ const payload = {
105
+ to: options.to,
106
+ userId: options.userId,
107
+ userExternalId: options.userExternalId,
108
+ type: options.type,
109
+ purpose: options.purpose,
110
+ context: options.context,
111
+ channel: options.channel,
112
+ fallbackChannel: options.fallbackChannel ?? options.fallback?.channel,
113
+ fallback: options.fallback,
114
+ ttlSeconds: options.ttlSeconds,
115
+ };
116
+ return this.request("POST", "challenges", payload);
117
+ },
118
+ getStatus: async (challengeId) => {
119
+ return this.request("GET", `challenges/${encodeURIComponent(challengeId)}`);
120
+ },
121
+ respond: async (challengeId, value) => {
122
+ return this.request("POST", `challenges/${encodeURIComponent(challengeId)}/respond`, { value });
123
+ },
124
+ /**
125
+ * SSE listener for challenge status (requires API key — server-side or trusted runtime).
126
+ * Resolves when status is approved | denied | expired | failed.
127
+ */
128
+ listen: async (challengeId, handlers) => {
129
+ const id = challengeId.trim();
130
+ if (!id) {
131
+ throw new errors_1.ZinduaError("challengeId is required.", { status: 0, code: "MISSING_FIELDS" });
132
+ }
133
+ const url = `${this.apiBase}/challenges/${encodeURIComponent(id)}/stream`;
134
+ const res = await fetch(url, {
135
+ method: "GET",
136
+ headers: this.buildHeaders(),
137
+ signal: handlers?.signal,
138
+ redirect: "error",
139
+ });
140
+ if (!res.ok) {
141
+ const text = await res.text();
142
+ let body = {};
143
+ try {
144
+ body = text ? JSON.parse(text) : {};
145
+ }
146
+ catch {
147
+ /* ignore */
148
+ }
149
+ throw parseApiError(res.status, body);
150
+ }
151
+ if (!res.body) {
152
+ throw new errors_1.ZinduaError("SSE stream unavailable.", { status: 0, code: "INVALID_RESPONSE" });
153
+ }
154
+ const reader = res.body.getReader();
155
+ const decoder = new TextDecoder();
156
+ let buffer = "";
157
+ while (true) {
158
+ const { done, value } = await reader.read();
159
+ if (done)
160
+ break;
161
+ buffer += decoder.decode(value, { stream: true });
162
+ const chunks = buffer.split("\n\n");
163
+ buffer = chunks.pop() || "";
164
+ for (const chunk of chunks) {
165
+ const line = chunk
166
+ .split("\n")
167
+ .find((l) => l.startsWith("data:"));
168
+ if (!line)
169
+ continue;
170
+ const raw = line.slice(5).trim();
171
+ let event;
172
+ try {
173
+ event = JSON.parse(raw);
174
+ }
175
+ catch {
176
+ continue;
177
+ }
178
+ if (!event.status || event.status === "connected" || event.status === "pending") {
179
+ handlers?.onEvent?.(event);
180
+ continue;
181
+ }
182
+ handlers?.onEvent?.(event);
183
+ if (event.status === "approved")
184
+ handlers?.onApproved?.();
185
+ if (event.status === "denied")
186
+ handlers?.onDenied?.();
187
+ if (event.status === "expired")
188
+ handlers?.onExpired?.();
189
+ if (["approved", "denied", "expired", "failed"].includes(event.status)) {
190
+ try {
191
+ await reader.cancel();
192
+ }
193
+ catch {
194
+ /* ignore */
195
+ }
196
+ return event;
197
+ }
198
+ }
199
+ }
200
+ throw new errors_1.ZinduaError("SSE stream ended without a terminal status.", {
201
+ status: 0,
202
+ code: "STREAM_ENDED",
203
+ });
204
+ },
205
+ };
206
+ /** @deprecated Prefer `pushMirror` — same API */
207
+ confirm = this.pushMirror;
208
+ /** Zindua Devices (FCM / APNs / WebPush Token Registration) API */
209
+ devices = {
210
+ register: async (options) => {
211
+ return this.request("POST", "devices", options);
212
+ },
213
+ };
101
214
  /** Returns true when using a znd_test_ key (sandbox / no real delivery). */
102
215
  isTestMode() {
103
216
  return this.apiKey.startsWith("znd_test_");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zindua/sdk",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "Official Zindua SDK for Node.js — transactional email and WhatsApp via POST /api/v1/send.",
5
5
  "author": "Zindua",
6
6
  "license": "MIT",