@usebecky/extension 0.0.0-stage → 0.2.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.
Files changed (65) hide show
  1. package/README.md +223 -2
  2. package/dist/accounts.d.ts +85 -0
  3. package/dist/accounts.js +149 -0
  4. package/dist/cli/index.d.ts +2 -0
  5. package/dist/cli/index.js +9 -0
  6. package/dist/cli/run.d.ts +25 -0
  7. package/dist/cli/run.js +330 -0
  8. package/dist/cli/skill.d.ts +16 -0
  9. package/dist/cli/skill.js +106 -0
  10. package/dist/crypto.d.ts +12 -0
  11. package/dist/crypto.js +56 -0
  12. package/dist/dev/pages.d.ts +155 -0
  13. package/dist/dev/pages.js +243 -0
  14. package/dist/dev/plans.d.ts +45 -0
  15. package/dist/dev/plans.js +135 -0
  16. package/dist/dev/samples.d.ts +61 -0
  17. package/dist/dev/samples.js +191 -0
  18. package/dist/dev/server.d.ts +120 -0
  19. package/dist/dev/server.js +1657 -0
  20. package/dist/dev/tagging.d.ts +16 -0
  21. package/dist/dev/tagging.js +81 -0
  22. package/dist/dev/ui-render.d.ts +25 -0
  23. package/dist/dev/ui-render.js +256 -0
  24. package/dist/dev/wordmark.d.ts +1 -0
  25. package/dist/dev/wordmark.js +2 -0
  26. package/dist/errors.d.ts +98 -0
  27. package/dist/errors.js +110 -0
  28. package/dist/events.d.ts +92 -0
  29. package/dist/events.js +54 -0
  30. package/dist/extension.d.ts +214 -0
  31. package/dist/extension.js +497 -0
  32. package/dist/handlers.d.ts +39 -0
  33. package/dist/handlers.js +113 -0
  34. package/dist/identity.d.ts +99 -0
  35. package/dist/identity.js +134 -0
  36. package/dist/index.d.ts +14 -0
  37. package/dist/index.js +17 -0
  38. package/dist/jwt.d.ts +62 -0
  39. package/dist/jwt.js +152 -0
  40. package/dist/manifest.d.ts +42 -0
  41. package/dist/manifest.js +785 -0
  42. package/dist/node.d.ts +10 -0
  43. package/dist/node.js +56 -0
  44. package/dist/pkce.d.ts +9 -0
  45. package/dist/pkce.js +16 -0
  46. package/dist/stores.d.ts +100 -0
  47. package/dist/stores.js +159 -0
  48. package/dist/types.d.ts +346 -0
  49. package/dist/types.js +70 -0
  50. package/dist/ui/handler.d.ts +66 -0
  51. package/dist/ui/handler.js +105 -0
  52. package/dist/ui/schema.d.ts +423 -0
  53. package/dist/ui/schema.js +666 -0
  54. package/dist/webhooks.d.ts +26 -0
  55. package/dist/webhooks.js +99 -0
  56. package/package.json +57 -4
  57. package/skill/usebecky-extension/SKILL.md +181 -0
  58. package/skill/usebecky-extension/references/accounts.md +125 -0
  59. package/skill/usebecky-extension/references/dev-server.md +102 -0
  60. package/skill/usebecky-extension/references/manifest.md +104 -0
  61. package/skill/usebecky-extension/references/oauth-and-tokens.md +179 -0
  62. package/skill/usebecky-extension/references/partner-api.md +87 -0
  63. package/skill/usebecky-extension/references/submit-and-pricing.md +31 -0
  64. package/skill/usebecky-extension/references/ui.md +172 -0
  65. package/skill/usebecky-extension/references/webhooks.md +97 -0
@@ -0,0 +1,97 @@
1
+ # Webhooks
2
+
3
+ usebecky POSTs JSON to your `webhook_url`, signed with your webhook signing secret (`whsec_...`):
4
+
5
+ ```text
6
+ usebecky-signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>
7
+ usebecky-event-id: evt_...
8
+ usebecky-event-type: extension.installed
9
+ user-agent: usebecky-webhooks/1
10
+ ```
11
+
12
+ The kit checks the signature in constant time, refuses timestamps more than 5 minutes off (replays), accepts several `v1` values and several secrets (`webhookSecret: [newSecret, oldSecret]` while you rotate yours with **Rotate secrets**). Never verify by hand. usebecky never follows redirects: answer at the exact `webhook_url`.
13
+
14
+ ## Events
15
+
16
+ Every body is `{ id, type, created_at, project, data }`. `project` is the project ref: the key of everything you store.
17
+
18
+ | Type | When | `data` |
19
+ |---|---|---|
20
+ | `extension.installed` | the install is usable: the customer allowed access, filled in the settings, or paid | `{ extension, installation_id, project: { ref, name }, scopes, settings, plan }` |
21
+ | `extension.uninstalled` | the customer disconnected it, or its subscription ended; its tokens are already revoked. The last event for that install | `{ extension, installation_id, project: { ref, name }, scopes, reason: 'customer' \| 'subscription_ended' }` |
22
+ | `extension.plan_changed` | a paid install's plan changed its `id`, `status`, `cancel_at_period_end`, `next_plan` or discount (upgrade, downgrade scheduled or taking over, trial ended, payment failed or recovered, set to end, a discount code applied or a discount ended) | `{ extension, installation_id, project, scopes, previous, plan }` |
23
+ | usebecky events you list in `events` (needs `events:read`) | e.g. `user.created`, `purchase.completed`, `form.submitted` | the same data as the project's own webhooks |
24
+
25
+ `plan` is the install's price plan (`Plan`: `{ id, name, amount, currency, status, trial_ends_at, current_period_end, cancel_at_period_end, next_plan, discount }`), `null` for a free extension; see `submit-and-pricing.md`. `settings` holds the setup fields without secrets (fields mode; `{}` for oauth extensions). A yes/no (`boolean`) field is `true` or `false`; treat a missing key as `false`.
26
+
27
+ **Test events** carry `"test": true` (top level, next to `id` and `type`); real projects' events have no `test` field.
28
+
29
+ - Every event of an install in a **test project** (a developer's free project for trying extensions). Handle them like real ones, so testing shows your extension working, but never bill or count them: payments there are simulated. The dev server sends these when started with `--test-project`.
30
+ - **Send test event** in the Developer area (and `npx @usebecky/extension send` without a dev server): the project `sampleproject`, installation id `00000000-0000-4000-8000-000000000000`. Nothing is installed there: store nothing for them.
31
+
32
+ ## The handler
33
+
34
+ ```ts
35
+ // src/webhooks.ts
36
+ import { createExtension, memoryTokenStore } from '@usebecky/extension'
37
+
38
+ const ext = createExtension({
39
+ clientId: process.env.USEBECKY_CLIENT_ID!,
40
+ clientSecret: process.env.USEBECKY_CLIENT_SECRET!,
41
+ webhookSecret: process.env.USEBECKY_WEBHOOK_SECRET,
42
+ redirectUri: `${process.env.PUBLIC_URL}/oauth/callback`,
43
+ tokenStore: memoryTokenStore(),
44
+ })
45
+ const seen = new Set<string>() // use your database in production
46
+
47
+ export const webhook = ext.webhookHandler({
48
+ 'extension.installed': async (event) => {
49
+ if (event.test && event.project === 'sampleproject') return // "Send test event": nothing is installed
50
+ const { project, settings } = event.data
51
+ console.log(`installed on ${project.name}; dark banner: ${settings.dark_banner === true}`)
52
+ },
53
+ 'extension.uninstalled': async (event) => {
54
+ // The kit already deleted the tokens. Delete your own data for event.project here.
55
+ console.log(`removed (${event.data.reason})`)
56
+ },
57
+ 'user.created': async (event) => {
58
+ if (seen.has(event.id)) return // the same event can arrive twice
59
+ seen.add(event.id)
60
+ },
61
+ })
62
+ ```
63
+
64
+ What the handler answers:
65
+
66
+ - **200** after your handler finished. Do the work first; usebecky then stops retrying.
67
+ - **400** when the signature, timestamp or body is wrong (your handler is not called).
68
+ - **500** when your handler throws: usebecky retries later (10 s, 1 min, 5 min, 30 min, 2 h). Make handlers safe to run twice.
69
+
70
+ Need the event yourself (a queue, another framework)? `const event = await ext.verifyWebhook(rawBody, request.headers)` throws `WebhookVerificationError` (`missing_signature`, `invalid_signature`, `stale_signature`, `invalid_payload`).
71
+
72
+ ## Raw bodies per framework
73
+
74
+ The signature covers the exact bytes. Give the kit the raw body, before any JSON parser:
75
+
76
+ | Framework | Do |
77
+ |---|---|
78
+ | Hono | `app.post('/webhooks/usebecky', (c) => webhook(c.req.raw))` and don't call `c.req.json()` before |
79
+ | Next.js App Router | `export const POST = webhook` in `app/webhooks/usebecky/route.ts` |
80
+ | Express | mount it **before** `app.use(express.json())`, or use `express.raw({ type: 'application/json' })` on that route, with `toNodeHandler(webhook)` |
81
+ | node:http | `toNodeHandler(webhook)(req, res)`: it reads the stream itself |
82
+
83
+ ```ts
84
+ // server.ts
85
+ import express from 'express'
86
+ import { toNodeHandler } from '@usebecky/extension'
87
+ import { webhook } from './src/webhooks.ts'
88
+
89
+ const app = express()
90
+ app.post('/webhooks/usebecky', express.raw({ type: 'application/json' }), toNodeHandler(webhook))
91
+ app.use(express.json()) // the rest of your routes
92
+ app.listen(3000)
93
+ ```
94
+
95
+ ## Uninstall: clean up
96
+
97
+ On `extension.uninstalled`, delete every row, file and cache entry you keep for `event.project`. The kit deletes the tokens by itself (only when `data.installation_id` is the install the tokens belong to, so a late retry never deletes a reinstall's tokens). Turn that off with `webhookHandler(handlers, { forgetOnUninstall: false })` if you need the tokens for something else (you don't: they no longer work).