@softr/sdk 1.9.0 → 1.10.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @softr/sdk
2
2
 
3
+ ## 1.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - `auth.updateProfile` and `auth.removeAvatar` answer `invalidPhoto` when the platform refuses a photo, instead of `invalidName`. Action: where a photo is saved or removed, handle `invalidPhoto` and say the photo cannot be used.
8
+
9
+ ## 1.9.1
10
+
11
+ ### Patch Changes
12
+
13
+ - record-mutations skill: write only the fields a form may change. email and file-uploads examples name each written field.
14
+
3
15
  ## 1.9.0
4
16
 
5
17
  ### Minor Changes
@@ -14,6 +14,10 @@ type Profile = Pick<User, "id" | "email" | "fullName" | "userGroupIds" | "avatar
14
14
  * loaded until its route context loads again, so call `router.invalidate()`
15
15
  * after a success.
16
16
  *
17
+ * A bad name answers `invalidName`. A photo the platform refuses, sent alone,
18
+ * answers `invalidPhoto`. When a name and a photo are sent together, a refusal
19
+ * answers `invalidName`.
20
+ *
17
21
  * @example
18
22
  * const result = await auth.updateProfile({ fullName: 'Ann Lee' })
19
23
  * const result = await auth.updateProfile({ avatar: file })
@@ -16,6 +16,10 @@ import { authError } from "./utils.js";
16
16
  * loaded until its route context loads again, so call `router.invalidate()`
17
17
  * after a success.
18
18
  *
19
+ * A bad name answers `invalidName`. A photo the platform refuses, sent alone,
20
+ * answers `invalidPhoto`. When a name and a photo are sent together, a refusal
21
+ * answers `invalidName`.
22
+ *
19
23
  * @example
20
24
  * const result = await auth.updateProfile({ fullName: 'Ann Lee' })
21
25
  * const result = await auth.updateProfile({ avatar: file })
@@ -27,7 +31,9 @@ async function updateProfile(changes) {
27
31
  const form = new FormData();
28
32
  if (changes.fullName !== void 0) form.set("fullName", changes.fullName);
29
33
  if (changes.avatar) form.set("avatar", changes.avatar);
30
- return answer(await makePlatform(sessionConfig().applicationId).updateProfile(token, form));
34
+ const platform = makePlatform(sessionConfig().applicationId);
35
+ const badRequest = changes.fullName === void 0 ? "invalidPhoto" : "invalidName";
36
+ return answer(await platform.updateProfile(token, form), badRequest);
31
37
  }
32
38
  /**
33
39
  * Removes the signed-in visitor's own photo. Like {@link updateProfile}, it
@@ -40,12 +46,12 @@ async function updateProfile(changes) {
40
46
  async function removeAvatar() {
41
47
  const token = readSessionToken();
42
48
  if (token === null) return authError("notAuthenticated", "Nobody is signed in.");
43
- return answer(await makePlatform(sessionConfig().applicationId).removeAvatar(token));
49
+ return answer(await makePlatform(sessionConfig().applicationId).removeAvatar(token), "invalidPhoto");
44
50
  }
45
- function answer(result) {
51
+ function answer(result, badRequest) {
46
52
  if (!result.ok) {
47
53
  switch (result.status) {
48
- case 400: return authError("invalidName", result.message);
54
+ case 400: return authError(badRequest, result.message);
49
55
  case 401: return authError("notAuthenticated", result.message);
50
56
  case 409: return authError("avatarNotSupported", result.message);
51
57
  case 413: return authError("fileTooLarge", result.message);
@@ -10,6 +10,6 @@ type AuthError = {
10
10
  code: AuthErrorCode;
11
11
  message: string;
12
12
  };
13
- type AuthErrorCode = "invalidCredentials" | "tooManyAttempts" | "emailTaken" | "invalidDetails" | "invalidCode" | "noAccount" | "accountDeactivated" | "notAuthenticated" | "emailDomainNotAllowed" | "notEnabled" | "linkExpired" | "invalidName" | "fileTooLarge" | "notAnImage" | "avatarNotSupported";
13
+ type AuthErrorCode = "invalidCredentials" | "tooManyAttempts" | "emailTaken" | "invalidDetails" | "invalidCode" | "noAccount" | "accountDeactivated" | "notAuthenticated" | "emailDomainNotAllowed" | "notEnabled" | "linkExpired" | "invalidName" | "invalidPhoto" | "fileTooLarge" | "notAnImage" | "avatarNotSupported";
14
14
  //#endregion
15
15
  export { AuthError, Result };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.9.0";
2
+ var version = "1.10.0";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: core
12
12
  library: softr-sdk
13
- library_version: 1.9.0
13
+ library_version: 1.10.0
14
14
  ---
15
15
 
16
16
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 1.9.0
16
+ library_version: 1.10.0
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.0
13
+ library_version: 1.10.0
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.
@@ -486,7 +486,7 @@ const result = await auth.updateProfile({ avatar: file }) // a File from an <i
486
486
  const result = await auth.removeAvatar() // removes the photo
487
487
  ```
488
488
 
489
- It answers with the updated `User`. Pass the file through a server function that takes `FormData`. The photo must be a PNG, JPEG or GIF of at most 5 MB, or it answers `notAnImage` or `fileTooLarge`. A blank or unacceptable name answers `invalidName`.
489
+ It answers with the updated `User`. Pass the file through a server function that takes `FormData`. The photo must be a PNG, JPEG or GIF of at most 5 MB, or it answers `notAnImage` or `fileTooLarge`. A blank or unacceptable name answers `invalidName`. A photo the platform refuses for another reason answers `invalidPhoto`. Sent together with a name, a refusal answers `invalidName`.
490
490
 
491
491
  Only some apps can store a photo. Show the photo controls only when `user.avatarEditable` is true; elsewhere a photo answers `avatarNotSupported`.
492
492
 
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.9.0
13
+ library_version: 1.10.0
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.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.9.0
14
+ library_version: 1.10.0
15
15
  ---
16
16
 
17
17
  # The current user
@@ -12,7 +12,7 @@ description: >-
12
12
  metadata:
13
13
  type: sub-skill
14
14
  library: softr-sdk
15
- library_version: 1.9.0
15
+ library_version: 1.10.0
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, { data })
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.0
10
+ library_version: 1.10.0
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((data: { name: string; resume: FileAttachment[] }) => data)
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((data: { id: string; resume: FileAttachment[]; photo: FileAttachment | null }) => data)
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.get('cv') as File)
197
+ const uploaded = await upload(createApplication, data.cv)
186
198
  await ds.create(createApplication, {
187
- data: { fullName: data.get('fullName') as string, cv: [uploaded] }, // cv holds several files; a one-file field takes `uploaded` itself
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
  })
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 1.9.0
11
+ library_version: 1.10.0
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.9.0
10
+ library_version: 1.10.0
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.0
12
+ library_version: 1.10.0
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.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.9.0
12
+ library_version: 1.10.0
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.9.0
13
+ library_version: 1.10.0
14
14
  ---
15
15
 
16
16
  # Installing the app
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.9.0
13
+ library_version: 1.10.0
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. Pass a zod schema straight to `.validator`; Start accepts it and types `data` from 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.0
14
+ library_version: 1.10.0
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.0
12
+ library_version: 1.10.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.9.0
12
+ library_version: 1.10.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.9.0
12
+ library_version: 1.10.0
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.