@primitivedotdev/sdk 1.7.0 → 1.9.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.
@@ -1,5 +1,5 @@
1
- import { I as WebhookEvent, P as ValidateEmailAuthResult, d as EmailAuth, f as EmailReceivedEvent } from "./types-QT2ss9ho.js";
2
- import { m as ReceivedEmail, u as WebhookValidationError } from "./errors-DyuAXctD.js";
1
+ import { I as WebhookEvent, P as ValidateEmailAuthResult, d as EmailAuth, f as EmailReceivedEvent } from "./types-BjnIxPED.js";
2
+ import { m as ReceivedEmail, u as WebhookValidationError } from "./errors-bXUNXAlf.js";
3
3
 
4
4
  //#region src/validation.d.ts
5
5
  interface ValidationSuccess<T> {
@@ -1454,20 +1454,7 @@ declare const WEBHOOK_VERSION = "2025-12-14";
1454
1454
  * }
1455
1455
  * ```
1456
1456
  */
1457
- declare function parseWebhookEvent(input: unknown): WebhookEvent;
1458
- /**
1459
- * Type guard to check if a webhook event is an EmailReceivedEvent.
1460
- *
1461
- * @example
1462
- * ```typescript
1463
- * const event = parseWebhookEvent(payload);
1464
- * if (isEmailReceivedEvent(event)) {
1465
- * // TypeScript knows event is EmailReceivedEvent
1466
- * console.log(event.email.headers.subject);
1467
- * }
1468
- * ```
1469
- */
1470
- declare function isEmailReceivedEvent(event: WebhookEvent | unknown): event is EmailReceivedEvent;
1457
+ declare function parseWebhookEvent(input: unknown, eventType?: string | null): WebhookEvent;
1471
1458
  /**
1472
1459
  * Request headers in any common format.
1473
1460
  *
@@ -1505,44 +1492,39 @@ interface HandleWebhookOptions {
1505
1492
  toleranceSeconds?: number;
1506
1493
  }
1507
1494
  /**
1508
- * Verify, parse, and validate a webhook in one call.
1495
+ * The header that names the webhook event for ALL event families
1496
+ * (`email.*`, `payment.*`, `interaction.*`). It is the primary discriminator
1497
+ * the parser keys on, because the stored body is sent verbatim with no envelope.
1498
+ */
1499
+ declare const WEBHOOK_EVENT_HEADER = "X-Webhook-Event";
1500
+ /**
1501
+ * Read the `X-Webhook-Event` header value (case-insensitive). Returns null when
1502
+ * the header is absent.
1503
+ */
1504
+ declare function getEventHeader(headers: WebhookHeaders): string | null;
1505
+ /**
1506
+ * Verify, then parse any webhook event into a typed value.
1509
1507
  *
1510
- * This is the recommended way to handle Primitive webhooks. It:
1511
- * 1. Verifies the signature to ensure the webhook is authentic
1512
- * 2. Parses the JSON body
1513
- * 3. Validates the payload against the canonical JSON schema
1514
- * 4. Returns a fully typed EmailReceivedEvent
1508
+ * Unlike {@link handleWebhook}, this returns the full {@link WebhookEvent}
1509
+ * union, so it handles `payment.*` and `interaction.x402.*` events in addition
1510
+ * to `email.*`. The flow is:
1515
1511
  *
1516
- * @param options - The webhook data and secret
1517
- * @returns A validated EmailReceivedEvent
1518
- * @throws {WebhookVerificationError} If signature verification fails
1519
- * @throws {WebhookPayloadError} If JSON parsing fails
1520
- * @throws {WebhookValidationError} If schema validation fails
1512
+ * 1. Verify the signature over the RAW body (works for every event family).
1513
+ * 2. Parse the JSON body.
1514
+ * 3. Classify on the `X-Webhook-Event` HEADER (the primary discriminator),
1515
+ * returning a typed event for known types and an UnknownEvent for the rest.
1521
1516
  *
1522
1517
  * @example
1523
1518
  * ```typescript
1524
- * import { handleWebhook, PrimitiveWebhookError } from '@primitivedotdev/sdk';
1525
- *
1526
- * app.post('/webhooks/email', express.raw({ type: 'application/json' }), (req, res) => {
1527
- * try {
1528
- * const event = handleWebhook({
1529
- * body: req.body,
1530
- * headers: req.headers,
1531
- * secret: process.env.PRIMITIVE_WEBHOOK_SECRET,
1532
- * });
1533
- *
1534
- * console.log('Email from:', event.email.headers.from);
1535
- * res.json({ received: true });
1536
- * } catch (err) {
1537
- * if (err instanceof PrimitiveWebhookError) {
1538
- * console.error(`[${err.code}] ${err.message}`);
1539
- * return res.status(400).json({ error: err.code });
1540
- * }
1541
- * throw err;
1542
- * }
1543
- * });
1519
+ * const event = handleWebhookEvent({ body, headers, secret });
1520
+ * if (isPaymentSettledEvent(event)) {
1521
+ * // typed PaymentSettledEvent
1522
+ * } else if (isInteractionX402Event(event)) {
1523
+ * // typed interaction.x402.* event
1524
+ * }
1544
1525
  * ```
1545
1526
  */
1527
+ declare function handleWebhookEvent(options: HandleWebhookOptions): WebhookEvent;
1546
1528
  declare function handleWebhook(options: HandleWebhookOptions): EmailReceivedEvent;
1547
1529
  interface ReceiveRequestOptions {
1548
1530
  secret: string;
@@ -1706,4 +1688,4 @@ declare function decodeRawEmail(event: EmailReceivedEvent, options?: DecodeRawEm
1706
1688
  */
1707
1689
  declare function verifyRawEmailDownload(downloaded: Buffer | ArrayBuffer | Uint8Array, event: EmailReceivedEvent): Buffer;
1708
1690
  //#endregion
1709
- export { VerifyOptions as A, signStandardWebhooksPayload as C, PRIMITIVE_CONFIRMED_HEADER as D, LEGACY_SIGNATURE_HEADER as E, VerifyDownloadTokenResult as F, generateDownloadToken as I, verifyDownloadToken as L, verifyWebhookSignature as M, GenerateDownloadTokenOptions as N, PRIMITIVE_SIGNATURE_HEADER as O, VerifyDownloadTokenOptions as P, safeValidateEmailReceivedEvent as R, StandardWebhooksVerifyOptions as S, LEGACY_CONFIRMED_HEADER as T, emailReceivedEventJsonSchema as _, confirmedHeaders as a, STANDARD_WEBHOOK_TIMESTAMP_HEADER as b, handleWebhook as c, isRawIncluded as d, parseWebhookEvent as f, validateEmailAuth as g, WEBHOOK_VERSION as h, WebhookHeaders as i, signWebhookPayload as j, SignResult as k, isDownloadExpired as l, verifyRawEmailDownload as m, HandleWebhookOptions as n, decodeRawEmail as o, receive as p, ReceiveRequestOptions as r, getDownloadTimeRemaining as s, DecodeRawEmailOptions as t, isEmailReceivedEvent as u, STANDARD_WEBHOOK_ID_HEADER as v, verifyStandardWebhooksSignature as w, StandardWebhooksSignResult as x, STANDARD_WEBHOOK_SIGNATURE_HEADER as y, validateEmailReceivedEvent as z };
1691
+ export { PRIMITIVE_SIGNATURE_HEADER as A, safeValidateEmailReceivedEvent as B, StandardWebhooksSignResult as C, LEGACY_CONFIRMED_HEADER as D, verifyStandardWebhooksSignature as E, GenerateDownloadTokenOptions as F, VerifyDownloadTokenOptions as I, VerifyDownloadTokenResult as L, VerifyOptions as M, signWebhookPayload as N, LEGACY_SIGNATURE_HEADER as O, verifyWebhookSignature as P, generateDownloadToken as R, STANDARD_WEBHOOK_TIMESTAMP_HEADER as S, signStandardWebhooksPayload as T, validateEmailReceivedEvent as V, WEBHOOK_VERSION as _, WebhookHeaders as a, STANDARD_WEBHOOK_ID_HEADER as b, getDownloadTimeRemaining as c, handleWebhookEvent as d, isDownloadExpired as f, verifyRawEmailDownload as g, receive as h, WEBHOOK_EVENT_HEADER as i, SignResult as j, PRIMITIVE_CONFIRMED_HEADER as k, getEventHeader as l, parseWebhookEvent as m, HandleWebhookOptions as n, confirmedHeaders as o, isRawIncluded as p, ReceiveRequestOptions as r, decodeRawEmail as s, DecodeRawEmailOptions as t, handleWebhook as u, validateEmailAuth as v, StandardWebhooksVerifyOptions as w, STANDARD_WEBHOOK_SIGNATURE_HEADER as x, emailReceivedEventJsonSchema as y, verifyDownloadToken as z };