@softr/sdk 1.9.0 → 1.9.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/CHANGELOG.md +6 -0
- package/dist/package.js +1 -1
- package/package.json +1 -1
- package/skills/softr-sdk/SKILL.md +1 -1
- package/skills/softr-sdk/ai/SKILL.md +1 -1
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +1 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/email/SKILL.md +4 -2
- package/skills/softr-sdk/file-uploads/SKILL.md +19 -7
- package/skills/softr-sdk/formatting/SKILL.md +1 -1
- package/skills/softr-sdk/integrations/SKILL.md +1 -1
- package/skills/softr-sdk/mcp/SKILL.md +1 -1
- package/skills/softr-sdk/push-notifications/SKILL.md +1 -1
- package/skills/softr-sdk/pwa/SKILL.md +1 -1
- package/skills/softr-sdk/record-mutations/SKILL.md +26 -2
- package/skills/softr-sdk/record-queries/SKILL.md +1 -1
- package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
package/CHANGELOG.md
CHANGED
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@ description: >-
|
|
|
13
13
|
metadata:
|
|
14
14
|
type: sub-skill
|
|
15
15
|
library: softr-sdk
|
|
16
|
-
library_version: 1.9.
|
|
16
|
+
library_version: 1.9.1
|
|
17
17
|
---
|
|
18
18
|
|
|
19
19
|
**Boundary.** `ai` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineAiAction` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.9.
|
|
13
|
+
library_version: 1.9.1
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.9.
|
|
13
|
+
library_version: 1.9.1
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A helper that calls `ds` outside a handler goes in `createServerOnlyFn` from `@tanstack/react-start` (the `record-queries` boundary). `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -12,7 +12,7 @@ description: >-
|
|
|
12
12
|
metadata:
|
|
13
13
|
type: sub-skill
|
|
14
14
|
library: softr-sdk
|
|
15
|
-
library_version: 1.9.
|
|
15
|
+
library_version: 1.9.1
|
|
16
16
|
---
|
|
17
17
|
|
|
18
18
|
**Boundary.** `email` and `EmailError` live on `@softr/sdk/server` and run only inside a `createServerFn` handler — importing them into a component fails the build. `defineEmailAction` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -97,7 +97,9 @@ Declare one action per kind of email: the owner's notification and the client's
|
|
|
97
97
|
const submit = createServerFn({ method: 'POST' })
|
|
98
98
|
.validator(contactSchema)
|
|
99
99
|
.handler(async ({ data }) => {
|
|
100
|
-
const saved = await ds.create(saveSubmission, {
|
|
100
|
+
const saved = await ds.create(saveSubmission, {
|
|
101
|
+
data: { name: data.name, email: data.email, message: data.message },
|
|
102
|
+
})
|
|
101
103
|
try {
|
|
102
104
|
await email.send(notifyOwner, {
|
|
103
105
|
subject: `New message from ${data.name}`,
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 1.9.
|
|
10
|
+
library_version: 1.9.1
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
**Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
|
|
@@ -19,6 +19,7 @@ metadata:
|
|
|
19
19
|
## Setup
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
+
import { z } from 'zod'
|
|
22
23
|
import { defineMutation, type FileAttachment, useFileUpload, type FileItem } from '@softr/sdk'
|
|
23
24
|
import { ds } from '@softr/sdk/server'
|
|
24
25
|
```
|
|
@@ -35,9 +36,15 @@ const createApplication = defineMutation({
|
|
|
35
36
|
})
|
|
36
37
|
|
|
37
38
|
const submitApplication = createServerFn({ method: 'POST' })
|
|
38
|
-
.validator(
|
|
39
|
+
.validator(
|
|
40
|
+
z.object({
|
|
41
|
+
name: z.string(),
|
|
42
|
+
// Zod drops a field the schema leaves out. `.nullish()` is how a record's attachment reads.
|
|
43
|
+
resume: z.array(z.object({ url: z.string(), filename: z.string().nullish(), id: z.string().nullish(), type: z.string().nullish() })),
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
39
46
|
.handler(async ({ data }) => {
|
|
40
|
-
await ds.create(createApplication, { data })
|
|
47
|
+
await ds.create(createApplication, { data: { name: data.name, resume: data.resume } })
|
|
41
48
|
})
|
|
42
49
|
|
|
43
50
|
function ApplicationForm() {
|
|
@@ -130,8 +137,10 @@ const loadApplication = createServerFn({ method: 'GET' })
|
|
|
130
137
|
return record
|
|
131
138
|
})
|
|
132
139
|
|
|
140
|
+
const Attachment = z.object({ url: z.string(), filename: z.string().nullish(), id: z.string().nullish(), type: z.string().nullish() })
|
|
141
|
+
|
|
133
142
|
const saveApplication = createServerFn({ method: 'POST' })
|
|
134
|
-
.validator((
|
|
143
|
+
.validator(z.object({ id: z.string(), resume: z.array(Attachment), photo: Attachment.nullable() }))
|
|
135
144
|
.handler(async ({ data }) => {
|
|
136
145
|
await ds.update(updateApplication, { id: data.id, data: { resume: data.resume, photo: data.photo } })
|
|
137
146
|
})
|
|
@@ -177,14 +186,17 @@ Pre-loaded attachments appear as `status: 'done'` items at the start of `files`.
|
|
|
177
186
|
For server-side use (no progress tracking needed) or plain HTML forms, use `upload` directly:
|
|
178
187
|
|
|
179
188
|
```ts
|
|
189
|
+
import { z } from 'zod'
|
|
180
190
|
import { upload, ds } from '@softr/sdk/server'
|
|
181
191
|
|
|
192
|
+
const Application = z.object({ fullName: z.string().min(1), cv: z.instanceof(File) })
|
|
193
|
+
|
|
182
194
|
const submitResume = createServerFn({ method: 'POST' })
|
|
183
|
-
.validator((fields: FormData) => fields)
|
|
195
|
+
.validator((fields: FormData) => Application.parse({ fullName: fields.get('fullName'), cv: fields.get('cv') }))
|
|
184
196
|
.handler(async ({ data }) => {
|
|
185
|
-
const uploaded = await upload(createApplication, data.
|
|
197
|
+
const uploaded = await upload(createApplication, data.cv)
|
|
186
198
|
await ds.create(createApplication, {
|
|
187
|
-
data: { fullName: data.
|
|
199
|
+
data: { fullName: data.fullName, cv: [uploaded] }, // cv holds several files; a one-file field takes `uploaded` itself
|
|
188
200
|
})
|
|
189
201
|
return new Response(null, { status: 303, headers: { Location: '/thank-you' } })
|
|
190
202
|
})
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 1.9.
|
|
10
|
+
library_version: 1.9.1
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
**Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.9.
|
|
12
|
+
library_version: 1.9.1
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.9.
|
|
13
|
+
library_version: 1.9.1
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -28,7 +28,7 @@ import { defineMutation } from '@softr/sdk'
|
|
|
28
28
|
import { ds } from '@softr/sdk/server'
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
A write's input arrives from the client, so validate it
|
|
31
|
+
A write's input arrives from the client, so validate it with a zod schema. Pass the schema straight to `.validator`; Start accepts it and types `data` from it. A typed function such as `(data: T) => data` checks nothing.
|
|
32
32
|
|
|
33
33
|
## Declare the write
|
|
34
34
|
|
|
@@ -142,6 +142,30 @@ A visitor who is signed out, or a user with no value for the token, changes noth
|
|
|
142
142
|
|
|
143
143
|
A create into a table that belongs to the visitor through its parent, such as a message in a chat, reads the parent through its filtered query before `ds.create` (see [record-queries](../record-queries/SKILL.md#a-table-that-belongs-to-the-visitor-through-its-parent)).
|
|
144
144
|
|
|
145
|
+
### Write only what the form may change
|
|
146
|
+
|
|
147
|
+
The browser can send any key. The datasource accepts every field the mutation declares. A form that shows two fields can therefore set a third one the mutation declares.
|
|
148
|
+
|
|
149
|
+
Declare one mutation per form, with only the fields that form edits. A second form that edits other fields gets its own mutation.
|
|
150
|
+
|
|
151
|
+
Validate with a zod schema that lists exactly those fields. Then name each field in `data`. Never pass the input on whole, and never spread it. Set a field the visitor must not choose, such as the owner, a status or a price, on the server, from `context` or a constant.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
// Wrong: the input goes in whole, so the browser decides every field the mutation declares.
|
|
155
|
+
const saveProfile = createServerFn({ method: 'POST' })
|
|
156
|
+
.validator((data: { id: string; name: string }) => data)
|
|
157
|
+
.handler(({ data }) => ds.update(updateProfile, { id: data.id, data }))
|
|
158
|
+
|
|
159
|
+
// Right: the schema lists the form's fields, and `data` names each one.
|
|
160
|
+
const ProfileForm = z.object({ id: z.string(), name: z.string(), bio: z.string() })
|
|
161
|
+
|
|
162
|
+
const saveProfile = createServerFn({ method: 'POST' })
|
|
163
|
+
.validator(ProfileForm)
|
|
164
|
+
.handler(({ data }) => ds.update(updateProfile, { id: data.id, data: { name: data.name, bio: data.bio } }))
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The lint rules `@softr/write-require-validator` and `@softr/write-no-raw-input` warn about both mistakes.
|
|
168
|
+
|
|
145
169
|
### Only the roles that change a table
|
|
146
170
|
|
|
147
171
|
Often fewer roles change a table than read it. A mutation's `customGroups` names only the roles that may change the table, also when more roles read it. In a barber app, every client reads the free hours, but only barbers open or close them. A barber also changes only their own slots. The read of slots has no filter, because every client reads every slot, so the update filters on the slot's barber:
|
|
@@ -11,7 +11,7 @@ description: >-
|
|
|
11
11
|
metadata:
|
|
12
12
|
type: sub-skill
|
|
13
13
|
library: softr-sdk
|
|
14
|
-
library_version: 1.9.
|
|
14
|
+
library_version: 1.9.1
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A route `loader` also runs in the browser, so it calls a server function, never `ds`. A helper that calls `ds` outside a handler, in this file or in another one, goes in `createServerOnlyFn` from `@tanstack/react-start`, or inside the handler. An exported helper without it keeps the server-only import in the browser bundle, and every page stops loading until it moves. Do not make such a helper a server function when it takes an id: any visitor can call a server function with any id. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.9.
|
|
12
|
+
library_version: 1.9.1
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.
|