@miguelmorales13/nestkit 0.31.4 → 0.32.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.
Files changed (56) hide show
  1. package/README.md +234 -0
  2. package/dist/chunk-2RQINZCR.cjs +17 -0
  3. package/dist/{chunk-LU3HO7B3.js → chunk-3WISEIHK.js} +4 -15
  4. package/dist/chunk-HMHUOXGB.cjs +63 -0
  5. package/dist/chunk-L2QK2HN5.js +17 -0
  6. package/dist/chunk-PBAPT3L5.js +63 -0
  7. package/dist/chunk-QVLTEABJ.cjs +248 -0
  8. package/dist/chunk-XKI3DJVA.js +248 -0
  9. package/dist/{chunk-33KFVAUZ.cjs → chunk-ZBOQKXGZ.cjs} +7 -18
  10. package/dist/payments/index.cjs +6 -5
  11. package/dist/payments/index.js +2 -1
  12. package/dist/resilience/index.cjs +48 -262
  13. package/dist/resilience/index.js +19 -233
  14. package/dist/security/index.cjs +4 -2
  15. package/dist/security/index.js +5 -3
  16. package/dist/telephony/adapters/meta-whatsapp.telephony.d.ts +21 -0
  17. package/dist/telephony/adapters/simulated.telephony.d.ts +49 -0
  18. package/dist/telephony/adapters/twilio.telephony.d.ts +54 -0
  19. package/dist/telephony/index.cjs +2269 -0
  20. package/dist/telephony/index.d.ts +20 -0
  21. package/dist/telephony/index.js +2269 -0
  22. package/dist/telephony/provider-http.d.ts +35 -0
  23. package/dist/telephony/relay/access.d.ts +72 -0
  24. package/dist/telephony/relay/backend.d.ts +81 -0
  25. package/dist/telephony/relay/index.cjs +483 -0
  26. package/dist/telephony/relay/index.d.ts +9 -0
  27. package/dist/telephony/relay/index.js +483 -0
  28. package/dist/telephony/relay/server.d.ts +89 -0
  29. package/dist/telephony/relay/session.d.ts +100 -0
  30. package/dist/telephony/rules/call-limits.d.ts +48 -0
  31. package/dist/telephony/rules/call-token.d.ts +30 -0
  32. package/dist/telephony/rules/destination-policy.d.ts +21 -0
  33. package/dist/telephony/rules/dial-destination.d.ts +21 -0
  34. package/dist/telephony/rules/event-deduper.d.ts +15 -0
  35. package/dist/telephony/rules/handoff.d.ts +7 -0
  36. package/dist/telephony/rules/index.d.ts +20 -0
  37. package/dist/telephony/rules/opt-out.d.ts +32 -0
  38. package/dist/telephony/rules/phone.d.ts +9 -0
  39. package/dist/telephony/rules/provider-gate.d.ts +31 -0
  40. package/dist/telephony/rules/redact.d.ts +11 -0
  41. package/dist/telephony/rules/sanitize.d.ts +10 -0
  42. package/dist/telephony/rules/sliding-window.d.ts +10 -0
  43. package/dist/telephony/rules/ttl-cache.d.ts +14 -0
  44. package/dist/telephony/rules/usage-counter.d.ts +16 -0
  45. package/dist/telephony/telephony.errors.d.ts +46 -0
  46. package/dist/telephony/telephony.facade.d.ts +71 -0
  47. package/dist/telephony/telephony.module.d.ts +38 -0
  48. package/dist/telephony/telephony.options.d.ts +187 -0
  49. package/dist/telephony/telephony.port.d.ts +421 -0
  50. package/dist/telephony/telephony.registry.d.ts +24 -0
  51. package/dist/telephony/telephony.webhook.controller.d.ts +22 -0
  52. package/dist/telephony/telephony.webhooks.d.ts +53 -0
  53. package/dist/whatsapp/index.cjs +28 -0
  54. package/dist/whatsapp/index.js +28 -0
  55. package/dist/whatsapp/whatsapp-client.d.ts +5 -0
  56. package/package.json +12 -2
@@ -0,0 +1,53 @@
1
+ import { type TelephonyHooks, type TelephonyOptions } from './telephony.options.js';
2
+ import { type AnswerResponse, type TelephonyEvent, type WebhookRequest } from './telephony.port.js';
3
+ import type { Telephony } from './telephony.facade.js';
4
+ export interface WebhookContext {
5
+ /** El tenant dueño de la ruta (p. ej. `/webhooks/twilio/:tenantId`): sus credenciales verifican la firma. */
6
+ tenantId?: string;
7
+ /** Origen de la petición (IP) para el tope de tasa. */
8
+ source?: string;
9
+ }
10
+ export interface WebhookResult {
11
+ /** 200 si se procesó; 403 firma inválida; 400 cuerpo ilegible; 404 proveedor desconocido; 429 demasiadas peticiones. */
12
+ status: number;
13
+ /** Lo que se le devuelve al proveedor. Vacío (sin detalle) cuando se rechaza. */
14
+ response: AnswerResponse;
15
+ /** Los eventos aceptados (firmados, no repetidos, de un tenant conocido). */
16
+ events: TelephonyEvent[];
17
+ /** Termina cuando los `onEvent` en segundo plano acabaron (las pruebas lo esperan; el controlador no). */
18
+ done: Promise<void>;
19
+ }
20
+ /**
21
+ * El camino único de todo webhook: **verificar firma → normalizar → descartar repeticiones → resolver tenant →
22
+ * registrar bajas → entregar al consumidor**. Corre sin tocar tu negocio; tú solo implementas `TelephonyHooks`.
23
+ *
24
+ * Rendimiento: la firma y el reparto son locales (sin red); solo la llamada **entrante** espera a tu `onEvent`, y como
25
+ * mucho `answerBudgetMs`: pasado el plazo se contesta con la respuesta de respaldo para que nadie se quede en silencio.
26
+ * Los demás eventos se acusan al instante y se procesan aparte.
27
+ */
28
+ export declare class TelephonyWebhooks {
29
+ private readonly telephony;
30
+ private readonly options;
31
+ private readonly hooks;
32
+ private readonly now;
33
+ private readonly deduper;
34
+ private readonly limiter?;
35
+ private readonly maxSkewMs;
36
+ private readonly answerBudgetMs;
37
+ private readonly fallback;
38
+ constructor(telephony: Telephony, options?: TelephonyOptions, hooks?: TelephonyHooks, now?: () => number);
39
+ /** Reto de verificación por GET (Meta): `null` si el proveedor no lo soporta o el token no coincide. */
40
+ challenge(provider: string, query: Record<string, string | undefined>, ctx?: WebhookContext): Promise<string | null>;
41
+ private adapter;
42
+ handle(provider: string, request: WebhookRequest, ctx?: WebhookContext): Promise<WebhookResult>;
43
+ private buildAnswer;
44
+ /** Espera a `onEvent` como mucho `answerBudgetMs`; falla o plazo vencido = `undefined` (respuesta de respaldo). */
45
+ private within;
46
+ /** `string` = tenant; `undefined` = no hay `resolveTenant` (se sigue sin tenant); `null` = de nadie (se ignora). */
47
+ private resolveTenant;
48
+ private numbersOf;
49
+ /** STOP/BAJA entrante: la baja queda registrada **antes** de entregar el evento. Obligatorio, no opcional. */
50
+ private recordKeyword;
51
+ private fail;
52
+ private log;
53
+ }
@@ -33,6 +33,34 @@ var WhatsAppClient = class {
33
33
  }
34
34
  return await response.json();
35
35
  }
36
+ /**
37
+ * Sends an approved template (the only thing WhatsApp allows outside the 24 h customer-service window).
38
+ * `variables` fill the body placeholders `{{1}}`, `{{2}}`… in order.
39
+ */
40
+ async sendTemplateMessage(to, template, language, variables = []) {
41
+ const response = await fetch(this.baseUrl, {
42
+ method: "POST",
43
+ headers: {
44
+ Authorization: `Bearer ${this.options.accessToken}`,
45
+ "Content-Type": "application/json"
46
+ },
47
+ body: JSON.stringify({
48
+ messaging_product: "whatsapp",
49
+ to,
50
+ type: "template",
51
+ template: {
52
+ name: template,
53
+ language: { code: language },
54
+ ...variables.length ? { components: [{ type: "body", parameters: variables.map((text) => ({ type: "text", text })) }] } : {}
55
+ }
56
+ })
57
+ });
58
+ if (!response.ok) {
59
+ const errorBody = await response.text();
60
+ throw new Error(`WhatsAppClient: send failed (${response.status}): ${errorBody}`);
61
+ }
62
+ return await response.json();
63
+ }
36
64
  };
37
65
 
38
66
  // src/whatsapp/whatsapp.module.ts
@@ -33,6 +33,34 @@ var WhatsAppClient = class {
33
33
  }
34
34
  return await response.json();
35
35
  }
36
+ /**
37
+ * Sends an approved template (the only thing WhatsApp allows outside the 24 h customer-service window).
38
+ * `variables` fill the body placeholders `{{1}}`, `{{2}}`… in order.
39
+ */
40
+ async sendTemplateMessage(to, template, language, variables = []) {
41
+ const response = await fetch(this.baseUrl, {
42
+ method: "POST",
43
+ headers: {
44
+ Authorization: `Bearer ${this.options.accessToken}`,
45
+ "Content-Type": "application/json"
46
+ },
47
+ body: JSON.stringify({
48
+ messaging_product: "whatsapp",
49
+ to,
50
+ type: "template",
51
+ template: {
52
+ name: template,
53
+ language: { code: language },
54
+ ...variables.length ? { components: [{ type: "body", parameters: variables.map((text) => ({ type: "text", text })) }] } : {}
55
+ }
56
+ })
57
+ });
58
+ if (!response.ok) {
59
+ const errorBody = await response.text();
60
+ throw new Error(`WhatsAppClient: send failed (${response.status}): ${errorBody}`);
61
+ }
62
+ return await response.json();
63
+ }
36
64
  };
37
65
 
38
66
  // src/whatsapp/whatsapp.module.ts
@@ -18,4 +18,9 @@ export declare class WhatsAppClient {
18
18
  private readonly baseUrl;
19
19
  constructor(options: WhatsAppClientOptions);
20
20
  sendTextMessage(to: string, body: string): Promise<WhatsAppSendResult>;
21
+ /**
22
+ * Sends an approved template (the only thing WhatsApp allows outside the 24 h customer-service window).
23
+ * `variables` fill the body placeholders `{{1}}`, `{{2}}`… in order.
24
+ */
25
+ sendTemplateMessage(to: string, template: string, language: string, variables?: string[]): Promise<WhatsAppSendResult>;
21
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miguelmorales13/nestkit",
3
- "version": "0.31.4",
3
+ "version": "0.32.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -8,7 +8,7 @@
8
8
  "url": "git+https://github.com/miguelmoralesr13/nestkit.git"
9
9
  },
10
10
  "type": "module",
11
- "description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth, Store/POS provisioning and a multi-tenant connections service (encrypted credentials, token refresh, webhooks, refunds, online checkout), signed stateless OAuth connect state, and CFDI 4.0 invoicing (Facturapi, Facturama).",
11
+ "description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip) with marketplace OAuth, Store/POS provisioning and a multi-tenant connections service (encrypted credentials, token refresh, webhooks, refunds, online checkout), signed stateless OAuth connect state, CFDI 4.0 invoicing (Facturapi, Facturama), and provider-agnostic telephony (Twilio, simulated) with a voice relay.",
12
12
  "publishConfig": {
13
13
  "access": "public"
14
14
  },
@@ -175,6 +175,16 @@
175
175
  "import": "./dist/invoicing/index.js",
176
176
  "require": "./dist/invoicing/index.cjs"
177
177
  },
178
+ "./telephony": {
179
+ "types": "./dist/telephony/index.d.ts",
180
+ "import": "./dist/telephony/index.js",
181
+ "require": "./dist/telephony/index.cjs"
182
+ },
183
+ "./telephony/relay": {
184
+ "types": "./dist/telephony/relay/index.d.ts",
185
+ "import": "./dist/telephony/relay/index.js",
186
+ "require": "./dist/telephony/relay/index.cjs"
187
+ },
178
188
  "./idempotency": {
179
189
  "types": "./dist/idempotency/index.d.ts",
180
190
  "import": "./dist/idempotency/index.js",