@sidekickdb/webhooks 2.0.1 → 3.0.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/.turbo/turbo-build.log +4 -4
- package/README.md +30 -26
- package/__tests__/src/signature.test.ts +1914 -0
- package/__tests__/src/validate.test.ts +5 -5
- package/dist/commonjs/__types__/typesTests.js +1 -1
- package/dist/commonjs/__types__/typesTests.js.map +1 -1
- package/dist/commonjs/index.d.ts +41 -37
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +316 -61
- package/dist/commonjs/index.js.map +1 -1
- package/dist/esm/__types__/typesTests.js +1 -1
- package/dist/esm/__types__/typesTests.js.map +1 -1
- package/dist/esm/index.d.ts +41 -37
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +317 -62
- package/dist/esm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__types__/typesTests.ts +1 -1
- package/src/index.ts +437 -97
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
|
|
2
|
-
> @sidekickdb/webhooks@
|
|
2
|
+
> @sidekickdb/webhooks@3.0.0 build /home/runner/work/sidekick/sidekick/client/packages/webhooks
|
|
3
3
|
> rm -rf dist; npm run build:tshy && npm run check-exports
|
|
4
4
|
|
|
5
5
|
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
6
6
|
|
|
7
|
-
> @sidekickdb/webhooks@
|
|
7
|
+
> @sidekickdb/webhooks@3.0.0 build:tshy
|
|
8
8
|
> tshy
|
|
9
9
|
|
|
10
10
|
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
11
11
|
|
|
12
|
-
> @sidekickdb/webhooks@
|
|
12
|
+
> @sidekickdb/webhooks@3.0.0 check-exports
|
|
13
13
|
> attw --pack .
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@sidekickdb/webhooks
|
|
16
|
+
@sidekickdb/webhooks v3.0.0
|
|
17
17
|
|
|
18
18
|
Build tools:
|
|
19
19
|
- @arethetypeswrong/cli@^0.18.5
|
package/README.md
CHANGED
|
@@ -20,15 +20,15 @@ The `Webhooks` class is re-exported from a few places. Use whichever fits your s
|
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
import { init } from '@sidekickdb/admin';
|
|
23
|
-
import schema from './
|
|
23
|
+
import schema from './sidekick.schema';
|
|
24
24
|
|
|
25
25
|
// adminToken is optional if you're only using the SDK for the receiving side
|
|
26
26
|
// of webhooks (processRequest / validate / etc.). Include it if you also want
|
|
27
27
|
// to use db.webhooks.manager.* — or any other admin-SDK feature that requires
|
|
28
28
|
// auth, like writes or impersonation.
|
|
29
29
|
const db = init({
|
|
30
|
-
appId: process.env.
|
|
31
|
-
apiURI: process.env.
|
|
30
|
+
appId: process.env.SIDEKICK_APP_ID!,
|
|
31
|
+
apiURI: process.env.SIDEKICK_API_URI!,
|
|
32
32
|
schema,
|
|
33
33
|
});
|
|
34
34
|
|
|
@@ -40,14 +40,14 @@ await db.webhooks.processRequest(handlers, req);
|
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
42
|
import { PlatformApi } from '@sidekickdb/platform';
|
|
43
|
-
import schema from './
|
|
43
|
+
import schema from './sidekick.schema';
|
|
44
44
|
|
|
45
45
|
// Auth is optional if you're only using PlatformApi for the receiving side of
|
|
46
46
|
// webhooks. Include it if you also want to use webhooks.manager.* (or any
|
|
47
47
|
// other platform-SDK feature). When provided, the platform SDK refreshes the
|
|
48
48
|
// OAuth token transparently for manager calls.
|
|
49
49
|
const api = new PlatformApi({
|
|
50
|
-
apiURI: process.env.
|
|
50
|
+
apiURI: process.env.SIDEKICK_API_URI!,
|
|
51
51
|
auth: { token: accessToken },
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -60,21 +60,21 @@ await webhooks.processRequest(handlers, req);
|
|
|
60
60
|
|
|
61
61
|
```ts
|
|
62
62
|
import { Webhooks } from '@sidekickdb/webhooks';
|
|
63
|
-
import schema from './
|
|
63
|
+
import schema from './sidekick.schema';
|
|
64
64
|
|
|
65
65
|
// For receiving only (validate / processRequest / processPayload), the
|
|
66
66
|
// adminToken is optional — you just need appId + schema for typed handlers.
|
|
67
67
|
const webhooks = new Webhooks<typeof schema>({
|
|
68
|
-
appId: process.env.
|
|
69
|
-
apiURI: process.env.
|
|
68
|
+
appId: process.env.SIDEKICK_APP_ID!,
|
|
69
|
+
apiURI: process.env.SIDEKICK_API_URI!,
|
|
70
70
|
schema,
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
// To also manage webhooks (manager.create, manager.list, …) pass adminToken.
|
|
74
74
|
const webhooksWithManager = new Webhooks<typeof schema>({
|
|
75
|
-
appId: process.env.
|
|
76
|
-
apiURI: process.env.
|
|
77
|
-
adminToken: process.env.
|
|
75
|
+
appId: process.env.SIDEKICK_APP_ID!,
|
|
76
|
+
apiURI: process.env.SIDEKICK_API_URI!,
|
|
77
|
+
adminToken: process.env.SIDEKICK_APP_ADMIN_TOKEN!,
|
|
78
78
|
schema,
|
|
79
79
|
});
|
|
80
80
|
```
|
|
@@ -95,20 +95,20 @@ The rest of this README uses `db.webhooks` in examples. The same methods exist o
|
|
|
95
95
|
|
|
96
96
|
The one-liner for handling an incoming webhook. It:
|
|
97
97
|
|
|
98
|
-
1. Verifies the `
|
|
98
|
+
1. Verifies the `Sidekick-Signature` header against the raw body.
|
|
99
99
|
2. Fetches the records via the short-lived `payloadUrl` + JWT in the body.
|
|
100
100
|
3. Dispatches each record to the matching handler.
|
|
101
101
|
|
|
102
102
|
Pass it a Web [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) (Next.js App Router, Cloudflare Workers, Deno, Bun, Hono, etc.):
|
|
103
103
|
|
|
104
104
|
```ts
|
|
105
|
-
// app/api/
|
|
105
|
+
// app/api/sidekick-webhook/route.ts
|
|
106
106
|
import { init } from '@sidekickdb/admin';
|
|
107
|
-
import schema from '@/
|
|
107
|
+
import schema from '@/sidekick.schema';
|
|
108
108
|
|
|
109
109
|
const db = init({
|
|
110
|
-
appId: process.env.
|
|
111
|
-
apiURI: process.env.
|
|
110
|
+
appId: process.env.SIDEKICK_APP_ID!,
|
|
111
|
+
apiURI: process.env.SIDEKICK_API_URI!,
|
|
112
112
|
schema,
|
|
113
113
|
});
|
|
114
114
|
|
|
@@ -134,8 +134,8 @@ export async function POST(req: Request) {
|
|
|
134
134
|
|
|
135
135
|
Options:
|
|
136
136
|
|
|
137
|
-
- `tolerance` —
|
|
138
|
-
- `receivedAt` — override the "now" used when checking signature
|
|
137
|
+
- `tolerance` — how far, in seconds, the signature timestamp may be before or after `receivedAt` (default `300`). Must be a finite number, `0` or more; anything else throws. Protects against replays; raising this weakens that.
|
|
138
|
+
- `receivedAt` — override the "now" used when checking the signature timestamp. Must be a valid `Date`; anything else throws, and leaving it out (or `null`) means now. Mostly useful in tests.
|
|
139
139
|
|
|
140
140
|
Handler resolution is most-specific-wins: `namespace` + `action` → that namespace's `$default` → top-level `$default`. Records with no matching handler are skipped.
|
|
141
141
|
|
|
@@ -150,7 +150,7 @@ The adapter picks up the raw body from, in order: `opts.body`, `req.rawBody`, `r
|
|
|
150
150
|
Next.js Pages Router:
|
|
151
151
|
|
|
152
152
|
```ts
|
|
153
|
-
// pages/api/
|
|
153
|
+
// pages/api/sidekick-webhook.ts
|
|
154
154
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
155
155
|
|
|
156
156
|
export const config = { api: { bodyParser: false } };
|
|
@@ -172,7 +172,7 @@ Express (use `express.raw` on the webhook route only):
|
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
174
|
app.post(
|
|
175
|
-
'/webhooks/
|
|
175
|
+
'/webhooks/sidekick',
|
|
176
176
|
express.raw({ type: 'application/json' }),
|
|
177
177
|
async (req, res) => {
|
|
178
178
|
try {
|
|
@@ -188,7 +188,7 @@ app.post(
|
|
|
188
188
|
Koa with `raw-body`:
|
|
189
189
|
|
|
190
190
|
```ts
|
|
191
|
-
router.post('/webhooks/
|
|
191
|
+
router.post('/webhooks/sidekick', async (ctx) => {
|
|
192
192
|
await db.webhooks.processNodeRequest(handlers, ctx.req, {
|
|
193
193
|
body: rawBody(ctx.req), // adapter awaits the Promise
|
|
194
194
|
});
|
|
@@ -201,7 +201,7 @@ NestJS (Express adapter, with `NestFactory.create(AppModule, { rawBody: true })`
|
|
|
201
201
|
```ts
|
|
202
202
|
@Controller('webhooks')
|
|
203
203
|
export class WebhooksController {
|
|
204
|
-
@Post('
|
|
204
|
+
@Post('sidekick')
|
|
205
205
|
@HttpCode(200)
|
|
206
206
|
async handle(@Req() req: Request) {
|
|
207
207
|
await db.webhooks.processNodeRequest(handlers, req);
|
|
@@ -225,7 +225,11 @@ Same resolution rules and concurrency model as `processRequest`: handlers run in
|
|
|
225
225
|
|
|
226
226
|
## `validate(signatureHeader, body, opts?)`
|
|
227
227
|
|
|
228
|
-
Parses
|
|
228
|
+
Parses a `Sidekick-Signature` header, verifies it against the body, and returns the `{ payloadUrl, token }` you'd use to fetch the records. Throws if the signature doesn't validate, its timestamp is not whole seconds or is more than `opts.tolerance` (default 300 seconds) before or after the time it was received, or the body isn't the expected shape.
|
|
229
|
+
|
|
230
|
+
<!-- SidekickDB: how signing keys are looked up. -->
|
|
231
|
+
|
|
232
|
+
The public signing keys come from your server's `/.well-known/webhooks/jwks.json`. A key is cached once it has been found there and checked against that set again 10 minutes after it was last confirmed: if the server no longer publishes it, it is dropped and the delivery is rejected. If the set cannot be fetched, the cached key keeps working for 6 hours after its last confirmation, then deliveries signed with it are rejected until a fetch succeeds. For a key id that is not cached, a key set fetched successfully in the last minute is reused, so after the server starts signing with a new key, deliveries signed with it can fail for up to a minute. SidekickDB retries a failed delivery after a minute or more, so the retry normally finds the new key. Fetches of the key set are limited per server; when the keys cannot be fetched (the server did not answer in time, or the limit was reached), validation throws an error ending in `try again later`, or the fetch error. In either case, answer with a non-2xx status other than `410` (a `410` stops deliveries to the webhook) and the delivery is retried.
|
|
229
233
|
|
|
230
234
|
```ts
|
|
231
235
|
const { payloadUrl, token } = await db.webhooks.validate(
|
|
@@ -239,7 +243,7 @@ const { payloadUrl, token } = await db.webhooks.validate(
|
|
|
239
243
|
|
|
240
244
|
## `validateRequest(req, opts?)`
|
|
241
245
|
|
|
242
|
-
Convenience wrapper around `validate`: pulls `
|
|
246
|
+
Convenience wrapper around `validate`: pulls `sidekick-signature` and the body off a Web `Request` for you.
|
|
243
247
|
|
|
244
248
|
```ts
|
|
245
249
|
const { payloadUrl, token } = await db.webhooks.validateRequest(req);
|
|
@@ -292,7 +296,7 @@ Creates a webhook in the `active` state. It starts receiving matching events imm
|
|
|
292
296
|
|
|
293
297
|
```ts
|
|
294
298
|
const webhook = await db.webhooks.manager.create({
|
|
295
|
-
url: 'https://example.com/
|
|
299
|
+
url: 'https://example.com/sidekick',
|
|
296
300
|
namespaces: ['posts', 'comments'],
|
|
297
301
|
actions: ['create', 'update'],
|
|
298
302
|
});
|
|
@@ -362,7 +366,7 @@ Each event's `attempts` array records, per attempt: HTTP status, response body (
|
|
|
362
366
|
|
|
363
367
|
## `manager.getEvent(webhookId, isn)`
|
|
364
368
|
|
|
365
|
-
Fetches a single event by its `isn` (
|
|
369
|
+
Fetches a single event by its `isn` (its sequence number).
|
|
366
370
|
|
|
367
371
|
```ts
|
|
368
372
|
const event = await db.webhooks.manager.getEvent(webhook.id, isn);
|