@zap-studio/webhooks 0.4.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -28
- package/LICENSE +1 -1
- package/README.md +71 -139
- package/dist/errors.d.ts +16 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1 -2
- package/dist/router.d.ts +83 -25
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +125 -52
- package/dist/router.js.map +1 -1
- package/dist/types.d.ts +146 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/verify.d.ts +5 -1
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +26 -6
- package/dist/verify.js.map +1 -1
- package/package.json +10 -10
- package/dist/utils.d.ts +0 -18
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -24
- package/dist/utils.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [1.1.0]
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
`WebhookRouter` (and `createWebhookRouter(...)`) gain an optional `logger?: Logger` option (from `@zap-studio/logger`). When provided, it logs each delivery attempt and handler dispatch at `debug`, and verification failures and unmatched routes at `warn`. Omitting it keeps zero logging overhead. See [Logging](https://www.zapstudio.dev/webhooks/logging).
|
|
12
|
+
|
|
13
|
+
## [1.0.0]
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
`WebhookRouter`'s stateless private static methods (`runBeforeHooks`, `runAfterHooks`, `createHandlerEntry`, `parseRequestBody`, `validatePayload`, `executeHandler`) are now module-level functions in `router.ts`. Internal-only change; the public API (`WebhookRouter`, `createWebhookRouter`, `.register()`, `.handle()`) is unaffected.
|
|
18
|
+
|
|
19
|
+
HMAC signature verification decodes the incoming header's hex signature to bytes and compares it against the computed digest byte-for-byte, instead of hex-encoding the digest and comparing hex text. Behavior is unchanged for valid requests; this only affects internals (fewer bytes compared, and the header's hex is no longer case-normalized as text since decoding handles case natively).
|
|
20
|
+
|
|
21
|
+
`constantTimeEquals` moved from `utils.ts` into `verify.ts` (its only consumer) and now compares `Uint8Array`s (bytes) instead of strings. Still exported from `@zap-studio/webhooks` and `@zap-studio/webhooks/verify`.
|
|
22
|
+
|
|
23
|
+
### Removed
|
|
24
|
+
|
|
25
|
+
Removed the `./utils` subpath export.
|
|
26
|
+
|
|
27
|
+
## [0.4.0]
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
4
30
|
|
|
5
31
|
The custom `NormalizedRequest`/`NormalizedResponse` contract is gone. `router.handle` now takes a standard Web API `Request` and returns a standard `Response`, so the router plugs directly into fetch-native runtimes (Bun, Deno, Cloudflare Workers, Next.js route handlers, Hono) with no adapter layer.
|
|
6
32
|
|
|
7
|
-
Breaking changes
|
|
33
|
+
**Breaking changes:**
|
|
8
34
|
|
|
9
35
|
- `handle(req: NormalizedRequest): Promise<NormalizedResponse>` → `handle(request: Request): Promise<Response>`.
|
|
10
36
|
- Handlers receive `{ request, rawBody, path, payload }` (a `WebhookContext` plus the validated `payload`) and return a `Response` or `undefined` (default `200` `"ok"`). The `ack` helper is removed — use `Response.json(body, init)`.
|
|
@@ -14,73 +40,68 @@ Breaking changes:
|
|
|
14
40
|
|
|
15
41
|
Behavior kept: hook execution order, prefix semantics (default `/webhooks/`), exact-match routing, HMAC verification, and the `404`/`400`/`500` error body shapes. Unknown routes now return `404` without reading the request body.
|
|
16
42
|
|
|
17
|
-
##
|
|
43
|
+
## [0.3.0]
|
|
18
44
|
|
|
19
|
-
###
|
|
45
|
+
### Changed
|
|
20
46
|
|
|
21
47
|
`Adapter` and `BaseAdapter` are now generic over the framework request/response types (`Adapter<TReq, TRes>`, `BaseAdapter<TReq, TRes>`), replacing the previous per-method generics. The mapping members (`toNormalizedRequest`, `toFrameworkResponse`, `handleWebhook`) are now arrow properties, so custom adapters must override them with property syntax rather than method syntax.
|
|
22
48
|
|
|
23
|
-
Also: `register()` now returns `this`, error hooks always receive a real `Error` instance, and `rawBody` is typed as `Uint8Array`.
|
|
24
|
-
|
|
25
|
-
# @zap-studio/webhooks
|
|
49
|
+
Also: `register()` now returns `this`, error hooks always receive a real `Error` instance, and `rawBody` is typed as `Uint8Array`. Internal formatting and lint cleanup migrated to ultracite.
|
|
26
50
|
|
|
27
|
-
## 0.2.2
|
|
51
|
+
## [0.2.2]
|
|
28
52
|
|
|
29
|
-
###
|
|
53
|
+
### Changed
|
|
30
54
|
|
|
31
55
|
- Updated dependency `@zap-studio/validation` to `0.3.4`.
|
|
32
56
|
|
|
33
|
-
## 0.2.1
|
|
34
|
-
|
|
35
|
-
### Fixed
|
|
36
|
-
|
|
37
|
-
- 3a950dc: Preserve registered hook assignment types while keeping the schema-first router API unchanged.
|
|
57
|
+
## [0.2.1]
|
|
38
58
|
|
|
39
59
|
### Changed
|
|
40
60
|
|
|
41
61
|
- 5fa58b1: Reduced webhook router complexity by consolidating hook normalization and handler entry creation.
|
|
42
62
|
- 7004e9f: Allow explicit `undefined` in option handling, then follow with d707800 to remove redundant `| undefined` unions from public types.
|
|
43
63
|
- 9f31f87: Switched the package build to ESNext-aligned output and updated package tooling and publish metadata.
|
|
64
|
+
- Updated dependency `@zap-studio/validation` to `0.3.3`.
|
|
44
65
|
|
|
45
|
-
###
|
|
66
|
+
### Fixed
|
|
46
67
|
|
|
47
|
-
-
|
|
68
|
+
- 3a950dc: Preserve registered hook assignment types while keeping the schema-first router API unchanged.
|
|
48
69
|
|
|
49
|
-
## 0.2.0
|
|
70
|
+
## [0.2.0]
|
|
50
71
|
|
|
51
|
-
###
|
|
72
|
+
### Changed
|
|
52
73
|
|
|
53
74
|
- c686862: Switch `createHmacVerifier` to Web Crypto and standardize the verifier around string secrets.
|
|
54
75
|
|
|
55
76
|
This change removes the Node `crypto` dependency from the verifier path, keeps `req.rawBody` as `Uint8Array`, simplifies `createHmacVerifier` to take a string secret, and adds public `VerificationError` in `@zap-studio/webhooks/errors` for verifier setup and signature failures.
|
|
56
77
|
|
|
57
|
-
## 0.1.4
|
|
78
|
+
## [0.1.4]
|
|
58
79
|
|
|
59
|
-
###
|
|
80
|
+
### Changed
|
|
60
81
|
|
|
61
82
|
- e26293e: Updated dependencies.
|
|
62
83
|
- @zap-studio/validation@0.3.2
|
|
63
84
|
|
|
64
|
-
## 0.1.3
|
|
85
|
+
## [0.1.3]
|
|
65
86
|
|
|
66
|
-
###
|
|
87
|
+
### Changed
|
|
67
88
|
|
|
68
89
|
- 5ea3d3b: Updated dependencies.
|
|
69
90
|
- @zap-studio/validation@0.3.1
|
|
70
91
|
|
|
71
|
-
## 0.1.2
|
|
92
|
+
## [0.1.2]
|
|
72
93
|
|
|
73
|
-
###
|
|
94
|
+
### Fixed
|
|
74
95
|
|
|
75
96
|
- c209a27: Fix payload schema validation internals to use the current async `standardValidate` options API (`{ throwOnError: false }`), restoring typecheck compatibility after the validation helper signature update.
|
|
76
97
|
|
|
77
|
-
## 0.1.1
|
|
98
|
+
## [0.1.1]
|
|
78
99
|
|
|
79
|
-
###
|
|
100
|
+
### Changed
|
|
80
101
|
|
|
81
102
|
- f75b984: Updated dependency `@zap-studio/validation` to `0.3.0`.
|
|
82
103
|
|
|
83
|
-
## 0.1.0
|
|
104
|
+
## [0.1.0]
|
|
84
105
|
|
|
85
106
|
### Added
|
|
86
107
|
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -2,66 +2,53 @@
|
|
|
2
2
|
|
|
3
3
|
Schema-first, type-safe webhook routing built on the standard Web API [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) primitives, with runtime-agnostic signature verification support.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Full documentation: [zapstudio.dev/webhooks](https://www.zapstudio.dev/webhooks)
|
|
6
6
|
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
Webhook handlers usually repeat the same plumbing:
|
|
10
|
-
|
|
11
|
-
- verify request authenticity
|
|
12
|
-
- parse and validate payloads
|
|
13
|
-
- route by event path
|
|
14
|
-
- normalize success/error responses
|
|
15
|
-
|
|
16
|
-
`@zap-studio/webhooks` isolates that plumbing so your handler code stays focused on business logic.
|
|
17
|
-
|
|
18
|
-
Schemas are the source of truth, and payload types are inferred from them.
|
|
19
|
-
|
|
20
|
-
## Install
|
|
7
|
+
## Installation
|
|
21
8
|
|
|
22
9
|
```bash
|
|
23
10
|
npm install @zap-studio/webhooks
|
|
24
11
|
```
|
|
25
12
|
|
|
26
|
-
|
|
13
|
+
You also need a schema library that implements [Standard Schema](https://github.com/standard-schema/standard-schema), such as Zod, Valibot, or ArkType.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Web API native** — `handle(request: Request)` returns a `Response`, so the router plugs directly into Bun, Deno, Cloudflare Workers, Next.js route handlers, Hono, and any other fetch-compatible runtime.
|
|
18
|
+
- **Type-safe routing** — handler payload types are inferred from the route schema.
|
|
19
|
+
- **Standard Schema validation** — bring Zod, Valibot, ArkType, or any compatible library.
|
|
20
|
+
- **Signature verification** — built-in HMAC verifier with constant-time comparison, or plug in your own `verify` function.
|
|
21
|
+
- **Lifecycle hooks** — global `before`, `after`, and `onError` hooks for cross-cutting behavior.
|
|
22
|
+
- **Runtime-agnostic** — uses the Web Crypto API, not Node-specific APIs.
|
|
23
|
+
- **Optional logging** through `createWebhookRouter({ logger })` ([`@zap-studio/logger`](https://www.npmjs.com/package/@zap-studio/logger)) — omit it and there's zero logging overhead.
|
|
24
|
+
- **Tree-shakeable** — validation and hook-running internals are standalone functions; unused exports are dropped by any modern bundler.
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
27
|
|
|
28
28
|
```ts
|
|
29
|
+
import { ConsoleLogger } from "@zap-studio/logger";
|
|
29
30
|
import { createWebhookRouter } from "@zap-studio/webhooks";
|
|
30
31
|
import { z } from "zod";
|
|
31
32
|
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
});
|
|
33
|
+
const logger = new ConsoleLogger({ minLevel: "debug" });
|
|
34
|
+
const router = createWebhookRouter({ prefix: "/webhooks", logger });
|
|
35
35
|
|
|
36
36
|
router.register("/payments/succeeded", {
|
|
37
|
-
schema: z.object({
|
|
38
|
-
id: z.string(),
|
|
39
|
-
amount: z.number().positive(),
|
|
40
|
-
currency: z.string().length(3),
|
|
41
|
-
}),
|
|
37
|
+
schema: z.object({ id: z.string(), amount: z.number().positive() }),
|
|
42
38
|
handler: ({ payload }) => {
|
|
43
39
|
// payload is inferred from schema
|
|
44
|
-
return Response.json(
|
|
40
|
+
return Response.json({ processed: payload.id });
|
|
45
41
|
},
|
|
46
42
|
});
|
|
47
43
|
|
|
48
|
-
// Any fetch-compatible runtime: Bun, Deno, Cloudflare Workers, ...
|
|
49
44
|
export default {
|
|
50
45
|
fetch: (request: Request) => router.handle(request),
|
|
51
46
|
};
|
|
52
47
|
```
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
Handlers can return a `Response`, or `undefined` to let the router reply with its default `200` acknowledgement.
|
|
57
|
-
|
|
58
|
-
### Paths and the prefix
|
|
59
|
-
|
|
60
|
-
Routes are registered with a leading slash (`"/payments/succeeded"`) and matched relative to the router's `prefix` (default `"/webhooks"`, no trailing slash) — so the example above answers on `/webhooks/payments/succeeded`. Paths are normalized internally: missing leading slashes are added, trailing slashes stripped, and duplicate slashes collapsed, on both registered routes and incoming request URLs. Set `prefix: ""` (or `"/"`) to mount routes at the root.
|
|
61
|
-
|
|
62
|
-
## Runtime integration
|
|
49
|
+
## Web API Native
|
|
63
50
|
|
|
64
|
-
|
|
51
|
+
`handle(request: Request)` returns a `Response`, so the router plugs directly into any fetch-compatible runtime.
|
|
65
52
|
|
|
66
53
|
```ts
|
|
67
54
|
// Bun / Deno / Cloudflare Workers
|
|
@@ -74,150 +61,95 @@ export const POST = (request: Request) => router.handle(request);
|
|
|
74
61
|
app.all("/webhooks/*", (c) => router.handle(c.req.raw));
|
|
75
62
|
```
|
|
76
63
|
|
|
77
|
-
|
|
64
|
+
## Type-Safe Routing
|
|
78
65
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
Hooks, verifiers, and handlers all receive a context object instead of the raw request stream. The router reads the request body exactly once, so the exact bytes stay available for signature verification:
|
|
66
|
+
Handler payload types are inferred from the route schema.
|
|
82
67
|
|
|
83
68
|
```ts
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
69
|
+
router.register("/payments/succeeded", {
|
|
70
|
+
schema: z.object({ id: z.string(), amount: z.number() }),
|
|
71
|
+
handler: ({ payload }) => {
|
|
72
|
+
// payload.id: string, payload.amount: number — inferred from schema
|
|
73
|
+
return Response.json({ ok: true });
|
|
74
|
+
},
|
|
75
|
+
});
|
|
89
76
|
```
|
|
90
77
|
|
|
91
|
-
|
|
78
|
+
## Standard Schema Validation
|
|
92
79
|
|
|
93
|
-
|
|
80
|
+
Bring Zod, Valibot, ArkType, or any compatible library.
|
|
94
81
|
|
|
95
82
|
```ts
|
|
96
|
-
import { createHmacVerifier, createWebhookRouter } from "@zap-studio/webhooks";
|
|
97
83
|
import { z } from "zod";
|
|
84
|
+
// or: import * as v from "valibot"; import { type } from "arktype";
|
|
98
85
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
secret: process.env.GITHUB_WEBHOOK_SECRET!,
|
|
103
|
-
}),
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
router.register("/github/push", {
|
|
107
|
-
schema: z.object({
|
|
108
|
-
ref: z.string(),
|
|
109
|
-
repository: z.object({
|
|
110
|
-
full_name: z.string(),
|
|
111
|
-
}),
|
|
112
|
-
}),
|
|
113
|
-
handler: ({ payload }) => {
|
|
114
|
-
console.log(`[github] ${payload.repository.full_name} ${payload.ref}`);
|
|
115
|
-
return undefined; // default 200 "ok"
|
|
116
|
-
},
|
|
86
|
+
router.register("/event", {
|
|
87
|
+
schema: z.object({ id: z.string() }),
|
|
88
|
+
handler: ({ payload }) => Response.json(payload),
|
|
117
89
|
});
|
|
118
90
|
```
|
|
119
91
|
|
|
120
|
-
##
|
|
92
|
+
## Signature Verification
|
|
121
93
|
|
|
122
|
-
|
|
123
|
-
import Stripe from "stripe";
|
|
124
|
-
import { createWebhookRouter } from "@zap-studio/webhooks";
|
|
125
|
-
import { z } from "zod";
|
|
94
|
+
Built-in HMAC verifier with constant-time comparison, or plug in your own `verify` function.
|
|
126
95
|
|
|
127
|
-
|
|
96
|
+
```ts
|
|
97
|
+
import {
|
|
98
|
+
createHmacVerifier,
|
|
99
|
+
createWebhookRouter,
|
|
100
|
+
VerificationError,
|
|
101
|
+
} from "@zap-studio/webhooks";
|
|
128
102
|
|
|
129
103
|
const router = createWebhookRouter({
|
|
130
|
-
verify: ({
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new Error("Missing Stripe signature");
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
stripe.webhooks.constructEvent(
|
|
137
|
-
Buffer.from(rawBody),
|
|
138
|
-
signature,
|
|
139
|
-
process.env.STRIPE_WEBHOOK_SECRET!
|
|
140
|
-
);
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
router.register("/stripe/payment_intent.succeeded", {
|
|
145
|
-
schema: z.object({
|
|
146
|
-
id: z.string(),
|
|
147
|
-
object: z.literal("event"),
|
|
148
|
-
type: z.literal("payment_intent.succeeded"),
|
|
104
|
+
verify: createHmacVerifier({
|
|
105
|
+
headerName: "x-hub-signature-256",
|
|
106
|
+
secret: process.env.WEBHOOK_SECRET!,
|
|
149
107
|
}),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
108
|
+
onError: (error) => {
|
|
109
|
+
if (error instanceof VerificationError) {
|
|
110
|
+
return Response.json({ error: "invalid signature" }, { status: 401 });
|
|
111
|
+
}
|
|
153
112
|
},
|
|
154
113
|
});
|
|
155
114
|
```
|
|
156
115
|
|
|
157
|
-
## Lifecycle
|
|
116
|
+
## Lifecycle Hooks
|
|
158
117
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
- `before`: run logic before verify/validation/handler (logging, tracing, rate-limit checks)
|
|
162
|
-
- `after`: run logic after successful handler execution (metrics, audit logs)
|
|
163
|
-
- `onError`: map thrown errors to consistent responses and centralize error reporting
|
|
118
|
+
Global `before`, `after`, and `onError` hooks for cross-cutting behavior.
|
|
164
119
|
|
|
165
120
|
```ts
|
|
166
121
|
const router = createWebhookRouter({
|
|
167
|
-
before: (ctx) =>
|
|
168
|
-
|
|
169
|
-
},
|
|
170
|
-
after: (_ctx, response) => {
|
|
171
|
-
console.log("status", response.status);
|
|
172
|
-
},
|
|
122
|
+
before: (ctx) => console.log("incoming", ctx.path),
|
|
123
|
+
after: (_ctx, response) => console.log("status", response.status),
|
|
173
124
|
onError: (error) => Response.json({ error: error.message }, { status: 500 }),
|
|
174
125
|
});
|
|
175
126
|
```
|
|
176
127
|
|
|
177
|
-
|
|
128
|
+
## Runtime-Agnostic
|
|
129
|
+
|
|
130
|
+
Uses the Web Crypto API, not Node-specific APIs.
|
|
178
131
|
|
|
179
132
|
```ts
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
},
|
|
133
|
+
// Uses globalThis.crypto.subtle — no Node `crypto` import required
|
|
134
|
+
const verify = createHmacVerifier({
|
|
135
|
+
headerName: "x-hub-signature-256",
|
|
136
|
+
secret: process.env.WEBHOOK_SECRET!,
|
|
185
137
|
});
|
|
186
138
|
```
|
|
187
139
|
|
|
188
|
-
##
|
|
189
|
-
|
|
190
|
-
`@zap-studio/webhooks` exports `createHmacVerifier`, a small helper that builds a `verify` function for HMAC-signed webhook providers.
|
|
140
|
+
## Logging
|
|
191
141
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
- reads a signature from the header you choose
|
|
195
|
-
- computes an HMAC from `ctx.rawBody`
|
|
196
|
-
- compares signatures in constant time
|
|
197
|
-
- uses the Web Crypto API instead of Node `crypto`
|
|
198
|
-
- works across runtimes that provide `globalThis.crypto.subtle`
|
|
199
|
-
- expects a string secret
|
|
200
|
-
- throws `VerificationError` on verifier setup or signature failures
|
|
142
|
+
Pass a `logger?: Logger` from [`@zap-studio/logger`](https://www.npmjs.com/package/@zap-studio/logger) to `createWebhookRouter(...)` to observe delivery attempts, dispatch, verification failures, and unmatched routes. Omit it and nothing is logged.
|
|
201
143
|
|
|
202
144
|
```ts
|
|
203
|
-
import {
|
|
204
|
-
|
|
205
|
-
const verify = createHmacVerifier({
|
|
206
|
-
headerName: "x-hub-signature-256",
|
|
207
|
-
secret: process.env.WEBHOOK_SECRET!,
|
|
208
|
-
algo: "sha256", // optional, defaults to sha256
|
|
209
|
-
});
|
|
145
|
+
import { ConsoleLogger } from "@zap-studio/logger";
|
|
146
|
+
import { createWebhookRouter } from "@zap-studio/webhooks";
|
|
210
147
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
} catch (error) {
|
|
214
|
-
if (error instanceof VerificationError) {
|
|
215
|
-
console.error("webhook verification failed", error.message);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
148
|
+
const logger = new ConsoleLogger({ minLevel: "debug" });
|
|
149
|
+
const router = createWebhookRouter({ prefix: "/webhooks", logger });
|
|
218
150
|
```
|
|
219
151
|
|
|
220
|
-
|
|
152
|
+
Each delivery attempt and handler dispatch logs at `debug`; verification failures and unmatched routes log at `warn`.
|
|
221
153
|
|
|
222
154
|
## Runtime Support
|
|
223
155
|
|
package/dist/errors.d.ts
CHANGED
|
@@ -9,6 +9,22 @@
|
|
|
9
9
|
*
|
|
10
10
|
* This error is used by verifier helpers such as `createHmacVerifier` so
|
|
11
11
|
* callers can distinguish verification failures from other webhook errors.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { VerificationError } from "@zap-studio/webhooks";
|
|
16
|
+
*
|
|
17
|
+
* const response = await router.handle(request);
|
|
18
|
+
* // Verification failures surface as a 500 response by default, or via onError:
|
|
19
|
+
* const routerWithHandler = createWebhookRouter({
|
|
20
|
+
* verify: createHmacVerifier({ headerName: "x-signature", secret }),
|
|
21
|
+
* onError: (error) => {
|
|
22
|
+
* if (error instanceof VerificationError) {
|
|
23
|
+
* return Response.json({ error: error.message }, { status: 401 });
|
|
24
|
+
* }
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
12
28
|
*/
|
|
13
29
|
declare class VerificationError extends Error {
|
|
14
30
|
/**
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","names":[],"sources":["../src/errors.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.d.ts","names":[],"sources":["../src/errors.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;cA4Ba,0BAA0B;;;;;;EAMrC,YAAY"}
|
package/dist/errors.js
CHANGED
|
@@ -9,6 +9,22 @@
|
|
|
9
9
|
*
|
|
10
10
|
* This error is used by verifier helpers such as `createHmacVerifier` so
|
|
11
11
|
* callers can distinguish verification failures from other webhook errors.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { VerificationError } from "@zap-studio/webhooks";
|
|
16
|
+
*
|
|
17
|
+
* const response = await router.handle(request);
|
|
18
|
+
* // Verification failures surface as a 500 response by default, or via onError:
|
|
19
|
+
* const routerWithHandler = createWebhookRouter({
|
|
20
|
+
* verify: createHmacVerifier({ headerName: "x-signature", secret }),
|
|
21
|
+
* onError: (error) => {
|
|
22
|
+
* if (error instanceof VerificationError) {
|
|
23
|
+
* return Response.json({ error: error.message }, { status: 401 });
|
|
24
|
+
* }
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
12
28
|
*/
|
|
13
29
|
var VerificationError = class extends Error {
|
|
14
30
|
/**
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Error primitives for webhook verification failures.\n *\n * @module @zap-studio/webhooks/errors\n */\n\n/**\n * Error thrown when webhook request verification fails.\n *\n * This error is used by verifier helpers such as `createHmacVerifier` so\n * callers can distinguish verification failures from other webhook errors.\n */\nexport class VerificationError extends Error {\n /**\n * Creates a verification error with a human-readable message.\n *\n * @param message - Error message describing the verification failure.\n */\n constructor(message: string) {\n super(message);\n this.name = \"VerificationError\";\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Error primitives for webhook verification failures.\n *\n * @module @zap-studio/webhooks/errors\n */\n\n/**\n * Error thrown when webhook request verification fails.\n *\n * This error is used by verifier helpers such as `createHmacVerifier` so\n * callers can distinguish verification failures from other webhook errors.\n *\n * @example\n * ```ts\n * import { VerificationError } from \"@zap-studio/webhooks\";\n *\n * const response = await router.handle(request);\n * // Verification failures surface as a 500 response by default, or via onError:\n * const routerWithHandler = createWebhookRouter({\n * verify: createHmacVerifier({ headerName: \"x-signature\", secret }),\n * onError: (error) => {\n * if (error instanceof VerificationError) {\n * return Response.json({ error: error.message }, { status: 401 });\n * }\n * },\n * });\n * ```\n */\nexport class VerificationError extends Error {\n /**\n * Creates a verification error with a human-readable message.\n *\n * @param message - Error message describing the verification failure.\n */\n constructor(message: string) {\n super(message);\n this.name = \"VerificationError\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAa,oBAAb,cAAuC,MAAM;;;;;;CAM3C,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { VerificationError } from "./errors.js";
|
|
2
|
-
import { AfterHook, BeforeHook, ErrorHook, HandlerContext, HandlerMap, InferSchemaOutput, InferWebhookMapFromRoutes, RegisterOptions, SchemaRouteOptions, SchemaRoutes, VerifyFn, WebhookContext, WebhookHandler } from "./types.js";
|
|
3
|
-
import { WebhookRouter,
|
|
4
|
-
import { constantTimeEquals } from "./
|
|
5
|
-
import { createHmacVerifier } from "./verify.js";
|
|
2
|
+
import { AfterHook, BeforeHook, ErrorHook, HandlerContext, HandlerMap, InferSchemaOutput, InferWebhookMapFromRoutes, RegisterOptions, SchemaRouteOptions, SchemaRoutes, VerifyFn, WebhookContext, WebhookHandler, WebhookRouterOptions } from "./types.js";
|
|
3
|
+
import { WebhookRouter, createWebhookRouter } from "./router.js";
|
|
4
|
+
import { constantTimeEquals, createHmacVerifier } from "./verify.js";
|
|
6
5
|
export { type AfterHook, type BeforeHook, type ErrorHook, type HandlerContext, type HandlerMap, type InferSchemaOutput, type InferWebhookMapFromRoutes, type RegisterOptions, type SchemaRouteOptions, type SchemaRoutes, VerificationError, type VerifyFn, type WebhookContext, type WebhookHandler, WebhookRouter, type WebhookRouterOptions, constantTimeEquals, createHmacVerifier, createWebhookRouter };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { VerificationError } from "./errors.js";
|
|
2
2
|
import { WebhookRouter, createWebhookRouter } from "./router.js";
|
|
3
|
-
import { constantTimeEquals } from "./
|
|
4
|
-
import { createHmacVerifier } from "./verify.js";
|
|
3
|
+
import { constantTimeEquals, createHmacVerifier } from "./verify.js";
|
|
5
4
|
export { VerificationError, WebhookRouter, constantTimeEquals, createHmacVerifier, createWebhookRouter };
|