@cosmicdrift/kumiko-samples 0.413.1 → 0.415.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/package.json +1 -1
- package/packages/bundled-features/package.json +16 -1
- package/packages/bundled-features/src/admin-shell/changes.json +6 -0
- package/packages/bundled-features/src/admin-shell/feature.ts +8 -7
- package/packages/bundled-features/src/agent-tools/agent-doc-lint.ts +5 -1
- package/packages/bundled-features/src/agent-tools/agent-manifest.ts +36 -27
- package/packages/bundled-features/src/agent-tools/api-docs/collect-events.ts +8 -1
- package/packages/bundled-features/src/agent-tools/api-docs/collect.ts +3 -2
- package/packages/bundled-features/src/agent-tools/api-docs/generate.ts +6 -0
- package/packages/bundled-features/src/agent-tools/api-docs/index.ts +1 -0
- package/packages/bundled-features/src/agent-tools/api-docs/problems.ts +38 -1
- package/packages/bundled-features/src/agent-tools/api-docs/render-openapi.ts +1 -0
- package/packages/bundled-features/src/agent-tools/api-docs/resolve-config.ts +17 -0
- package/packages/bundled-features/src/agent-tools/api-docs/scope-slug-collisions.ts +17 -0
- package/packages/bundled-features/src/agent-tools/api-docs/translation-gaps.ts +42 -0
- package/packages/bundled-features/src/agent-tools/api-docs/webhook-example-problems.ts +30 -0
- package/packages/bundled-features/src/agent-tools/changes.json +12 -0
- package/packages/bundled-features/src/agent-tools/index.ts +1 -0
- package/packages/bundled-features/src/agent-tools/labels.ts +19 -6
- package/packages/bundled-features/src/agent-tools/tool-catalog.ts +18 -7
- package/packages/bundled-features/src/agent-tools/tool-dispatch.ts +14 -1
- package/packages/bundled-features/src/agent-tools/types.ts +4 -0
- package/packages/bundled-features/src/audit/event-classification.ts +6 -0
- package/packages/bundled-features/src/audit/web/i18n.ts +1 -0
- package/packages/bundled-features/src/auth-email-password/changes.json +18 -0
- package/packages/bundled-features/src/auth-email-password/errors.ts +0 -13
- package/packages/bundled-features/src/auth-email-password/handlers/invite-signup-complete.write.ts +147 -121
- package/packages/bundled-features/src/auth-email-password/handlers/login-options.query.ts +1 -1
- package/packages/bundled-features/src/auth-email-password/handlers/request-account-unlock.write.ts +1 -1
- package/packages/bundled-features/src/auth-email-password/handlers/request-email-verification.write.ts +0 -1
- package/packages/bundled-features/src/auth-email-password/handlers/request-password-reset.write.ts +0 -1
- package/packages/bundled-features/src/auth-email-password/handlers/self-registration-status.query.ts +1 -1
- package/packages/bundled-features/src/auth-email-password/reset-token.ts +0 -2
- package/packages/bundled-features/src/auth-email-password/seeding.ts +39 -7
- package/packages/bundled-features/src/auth-email-password/signup-token-store.ts +2 -3
- package/packages/bundled-features/src/auth-email-password/unlock-token.ts +0 -2
- package/packages/bundled-features/src/auth-email-password/verification-token.ts +0 -2
- package/packages/bundled-features/src/auth-email-password/web/auth-form-primitives.tsx +2 -2
- package/packages/bundled-features/src/auth-email-password/web/auth-gate.tsx +2 -2
- package/packages/bundled-features/src/auth-email-password/web/auth-redirect.ts +5 -0
- package/packages/bundled-features/src/auth-email-password/web/forgot-password-screen.tsx +1 -6
- package/packages/bundled-features/src/auth-email-password/web/invite-accept-screen.tsx +1 -1
- package/packages/bundled-features/src/auth-email-password/web/return-path.ts +2 -1
- package/packages/bundled-features/src/auth-email-password/web/session.tsx +12 -13
- package/packages/bundled-features/src/auth-email-password/web/signup-screen.tsx +1 -6
- package/packages/bundled-features/src/auth-email-password/web/static-account.ts +6 -1
- package/packages/bundled-features/src/auth-email-password/web/tenant-switcher.tsx +1 -1
- package/packages/bundled-features/src/auth-email-password/web/token-confirm-screen.tsx +4 -5
- package/packages/bundled-features/src/auth-email-password/web/user-menu.tsx +1 -1
- package/packages/bundled-features/src/auth-mfa/changes.json +6 -0
- package/packages/bundled-features/src/auth-mfa/web/mfa-code-or-recovery-field.tsx +17 -1
- package/packages/bundled-features/src/auth-mfa/web/mfa-disable-dialog.tsx +1 -1
- package/packages/bundled-features/src/auth-mfa/web/mfa-regenerate-recovery-dialog.tsx +1 -1
- package/packages/bundled-features/src/auth-mfa/web/mfa-setup-preauth-screen.tsx +1 -1
- package/packages/bundled-features/src/auth-mfa/web/mfa-verify-screen.tsx +16 -47
- package/packages/bundled-features/src/auth-mfa-user-data/index.ts +5 -6
- package/packages/bundled-features/src/billing-foundation/aggregate-id.ts +0 -4
- package/packages/bundled-features/src/billing-foundation/changes.json +61 -0
- package/packages/bundled-features/src/billing-foundation/checkout-core.ts +19 -1
- package/packages/bundled-features/src/billing-foundation/checkout-ledger.ts +67 -0
- package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-gate.ts +2 -1
- package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-text.ts +49 -2
- package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-open-cancels.ts +5 -0
- package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-provider-timeout.ts +23 -0
- package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-record.ts +12 -7
- package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-revoke.ts +1 -1
- package/packages/bundled-features/src/billing-foundation/events.ts +51 -10
- package/packages/bundled-features/src/billing-foundation/feature.ts +15 -1
- package/packages/bundled-features/src/billing-foundation/handlers/billing-plans.query.ts +47 -14
- package/packages/bundled-features/src/billing-foundation/handlers/create-checkout-session.write.ts +9 -1
- package/packages/bundled-features/src/billing-foundation/handlers/process-event.write.ts +183 -17
- package/packages/bundled-features/src/billing-foundation/handlers/process-payment-event.write.ts +35 -10
- package/packages/bundled-features/src/billing-foundation/handlers/record-checkout-opened.write.ts +25 -0
- package/packages/bundled-features/src/billing-foundation/handlers/start-plan-checkout.write.ts +9 -1
- package/packages/bundled-features/src/billing-foundation/handlers/switch-plan.write.ts +16 -4
- package/packages/bundled-features/src/billing-foundation/i18n.ts +3 -0
- package/packages/bundled-features/src/billing-foundation/pending-plan-switch.ts +80 -0
- package/packages/bundled-features/src/billing-foundation/scrub-raw-payload-headers.ts +50 -0
- package/packages/bundled-features/src/billing-foundation/types.ts +47 -30
- package/packages/bundled-features/src/billing-foundation/validate-options.ts +18 -0
- package/packages/bundled-features/src/billing-foundation/web/billing-plans-panel.tsx +10 -5
- package/packages/bundled-features/src/billing-foundation/web/cancel-contract-dialog.tsx +1 -1
- package/packages/bundled-features/src/billing-foundation/web/checkout-consent-dialog.tsx +1 -1
- package/packages/bundled-features/src/billing-foundation/web/constants.ts +5 -2
- package/packages/bundled-features/src/billing-foundation/web/plan-switch-confirm-dialog.tsx +1 -1
- package/packages/bundled-features/src/billing-foundation/webhook-handler.ts +65 -17
- package/packages/bundled-features/src/cap-counter/aggregate-id.ts +0 -2
- package/packages/bundled-features/src/cap-counter/changes.json +6 -0
- package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +19 -4
- package/packages/bundled-features/src/cap-overview/changes.json +6 -0
- package/packages/bundled-features/src/cap-overview/handlers/tenant-caps-list.query.ts +22 -6
- package/packages/bundled-features/src/cap-overview/i18n.ts +1 -0
- package/packages/bundled-features/src/cap-overview/index.ts +1 -0
- package/packages/bundled-features/src/cap-overview/types.ts +11 -1
- package/packages/bundled-features/src/cap-overview/web/cap-cards-panel.tsx +6 -2
- package/packages/bundled-features/src/cap-overview/web/cap-usage-bar.tsx +10 -1
- package/packages/bundled-features/src/cap-overview/web/usage-math.ts +5 -0
- package/packages/bundled-features/src/channel-in-app/changes.json +6 -0
- package/packages/bundled-features/src/channel-in-app/handlers/read-state-by-keys.query.ts +2 -2
- package/packages/bundled-features/src/channel-whatsapp/aggregate-id.ts +29 -0
- package/packages/bundled-features/src/channel-whatsapp/changes.json +32 -0
- package/packages/bundled-features/src/channel-whatsapp/constants.ts +46 -0
- package/packages/bundled-features/src/channel-whatsapp/events.ts +94 -0
- package/packages/bundled-features/src/channel-whatsapp/feature.ts +72 -0
- package/packages/bundled-features/src/channel-whatsapp/graph-client.ts +398 -0
- package/packages/bundled-features/src/channel-whatsapp/in-memory-meta.ts +300 -0
- package/packages/bundled-features/src/channel-whatsapp/inbound-handlers.ts +210 -0
- package/packages/bundled-features/src/channel-whatsapp/index.ts +66 -0
- package/packages/bundled-features/src/channel-whatsapp/meta-signature.ts +26 -0
- package/packages/bundled-features/src/channel-whatsapp/options.ts +149 -0
- package/packages/bundled-features/src/channel-whatsapp/pii.ts +109 -0
- package/packages/bundled-features/src/channel-whatsapp/webhook-normalize.ts +247 -0
- package/packages/bundled-features/src/channel-whatsapp/webhook-routes.ts +185 -0
- package/packages/bundled-features/src/compliance-profiles/changes.json +5 -0
- package/packages/bundled-features/src/compliance-profiles/handlers/set-profile.write.ts +2 -0
- package/packages/bundled-features/src/compliance-profiles/handlers/sub-processors.query.ts +1 -1
- package/packages/bundled-features/src/config/changes.json +12 -0
- package/packages/bundled-features/src/config/handlers/set.write.ts +7 -0
- package/packages/bundled-features/src/config/handlers/values.query.ts +8 -1
- package/packages/bundled-features/src/config/index.ts +1 -0
- package/packages/bundled-features/src/config/read-redaction.ts +6 -3
- package/packages/bundled-features/src/config/table.ts +1 -0
- package/packages/bundled-features/src/config/write-helpers.ts +3 -2
- package/packages/bundled-features/src/crypto-shredding/changes.json +18 -0
- package/packages/bundled-features/src/crypto-shredding/constants.ts +2 -0
- package/packages/bundled-features/src/crypto-shredding/handlers/forget-subject.write.ts +38 -18
- package/packages/bundled-features/src/crypto-shredding/index.ts +4 -0
- package/packages/bundled-features/src/crypto-shredding/subject-forget-after-delete-hook.ts +49 -0
- package/packages/bundled-features/src/custom-fields/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/custom-fields/entity.ts +1 -0
- package/packages/bundled-features/src/custom-fields/wire-for-entity.ts +0 -1
- package/packages/bundled-features/src/data-retention/changes.json +12 -0
- package/packages/bundled-features/src/data-retention/erase-notes-of-deleted-row.ts +23 -0
- package/packages/bundled-features/src/data-retention/feature.ts +2 -5
- package/packages/bundled-features/src/data-retention/run-retention-cleanup.ts +38 -18
- package/packages/bundled-features/src/data-retention/tenant-destruction-hold.ts +9 -0
- package/packages/bundled-features/src/delivery/attempt-log-factory.ts +11 -0
- package/packages/bundled-features/src/delivery/attempt-log.ts +3 -3
- package/packages/bundled-features/src/delivery/changes.json +12 -0
- package/packages/bundled-features/src/delivery/channel-context.ts +6 -3
- package/packages/bundled-features/src/delivery/db/queries/preferences.ts +0 -1
- package/packages/bundled-features/src/delivery/delivery-service.ts +3 -0
- package/packages/bundled-features/src/delivery/feature.ts +4 -13
- package/packages/bundled-features/src/delivery/handlers/opted-in-user-ids.query.ts +54 -0
- package/packages/bundled-features/src/delivery/handlers/preferences.query.ts +2 -20
- package/packages/bundled-features/src/delivery/handlers/resubscribe-user.write.ts +2 -25
- package/packages/bundled-features/src/delivery/handlers/unsubscribe-page-chrome.query.ts +2 -0
- package/packages/bundled-features/src/delivery/handlers/unsubscribe-user.write.ts +2 -27
- package/packages/bundled-features/src/delivery/index.ts +3 -1
- package/packages/bundled-features/src/delivery/options.ts +11 -0
- package/packages/bundled-features/src/delivery/preference-rows.ts +33 -0
- package/packages/bundled-features/src/delivery/set-preference-enabled.ts +40 -0
- package/packages/bundled-features/src/delivery/testing.ts +3 -1
- package/packages/bundled-features/src/delivery/types.ts +1 -0
- package/packages/bundled-features/src/delivery/unsubscribe-page.ts +7 -1
- package/packages/bundled-features/src/delivery/upsert-preference.ts +0 -1
- package/packages/bundled-features/src/delivery/web/public-names.ts +1 -0
- package/packages/bundled-features/src/derivatives-sharp/render.ts +9 -9
- package/packages/bundled-features/src/document-ingest-foundation/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/document-ingest-foundation/append-missing-extract-event.ts +37 -25
- package/packages/bundled-features/src/document-ingest-foundation/changes.json +6 -0
- package/packages/bundled-features/src/document-ingest-foundation/feature.ts +1 -0
- package/packages/bundled-features/src/document-ingest-foundation/report-ingest-progress.ts +34 -14
- package/packages/bundled-features/src/documents/aggregate-id.ts +0 -2
- package/packages/bundled-features/src/documents/changes.json +24 -0
- package/packages/bundled-features/src/documents/constants.ts +10 -2
- package/packages/bundled-features/src/documents/ensure-link.ts +3 -4
- package/packages/bundled-features/src/documents/feature.ts +33 -6
- package/packages/bundled-features/src/documents/handlers/import.write.ts +2 -2
- package/packages/bundled-features/src/documents/queries/expiry-overview.query.ts +2 -1
- package/packages/bundled-features/src/documents/queries/for-record.query.ts +3 -4
- package/packages/bundled-features/src/documents/queries/list-page.ts +11 -3
- package/packages/bundled-features/src/documents/queries/overview.query.ts +2 -2
- package/packages/bundled-features/src/documents/screens.ts +8 -3
- package/packages/bundled-features/src/documents/tab.ts +1 -1
- package/packages/bundled-features/src/feature-toggles/toggle-sync-signal.ts +6 -3
- package/packages/bundled-features/src/file-derivatives/handlers/public-variant-by-file-ref.query.ts +1 -1
- package/packages/bundled-features/src/file-derivatives/handlers/public-variant.query.ts +1 -1
- package/packages/bundled-features/src/file-foundation/probe.ts +9 -3
- package/packages/bundled-features/src/file-foundation/read-file-ref-content.ts +24 -11
- package/packages/bundled-features/src/file-provider-inmemory/feature.ts +1 -1
- package/packages/bundled-features/src/file-provider-s3/changes.json +14 -0
- package/packages/bundled-features/src/file-provider-s3/env-feature.ts +1 -1
- package/packages/bundled-features/src/file-provider-s3/env-named-feature.ts +1 -1
- package/packages/bundled-features/src/file-provider-s3/env-schema.ts +24 -7
- package/packages/bundled-features/src/file-provider-s3/feature.ts +1 -1
- package/packages/bundled-features/src/file-provider-s3/index.ts +7 -1
- package/packages/bundled-features/src/file-provider-s3/lib/env-helper.ts +21 -13
- package/packages/bundled-features/src/file-provider-s3/lib/s3-provider.ts +8 -1
- package/packages/bundled-features/src/files/changes.json +7 -0
- package/packages/bundled-features/src/folders/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/folders/web/folder-manager.tsx +6 -6
- package/packages/bundled-features/src/folders-user-data/index.ts +12 -14
- package/packages/bundled-features/src/form-draft/feature.ts +1 -0
- package/packages/bundled-features/src/helpdesk/changes.json +56 -0
- package/packages/bundled-features/src/helpdesk/entity.ts +4 -1
- package/packages/bundled-features/src/helpdesk/feature.ts +42 -0
- package/packages/bundled-features/src/helpdesk/handlers/access.ts +0 -10
- package/packages/bundled-features/src/helpdesk/handlers/attempts.ts +7 -14
- package/packages/bundled-features/src/helpdesk/handlers/challenge.query.ts +1 -2
- package/packages/bundled-features/src/helpdesk/handlers/reply.write.ts +5 -12
- package/packages/bundled-features/src/helpdesk/handlers/report.write.ts +4 -10
- package/packages/bundled-features/src/helpdesk/handlers/submit.write.ts +5 -12
- package/packages/bundled-features/src/helpdesk/index.ts +1 -0
- package/packages/bundled-features/src/helpdesk/legacy-contact-config.ts +61 -0
- package/packages/bundled-features/src/helpdesk/legacy-contact-erasure.ts +45 -0
- package/packages/bundled-features/src/helpdesk/legacy-contact-retention.ts +65 -0
- package/packages/bundled-features/src/helpdesk/legacy-contact-streams.ts +187 -0
- package/packages/bundled-features/src/helpdesk/legacy-contact-user-data.ts +84 -0
- package/packages/bundled-features/src/helpdesk/lib.ts +55 -37
- package/packages/bundled-features/src/helpdesk/user-data-hooks.ts +89 -48
- package/packages/bundled-features/src/helpdesk/web/constants.ts +0 -1
- package/packages/bundled-features/src/inbound-mail-foundation/account-health.ts +1 -1
- package/packages/bundled-features/src/inbound-mail-foundation/account-poll.ts +9 -2
- package/packages/bundled-features/src/inbound-mail-foundation/aggregate-id.ts +0 -2
- package/packages/bundled-features/src/inbound-mail-foundation/changes.json +54 -0
- package/packages/bundled-features/src/inbound-mail-foundation/constants.ts +6 -0
- package/packages/bundled-features/src/inbound-mail-foundation/content-refetch.ts +23 -2
- package/packages/bundled-features/src/inbound-mail-foundation/entities.ts +73 -15
- package/packages/bundled-features/src/inbound-mail-foundation/events.ts +24 -16
- package/packages/bundled-features/src/inbound-mail-foundation/feature.ts +3 -1
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/account-address.ts +16 -2
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/attach-message-content.write.ts +10 -3
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/connect-account.write.ts +23 -9
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/forget-message.write.ts +79 -0
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/ingest-message-schema.ts +18 -5
- package/packages/bundled-features/src/inbound-mail-foundation/i18n.ts +3 -0
- package/packages/bundled-features/src/inbound-mail-foundation/index.ts +1 -0
- package/packages/bundled-features/src/inbound-mail-foundation/normalize-inbound-message.ts +22 -8
- package/packages/bundled-features/src/inbound-mail-foundation/oauth-access-token.ts +1 -1
- package/packages/bundled-features/src/inbound-mail-foundation/outbound-mail.ts +2 -0
- package/packages/bundled-features/src/inbound-mail-foundation/personal-address.ts +20 -0
- package/packages/bundled-features/src/inbound-mail-foundation/purge-messages.ts +54 -0
- package/packages/bundled-features/src/inbound-mail-foundation/quarantine.ts +15 -5
- package/packages/bundled-features/src/inbound-mail-foundation/retention-job.ts +1 -5
- package/packages/bundled-features/src/inbound-mail-foundation/retention-sweep.ts +21 -34
- package/packages/bundled-features/src/inbound-mail-foundation/sanitize-inbound-html.ts +2 -5
- package/packages/bundled-features/src/inbound-mail-foundation/send-from-account.ts +9 -1
- package/packages/bundled-features/src/inbound-mail-foundation/supervisor-ingest.ts +7 -1
- package/packages/bundled-features/src/inbound-mail-foundation/sync-account-now.ts +1 -1
- package/packages/bundled-features/src/inbound-mail-foundation/tenant-destroy-hook.ts +14 -17
- package/packages/bundled-features/src/inbound-mail-foundation/watch-supervisor.ts +1 -1
- package/packages/bundled-features/src/inbound-provider-imap/byte-budget.ts +80 -0
- package/packages/bundled-features/src/inbound-provider-imap/changes.json +13 -0
- package/packages/bundled-features/src/inbound-provider-imap/connect-mailbox.ts +8 -4
- package/packages/bundled-features/src/inbound-provider-imap/connect-my-mailbox.ts +23 -6
- package/packages/bundled-features/src/inbound-provider-imap/connect-target-rate-limit.ts +46 -0
- package/packages/bundled-features/src/inbound-provider-imap/download-batch.ts +47 -10
- package/packages/bundled-features/src/inbound-provider-imap/feature.ts +7 -4
- package/packages/bundled-features/src/inbound-provider-imap/fetch-content.ts +43 -29
- package/packages/bundled-features/src/inbound-provider-imap/i18n.ts +2 -2
- package/packages/bundled-features/src/inbound-provider-imap/parse-message.ts +14 -0
- package/packages/bundled-features/src/inbound-provider-imap/reconnect-mailbox.ts +5 -4
- package/packages/bundled-features/src/inbound-provider-imap/send-mail.ts +39 -7
- package/packages/bundled-features/src/inbound-provider-imap/smtp-client.ts +19 -4
- package/packages/bundled-features/src/inbound-provider-imap/watch-mailbox.ts +8 -23
- package/packages/bundled-features/src/inbound-provider-inmemory/feature.ts +1 -1
- package/packages/bundled-features/src/jobs/events.ts +10 -0
- package/packages/bundled-features/src/jobs/feature.ts +2 -0
- package/packages/bundled-features/src/jobs/handlers/consumer.write.ts +41 -16
- package/packages/bundled-features/src/jobs/web/constants.ts +4 -0
- package/packages/bundled-features/src/mail-foundation/changes.json +6 -0
- package/packages/bundled-features/src/mail-transport-inmemory/feature.ts +1 -1
- package/packages/bundled-features/src/mail-transport-smtp/feature.ts +3 -2
- package/packages/bundled-features/src/managed-pages/changes.json +12 -0
- package/packages/bundled-features/src/managed-pages/handlers/branding.query.ts +1 -1
- package/packages/bundled-features/src/managed-pages/handlers/by-slug.query.ts +1 -1
- package/packages/bundled-features/src/managed-pages/handlers/by-tenant-published.query.ts +1 -1
- package/packages/bundled-features/src/managed-pages/render-page-body.ts +18 -10
- package/packages/bundled-features/src/managed-pages/screens/page-screens.ts +18 -38
- package/packages/bundled-features/src/managed-pages/table.ts +1 -0
- package/packages/bundled-features/src/managed-pages/web/client-plugin.tsx +8 -6
- package/packages/bundled-features/src/managed-pages/web/constants.ts +8 -1
- package/packages/bundled-features/src/managed-pages/web/i18n.ts +11 -3
- package/packages/bundled-features/src/managed-pages/web/markdown-to-document.ts +1 -1
- package/packages/bundled-features/src/managed-pages/web/page-edit-draft.ts +108 -0
- package/packages/bundled-features/src/managed-pages/web/page-edit-screen.tsx +204 -0
- package/packages/bundled-features/src/managed-pages/web/page-settings-rail.tsx +117 -0
- package/packages/bundled-features/src/notes-history-user-data/index.ts +8 -14
- package/packages/bundled-features/src/personal-access-tokens/boot-rules.ts +5 -0
- package/packages/bundled-features/src/personal-access-tokens/handlers/create.write.ts +0 -1
- package/packages/bundled-features/src/personal-access-tokens/scope-events-check.ts +31 -5
- package/packages/bundled-features/src/presence/changes.json +6 -0
- package/packages/bundled-features/src/presence/handlers/list.query.ts +28 -11
- package/packages/bundled-features/src/presence/room.ts +21 -7
- package/packages/bundled-features/src/presence/store.ts +79 -21
- package/packages/bundled-features/src/readiness/changes.json +25 -0
- package/packages/bundled-features/src/readiness/handlers/latest-summary.query.ts +11 -6
- package/packages/bundled-features/src/readiness/handlers/run-probes.write.ts +35 -5
- package/packages/bundled-features/src/readiness/handlers/run-tone.ts +3 -2
- package/packages/bundled-features/src/readiness/handlers/status.query.ts +15 -1
- package/packages/bundled-features/src/readiness/probe-context.ts +11 -10
- package/packages/bundled-features/src/readiness/screens.ts +4 -4
- package/packages/bundled-features/src/readiness/visible-results.ts +5 -3
- package/packages/bundled-features/src/renderer-foundation/api.ts +6 -13
- package/packages/bundled-features/src/renderer-foundation/types.ts +2 -2
- package/packages/bundled-features/src/secrets/changes.json +7 -0
- package/packages/bundled-features/src/secrets/db/queries/read.ts +18 -4
- package/packages/bundled-features/src/secrets/handlers/list.query.ts +8 -2
- package/packages/bundled-features/src/secrets/secret-scope.ts +2 -3
- package/packages/bundled-features/src/secrets/secrets-context.ts +6 -4
- package/packages/bundled-features/src/secrets/write-gate.ts +4 -2
- package/packages/bundled-features/src/seo/handlers/seo-config.query.ts +1 -1
- package/packages/bundled-features/src/shared/acknowledged-system-db.ts +20 -0
- package/packages/bundled-features/src/shared/assert-billing-live.ts +9 -2
- package/packages/bundled-features/src/shared/changes.json +12 -0
- package/packages/bundled-features/src/shared/hmac-signature.ts +4 -2
- package/packages/bundled-features/src/shared/index.ts +7 -1
- package/packages/bundled-features/src/shared/normalize-email.ts +3 -0
- package/packages/bundled-features/src/shared/parent-visibility.ts +25 -1
- package/packages/bundled-features/src/shared/password-hashing.ts +0 -1
- package/packages/bundled-features/src/shared/price-env-shape.ts +1 -4
- package/packages/bundled-features/src/shared/register-user-data-hooks.ts +29 -0
- package/packages/bundled-features/src/shared/require-bootstrap-extension.ts +26 -0
- package/packages/bundled-features/src/shared/send-best-effort.ts +29 -0
- package/packages/bundled-features/src/shared/stream-tenant-backfill.ts +22 -1
- package/packages/bundled-features/src/shared/stream-tenant-derived-state.ts +31 -1
- package/packages/bundled-features/src/step-dispatcher/changes.json +18 -0
- package/packages/bundled-features/src/step-dispatcher/constants.ts +13 -0
- package/packages/bundled-features/src/step-dispatcher/deliver-dispatch-request.ts +50 -2
- package/packages/bundled-features/src/step-dispatcher/feature.ts +42 -1
- package/packages/bundled-features/src/step-dispatcher/mail-runner.ts +0 -1
- package/packages/bundled-features/src/step-dispatcher/sweep-abandoned.ts +61 -0
- package/packages/bundled-features/src/subscription-mollie/changes.json +7 -0
- package/packages/bundled-features/src/subscription-mollie/plugin-methods.ts +5 -3
- package/packages/bundled-features/src/subscription-mollie/verify-webhook.ts +5 -5
- package/packages/bundled-features/src/subscription-paddle/backfill-checkout-ledger.ts +161 -0
- package/packages/bundled-features/src/subscription-paddle/changes.json +19 -0
- package/packages/bundled-features/src/subscription-paddle/checkout-config.query.ts +1 -1
- package/packages/bundled-features/src/subscription-paddle/checkout-methods.ts +2 -8
- package/packages/bundled-features/src/subscription-paddle/constants.ts +1 -2
- package/packages/bundled-features/src/subscription-paddle/feature.ts +26 -24
- package/packages/bundled-features/src/subscription-paddle/ledger-backfill-marker.ts +70 -0
- package/packages/bundled-features/src/subscription-paddle/plan-switch-methods.ts +2 -8
- package/packages/bundled-features/src/subscription-paddle/redeliver-legacy-checkout.ts +57 -0
- package/packages/bundled-features/src/subscription-paddle/runtime.ts +13 -24
- package/packages/bundled-features/src/subscription-paddle/subscription-state.ts +1 -0
- package/packages/bundled-features/src/subscription-paddle/webhook-events.ts +68 -21
- package/packages/bundled-features/src/subscription-stripe/changes.json +13 -0
- package/packages/bundled-features/src/subscription-stripe/feature.ts +5 -0
- package/packages/bundled-features/src/subscription-stripe/plugin-methods.ts +6 -6
- package/packages/bundled-features/src/subscription-stripe/runtime.ts +8 -7
- package/packages/bundled-features/src/subscription-stripe/verify-webhook.ts +38 -6
- package/packages/bundled-features/src/tags/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/tags/web/tag-chip.tsx +4 -5
- package/packages/bundled-features/src/tags/web/tag-manager.tsx +2 -1
- package/packages/bundled-features/src/template-resolver/api.ts +6 -13
- package/packages/bundled-features/src/template-resolver/changes.json +48 -0
- package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +25 -0
- package/packages/bundled-features/src/template-resolver/feature.ts +2 -0
- package/packages/bundled-features/src/template-resolver/handlers/body-preview.query.ts +41 -0
- package/packages/bundled-features/src/template-resolver/handlers/by-slug.query.ts +1 -1
- package/packages/bundled-features/src/template-resolver/handlers/by-tenant.query.ts +1 -1
- package/packages/bundled-features/src/template-resolver/handlers/collection-shared.ts +5 -11
- package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +3 -2
- package/packages/bundled-features/src/template-resolver/handlers/send-test.write.ts +37 -8
- package/packages/bundled-features/src/template-resolver/linked-resources.ts +2 -5
- package/packages/bundled-features/src/template-resolver/seeding.ts +107 -0
- package/packages/bundled-features/src/template-resolver/table.ts +1 -0
- package/packages/bundled-features/src/template-resolver/template-draft.ts +2 -1
- package/packages/bundled-features/src/template-resolver/template-preview.ts +14 -3
- package/packages/bundled-features/src/template-resolver/testing.ts +0 -2
- package/packages/bundled-features/src/template-resolver/web/client-plugin.tsx +3 -0
- package/packages/bundled-features/src/template-resolver/web/collection-entry-create-form.tsx +33 -33
- package/packages/bundled-features/src/template-resolver/web/constants.ts +7 -0
- package/packages/bundled-features/src/template-resolver/web/content-tree-provider.ts +0 -1
- package/packages/bundled-features/src/template-resolver/web/content-workbench.tsx +142 -0
- package/packages/bundled-features/src/template-resolver/web/document-insert-model.ts +3 -8
- package/packages/bundled-features/src/template-resolver/web/document-link-policy.ts +58 -0
- package/packages/bundled-features/src/template-resolver/web/document-schema.ts +11 -57
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-bubble.tsx +2 -4
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-context.ts +4 -5
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-editor.tsx +2 -2
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-helpers.ts +6 -3
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar-groups.tsx +2 -3
- package/packages/bundled-features/src/template-resolver/web/editor-handle.ts +1 -0
- package/packages/bundled-features/src/template-resolver/web/fill-document-variables.ts +24 -0
- package/packages/bundled-features/src/template-resolver/web/i18n.ts +14 -0
- package/packages/bundled-features/src/template-resolver/web/index.ts +3 -0
- package/packages/bundled-features/src/template-resolver/web/preview-targets.ts +25 -0
- package/packages/bundled-features/src/template-resolver/web/qualified-names.ts +1 -0
- package/packages/bundled-features/src/template-resolver/web/snippet-preview-renderer.tsx +24 -0
- package/packages/bundled-features/src/template-resolver/web/string-values.ts +7 -0
- package/packages/bundled-features/src/template-resolver/web/text-block-editor.tsx +38 -50
- package/packages/bundled-features/src/template-resolver/web/use-collection-entry-create.ts +7 -0
- package/packages/bundled-features/src/template-resolver/web/use-content-write.ts +1 -3
- package/packages/bundled-features/src/template-resolver/web/use-document-tiptap-model.ts +1 -2
- package/packages/bundled-features/src/template-resolver/web/use-text-block-editor.ts +28 -7
- package/packages/bundled-features/src/template-resolver/web/web-preview-renderer.tsx +132 -0
- package/packages/bundled-features/src/template-resolver-user-data/index.ts +11 -10
- package/packages/bundled-features/src/tenant/changes.json +6 -0
- package/packages/bundled-features/src/tenant/handlers/create.write.ts +7 -8
- package/packages/bundled-features/src/tenant/handlers/list.query.ts +7 -9
- package/packages/bundled-features/src/tenant/handlers/member-directory.query.ts +10 -4
- package/packages/bundled-features/src/tenant/handlers/toggle-enabled.write.ts +1 -1
- package/packages/bundled-features/src/tenant/invited-membership.ts +10 -1
- package/packages/bundled-features/src/tenant/seeding.ts +4 -2
- package/packages/bundled-features/src/tenant/stream-tenant-backfill.ts +5 -1
- package/packages/bundled-features/src/tenant/testing.ts +1 -1
- package/packages/bundled-features/src/tenant-inactivity/changes.json +6 -0
- package/packages/bundled-features/src/tenant-inactivity/check-inactive-tenants.ts +36 -17
- package/packages/bundled-features/src/tenant-inactivity/constants.ts +1 -2
- package/packages/bundled-features/src/tenant-inactivity/handlers/keep-account.write.ts +7 -2
- package/packages/bundled-features/src/tenant-inactivity/state-store.ts +22 -2
- package/packages/bundled-features/src/tenant-lifecycle/changes.json +24 -0
- package/packages/bundled-features/src/tenant-lifecycle/constants.ts +4 -0
- package/packages/bundled-features/src/tenant-lifecycle/feature.ts +1 -0
- package/packages/bundled-features/src/tenant-lifecycle/run-tenant-destroy.ts +43 -6
- package/packages/bundled-features/src/tenant-lifecycle/stages.ts +15 -2
- package/packages/bundled-features/src/tier-engine/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/tier-engine/changes.json +6 -0
- package/packages/bundled-features/src/tier-engine/feature.ts +8 -2
- package/packages/bundled-features/src/user/changes.json +6 -0
- package/packages/bundled-features/src/user/db/queries/stream-tenant-backfill.ts +2 -1
- package/packages/bundled-features/src/user/handlers/me.query.ts +3 -1
- package/packages/bundled-features/src/user/index.ts +1 -0
- package/packages/bundled-features/src/user/resolve-verified-user-email.ts +17 -0
- package/packages/bundled-features/src/user-data-rights/db/queries/export-jobs.ts +0 -1
- package/packages/bundled-features/src/user-data-rights/feature.ts +1 -0
- package/packages/bundled-features/src/user-data-rights/handlers/confirm-deletion-by-token.write.ts +1 -1
- package/packages/bundled-features/src/user-data-rights/handlers/download-by-token.query.ts +1 -1
- package/packages/bundled-features/src/user-data-rights/handlers/request-deletion-by-email.write.ts +1 -1
- package/packages/bundled-features/src/user-data-rights/run-export-jobs.ts +0 -2
- package/packages/bundled-features/src/user-data-rights/run-forget-cleanup.ts +3 -3
- package/packages/bundled-features/src/user-data-rights/schema/download-attempt.ts +3 -2
- package/packages/bundled-features/src/user-data-rights/web/confirm-deletion-screen.tsx +1 -1
- package/packages/bundled-features/src/user-data-rights/web/privacy-center-screen.tsx +1 -1
- package/packages/bundled-features/src/user-data-rights/web/request-deletion-screen.tsx +1 -1
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/api-token.userdata-hook.ts +5 -5
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/hard-delete-own-rows-hook.ts +17 -0
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/in-app-message.userdata-hook.ts +5 -5
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/job-run.userdata-hook.ts +2 -1
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/user-session.userdata-hook.ts +5 -5
- package/packages/bundled-features/src/user-profile/handlers/change-email.write.ts +0 -1
- package/packages/bundled-features/src/waitlist/changes.json +6 -0
- package/packages/bundled-features/src/waitlist/handlers/reject.write.ts +1 -1
- package/packages/bundled-features/src/waitlist/handlers/submit.write.ts +5 -29
- package/packages/bundled-features/src/waitlist/import.ts +2 -1
- package/packages/bundled-features/src/waitlist/lib.ts +2 -11
- package/packages/bundled-features/src/waitlist/link-on-signup.ts +2 -2
- package/packages/bundled-features/src/waitlist/user-data-hooks.ts +6 -19
- package/packages/bundled-features/src/workflow-runner/aggregate-id.ts +0 -1
- package/packages/bundled-features/src/workflow-runner/changes.json +6 -0
- package/packages/bundled-features/src/workflow-runner/event-trigger.ts +6 -2
- package/packages/bundled-features/src/workflow-runner/feature.ts +1 -5
- package/packages/bundled-features/src/workflow-runner/handlers/resume-run.write.ts +7 -4
- package/packages/bundled-features/src/workflow-runner/runner.ts +2 -0
- package/samples/apps/admin-console/src/app/shell.tsx +1 -0
- package/samples/apps/showcase/src/features/demos/web/pages/dialog.tsx +0 -1
- package/samples/apps/showcase/src/features/demos/web/pages/layout.tsx +1 -1
- package/samples/apps/showcase/src/features/demos/web/pages/sidebar-panel.tsx +0 -1
- package/samples/apps/styleguide/src/features/gallery/web/Gallery.tsx +1 -4
- package/samples/apps/ui-walkthrough/src/features/subscription-plans/feature.ts +6 -2
- package/samples/apps/use-all-bundled/e2e-billing-gallery/gallery-features.ts +12 -14
- package/samples/apps/use-all-bundled/feature-manifest.json +60 -29
- package/samples/apps/use-all-bundled/playwright.billing-docs.config.ts +1 -1
- package/samples/apps/use-all-bundled/playwright.config.ts +1 -0
- package/samples/apps/use-all-bundled/src/app/seed.ts +1 -1
- package/samples/apps/use-all-bundled/src/run-config.ts +2 -1
- package/samples/recipes/auth-mfa/src/web/confirmation-code-step.tsx +32 -10
- package/samples/recipes/chat-channels/README.md +2 -0
- package/samples/recipes/crypto-shredding-hr/README.md +11 -0
- package/samples/recipes/crypto-shredding-hr/src/feature.ts +27 -0
- package/samples/recipes/currencies-global/src/entities/currency.ts +1 -0
- package/samples/recipes/managed-pages/README.md +5 -1
- package/samples/recipes/managed-pages/package.json +3 -2
- package/samples/recipes/managed-pages/src/web/branded-preview.tsx +21 -0
- package/samples/recipes/managed-pages/tsconfig.json +8 -0
- package/samples/recipes/pwa-shell/README.md +52 -0
- package/samples/recipes/pwa-shell/bin/server.ts +12 -0
- package/samples/recipes/pwa-shell/package.json +13 -0
- package/samples/recipes/pwa-shell/src/app.ts +34 -0
- package/samples/recipes/rate-limiting/README.md +3 -3
- package/samples/recipes/record-detail-layout/README.md +33 -0
- package/samples/recipes/record-detail-layout/src/feature.ts +556 -44
- package/samples/recipes/record-detail-layout/src/order-board.ts +529 -0
- package/packages/bundled-features/src/managed-pages/web/page-body-section.tsx +0 -72
- package/packages/bundled-features/src/subscription-paddle/tenant-binding.ts +0 -34
- package/packages/bundled-features/src/template-resolver/web/content-field.tsx +0 -61
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { BlockedHostError, egress } from "@cosmicdrift/kumiko-framework/http";
|
|
2
|
+
import type { SecretsContext } from "@cosmicdrift/kumiko-framework/secrets";
|
|
3
|
+
import type { TenantId } from "@cosmicdrift/kumiko-types/identifiers";
|
|
4
|
+
import { SYSTEM_USER_ID } from "@cosmicdrift/kumiko-types/identifiers";
|
|
5
|
+
import { propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
6
|
+
import { checkChatWebhookTarget } from "../delivery/index.ts";
|
|
7
|
+
import {
|
|
8
|
+
WHATSAPP_BUTTON_PAYLOAD_MAX_CHARS,
|
|
9
|
+
WHATSAPP_BUTTON_TITLE_MAX_CHARS,
|
|
10
|
+
WHATSAPP_MAX_REPLY_BUTTONS,
|
|
11
|
+
WHATSAPP_SECRET_KEYS,
|
|
12
|
+
WHATSAPP_TEXT_MAX_CHARS,
|
|
13
|
+
} from "./constants.ts";
|
|
14
|
+
import {
|
|
15
|
+
type ChannelWhatsappOptions,
|
|
16
|
+
type ResolvedWhatsappOptions,
|
|
17
|
+
resolveWhatsappOptions,
|
|
18
|
+
} from "./options.ts";
|
|
19
|
+
|
|
20
|
+
export type WhatsappSendFailure =
|
|
21
|
+
| { readonly code: "missing_credentials" }
|
|
22
|
+
| { readonly code: "invalid_phone_number_id" }
|
|
23
|
+
| { readonly code: "invalid_recipient" }
|
|
24
|
+
| { readonly code: "invalid_message" }
|
|
25
|
+
| { readonly code: "host_not_allowed" }
|
|
26
|
+
| { readonly code: "timeout" }
|
|
27
|
+
| { readonly code: "unreachable" }
|
|
28
|
+
| { readonly code: "invalid_response" }
|
|
29
|
+
// Meta answered with an error; `metaCode` is its error code (e.g. 131047: outside the 24h window).
|
|
30
|
+
| { readonly code: "rejected"; readonly httpStatus: number; readonly metaCode: number | null };
|
|
31
|
+
|
|
32
|
+
export type WhatsappSendResult =
|
|
33
|
+
| { readonly ok: true; readonly wamid: string }
|
|
34
|
+
| ({ readonly ok: false } & WhatsappSendFailure);
|
|
35
|
+
|
|
36
|
+
export type WhatsappMediaFailure =
|
|
37
|
+
| WhatsappSendFailure
|
|
38
|
+
| { readonly code: "invalid_media_id" }
|
|
39
|
+
| { readonly code: "redirect_blocked" }
|
|
40
|
+
| { readonly code: "too_large" };
|
|
41
|
+
|
|
42
|
+
export type WhatsappMediaResult =
|
|
43
|
+
| { readonly ok: true; readonly bytes: Uint8Array; readonly mimeType: string }
|
|
44
|
+
| ({ readonly ok: false } & WhatsappMediaFailure);
|
|
45
|
+
|
|
46
|
+
/** What a send needs from the caller: the tenant and `ctx.services.secrets` of the handler or job. */
|
|
47
|
+
export type WhatsappCallContext = {
|
|
48
|
+
readonly tenantId: TenantId;
|
|
49
|
+
readonly secrets: SecretsContext | undefined;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
type SendBase = {
|
|
53
|
+
// The sending number; one WhatsApp Business account can own several, so it is never config.
|
|
54
|
+
readonly phoneNumberId: string;
|
|
55
|
+
readonly to: string;
|
|
56
|
+
// wamid of the message this one answers (shown as a quoted reply).
|
|
57
|
+
readonly replyToWamid?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type WhatsappTextInput = SendBase & { readonly text: string };
|
|
61
|
+
export type WhatsappTemplateInput = SendBase & {
|
|
62
|
+
readonly templateName: string;
|
|
63
|
+
readonly languageCode: string;
|
|
64
|
+
readonly bodyParameters?: readonly string[];
|
|
65
|
+
};
|
|
66
|
+
export type WhatsappReplyButton = {
|
|
67
|
+
// Echoed back unchanged in the `buttonPayload` of the answering message.
|
|
68
|
+
readonly payload: string;
|
|
69
|
+
readonly title: string;
|
|
70
|
+
};
|
|
71
|
+
export type WhatsappButtonsInput = SendBase & {
|
|
72
|
+
readonly text: string;
|
|
73
|
+
readonly buttons: readonly WhatsappReplyButton[];
|
|
74
|
+
};
|
|
75
|
+
export type WhatsappMediaInput = SendBase & {
|
|
76
|
+
readonly kind: "image";
|
|
77
|
+
readonly link: string;
|
|
78
|
+
readonly caption?: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type WhatsappClient = {
|
|
82
|
+
sendText(ctx: WhatsappCallContext, input: WhatsappTextInput): Promise<WhatsappSendResult>;
|
|
83
|
+
sendTemplate(ctx: WhatsappCallContext, input: WhatsappTemplateInput): Promise<WhatsappSendResult>;
|
|
84
|
+
sendButtons(ctx: WhatsappCallContext, input: WhatsappButtonsInput): Promise<WhatsappSendResult>;
|
|
85
|
+
sendMedia(ctx: WhatsappCallContext, input: WhatsappMediaInput): Promise<WhatsappSendResult>;
|
|
86
|
+
fetchMedia(ctx: WhatsappCallContext, mediaId: string): Promise<WhatsappMediaResult>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const PHONE_NUMBER_ID_PATTERN = /^\d{1,32}$/;
|
|
90
|
+
const RECIPIENT_PATTERN = /^\+?\d{5,20}$/;
|
|
91
|
+
const MEDIA_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
|
|
92
|
+
const TEMPLATE_NAME_PATTERN = /^[a-z0-9_]{1,512}$/;
|
|
93
|
+
const LANGUAGE_CODE_PATTERN = /^[a-z]{2,3}(_[A-Za-z]{2,4})?$/;
|
|
94
|
+
const SECRET_AUDIT_HANDLER = "channel-whatsapp:graph-client";
|
|
95
|
+
|
|
96
|
+
type GraphOutcome =
|
|
97
|
+
| { readonly ok: true; readonly response: Response }
|
|
98
|
+
| ({ readonly ok: false } & WhatsappSendFailure);
|
|
99
|
+
|
|
100
|
+
type AccessTokenOutcome =
|
|
101
|
+
| { readonly ok: true; readonly token: string }
|
|
102
|
+
| ({ readonly ok: false } & Extract<
|
|
103
|
+
WhatsappSendFailure,
|
|
104
|
+
{ readonly code: "missing_credentials" | "unreachable" }
|
|
105
|
+
>);
|
|
106
|
+
|
|
107
|
+
// Only an absent or empty secret is missing_credentials; a failing secret store is a transient fault.
|
|
108
|
+
async function readAccessToken(ctx: WhatsappCallContext): Promise<AccessTokenOutcome> {
|
|
109
|
+
try {
|
|
110
|
+
const revealed = await ctx.secrets?.get(ctx.tenantId, WHATSAPP_SECRET_KEYS.accessToken, {
|
|
111
|
+
userId: SYSTEM_USER_ID,
|
|
112
|
+
handlerName: SECRET_AUDIT_HANDLER,
|
|
113
|
+
});
|
|
114
|
+
const token = revealed?.reveal();
|
|
115
|
+
return token === undefined || token === ""
|
|
116
|
+
? { ok: false, code: "missing_credentials" }
|
|
117
|
+
: { ok: true, token };
|
|
118
|
+
} catch {
|
|
119
|
+
return { ok: false, code: "unreachable" };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The link is fetched by Meta, not by us, so no allowlist applies; it must still be a plain URL.
|
|
124
|
+
function isPublicMediaLink(link: string, requireHttps: boolean): boolean {
|
|
125
|
+
try {
|
|
126
|
+
const parsed = new URL(link);
|
|
127
|
+
const protocolAllowed =
|
|
128
|
+
parsed.protocol === "https:" || (!requireHttps && parsed.protocol === "http:");
|
|
129
|
+
return protocolAllowed && parsed.username === "" && parsed.password === "";
|
|
130
|
+
} catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isTimeout(err: unknown): boolean {
|
|
136
|
+
return err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function classifyTransportError(err: unknown): WhatsappSendFailure {
|
|
140
|
+
if (err instanceof BlockedHostError) return { code: "host_not_allowed" };
|
|
141
|
+
return { code: isTimeout(err) ? "timeout" : "unreachable" };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function createWhatsappClient(options: ChannelWhatsappOptions = {}): WhatsappClient {
|
|
145
|
+
return buildWhatsappClient(resolveWhatsappOptions(options));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function buildWhatsappClient(resolved: ResolvedWhatsappOptions): WhatsappClient {
|
|
149
|
+
const target = {
|
|
150
|
+
allowedHosts: resolved.allowedHosts,
|
|
151
|
+
requireHttps: resolved.requireHttps,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// The host and https check runs before any transport, so a swapped transport cannot
|
|
155
|
+
// route the bearer token to a host outside the allowlist.
|
|
156
|
+
async function callGraph(
|
|
157
|
+
url: string,
|
|
158
|
+
token: string,
|
|
159
|
+
init: { readonly method: "GET" | "POST"; readonly body?: unknown },
|
|
160
|
+
): Promise<GraphOutcome> {
|
|
161
|
+
const checked = checkChatWebhookTarget(target, url);
|
|
162
|
+
if (!checked) return { ok: false, code: "host_not_allowed" };
|
|
163
|
+
try {
|
|
164
|
+
const send =
|
|
165
|
+
resolved.transport ??
|
|
166
|
+
egress(
|
|
167
|
+
resolved.requireHttps
|
|
168
|
+
? { kind: "external" }
|
|
169
|
+
: { kind: "internal", allowOrigins: [checked.origin] },
|
|
170
|
+
);
|
|
171
|
+
const response = await send(checked.toString(), {
|
|
172
|
+
method: init.method,
|
|
173
|
+
redirect: "manual",
|
|
174
|
+
headers: {
|
|
175
|
+
authorization: `Bearer ${token}`,
|
|
176
|
+
...(init.body !== undefined && { "content-type": "application/json" }),
|
|
177
|
+
},
|
|
178
|
+
...(init.body !== undefined && { body: JSON.stringify(init.body) }),
|
|
179
|
+
signal: AbortSignal.timeout(resolved.timeoutMs),
|
|
180
|
+
});
|
|
181
|
+
return { ok: true, response };
|
|
182
|
+
} catch (err) {
|
|
183
|
+
return { ok: false, ...classifyTransportError(err) };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
type MediaAnnouncement =
|
|
188
|
+
| { readonly ok: true; readonly url: string; readonly mimeType: string | undefined }
|
|
189
|
+
| ({ readonly ok: false } & WhatsappSendFailure);
|
|
190
|
+
|
|
191
|
+
async function readMediaAnnouncement(response: Response): Promise<MediaAnnouncement> {
|
|
192
|
+
try {
|
|
193
|
+
const body = await response.json();
|
|
194
|
+
const url = propertyOf(body, "url");
|
|
195
|
+
const mimeType = propertyOf(body, "mime_type");
|
|
196
|
+
if (typeof url !== "string") return { ok: false, code: "invalid_response" };
|
|
197
|
+
return { ok: true, url, mimeType: typeof mimeType === "string" ? mimeType : undefined };
|
|
198
|
+
} catch {
|
|
199
|
+
return { ok: false, code: "invalid_response" };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
type BoundedBytes =
|
|
204
|
+
| { readonly ok: true; readonly bytes: Uint8Array }
|
|
205
|
+
| ({ readonly ok: false } & WhatsappMediaFailure);
|
|
206
|
+
|
|
207
|
+
async function readBoundedBytes(response: Response, maxBytes: number): Promise<BoundedBytes> {
|
|
208
|
+
const announcedLength = Number(response.headers.get("content-length"));
|
|
209
|
+
if (announcedLength > maxBytes) return { ok: false, code: "too_large" };
|
|
210
|
+
try {
|
|
211
|
+
if (!response.body) {
|
|
212
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
213
|
+
return bytes.byteLength > maxBytes ? { ok: false, code: "too_large" } : { ok: true, bytes };
|
|
214
|
+
}
|
|
215
|
+
const reader = response.body.getReader();
|
|
216
|
+
const chunks: Uint8Array[] = [];
|
|
217
|
+
let total = 0;
|
|
218
|
+
for (;;) {
|
|
219
|
+
const { done, value } = await reader.read();
|
|
220
|
+
if (done) break;
|
|
221
|
+
total += value.byteLength;
|
|
222
|
+
if (total > maxBytes) {
|
|
223
|
+
await reader.cancel();
|
|
224
|
+
return { ok: false, code: "too_large" };
|
|
225
|
+
}
|
|
226
|
+
chunks.push(value);
|
|
227
|
+
}
|
|
228
|
+
const bytes = new Uint8Array(total);
|
|
229
|
+
let offset = 0;
|
|
230
|
+
for (const chunk of chunks) {
|
|
231
|
+
bytes.set(chunk, offset);
|
|
232
|
+
offset += chunk.byteLength;
|
|
233
|
+
}
|
|
234
|
+
return { ok: true, bytes };
|
|
235
|
+
} catch (err) {
|
|
236
|
+
return { ok: false, ...classifyTransportError(err) };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function rejection(response: Response): Promise<WhatsappSendFailure> {
|
|
241
|
+
let metaCode: number | null = null;
|
|
242
|
+
try {
|
|
243
|
+
const code = propertyOf(propertyOf(await response.json(), "error"), "code");
|
|
244
|
+
metaCode = typeof code === "number" ? code : null;
|
|
245
|
+
} catch {
|
|
246
|
+
metaCode = null;
|
|
247
|
+
}
|
|
248
|
+
return { code: "rejected", httpStatus: response.status, metaCode };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function postMessage(
|
|
252
|
+
ctx: WhatsappCallContext,
|
|
253
|
+
base: SendBase,
|
|
254
|
+
content: Readonly<Record<string, unknown>>,
|
|
255
|
+
): Promise<WhatsappSendResult> {
|
|
256
|
+
if (!PHONE_NUMBER_ID_PATTERN.test(base.phoneNumberId)) {
|
|
257
|
+
return { ok: false, code: "invalid_phone_number_id" };
|
|
258
|
+
}
|
|
259
|
+
if (!RECIPIENT_PATTERN.test(base.to)) return { ok: false, code: "invalid_recipient" };
|
|
260
|
+
const credentials = await readAccessToken(ctx);
|
|
261
|
+
if (!credentials.ok) return credentials;
|
|
262
|
+
|
|
263
|
+
const outcome = await callGraph(
|
|
264
|
+
`${resolved.apiBaseUrl}/${resolved.apiVersion}/${base.phoneNumberId}/messages`,
|
|
265
|
+
credentials.token,
|
|
266
|
+
{
|
|
267
|
+
method: "POST",
|
|
268
|
+
body: {
|
|
269
|
+
messaging_product: "whatsapp",
|
|
270
|
+
recipient_type: "individual",
|
|
271
|
+
to: base.to.replace(/^\+/, ""),
|
|
272
|
+
...(base.replyToWamid !== undefined && { context: { message_id: base.replyToWamid } }),
|
|
273
|
+
...content,
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
);
|
|
277
|
+
if (!outcome.ok) return outcome;
|
|
278
|
+
if (!outcome.response.ok) return { ok: false, ...(await rejection(outcome.response)) };
|
|
279
|
+
try {
|
|
280
|
+
const messages = propertyOf(await outcome.response.json(), "messages");
|
|
281
|
+
const wamid = Array.isArray(messages) ? propertyOf(messages[0], "id") : undefined;
|
|
282
|
+
return typeof wamid === "string" && wamid !== ""
|
|
283
|
+
? { ok: true, wamid }
|
|
284
|
+
: { ok: false, code: "invalid_response" };
|
|
285
|
+
} catch {
|
|
286
|
+
return { ok: false, code: "invalid_response" };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function fetchMedia(
|
|
291
|
+
ctx: WhatsappCallContext,
|
|
292
|
+
mediaId: string,
|
|
293
|
+
): Promise<WhatsappMediaResult> {
|
|
294
|
+
if (!MEDIA_ID_PATTERN.test(mediaId)) return { ok: false, code: "invalid_media_id" };
|
|
295
|
+
const credentials = await readAccessToken(ctx);
|
|
296
|
+
if (!credentials.ok) return credentials;
|
|
297
|
+
const { token } = credentials;
|
|
298
|
+
|
|
299
|
+
const meta = await callGraph(
|
|
300
|
+
`${resolved.apiBaseUrl}/${resolved.apiVersion}/${mediaId}`,
|
|
301
|
+
token,
|
|
302
|
+
{ method: "GET" },
|
|
303
|
+
);
|
|
304
|
+
if (!meta.ok) return meta;
|
|
305
|
+
if (!meta.response.ok) return { ok: false, ...(await rejection(meta.response)) };
|
|
306
|
+
const announced = await readMediaAnnouncement(meta.response);
|
|
307
|
+
if (!announced.ok) return announced;
|
|
308
|
+
|
|
309
|
+
// The URL comes from the Graph answer and receives the bearer token: callGraph checks
|
|
310
|
+
// it against the same allowlist and https rule before anything is sent.
|
|
311
|
+
const download = await callGraph(announced.url, token, { method: "GET" });
|
|
312
|
+
if (!download.ok) return download;
|
|
313
|
+
const { response } = download;
|
|
314
|
+
if (response.status >= 300 && response.status < 400) {
|
|
315
|
+
return { ok: false, code: "redirect_blocked" };
|
|
316
|
+
}
|
|
317
|
+
if (!response.ok) return { ok: false, ...(await rejection(response)) };
|
|
318
|
+
const body = await readBoundedBytes(response, resolved.mediaMaxBytes);
|
|
319
|
+
if (!body.ok) return body;
|
|
320
|
+
const mimeType =
|
|
321
|
+
announced.mimeType ?? response.headers.get("content-type") ?? "application/octet-stream";
|
|
322
|
+
return { ok: true, bytes: body.bytes, mimeType };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
sendText: (ctx, input) => {
|
|
327
|
+
if (input.text === "" || input.text.length > WHATSAPP_TEXT_MAX_CHARS) {
|
|
328
|
+
return Promise.resolve({ ok: false, code: "invalid_message" });
|
|
329
|
+
}
|
|
330
|
+
return postMessage(ctx, input, {
|
|
331
|
+
type: "text",
|
|
332
|
+
text: { body: input.text, preview_url: false },
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
sendTemplate: (ctx, input) => {
|
|
337
|
+
if (
|
|
338
|
+
!TEMPLATE_NAME_PATTERN.test(input.templateName) ||
|
|
339
|
+
!LANGUAGE_CODE_PATTERN.test(input.languageCode)
|
|
340
|
+
) {
|
|
341
|
+
return Promise.resolve({ ok: false, code: "invalid_message" });
|
|
342
|
+
}
|
|
343
|
+
const parameters = (input.bodyParameters ?? []).map((text) => ({ type: "text", text }));
|
|
344
|
+
return postMessage(ctx, input, {
|
|
345
|
+
type: "template",
|
|
346
|
+
template: {
|
|
347
|
+
name: input.templateName,
|
|
348
|
+
language: { code: input.languageCode },
|
|
349
|
+
...(parameters.length > 0 && { components: [{ type: "body", parameters }] }),
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
sendButtons: (ctx, input) => {
|
|
355
|
+
const invalid =
|
|
356
|
+
input.text === "" ||
|
|
357
|
+
input.text.length > WHATSAPP_TEXT_MAX_CHARS ||
|
|
358
|
+
input.buttons.length === 0 ||
|
|
359
|
+
input.buttons.length > WHATSAPP_MAX_REPLY_BUTTONS ||
|
|
360
|
+
input.buttons.some(
|
|
361
|
+
({ payload, title }) =>
|
|
362
|
+
payload === "" ||
|
|
363
|
+
payload.length > WHATSAPP_BUTTON_PAYLOAD_MAX_CHARS ||
|
|
364
|
+
title === "" ||
|
|
365
|
+
title.length > WHATSAPP_BUTTON_TITLE_MAX_CHARS,
|
|
366
|
+
);
|
|
367
|
+
if (invalid) return Promise.resolve({ ok: false, code: "invalid_message" });
|
|
368
|
+
return postMessage(ctx, input, {
|
|
369
|
+
type: "interactive",
|
|
370
|
+
interactive: {
|
|
371
|
+
type: "button",
|
|
372
|
+
body: { text: input.text },
|
|
373
|
+
action: {
|
|
374
|
+
buttons: input.buttons.map(({ payload, title }) => ({
|
|
375
|
+
type: "reply",
|
|
376
|
+
reply: { id: payload, title },
|
|
377
|
+
})),
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
sendMedia: (ctx, input) => {
|
|
384
|
+
if (!isPublicMediaLink(input.link, resolved.requireHttps)) {
|
|
385
|
+
return Promise.resolve({ ok: false, code: "invalid_message" });
|
|
386
|
+
}
|
|
387
|
+
return postMessage(ctx, input, {
|
|
388
|
+
type: input.kind,
|
|
389
|
+
[input.kind]: {
|
|
390
|
+
link: input.link,
|
|
391
|
+
...(input.caption !== undefined && { caption: input.caption }),
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
fetchMedia,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// In-memory stand-in for Meta, for tests and local runs: serves the Graph endpoints the client
|
|
2
|
+
// calls, records every request and builds webhooks in Meta's wire format. No network involved.
|
|
3
|
+
import { parseJsonOrThrow } from "@cosmicdrift/kumiko-framework/utils";
|
|
4
|
+
import { WHATSAPP_DEFAULT_WEBHOOK_PATH } from "./constants.ts";
|
|
5
|
+
import { signMetaBody } from "./meta-signature.ts";
|
|
6
|
+
import type { WhatsappTransport } from "./options.ts";
|
|
7
|
+
|
|
8
|
+
export type InMemoryMetaRequest = {
|
|
9
|
+
readonly method: string;
|
|
10
|
+
readonly url: string;
|
|
11
|
+
readonly authorization: string | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type InMemoryMetaSentMessage = {
|
|
15
|
+
readonly wamid: string;
|
|
16
|
+
readonly phoneNumberId: string;
|
|
17
|
+
readonly authorization: string | null;
|
|
18
|
+
// The JSON body exactly as the Graph API receives it.
|
|
19
|
+
readonly body: Readonly<Record<string, unknown>>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type InMemoryMetaRejection = {
|
|
23
|
+
readonly httpStatus: number;
|
|
24
|
+
readonly metaCode: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type SeededMedia = {
|
|
28
|
+
readonly bytes: Uint8Array<ArrayBuffer>;
|
|
29
|
+
readonly mimeType: string;
|
|
30
|
+
readonly host: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type InMemoryMeta = {
|
|
34
|
+
readonly transport: WhatsappTransport;
|
|
35
|
+
readonly requests: InMemoryMetaRequest[];
|
|
36
|
+
readonly sent: InMemoryMetaSentMessage[];
|
|
37
|
+
seedMedia(
|
|
38
|
+
mediaId: string,
|
|
39
|
+
media: { bytes: Uint8Array<ArrayBuffer>; mimeType: string; host?: string },
|
|
40
|
+
): void;
|
|
41
|
+
// The next send answers this Graph error instead of a wamid.
|
|
42
|
+
rejectNextSend(rejection: InMemoryMetaRejection): void;
|
|
43
|
+
reset(): void;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const MESSAGES_PATH = /^\/v\d+\.\d+\/(\d+)\/messages$/;
|
|
47
|
+
const MEDIA_META_PATH = /^\/v\d+\.\d+\/([A-Za-z0-9_-]+)$/;
|
|
48
|
+
const MEDIA_BLOB_PATH = /^\/media\/([A-Za-z0-9_-]+)$/;
|
|
49
|
+
const DEFAULT_MEDIA_HOST = "lookaside.fbsbx.com";
|
|
50
|
+
|
|
51
|
+
function json(body: unknown, status = 200): Response {
|
|
52
|
+
return new Response(JSON.stringify(body), {
|
|
53
|
+
status,
|
|
54
|
+
headers: { "content-type": "application/json" },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function graphError(status: number, code: number): Response {
|
|
59
|
+
return json({ error: { message: "in-memory meta error", type: "OAuthException", code } }, status);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createInMemoryMeta(): InMemoryMeta {
|
|
63
|
+
const requests: InMemoryMetaRequest[] = [];
|
|
64
|
+
const sent: InMemoryMetaSentMessage[] = [];
|
|
65
|
+
const media = new Map<string, SeededMedia>();
|
|
66
|
+
let nextRejection: InMemoryMetaRejection | undefined;
|
|
67
|
+
let counter = 0;
|
|
68
|
+
|
|
69
|
+
function answerSend(
|
|
70
|
+
phoneNumberId: string,
|
|
71
|
+
authorization: string | null,
|
|
72
|
+
rawBody: string,
|
|
73
|
+
): Response {
|
|
74
|
+
if (nextRejection) {
|
|
75
|
+
const { httpStatus, metaCode } = nextRejection;
|
|
76
|
+
nextRejection = undefined;
|
|
77
|
+
return graphError(httpStatus, metaCode);
|
|
78
|
+
}
|
|
79
|
+
counter += 1;
|
|
80
|
+
const wamid = `wamid.INMEMORY${counter}`;
|
|
81
|
+
const body: Record<string, unknown> = parseJsonOrThrow(rawBody, "in-memory meta request body");
|
|
82
|
+
sent.push({ wamid, phoneNumberId, authorization, body });
|
|
83
|
+
return json({
|
|
84
|
+
messaging_product: "whatsapp",
|
|
85
|
+
contacts: [{ input: String(body["to"]), wa_id: String(body["to"]) }],
|
|
86
|
+
messages: [{ id: wamid }],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function answerMediaBlob(mediaId: string, hostname: string): Response {
|
|
91
|
+
const seeded = media.get(mediaId);
|
|
92
|
+
if (!seeded || seeded.host !== hostname) return graphError(404, 100);
|
|
93
|
+
return new Response(seeded.bytes, { headers: { "content-type": seeded.mimeType } });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function answerMediaMetadata(mediaId: string): Response {
|
|
97
|
+
const seeded = media.get(mediaId);
|
|
98
|
+
if (!seeded) return graphError(404, 100);
|
|
99
|
+
return json({
|
|
100
|
+
id: mediaId,
|
|
101
|
+
url: `https://${seeded.host}/media/${mediaId}`,
|
|
102
|
+
mime_type: seeded.mimeType,
|
|
103
|
+
file_size: seeded.bytes.byteLength,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const transport: WhatsappTransport = async (rawUrl, init) => {
|
|
108
|
+
const url = new URL(rawUrl);
|
|
109
|
+
const authorization = new Headers(init.headers).get("authorization");
|
|
110
|
+
requests.push({ method: init.method ?? "GET", url: rawUrl, authorization });
|
|
111
|
+
|
|
112
|
+
const sendMatch = MESSAGES_PATH.exec(url.pathname);
|
|
113
|
+
if (init.method === "POST" && sendMatch) {
|
|
114
|
+
return answerSend(sendMatch[1] ?? "", authorization, String(init.body));
|
|
115
|
+
}
|
|
116
|
+
const blobMatch = MEDIA_BLOB_PATH.exec(url.pathname);
|
|
117
|
+
if (init.method === "GET" && blobMatch)
|
|
118
|
+
return answerMediaBlob(blobMatch[1] ?? "", url.hostname);
|
|
119
|
+
const metaMatch = MEDIA_META_PATH.exec(url.pathname);
|
|
120
|
+
if (init.method === "GET" && metaMatch) return answerMediaMetadata(metaMatch[1] ?? "");
|
|
121
|
+
return graphError(404, 100);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
transport,
|
|
126
|
+
requests,
|
|
127
|
+
sent,
|
|
128
|
+
seedMedia(mediaId, seeded) {
|
|
129
|
+
media.set(mediaId, { host: DEFAULT_MEDIA_HOST, ...seeded });
|
|
130
|
+
},
|
|
131
|
+
rejectNextSend(rejection) {
|
|
132
|
+
nextRejection = rejection;
|
|
133
|
+
},
|
|
134
|
+
reset() {
|
|
135
|
+
requests.length = 0;
|
|
136
|
+
sent.length = 0;
|
|
137
|
+
media.clear();
|
|
138
|
+
nextRejection = undefined;
|
|
139
|
+
counter = 0;
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// --- Webhooks in Meta's format -------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
export type MetaWebhookMessage = Readonly<Record<string, unknown>>;
|
|
147
|
+
|
|
148
|
+
export function metaTextMessage(input: {
|
|
149
|
+
readonly from: string;
|
|
150
|
+
readonly wamid: string;
|
|
151
|
+
readonly text: string;
|
|
152
|
+
readonly timestamp?: string;
|
|
153
|
+
readonly contextWamid?: string;
|
|
154
|
+
}): MetaWebhookMessage {
|
|
155
|
+
return {
|
|
156
|
+
from: input.from,
|
|
157
|
+
id: input.wamid,
|
|
158
|
+
timestamp: input.timestamp ?? "1762300800",
|
|
159
|
+
type: "text",
|
|
160
|
+
text: { body: input.text },
|
|
161
|
+
...(input.contextWamid !== undefined && {
|
|
162
|
+
context: { from: "491700000000", id: input.contextWamid },
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function metaButtonReplyMessage(input: {
|
|
168
|
+
readonly from: string;
|
|
169
|
+
readonly wamid: string;
|
|
170
|
+
readonly payload: string;
|
|
171
|
+
readonly title: string;
|
|
172
|
+
readonly contextWamid: string;
|
|
173
|
+
readonly timestamp?: string;
|
|
174
|
+
}): MetaWebhookMessage {
|
|
175
|
+
return {
|
|
176
|
+
from: input.from,
|
|
177
|
+
id: input.wamid,
|
|
178
|
+
timestamp: input.timestamp ?? "1762300800",
|
|
179
|
+
type: "interactive",
|
|
180
|
+
context: { from: "491700000000", id: input.contextWamid },
|
|
181
|
+
interactive: { type: "button_reply", button_reply: { id: input.payload, title: input.title } },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function metaMediaMessage(input: {
|
|
186
|
+
readonly from: string;
|
|
187
|
+
readonly wamid: string;
|
|
188
|
+
readonly kind: "audio" | "image";
|
|
189
|
+
readonly mediaId: string;
|
|
190
|
+
readonly mimeType: string;
|
|
191
|
+
readonly timestamp?: string;
|
|
192
|
+
}): MetaWebhookMessage {
|
|
193
|
+
return {
|
|
194
|
+
from: input.from,
|
|
195
|
+
id: input.wamid,
|
|
196
|
+
timestamp: input.timestamp ?? "1762300800",
|
|
197
|
+
type: input.kind,
|
|
198
|
+
[input.kind]: { id: input.mediaId, mime_type: input.mimeType },
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function metaStatus(input: {
|
|
203
|
+
readonly wamid: string;
|
|
204
|
+
readonly status: string;
|
|
205
|
+
readonly recipient: string;
|
|
206
|
+
readonly timestamp?: string;
|
|
207
|
+
readonly errorCode?: number;
|
|
208
|
+
}): Readonly<Record<string, unknown>> {
|
|
209
|
+
return {
|
|
210
|
+
id: input.wamid,
|
|
211
|
+
status: input.status,
|
|
212
|
+
timestamp: input.timestamp ?? "1762300800",
|
|
213
|
+
recipient_id: input.recipient,
|
|
214
|
+
...(input.errorCode !== undefined && { errors: [{ code: input.errorCode, title: "failed" }] }),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function metaWebhookPayload(input: {
|
|
219
|
+
readonly phoneNumberId: string;
|
|
220
|
+
readonly messages?: readonly MetaWebhookMessage[];
|
|
221
|
+
readonly statuses?: readonly Readonly<Record<string, unknown>>[];
|
|
222
|
+
}): Readonly<Record<string, unknown>> {
|
|
223
|
+
return {
|
|
224
|
+
object: "whatsapp_business_account",
|
|
225
|
+
entry: [
|
|
226
|
+
{
|
|
227
|
+
id: "100000000000001",
|
|
228
|
+
changes: [
|
|
229
|
+
{
|
|
230
|
+
field: "messages",
|
|
231
|
+
value: {
|
|
232
|
+
messaging_product: "whatsapp",
|
|
233
|
+
metadata: {
|
|
234
|
+
display_phone_number: "491700000000",
|
|
235
|
+
phone_number_id: input.phoneNumberId,
|
|
236
|
+
},
|
|
237
|
+
...(input.messages && { messages: input.messages }),
|
|
238
|
+
...(input.statuses && { statuses: input.statuses }),
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
},
|
|
243
|
+
],
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export type WebhookTarget = {
|
|
248
|
+
request(input: string, init?: RequestInit): Response | Promise<Response>;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/** Posts a payload to the mounted webhook route, signed with the app secret as Meta signs it. */
|
|
252
|
+
export function deliverMetaWebhook(
|
|
253
|
+
app: WebhookTarget,
|
|
254
|
+
input: {
|
|
255
|
+
readonly tenantId: string;
|
|
256
|
+
readonly appSecret: string;
|
|
257
|
+
readonly payload: unknown;
|
|
258
|
+
readonly path?: string;
|
|
259
|
+
// Overrides the computed signature header (tests for a wrong or missing signature).
|
|
260
|
+
readonly signatureHeader?: string | null;
|
|
261
|
+
},
|
|
262
|
+
): Promise<Response> {
|
|
263
|
+
const rawBody = JSON.stringify(input.payload);
|
|
264
|
+
const signature =
|
|
265
|
+
input.signatureHeader === undefined
|
|
266
|
+
? signMetaBody(input.appSecret, rawBody)
|
|
267
|
+
: input.signatureHeader;
|
|
268
|
+
return Promise.resolve(
|
|
269
|
+
app.request(
|
|
270
|
+
(input.path ?? WHATSAPP_DEFAULT_WEBHOOK_PATH).replace(":tenantId", input.tenantId),
|
|
271
|
+
{
|
|
272
|
+
method: "POST",
|
|
273
|
+
body: rawBody,
|
|
274
|
+
headers: {
|
|
275
|
+
"content-type": "application/json",
|
|
276
|
+
...(signature !== null && { "x-hub-signature-256": signature }),
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
),
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Meta's subscription handshake against the mounted webhook route. */
|
|
284
|
+
export function requestMetaVerification(
|
|
285
|
+
app: WebhookTarget,
|
|
286
|
+
input: {
|
|
287
|
+
readonly tenantId: string;
|
|
288
|
+
readonly verifyToken: string;
|
|
289
|
+
readonly challenge: string;
|
|
290
|
+
readonly path?: string;
|
|
291
|
+
},
|
|
292
|
+
): Promise<Response> {
|
|
293
|
+
const query = new URLSearchParams({
|
|
294
|
+
"hub.mode": "subscribe",
|
|
295
|
+
"hub.verify_token": input.verifyToken,
|
|
296
|
+
"hub.challenge": input.challenge,
|
|
297
|
+
});
|
|
298
|
+
const path = (input.path ?? WHATSAPP_DEFAULT_WEBHOOK_PATH).replace(":tenantId", input.tenantId);
|
|
299
|
+
return Promise.resolve(app.request(`${path}?${query.toString()}`));
|
|
300
|
+
}
|