@reachflow/sdk 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,352 @@
1
+ // src/errors.ts
2
+ var ReachFlowError = class _ReachFlowError extends Error {
3
+ statusCode;
4
+ code;
5
+ retryable;
6
+ retryAfterMs;
7
+ body;
8
+ constructor(params) {
9
+ super(params.message, params.cause ? { cause: params.cause } : void 0);
10
+ this.name = "ReachFlowError";
11
+ this.statusCode = params.statusCode;
12
+ this.code = params.code;
13
+ this.retryable = params.retryable ?? false;
14
+ this.retryAfterMs = params.retryAfterMs;
15
+ this.body = params.body;
16
+ }
17
+ static fromResponse(status, body, fallbackMessage) {
18
+ const parsed = body ?? {};
19
+ const apiError = parsed.error;
20
+ const message = parsed.message ?? fallbackMessage ?? `ReachFlow API error (HTTP ${status})`;
21
+ const code = mapApiErrorToCode(status, apiError);
22
+ const retryable = status === 429 || status === 408 || status >= 500 && status < 600;
23
+ return new _ReachFlowError({
24
+ message,
25
+ statusCode: status,
26
+ code,
27
+ retryable,
28
+ body
29
+ });
30
+ }
31
+ static network(cause) {
32
+ return new _ReachFlowError({
33
+ message: "Network error while calling ReachFlow API",
34
+ statusCode: 0,
35
+ code: "network_error",
36
+ retryable: true,
37
+ cause
38
+ });
39
+ }
40
+ static timeout(timeoutMs) {
41
+ return new _ReachFlowError({
42
+ message: `Request timed out after ${timeoutMs}ms`,
43
+ statusCode: 408,
44
+ code: "timeout",
45
+ retryable: true
46
+ });
47
+ }
48
+ };
49
+ function mapApiErrorToCode(status, apiError) {
50
+ switch (apiError) {
51
+ case "unauthorized":
52
+ return "unauthorized";
53
+ case "plan_required":
54
+ return "plan_required";
55
+ case "insufficient_scope":
56
+ return "insufficient_scope";
57
+ case "rate_limit_exceeded":
58
+ return "rate_limit_exceeded";
59
+ case "too_many_auth_failures":
60
+ return "too_many_auth_failures";
61
+ default:
62
+ break;
63
+ }
64
+ if (status === 401) return "unauthorized";
65
+ if (status === 403) return "insufficient_scope";
66
+ if (status === 404) return "not_found";
67
+ if (status === 400 || status === 422) return "validation_error";
68
+ if (status === 429) return "rate_limit_exceeded";
69
+ return "api_error";
70
+ }
71
+ function parseRetryAfterMs(header) {
72
+ if (!header) return void 0;
73
+ const seconds = Number(header);
74
+ if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1e3);
75
+ const date = Date.parse(header);
76
+ if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
77
+ return void 0;
78
+ }
79
+
80
+ // src/types.ts
81
+ var TERMINAL_MESSAGE_STATUSES = /* @__PURE__ */ new Set([
82
+ "sent",
83
+ "delivered",
84
+ "failed",
85
+ "cancelled"
86
+ ]);
87
+ var DEFAULT_BASE_URL = "https://sandbox-api.reachflow.me";
88
+
89
+ // src/http.ts
90
+ function resolveConfig(options) {
91
+ const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
92
+ return {
93
+ apiKey: options.apiKey.trim(),
94
+ baseUrl,
95
+ apiPrefix: `${baseUrl}/api/v1`,
96
+ timeoutMs: options.timeoutMs ?? 3e4,
97
+ fetchImpl: options.fetch ?? globalThis.fetch,
98
+ maxRetries: options.maxRetries ?? 2
99
+ };
100
+ }
101
+ var HttpClient = class {
102
+ constructor(config) {
103
+ this.config = config;
104
+ }
105
+ config;
106
+ async request(options) {
107
+ let attempt = 0;
108
+ let lastError;
109
+ while (attempt <= this.config.maxRetries) {
110
+ try {
111
+ return await this.requestOnce(options);
112
+ } catch (err) {
113
+ if (!(err instanceof ReachFlowError)) throw err;
114
+ lastError = err;
115
+ if (!err.retryable || attempt >= this.config.maxRetries) throw err;
116
+ const delayMs = err.retryAfterMs ?? backoffMs(attempt);
117
+ await sleep(delayMs);
118
+ attempt += 1;
119
+ }
120
+ }
121
+ throw lastError ?? new ReachFlowError({
122
+ message: "Request failed",
123
+ statusCode: 0,
124
+ code: "api_error"
125
+ });
126
+ }
127
+ async requestOnce(options) {
128
+ const url = `${this.config.apiPrefix}${options.path}`;
129
+ const headers = {
130
+ Accept: "application/json",
131
+ "X-API-Key": this.config.apiKey
132
+ };
133
+ if (options.body !== void 0) {
134
+ headers["Content-Type"] = "application/json";
135
+ }
136
+ if (options.idempotencyKey) {
137
+ headers["Idempotency-Key"] = options.idempotencyKey;
138
+ }
139
+ const controller = new AbortController();
140
+ const timer = setTimeout(() => controller.abort(), this.config.timeoutMs);
141
+ try {
142
+ const response = await this.config.fetchImpl(url, {
143
+ method: options.method,
144
+ headers,
145
+ body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
146
+ signal: controller.signal
147
+ });
148
+ const text = await response.text();
149
+ let data = null;
150
+ if (text) {
151
+ try {
152
+ data = JSON.parse(text);
153
+ } catch {
154
+ data = { raw: text.slice(0, 500) };
155
+ }
156
+ }
157
+ if (!response.ok) {
158
+ const err = ReachFlowError.fromResponse(
159
+ response.status,
160
+ data
161
+ );
162
+ const retryAfterMs = parseRetryAfterMs(
163
+ response.headers.get("Retry-After")
164
+ );
165
+ if (retryAfterMs !== void 0) {
166
+ throw new ReachFlowError({
167
+ message: err.message,
168
+ statusCode: err.statusCode,
169
+ code: err.code,
170
+ retryable: err.retryable,
171
+ retryAfterMs,
172
+ body: err.body
173
+ });
174
+ }
175
+ throw err;
176
+ }
177
+ return data;
178
+ } catch (err) {
179
+ if (err instanceof ReachFlowError) throw err;
180
+ if (err instanceof Error && err.name === "AbortError") {
181
+ throw ReachFlowError.timeout(this.config.timeoutMs);
182
+ }
183
+ throw ReachFlowError.network(err);
184
+ } finally {
185
+ clearTimeout(timer);
186
+ }
187
+ }
188
+ };
189
+ function backoffMs(attempt) {
190
+ return Math.min(1e3 * 2 ** attempt, 1e4);
191
+ }
192
+ function sleep(ms) {
193
+ return new Promise((resolve) => setTimeout(resolve, ms));
194
+ }
195
+
196
+ // src/resources/messages.ts
197
+ var MessagesResource = class {
198
+ constructor(http) {
199
+ this.http = http;
200
+ }
201
+ http;
202
+ /** Envoie un message texte (réponse HTTP 202). */
203
+ async send(params) {
204
+ const { idempotencyKey, ...body } = params;
205
+ return this.http.request({
206
+ method: "POST",
207
+ path: "/messages/send",
208
+ body,
209
+ idempotencyKey
210
+ });
211
+ }
212
+ /** Envoie un média via URL HTTPS publique (réponse HTTP 202). */
213
+ async sendMedia(params) {
214
+ const { idempotencyKey, ...body } = params;
215
+ return this.http.request({
216
+ method: "POST",
217
+ path: "/messages/send-media",
218
+ body,
219
+ idempotencyKey
220
+ });
221
+ }
222
+ /** Envoie un lot de messages (même modèle, destinataires multiples). */
223
+ async sendBulk(params) {
224
+ const { idempotencyKey, ...body } = params;
225
+ return this.http.request({
226
+ method: "POST",
227
+ path: "/messages/send-bulk",
228
+ body,
229
+ idempotencyKey
230
+ });
231
+ }
232
+ /** Consulte le statut d'un message précédemment accepté. */
233
+ async getStatus(messageId) {
234
+ return this.http.request({
235
+ method: "GET",
236
+ path: `/messages/${messageId}`
237
+ });
238
+ }
239
+ /**
240
+ * Poll jusqu'à un statut terminal (`sent`, `delivered`, `failed`, `cancelled`).
241
+ * Lance `ReachFlowError` si timeout.
242
+ */
243
+ async waitForTerminal(messageId, options) {
244
+ const pollIntervalMs = options?.pollIntervalMs ?? 2e3;
245
+ const timeoutMs = options?.timeoutMs ?? 12e4;
246
+ const started = Date.now();
247
+ while (Date.now() - started < timeoutMs) {
248
+ const status = await this.getStatus(messageId);
249
+ if (TERMINAL_MESSAGE_STATUSES.has(status.status)) {
250
+ return status;
251
+ }
252
+ await sleep2(pollIntervalMs);
253
+ }
254
+ throw new ReachFlowError({
255
+ message: `Message ${messageId} did not reach a terminal status within ${timeoutMs}ms`,
256
+ statusCode: 408,
257
+ code: "timeout",
258
+ retryable: false
259
+ });
260
+ }
261
+ };
262
+ function sleep2(ms) {
263
+ return new Promise((resolve) => setTimeout(resolve, ms));
264
+ }
265
+
266
+ // src/resources/otp.ts
267
+ var OtpResource = class {
268
+ constructor(http) {
269
+ this.http = http;
270
+ }
271
+ http;
272
+ /**
273
+ * Génère et envoie un code OTP par WhatsApp.
274
+ * Le code n'est jamais retourné par l'API — uniquement sur WhatsApp.
275
+ */
276
+ async send(params) {
277
+ return this.http.request({
278
+ method: "POST",
279
+ path: "/otp/send",
280
+ body: params
281
+ });
282
+ }
283
+ /** Vérifie un code saisi par l'utilisateur final. */
284
+ async verify(params) {
285
+ return this.http.request({
286
+ method: "POST",
287
+ path: "/otp/verify",
288
+ body: params
289
+ });
290
+ }
291
+ };
292
+
293
+ // src/resources/providers.ts
294
+ var ProvidersResource = class {
295
+ constructor(http) {
296
+ this.http = http;
297
+ }
298
+ http;
299
+ /** Liste les numéros WhatsApp accessibles via l'API. */
300
+ async list() {
301
+ return this.http.request({
302
+ method: "GET",
303
+ path: "/providers"
304
+ });
305
+ }
306
+ /** Détail d'un provider avec statistiques journalières. */
307
+ async get(providerId) {
308
+ return this.http.request({
309
+ method: "GET",
310
+ path: `/providers/${providerId}`
311
+ });
312
+ }
313
+ /**
314
+ * Retourne le premier provider au statut `connected`, ou le premier de la liste.
315
+ * Utile pour les scripts de test / démarrage rapide.
316
+ */
317
+ async findConnected() {
318
+ const { providers } = await this.list();
319
+ if (providers.length === 0) return null;
320
+ return providers.find((p) => p.status.toLowerCase() === "connected") ?? providers[0] ?? null;
321
+ }
322
+ };
323
+
324
+ // src/client.ts
325
+ var ReachFlow = class {
326
+ messages;
327
+ providers;
328
+ otp;
329
+ http;
330
+ config;
331
+ constructor(options) {
332
+ if (!options.apiKey?.trim()) {
333
+ throw new Error("ReachFlow: apiKey is required");
334
+ }
335
+ this.config = resolveConfig(options);
336
+ this.http = new HttpClient(this.config);
337
+ this.messages = new MessagesResource(this.http);
338
+ this.providers = new ProvidersResource(this.http);
339
+ this.otp = new OtpResource(this.http);
340
+ }
341
+ /** URL de base configurée (sans `/api/v1`). */
342
+ get baseUrl() {
343
+ return this.config.baseUrl;
344
+ }
345
+ };
346
+ export {
347
+ DEFAULT_BASE_URL,
348
+ ReachFlow,
349
+ ReachFlowError,
350
+ TERMINAL_MESSAGE_STATUSES
351
+ };
352
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/types.ts","../src/http.ts","../src/resources/messages.ts","../src/resources/otp.ts","../src/resources/providers.ts","../src/client.ts"],"sourcesContent":["export type ReachFlowErrorCode =\n | 'unauthorized'\n | 'plan_required'\n | 'insufficient_scope'\n | 'rate_limit_exceeded'\n | 'too_many_auth_failures'\n | 'validation_error'\n | 'not_found'\n | 'api_error'\n | 'network_error'\n | 'timeout';\n\nexport interface ReachFlowErrorBody {\n statusCode?: number;\n error?: string;\n message?: string;\n}\n\nexport class ReachFlowError extends Error {\n readonly statusCode: number;\n readonly code: ReachFlowErrorCode;\n readonly retryable: boolean;\n readonly retryAfterMs?: number;\n readonly body?: unknown;\n\n constructor(params: {\n message: string;\n statusCode: number;\n code: ReachFlowErrorCode;\n retryable?: boolean;\n retryAfterMs?: number;\n body?: unknown;\n cause?: unknown;\n }) {\n super(params.message, params.cause ? { cause: params.cause } : undefined);\n this.name = 'ReachFlowError';\n this.statusCode = params.statusCode;\n this.code = params.code;\n this.retryable = params.retryable ?? false;\n this.retryAfterMs = params.retryAfterMs;\n this.body = params.body;\n }\n\n static fromResponse(\n status: number,\n body: unknown,\n fallbackMessage?: string,\n ): ReachFlowError {\n const parsed = (body ?? {}) as ReachFlowErrorBody;\n const apiError = parsed.error;\n const message =\n parsed.message ??\n fallbackMessage ??\n `ReachFlow API error (HTTP ${status})`;\n\n const code = mapApiErrorToCode(status, apiError);\n const retryable =\n status === 429 || status === 408 || (status >= 500 && status < 600);\n\n return new ReachFlowError({\n message,\n statusCode: status,\n code,\n retryable,\n body,\n });\n }\n\n static network(cause: unknown): ReachFlowError {\n return new ReachFlowError({\n message: 'Network error while calling ReachFlow API',\n statusCode: 0,\n code: 'network_error',\n retryable: true,\n cause,\n });\n }\n\n static timeout(timeoutMs: number): ReachFlowError {\n return new ReachFlowError({\n message: `Request timed out after ${timeoutMs}ms`,\n statusCode: 408,\n code: 'timeout',\n retryable: true,\n });\n }\n}\n\nfunction mapApiErrorToCode(\n status: number,\n apiError?: string,\n): ReachFlowErrorCode {\n switch (apiError) {\n case 'unauthorized':\n return 'unauthorized';\n case 'plan_required':\n return 'plan_required';\n case 'insufficient_scope':\n return 'insufficient_scope';\n case 'rate_limit_exceeded':\n return 'rate_limit_exceeded';\n case 'too_many_auth_failures':\n return 'too_many_auth_failures';\n default:\n break;\n }\n\n if (status === 401) return 'unauthorized';\n if (status === 403) return 'insufficient_scope';\n if (status === 404) return 'not_found';\n if (status === 400 || status === 422) return 'validation_error';\n if (status === 429) return 'rate_limit_exceeded';\n return 'api_error';\n}\n\nexport function parseRetryAfterMs(header: string | null): number | undefined {\n if (!header) return undefined;\n const seconds = Number(header);\n if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1000);\n const date = Date.parse(header);\n if (!Number.isNaN(date)) return Math.max(0, date - Date.now());\n return undefined;\n}\n","/** Statut de cycle de vie d'un message API. */\nexport type MessageStatus =\n | 'queued'\n | 'processing'\n | 'sent'\n | 'delivered'\n | 'failed'\n | 'cancelled';\n\nexport type MediaType = 'image' | 'document' | 'audio' | 'video';\n\nexport type FailureCode =\n | 'warmup_daily_limit'\n | 'risk_circuit_open'\n | 'provider_disconnected'\n | 'provider_banned'\n | 'send_not_allowed'\n | 'instance_busy'\n | 'delivery_timeout'\n | 'delivery_failed'\n | 'send_error'\n | 'provider_not_found';\n\nexport type OtpVerifyReason =\n | 'invalidated'\n | 'already_used'\n | 'expired'\n | 'max_attempts_reached'\n | 'invalid_code'\n | 'not_found';\n\nexport interface ReachFlowClientOptions {\n /** Clé API `rfl_live_…` ou `rfl_test_…`. */\n apiKey: string;\n /**\n * URL de base sans `/api/v1` (défaut : sandbox ReachFlow).\n * @default \"https://sandbox-api.reachflow.me\"\n */\n baseUrl?: string;\n /** Timeout HTTP en millisecondes. @default 30000 */\n timeoutMs?: number;\n /** Implémentation fetch (tests, Node < 18). @default global fetch */\n fetch?: typeof fetch;\n /** Nombre max de retries sur 429 / 5xx retryables. @default 2 */\n maxRetries?: number;\n}\n\nexport interface SendMessageParams {\n providerId: string;\n to: string;\n message: string;\n variables?: Record<string, string>;\n scheduleAt?: string;\n saveContact?: boolean;\n idempotencyKey?: string;\n}\n\nexport interface SendMediaParams {\n providerId: string;\n to: string;\n mediaUrl: string;\n mediaType: MediaType;\n caption?: string;\n saveContact?: boolean;\n idempotencyKey?: string;\n}\n\nexport interface BulkRecipient {\n to: string;\n variables?: Record<string, string>;\n}\n\nexport interface SendBulkParams {\n providerId: string;\n messageTemplate: string;\n recipients: BulkRecipient[];\n scheduleAt?: string;\n saveContact?: boolean;\n idempotencyKey?: string;\n}\n\nexport interface SendAcceptedResponse {\n messageId: string;\n status: 'queued';\n queuedAt: string;\n}\n\nexport interface BulkRejection {\n to: string;\n reason: string;\n}\n\nexport interface SendBulkResponse {\n bulkId: string;\n accepted: number;\n rejected: number;\n rejections: BulkRejection[];\n messageIds: string[];\n}\n\nexport interface MessageStatusResponse {\n messageId: string;\n status: MessageStatus;\n to: string;\n providerId: string;\n queuedAt: string;\n sentAt: string | null;\n deliveredAt: string | null;\n failedAt: string | null;\n failureCode: FailureCode | null;\n failureReason: string | null;\n}\n\nexport interface ProviderSummary {\n id: string;\n name: string;\n phoneNumber: string | null;\n status: string;\n warmupDay: number;\n riskScore: number;\n}\n\nexport interface ProviderDailyStats {\n sent: number;\n delivered: number;\n failed: number;\n messagesLimit: number | null;\n newContactsLimit: number | null;\n}\n\nexport interface ProviderDetail extends ProviderSummary {\n dailyStats: ProviderDailyStats;\n}\n\nexport interface ProviderListResponse {\n providers: ProviderSummary[];\n}\n\nexport interface OtpSendParams {\n providerId: string;\n phoneNumber: string;\n codeLength?: number;\n expiresIn?: number;\n brandName?: string;\n template?: string;\n saveContact?: boolean;\n}\n\nexport interface OtpSendResponse {\n otpId: string;\n messageId: string;\n expiresAt: string;\n}\n\nexport interface OtpVerifyParams {\n otpId: string;\n code: string;\n}\n\nexport interface OtpVerifyResponse {\n valid: boolean;\n reason?: OtpVerifyReason;\n attemptsLeft?: number;\n}\n\nexport interface WaitForTerminalOptions {\n /** Intervalle entre polls en ms. @default 2000 */\n pollIntervalMs?: number;\n /** Timeout total en ms. @default 120000 */\n timeoutMs?: number;\n}\n\nexport const TERMINAL_MESSAGE_STATUSES: ReadonlySet<MessageStatus> = new Set([\n 'sent',\n 'delivered',\n 'failed',\n 'cancelled',\n]);\n\nexport const DEFAULT_BASE_URL = 'https://sandbox-api.reachflow.me';\n","import { ReachFlowError, parseRetryAfterMs } from './errors.js';\nimport type { ReachFlowClientOptions } from './types.js';\nimport { DEFAULT_BASE_URL } from './types.js';\n\nexport interface RequestOptions {\n method: 'GET' | 'POST';\n path: string;\n body?: unknown;\n idempotencyKey?: string;\n}\n\nexport interface ResolvedClientConfig {\n apiKey: string;\n baseUrl: string;\n apiPrefix: string;\n timeoutMs: number;\n fetchImpl: typeof fetch;\n maxRetries: number;\n}\n\nexport function resolveConfig(\n options: ReachFlowClientOptions,\n): ResolvedClientConfig {\n const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/$/, '');\n return {\n apiKey: options.apiKey.trim(),\n baseUrl,\n apiPrefix: `${baseUrl}/api/v1`,\n timeoutMs: options.timeoutMs ?? 30_000,\n fetchImpl: options.fetch ?? globalThis.fetch,\n maxRetries: options.maxRetries ?? 2,\n };\n}\n\nexport class HttpClient {\n constructor(private readonly config: ResolvedClientConfig) {}\n\n async request<T>(options: RequestOptions): Promise<T> {\n let attempt = 0;\n let lastError: ReachFlowError | undefined;\n\n while (attempt <= this.config.maxRetries) {\n try {\n return await this.requestOnce<T>(options);\n } catch (err) {\n if (!(err instanceof ReachFlowError)) throw err;\n lastError = err;\n if (!err.retryable || attempt >= this.config.maxRetries) throw err;\n const delayMs = err.retryAfterMs ?? backoffMs(attempt);\n await sleep(delayMs);\n attempt += 1;\n }\n }\n\n throw lastError ?? new ReachFlowError({\n message: 'Request failed',\n statusCode: 0,\n code: 'api_error',\n });\n }\n\n private async requestOnce<T>(options: RequestOptions): Promise<T> {\n const url = `${this.config.apiPrefix}${options.path}`;\n const headers: Record<string, string> = {\n Accept: 'application/json',\n 'X-API-Key': this.config.apiKey,\n };\n\n if (options.body !== undefined) {\n headers['Content-Type'] = 'application/json';\n }\n if (options.idempotencyKey) {\n headers['Idempotency-Key'] = options.idempotencyKey;\n }\n\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), this.config.timeoutMs);\n\n try {\n const response = await this.config.fetchImpl(url, {\n method: options.method,\n headers,\n body:\n options.body !== undefined\n ? JSON.stringify(options.body)\n : undefined,\n signal: controller.signal,\n });\n\n const text = await response.text();\n let data: unknown = null;\n if (text) {\n try {\n data = JSON.parse(text) as unknown;\n } catch {\n data = { raw: text.slice(0, 500) };\n }\n }\n\n if (!response.ok) {\n const err = ReachFlowError.fromResponse(\n response.status,\n data,\n );\n const retryAfterMs = parseRetryAfterMs(\n response.headers.get('Retry-After'),\n );\n if (retryAfterMs !== undefined) {\n throw new ReachFlowError({\n message: err.message,\n statusCode: err.statusCode,\n code: err.code,\n retryable: err.retryable,\n retryAfterMs,\n body: err.body,\n });\n }\n throw err;\n }\n\n return data as T;\n } catch (err) {\n if (err instanceof ReachFlowError) throw err;\n if (err instanceof Error && err.name === 'AbortError') {\n throw ReachFlowError.timeout(this.config.timeoutMs);\n }\n throw ReachFlowError.network(err);\n } finally {\n clearTimeout(timer);\n }\n }\n}\n\nfunction backoffMs(attempt: number): number {\n return Math.min(1000 * 2 ** attempt, 10_000);\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n","import type { HttpClient } from '../http.js';\nimport { ReachFlowError } from '../errors.js';\nimport type {\n MessageStatusResponse,\n SendAcceptedResponse,\n SendBulkParams,\n SendBulkResponse,\n SendMediaParams,\n SendMessageParams,\n WaitForTerminalOptions,\n} from '../types.js';\nimport { TERMINAL_MESSAGE_STATUSES } from '../types.js';\n\nexport class MessagesResource {\n constructor(private readonly http: HttpClient) {}\n\n /** Envoie un message texte (réponse HTTP 202). */\n async send(params: SendMessageParams): Promise<SendAcceptedResponse> {\n const { idempotencyKey, ...body } = params;\n return this.http.request<SendAcceptedResponse>({\n method: 'POST',\n path: '/messages/send',\n body,\n idempotencyKey,\n });\n }\n\n /** Envoie un média via URL HTTPS publique (réponse HTTP 202). */\n async sendMedia(params: SendMediaParams): Promise<SendAcceptedResponse> {\n const { idempotencyKey, ...body } = params;\n return this.http.request<SendAcceptedResponse>({\n method: 'POST',\n path: '/messages/send-media',\n body,\n idempotencyKey,\n });\n }\n\n /** Envoie un lot de messages (même modèle, destinataires multiples). */\n async sendBulk(params: SendBulkParams): Promise<SendBulkResponse> {\n const { idempotencyKey, ...body } = params;\n return this.http.request<SendBulkResponse>({\n method: 'POST',\n path: '/messages/send-bulk',\n body,\n idempotencyKey,\n });\n }\n\n /** Consulte le statut d'un message précédemment accepté. */\n async getStatus(messageId: string): Promise<MessageStatusResponse> {\n return this.http.request<MessageStatusResponse>({\n method: 'GET',\n path: `/messages/${messageId}`,\n });\n }\n\n /**\n * Poll jusqu'à un statut terminal (`sent`, `delivered`, `failed`, `cancelled`).\n * Lance `ReachFlowError` si timeout.\n */\n async waitForTerminal(\n messageId: string,\n options?: WaitForTerminalOptions,\n ): Promise<MessageStatusResponse> {\n const pollIntervalMs = options?.pollIntervalMs ?? 2_000;\n const timeoutMs = options?.timeoutMs ?? 120_000;\n const started = Date.now();\n\n while (Date.now() - started < timeoutMs) {\n const status = await this.getStatus(messageId);\n if (TERMINAL_MESSAGE_STATUSES.has(status.status)) {\n return status;\n }\n await sleep(pollIntervalMs);\n }\n\n throw new ReachFlowError({\n message: `Message ${messageId} did not reach a terminal status within ${timeoutMs}ms`,\n statusCode: 408,\n code: 'timeout',\n retryable: false,\n });\n }\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n","import type { HttpClient } from '../http.js';\nimport type {\n OtpSendParams,\n OtpSendResponse,\n OtpVerifyParams,\n OtpVerifyResponse,\n} from '../types.js';\n\nexport class OtpResource {\n constructor(private readonly http: HttpClient) {}\n\n /**\n * Génère et envoie un code OTP par WhatsApp.\n * Le code n'est jamais retourné par l'API — uniquement sur WhatsApp.\n */\n async send(params: OtpSendParams): Promise<OtpSendResponse> {\n return this.http.request<OtpSendResponse>({\n method: 'POST',\n path: '/otp/send',\n body: params,\n });\n }\n\n /** Vérifie un code saisi par l'utilisateur final. */\n async verify(params: OtpVerifyParams): Promise<OtpVerifyResponse> {\n return this.http.request<OtpVerifyResponse>({\n method: 'POST',\n path: '/otp/verify',\n body: params,\n });\n }\n}\n","import type { HttpClient } from '../http.js';\nimport type { ProviderDetail, ProviderListResponse } from '../types.js';\n\nexport class ProvidersResource {\n constructor(private readonly http: HttpClient) {}\n\n /** Liste les numéros WhatsApp accessibles via l'API. */\n async list(): Promise<ProviderListResponse> {\n return this.http.request<ProviderListResponse>({\n method: 'GET',\n path: '/providers',\n });\n }\n\n /** Détail d'un provider avec statistiques journalières. */\n async get(providerId: string): Promise<ProviderDetail> {\n return this.http.request<ProviderDetail>({\n method: 'GET',\n path: `/providers/${providerId}`,\n });\n }\n\n /**\n * Retourne le premier provider au statut `connected`, ou le premier de la liste.\n * Utile pour les scripts de test / démarrage rapide.\n */\n async findConnected(): Promise<ProviderDetail | ProviderListResponse['providers'][number] | null> {\n const { providers } = await this.list();\n if (providers.length === 0) return null;\n return (\n providers.find((p) => p.status.toLowerCase() === 'connected') ??\n providers[0] ??\n null\n );\n }\n}\n","import { HttpClient, resolveConfig } from './http.js';\nimport { MessagesResource } from './resources/messages.js';\nimport { OtpResource } from './resources/otp.js';\nimport { ProvidersResource } from './resources/providers.js';\nimport type { ReachFlowClientOptions } from './types.js';\n\n/**\n * Client officiel pour l'API publique ReachFlow (REST v1).\n *\n * @example\n * ```ts\n * const client = new ReachFlow({ apiKey: process.env.REACHFLOW_API_KEY! });\n * const { providers } = await client.providers.list();\n * ```\n */\nexport class ReachFlow {\n readonly messages: MessagesResource;\n readonly providers: ProvidersResource;\n readonly otp: OtpResource;\n\n private readonly http: HttpClient;\n private readonly config: ReturnType<typeof resolveConfig>;\n\n constructor(options: ReachFlowClientOptions) {\n if (!options.apiKey?.trim()) {\n throw new Error('ReachFlow: apiKey is required');\n }\n\n this.config = resolveConfig(options);\n this.http = new HttpClient(this.config);\n this.messages = new MessagesResource(this.http);\n this.providers = new ProvidersResource(this.http);\n this.otp = new OtpResource(this.http);\n }\n\n /** URL de base configurée (sans `/api/v1`). */\n get baseUrl(): string {\n return this.config.baseUrl;\n }\n}\n"],"mappings":";AAkBO,IAAM,iBAAN,MAAM,wBAAuB,MAAM;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,QAQT;AACD,UAAM,OAAO,SAAS,OAAO,QAAQ,EAAE,OAAO,OAAO,MAAM,IAAI,MAAS;AACxE,SAAK,OAAO;AACZ,SAAK,aAAa,OAAO;AACzB,SAAK,OAAO,OAAO;AACnB,SAAK,YAAY,OAAO,aAAa;AACrC,SAAK,eAAe,OAAO;AAC3B,SAAK,OAAO,OAAO;AAAA,EACrB;AAAA,EAEA,OAAO,aACL,QACA,MACA,iBACgB;AAChB,UAAM,SAAU,QAAQ,CAAC;AACzB,UAAM,WAAW,OAAO;AACxB,UAAM,UACJ,OAAO,WACP,mBACA,6BAA6B,MAAM;AAErC,UAAM,OAAO,kBAAkB,QAAQ,QAAQ;AAC/C,UAAM,YACJ,WAAW,OAAO,WAAW,OAAQ,UAAU,OAAO,SAAS;AAEjE,WAAO,IAAI,gBAAe;AAAA,MACxB;AAAA,MACA,YAAY;AAAA,MACZ;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,OAAO,QAAQ,OAAgC;AAC7C,WAAO,IAAI,gBAAe;AAAA,MACxB,SAA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EQ;AAAA,EACA;AAAA,EAEjB,YAAY,SAAiC;AAC3C,QAAI,CAAC,QAAQ,QAAQ,KAAK,GAAG;AAC3B,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AAEA,SAAK,SAAS,cAAc,OAAO;AACnC,SAAK,OAAO,IAAI,WAAW,KAAK,MAAM;AACtC,SAAK,WAAW,IAAI,iBAAiB,KAAK,IAAI;AAC9C,SAAK,YAAY,IAAI,kBAAkB,KAAK,IAAI;AAChD,SAAK,MAAM,IAAI,YAAY,KAAK,IAAI;AAAA,EACtC;AAAA;AAAA,EAGA,IAAI,UAAkB;AACpB,WAAO,KAAK,OAAO;AAAA,EACrB;AACF;","names":["sleep"]}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@reachflow/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Client officiel Node.js / TypeScript pour l'API publique ReachFlow",
5
+ "author": "ReachFlow",
6
+ "license": "MIT",
7
+ "homepage": "https://docs.reachflow.me/developpeurs",
8
+ "bugs": {
9
+ "url": "https://github.com/reachflow/reachflow-node/issues"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ }
28
+ }
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "README.md"
33
+ ],
34
+ "engines": {
35
+ "node": ">=18"
36
+ },
37
+ "scripts": {
38
+ "build": "tsup",
39
+ "test": "vitest run",
40
+ "test:watch": "vitest",
41
+ "typecheck": "tsc --noEmit",
42
+ "prepublishOnly": "npm run build && npm test"
43
+ },
44
+ "keywords": [
45
+ "reachflow",
46
+ "whatsapp",
47
+ "api",
48
+ "sdk",
49
+ "otp"
50
+ ],
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/reachflow/reachflow-node.git"
54
+ },
55
+ "devDependencies": {
56
+ "@types/node": "^22.10.0",
57
+ "tsup": "^8.3.5",
58
+ "typescript": "^5.7.2",
59
+ "vitest": "^2.1.8"
60
+ }
61
+ }