@palbase/backend 2.0.2 → 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/dist/{chunk-4J3F32SH.js → chunk-B7EUJP5W.js} +38 -9
- package/dist/chunk-B7EUJP5W.js.map +1 -0
- package/dist/{chunk-L36JLUPO.js → chunk-PHAFZGHN.js} +43 -46
- package/dist/chunk-PHAFZGHN.js.map +1 -0
- package/dist/db/env.cjs +19 -0
- package/dist/db/env.cjs.map +1 -0
- package/dist/db/env.d.cts +45 -0
- package/dist/db/env.d.ts +45 -0
- package/dist/db/env.js +1 -0
- package/dist/db/env.js.map +1 -0
- package/dist/db/index.cjs +28 -231
- package/dist/db/index.cjs.map +1 -1
- package/dist/db/index.d.cts +4 -20
- package/dist/db/index.d.ts +4 -20
- package/dist/db/index.js +3 -233
- package/dist/db/index.js.map +1 -1
- package/dist/{endpoint-Djk5L6G2.d.ts → endpoint-DJ98tQd6.d.cts} +30 -68
- package/dist/{endpoint-BlcY2xNA.d.cts → endpoint-DJ98tQd6.d.ts} +30 -68
- package/dist/index-CXUs9iTQ.d.ts +294 -0
- package/dist/index-CZAwpQE1.d.cts +294 -0
- package/dist/index.cjs +229 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +398 -154
- package/dist/index.d.ts +398 -154
- package/dist/index.js +147 -12
- package/dist/index.js.map +1 -1
- package/dist/test/index.cjs +41 -1
- package/dist/test/index.cjs.map +1 -1
- package/dist/test/index.d.cts +1 -2
- package/dist/test/index.d.ts +1 -2
- package/dist/test/index.js +1 -1
- package/docs/README.md +31 -10
- package/docs/background.md +19 -13
- package/docs/database.md +30 -17
- package/docs/endpoints.md +42 -24
- package/docs/errors.md +3 -4
- package/docs/events.md +25 -17
- package/docs/getting-started.md +25 -9
- package/docs/llms-full.txt +489 -164
- package/docs/llms.txt +3 -1
- package/docs/migrations.md +98 -0
- package/docs/resources.md +94 -0
- package/docs/routing.md +59 -26
- package/docs/schema.md +48 -38
- package/docs/services.md +5 -6
- package/package.json +11 -1
- package/dist/chunk-4J3F32SH.js.map +0 -1
- package/dist/chunk-L36JLUPO.js.map +0 -1
- package/dist/schema-BqfEhIC0.d.cts +0 -133
- package/dist/schema-BqfEhIC0.d.ts +0 -133
package/docs/events.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# Hooks & Webhooks
|
|
2
2
|
|
|
3
|
-
Like workers/jobs, hooks and webhooks use the
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Like workers/jobs, hooks and webhooks use the **singleton model** — the same
|
|
4
|
+
imported service singletons as endpoints (`import { Database, Log } from
|
|
5
|
+
"@palbase/backend"`). They do **not** receive a `req`. A second `meta` argument
|
|
6
|
+
carries the non-service data (`env`, `projectId`, `environmentId`; webhooks also
|
|
7
|
+
get `requestId`).
|
|
6
8
|
|
|
7
9
|
## Hooks (platform events)
|
|
8
10
|
|
|
@@ -11,21 +13,24 @@ are imported from `@palbase/backend`: `auth`, `storage`, `documents`.
|
|
|
11
13
|
|
|
12
14
|
```ts
|
|
13
15
|
// hooks/auth.ts
|
|
14
|
-
import { auth } from "@palbase/backend";
|
|
16
|
+
import { auth, Database, Log } from "@palbase/backend";
|
|
15
17
|
|
|
16
|
-
export const onUserCreated = auth.onUserCreated(async (
|
|
17
|
-
|
|
18
|
-
await
|
|
18
|
+
export const onUserCreated = auth.onUserCreated(async (event, meta) => {
|
|
19
|
+
Log.info(`new user: ${event.user.email}`);
|
|
20
|
+
await Database.insert("profiles", {
|
|
19
21
|
user_id: event.user.id,
|
|
20
22
|
email: event.user.email,
|
|
21
23
|
});
|
|
22
24
|
});
|
|
23
25
|
|
|
24
|
-
export const onSignIn = auth.onSignIn(async (
|
|
25
|
-
|
|
26
|
+
export const onSignIn = auth.onSignIn(async (event, meta) => {
|
|
27
|
+
Log.info(`sign in: ${event.user.email} via ${event.provider}`);
|
|
26
28
|
});
|
|
27
29
|
```
|
|
28
30
|
|
|
31
|
+
`meta` shape: `{ env, projectId, environmentId }`. Branch env vars are in
|
|
32
|
+
`meta.env`; services come from the imported singletons.
|
|
33
|
+
|
|
29
34
|
Available hook builders: `auth.onUserCreated`, `auth.onSignIn`, `auth.onSignOut`,
|
|
30
35
|
`auth.onPasswordReset`, `storage.onFileUploaded`, `storage.onFileDeleted`,
|
|
31
36
|
`documents.onDocumentCreated`, `documents.onDocumentUpdated`,
|
|
@@ -38,22 +43,25 @@ Receive and verify webhooks from third-party providers. Files live under
|
|
|
38
43
|
|
|
39
44
|
```ts
|
|
40
45
|
// webhooks/stripe.ts
|
|
41
|
-
import { defineWebhook } from "@palbase/backend";
|
|
46
|
+
import { defineWebhook, Database, Log } from "@palbase/backend";
|
|
42
47
|
|
|
43
48
|
export default defineWebhook({
|
|
44
49
|
provider: "stripe",
|
|
45
50
|
secret: { env: "STRIPE_WEBHOOK_SECRET" }, // signing secret resolved from env
|
|
46
51
|
events: {
|
|
47
|
-
"checkout.session.completed": async (
|
|
48
|
-
await
|
|
52
|
+
"checkout.session.completed": async (event, meta) => {
|
|
53
|
+
await Database.insert("orders", { status: "paid", data: event });
|
|
49
54
|
},
|
|
50
|
-
"payment_intent.payment_failed": async (
|
|
51
|
-
|
|
52
|
-
await
|
|
55
|
+
"payment_intent.payment_failed": async (event, meta) => {
|
|
56
|
+
Log.error("payment failed");
|
|
57
|
+
await Database.insert("payment_failures", { data: event });
|
|
53
58
|
},
|
|
54
59
|
},
|
|
55
60
|
});
|
|
56
61
|
```
|
|
57
62
|
|
|
58
|
-
The signing secret is
|
|
59
|
-
|
|
63
|
+
The signing secret is resolved by the runtime from `secret: { env: "NAME" }`;
|
|
64
|
+
your handlers access branch env vars via `meta.env`. The runtime verifies the
|
|
65
|
+
signature before dispatching to your event handlers.
|
|
66
|
+
|
|
67
|
+
`meta` shape: `{ env, requestId, projectId, environmentId }`.
|
package/docs/getting-started.md
CHANGED
|
@@ -13,7 +13,6 @@ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
|
|
|
13
13
|
"private": true,
|
|
14
14
|
"scripts": {
|
|
15
15
|
"dev": "palbase serve",
|
|
16
|
-
"deploy": "palbase push",
|
|
17
16
|
"typecheck": "tsc --noEmit"
|
|
18
17
|
},
|
|
19
18
|
"dependencies": { "@palbase/backend": "latest" },
|
|
@@ -23,19 +22,24 @@ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
|
|
|
23
22
|
|
|
24
23
|
## Local dev loop
|
|
25
24
|
|
|
26
|
-
- `palbase serve` — run your backend locally with hot reload.
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
- `palbase serve` — run your backend locally with hot reload. It runs your
|
|
26
|
+
`controllers/` locally but proxies `Database`/`ctx.*` to the **deployed**
|
|
27
|
+
branch, so the branch must already be deployed (serve tells you to push first
|
|
28
|
+
if it isn't). See [migrations.md](./migrations.md) for the schema/migration
|
|
29
|
+
side of this.
|
|
30
|
+
- **Deploy is GitHub-native** — there is no `palbase push`. Commit and
|
|
31
|
+
`git push` to your project's repo; the push triggers a deploy of the backend
|
|
32
|
+
runtime. Push a **branch** to deploy that branch instead of `main`.
|
|
29
33
|
|
|
30
34
|
## Your first endpoint
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
A handler is one endpoint unit; a controller maps method+path to it. Create
|
|
37
|
+
`handlers/hello.ts`:
|
|
33
38
|
|
|
34
39
|
```ts
|
|
35
|
-
import {
|
|
40
|
+
import { defineHandler, z } from "@palbase/backend";
|
|
36
41
|
|
|
37
|
-
export default
|
|
38
|
-
method: "GET",
|
|
42
|
+
export default defineHandler({
|
|
39
43
|
input: z.object({ name: z.string().optional() }),
|
|
40
44
|
output: z.object({ message: z.string(), user: z.string().nullable() }),
|
|
41
45
|
handler: async (req) => ({
|
|
@@ -45,5 +49,17 @@ export default defineEndpoint({
|
|
|
45
49
|
});
|
|
46
50
|
```
|
|
47
51
|
|
|
52
|
+
Then mount it in `controllers/hello.controller.ts`:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { defineController, route } from "@palbase/backend";
|
|
56
|
+
import hello from "../handlers/hello.js";
|
|
57
|
+
|
|
58
|
+
export default defineController("/hello", {
|
|
59
|
+
hello: route.get("/", hello),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
48
63
|
This is served at `GET /hello`. The Zod `input` schema validates the request and
|
|
49
|
-
flows into `req.input`; the `output` schema validates your return value.
|
|
64
|
+
flows into `req.input`; the `output` schema validates your return value. See
|
|
65
|
+
[routing.md](./routing.md) for the handler + controller model.
|