@usethrottle/webhook-types 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -15
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -10,39 +10,46 @@ npm install @usethrottle/webhook-types
|
|
|
10
10
|
|
|
11
11
|
```ts
|
|
12
12
|
import {
|
|
13
|
-
type
|
|
13
|
+
type ThrottleEvent,
|
|
14
|
+
type ThrottleEventEnvelope,
|
|
14
15
|
verifyWebhookSignature,
|
|
15
16
|
} from '@usethrottle/webhook-types';
|
|
16
17
|
|
|
17
18
|
export async function POST(req: Request) {
|
|
18
19
|
const rawBody = await req.text();
|
|
19
20
|
const ok = verifyWebhookSignature({
|
|
20
|
-
header: req.headers.get('
|
|
21
|
+
header: req.headers.get('X-Throttle-Signature')!,
|
|
21
22
|
rawBody,
|
|
22
23
|
secret: process.env.THROTTLE_WEBHOOK_SECRET!,
|
|
23
24
|
});
|
|
24
25
|
if (!ok) return new Response('bad signature', { status: 400 });
|
|
25
26
|
|
|
26
|
-
const envelope = JSON.parse(rawBody) as
|
|
27
|
-
const
|
|
27
|
+
const envelope = JSON.parse(rawBody) as ThrottleEvent;
|
|
28
|
+
const wireEnvelope: ThrottleEventEnvelope = envelope;
|
|
29
|
+
await audit.record({
|
|
30
|
+
eventId: wireEnvelope.id,
|
|
31
|
+
eventType: wireEnvelope.type,
|
|
32
|
+
eventVersion: wireEnvelope.version,
|
|
33
|
+
environmentId: wireEnvelope.environmentId,
|
|
34
|
+
});
|
|
28
35
|
|
|
29
|
-
//
|
|
30
|
-
switch (
|
|
36
|
+
// envelope.data is narrowed by envelope.type — no casts.
|
|
37
|
+
switch (envelope.type) {
|
|
31
38
|
case 'cart.item_added':
|
|
32
|
-
//
|
|
33
|
-
analytics.track('item_added',
|
|
39
|
+
// envelope.data: { cartId, sequence, itemId, name, quantity, unitPrice }
|
|
40
|
+
analytics.track('item_added', envelope.data);
|
|
34
41
|
break;
|
|
35
42
|
case 'order.created':
|
|
36
|
-
//
|
|
37
|
-
await fulfill(
|
|
43
|
+
// envelope.data: { order, fromCart?, sessionId? }
|
|
44
|
+
await fulfill(envelope.data.order as { id: string });
|
|
38
45
|
break;
|
|
39
46
|
case 'payment.disputed':
|
|
40
|
-
//
|
|
41
|
-
await ops.alert(
|
|
47
|
+
// envelope.data: { paymentId, reason, openedAt }
|
|
48
|
+
await ops.alert(envelope.data.paymentId, envelope.data.reason);
|
|
42
49
|
break;
|
|
43
50
|
case 'workspace.payment_method.backup_used':
|
|
44
|
-
//
|
|
45
|
-
await notifyOps('backup PM used',
|
|
51
|
+
// envelope.data: { workspace, primaryPaymentMethod, backupPaymentMethod, charge }
|
|
52
|
+
await notifyOps('backup PM used', envelope.data);
|
|
46
53
|
break;
|
|
47
54
|
}
|
|
48
55
|
|
|
@@ -52,7 +59,9 @@ export async function POST(req: Request) {
|
|
|
52
59
|
|
|
53
60
|
## Coverage
|
|
54
61
|
|
|
55
|
-
- **
|
|
62
|
+
- **ThrottleEventEnvelope** — top-level wire envelope with `id`, `type`, `version`, `workspaceId`, `environmentId`, `createdAt`, and typed `data`.
|
|
63
|
+
- **ThrottleEvent** — discriminated union of all `ThrottleEventEnvelope` variants currently emitted by the Throttle outbound bus (cart, order, payment, fulfillment, subscription, discount, customer, invoice, shipping/tax, workspace lifecycle).
|
|
64
|
+
- **WebhookEvent** — legacy nested event union retained for internal/back-compat consumers.
|
|
56
65
|
- **WebhookEventType** — `WebhookEvent['type']`; use it to constrain switch statements or event-name handlers.
|
|
57
66
|
- **Typed payloads** — primitive fields (ids, amounts, codes, timestamps) are fully typed. Embedded domain objects (`order`, `payment`, `subscription`, `workspace`, etc.) are typed as `Record<string, unknown>` because the canonical schemas live in the API server and shouldn't be duplicated here. Cast through the OpenAPI client (`@usethrottle/api-client`) for the embedded shapes.
|
|
58
67
|
- **verifyWebhookSignature** — Stripe-compatible `t=…,v1=…` header parsing with timing-safe HMAC-SHA256 comparison + 5-minute replay window by default.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usethrottle/webhook-types",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "TypeScript discriminated unions for every Throttle outbound webhook event.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,21 +10,21 @@
|
|
|
10
10
|
"dist",
|
|
11
11
|
"README.md"
|
|
12
12
|
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "tsc",
|
|
15
|
-
"dev": "tsc --watch",
|
|
16
|
-
"test": "tsc --noEmit && vitest run --passWithNoTests"
|
|
17
|
-
},
|
|
18
13
|
"devDependencies": {
|
|
19
|
-
"@core/scopes": "workspace:*",
|
|
20
14
|
"@types/node": "^20.0.0",
|
|
21
15
|
"typescript": "^5.7.0",
|
|
22
|
-
"vitest": "^2.1.9"
|
|
16
|
+
"vitest": "^2.1.9",
|
|
17
|
+
"@core/scopes": "0.0.1"
|
|
23
18
|
},
|
|
24
19
|
"publishConfig": {
|
|
25
20
|
"access": "public"
|
|
26
21
|
},
|
|
27
22
|
"engines": {
|
|
28
23
|
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"dev": "tsc --watch",
|
|
28
|
+
"test": "tsc --noEmit && vitest run --passWithNoTests"
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
}
|