@softr/sdk 0.0.88-signing.0 → 0.0.89-alpha.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/agent/agent-fns.d.ts +30 -0
- package/dist/agent/agent-fns.js +172 -0
- package/dist/agent/use-agent-chat.d.ts +100 -0
- package/dist/agent/use-agent-chat.js +419 -0
- package/dist/auth/isomorphic.d.ts +3 -2
- package/dist/auth/isomorphic.js +4 -1
- package/dist/auth/methods/authenticator.js +3 -3
- package/dist/auth/methods/code.js +1 -1
- package/dist/auth/methods/magicLink.js +1 -1
- package/dist/auth/methods/password.js +1 -1
- package/dist/auth/methods/passwordLink.js +1 -1
- package/dist/auth/methods/signup.js +1 -1
- package/dist/auth/methods/social.js +1 -1
- package/dist/auth/platform.js +2 -2
- package/dist/auth/session.js +8 -7
- package/dist/codegen/datasource-docs.js +9 -3
- package/dist/cookies.js +29 -3
- package/dist/core/file-upload.js +1 -1
- package/dist/core/workflow.js +1 -1
- package/dist/datasource/record.js +1 -1
- package/dist/embedding.js +33 -0
- package/dist/fetch.js +1 -61
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/locale-settings.js +1 -1
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +6 -4
- package/dist/package.js +1 -1
- package/dist/pwa.js +2 -2
- package/dist/route/route.js +1 -1
- package/dist/softr-config.schema.js +3 -1
- package/package.json +1 -1
- package/schema/softr.schema.json +5 -0
- package/skills/softr-sdk/SKILL.md +14 -5
- package/skills/softr-sdk/agent-chat/SKILL.md +122 -0
- 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 +9 -5
- package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
- 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/pwa/SKILL.md +1 -1
- package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
- package/skills/softr-sdk/record-queries/SKILL.md +2 -2
- package/skills/softr-sdk/region-visibility/SKILL.md +3 -5
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +85 -44
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 0.0.
|
|
12
|
+
library_version: 0.0.89-alpha.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
|
|
@@ -47,11 +47,9 @@ The rules are the same four a route uses, and [routes-and-visibility](../routes-
|
|
|
47
47
|
|
|
48
48
|
### Where the rule is declared
|
|
49
49
|
|
|
50
|
-
A region's rule is an ordinary value. It can sit beside the component, or
|
|
50
|
+
A region's rule is an ordinary value. It can sit beside the component, in the route file, or in a module the whole app shares so several regions read the same one.
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
A route file may declare a region's rule alongside its own. The route's rule is the one `beforeLoad` names.
|
|
52
|
+
It is separate from the route's own rule. That one is the `visibility` of the route's `definePage`, which `beforeLoad` applies: see [routes-and-visibility/SKILL.md](../routes-and-visibility/SKILL.md).
|
|
55
53
|
|
|
56
54
|
### Hiding a control is not authorization
|
|
57
55
|
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: routes-and-visibility
|
|
3
3
|
description: >-
|
|
4
|
-
Add a route file and declare
|
|
5
|
-
LOGGED_IN_USERS, NON_LOGGED_IN_USERS and customGroups
|
|
6
|
-
assertRouteAccess in
|
|
7
|
-
an access error, and the
|
|
4
|
+
Add a route file and declare its settings: definePage with its visibility
|
|
5
|
+
(LOGGED_IN_USERS, NON_LOGGED_IN_USERS and customGroups) and its title,
|
|
6
|
+
guardRoute or assertRouteAccess in beforeLoad, pageHead in head,
|
|
7
|
+
isAuthorized and checkAccess anywhere, rendering an access error, and the
|
|
8
|
+
lint rules that require the declaration.
|
|
8
9
|
metadata:
|
|
9
10
|
type: sub-skill
|
|
10
11
|
library: softr-sdk
|
|
11
|
-
library_version: 0.0.
|
|
12
|
+
library_version: 0.0.89-alpha.0
|
|
12
13
|
---
|
|
13
14
|
|
|
14
|
-
**Boundary.** `
|
|
15
|
+
**Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
|
|
15
16
|
|
|
16
17
|
# Route visibility
|
|
17
18
|
|
|
18
|
-
Every route file in a Softr app declares who may see it. Softr reads
|
|
19
|
+
Every route file in a Softr app declares its settings: who may see it, its title and its SEO tags. Softr reads them out of the source text without executing it and shows them in Softr Studio, where the app owner sees every route and its audience and can change them. That is why the rules below are strict: a setting Softr cannot read is a setting Studio cannot show.
|
|
19
20
|
|
|
20
21
|
## Setup
|
|
21
22
|
|
|
@@ -23,12 +24,16 @@ Every route file, without exception, looks like this:
|
|
|
23
24
|
|
|
24
25
|
```tsx
|
|
25
26
|
import { createFileRoute } from '@tanstack/react-router'
|
|
26
|
-
import {
|
|
27
|
+
import { definePage, guardRoute, pageHead } from '@softr/sdk'
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
+
const pageSettings = definePage({
|
|
30
|
+
visibility: { userGroupTypes: ['LOGGED_IN_USERS'] },
|
|
31
|
+
title: 'Billing',
|
|
32
|
+
})
|
|
29
33
|
|
|
30
34
|
export const Route = createFileRoute('/_app/billing')({
|
|
31
|
-
beforeLoad: guardRoute(
|
|
35
|
+
beforeLoad: guardRoute(pageSettings),
|
|
36
|
+
head: pageHead(pageSettings),
|
|
32
37
|
component: Billing,
|
|
33
38
|
})
|
|
34
39
|
|
|
@@ -37,30 +42,31 @@ function Billing() {
|
|
|
37
42
|
}
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
The route's
|
|
45
|
+
The route's settings are one `definePage` call at module scope, with an inline object literal as its argument. It always states `visibility`, and a page states its `title` too. `beforeLoad` applies the visibility, and `head` renders the title and the SEO tags.
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
These constraints hold, because Softr reads the file rather than running it:
|
|
43
48
|
|
|
44
49
|
- The call must initialise a top-level `const`.
|
|
45
50
|
- The import must be a direct named import from `@softr/sdk`.
|
|
46
|
-
-
|
|
47
|
-
- Every value inside the literal is written there too. A group id taken from a `const`, a spread or an import
|
|
48
|
-
-
|
|
51
|
+
- A route file has one `definePage`. The route names it in `beforeLoad` and in `head`. A page imported from elsewhere is rejected.
|
|
52
|
+
- Every value inside the literal is written there too. A group id taken from a `const`, a spread or an import still type-checks, but Studio can no longer change that setting. Repeat the id on each route, even when two routes share it.
|
|
53
|
+
- A head setting that depends on the route's data is a function of its head context: `title: ({ loaderData }) => loaderData?.name`.
|
|
54
|
+
- Never state `visibility` on `__root.tsx`. The root cannot carry an audience, so a rule there would gate nothing while looking like it does. Its `definePage` holds the app-wide head defaults only. Put the rule on a layout below the root instead.
|
|
49
55
|
|
|
50
56
|
`userGroupTypes` is a set, so a rule names one audience or several:
|
|
51
57
|
|
|
52
58
|
```ts
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
visibility: { userGroupTypes: ['NON_LOGGED_IN_USERS', 'LOGGED_IN_USERS'] } // everyone, signed in or not
|
|
60
|
+
visibility: { userGroupTypes: ['NON_LOGGED_IN_USERS'] } // signed-out only: login, signup
|
|
61
|
+
visibility: { userGroupTypes: ['LOGGED_IN_USERS'] } // any signed-in visitor
|
|
62
|
+
visibility: { // those groups only
|
|
57
63
|
userGroupTypes: ['LOGGED_IN_USERS'],
|
|
58
64
|
customGroups: ['00000000-0000-0000-0000-00000000000a'],
|
|
59
|
-
}
|
|
60
|
-
|
|
65
|
+
}
|
|
66
|
+
visibility: { // signed-out visitors AND those groups
|
|
61
67
|
userGroupTypes: ['NON_LOGGED_IN_USERS', 'LOGGED_IN_USERS'],
|
|
62
68
|
customGroups: ['00000000-0000-0000-0000-00000000000a'],
|
|
63
|
-
}
|
|
69
|
+
}
|
|
64
70
|
```
|
|
65
71
|
|
|
66
72
|
Everyone is both audiences, as in the first line above. A set names each audience at most once and is never empty — the type and the build both reject one that does not. `customGroups` never stands alone: it narrows the `LOGGED_IN_USERS` half of the set, so the rule still names that half, and one without `userGroupTypes` is a type error. Absent or empty it means **any** signed-in visitor, not "nobody".
|
|
@@ -74,17 +80,20 @@ Take every id in `customGroups` from `docs/user-groups.md`, which lists each gro
|
|
|
74
80
|
```tsx
|
|
75
81
|
// src/routes/_app.tsx: guards every route under /_app
|
|
76
82
|
import { Outlet, createFileRoute } from '@tanstack/react-router'
|
|
77
|
-
import {
|
|
83
|
+
import { definePage, guardRoute, pageHead } from '@softr/sdk'
|
|
78
84
|
|
|
79
|
-
const
|
|
85
|
+
const pageSettings = definePage({ visibility: { userGroupTypes: ['LOGGED_IN_USERS'] } })
|
|
80
86
|
|
|
81
87
|
export const Route = createFileRoute('/_app')({
|
|
82
|
-
beforeLoad: guardRoute(
|
|
88
|
+
beforeLoad: guardRoute(pageSettings),
|
|
89
|
+
head: pageHead(pageSettings),
|
|
83
90
|
component: () => <Outlet />,
|
|
84
91
|
})
|
|
85
92
|
```
|
|
86
93
|
|
|
87
|
-
|
|
94
|
+
A pathless layout like this one is not a page, so it needs no `title`.
|
|
95
|
+
|
|
96
|
+
The whole match chain runs, so a layout guards everything beneath it. Sibling routes do not guard each other. `/admin/index.tsx` does nothing for `/admin/users.tsx` unless an `admin.tsx` layout exists. A route below a guarded layout declares and guards its own settings too.
|
|
88
97
|
|
|
89
98
|
A thrown error renders in place of the guarded route's component. A shell layout guarded with `LOGGED_IN_USERS`, where `unauthenticated` redirects, sends a signed-out visitor away before anything renders there. A rule that can _throw_ — a `customGroups` rule with `forbidden` set to throw — should not sit on the shell itself, or the error replaces the nav and chrome. Put it on a layout inside the shell, or on the restricted routes, so the error renders inside the chrome.
|
|
90
99
|
|
|
@@ -93,9 +102,9 @@ A thrown error renders in place of the guarded route's component. A shell layout
|
|
|
93
102
|
`guardRoute` takes per-reason overrides as a second argument. A bare string is shorthand for a redirect.
|
|
94
103
|
|
|
95
104
|
```ts
|
|
96
|
-
beforeLoad: guardRoute(
|
|
97
|
-
beforeLoad: guardRoute(
|
|
98
|
-
beforeLoad: guardRoute(
|
|
105
|
+
beforeLoad: guardRoute(pageSettings, { unauthenticated: '/marketing/sign-up' }),
|
|
106
|
+
beforeLoad: guardRoute(pageSettings, { forbidden: { strategy: 'throw' } }),
|
|
107
|
+
beforeLoad: guardRoute(pageSettings, {
|
|
99
108
|
unauthenticated: { strategy: 'redirect', to: '/marketing/sign-up' },
|
|
100
109
|
forbidden: { strategy: 'throw' },
|
|
101
110
|
}),
|
|
@@ -142,15 +151,19 @@ That file covers every route at once; a route's own overrides win over it. A rul
|
|
|
142
151
|
|
|
143
152
|
```tsx
|
|
144
153
|
import { createFileRoute } from '@tanstack/react-router'
|
|
145
|
-
import {
|
|
154
|
+
import { definePage, guardRoute, isAccessError, isForbiddenError, pageHead } from '@softr/sdk'
|
|
146
155
|
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
156
|
+
const pageSettings = definePage({
|
|
157
|
+
visibility: {
|
|
158
|
+
userGroupTypes: ['LOGGED_IN_USERS'],
|
|
159
|
+
customGroups: ['00000000-0000-0000-0000-00000000000a'],
|
|
160
|
+
},
|
|
161
|
+
title: 'Billing',
|
|
150
162
|
})
|
|
151
163
|
|
|
152
164
|
export const Route = createFileRoute('/_app/billing')({
|
|
153
|
-
beforeLoad: guardRoute(
|
|
165
|
+
beforeLoad: guardRoute(pageSettings),
|
|
166
|
+
head: pageHead(pageSettings),
|
|
154
167
|
component: Billing,
|
|
155
168
|
errorComponent: ({ error }: { error: unknown }) => {
|
|
156
169
|
if (isForbiddenError(error)) return <UpgradePrompt />
|
|
@@ -166,25 +179,53 @@ Use the predicates: `isAccessError` for any of the three, and `isUnauthenticated
|
|
|
166
179
|
|
|
167
180
|
### Run the check and then keep going
|
|
168
181
|
|
|
169
|
-
`guardRoute` covers almost everything. When a route needs its own logic afterwards, `assertRouteAccess(
|
|
182
|
+
`guardRoute` covers almost everything. When a route needs its own logic afterwards, `assertRouteAccess(pageSettings, opts)` applies the same rule to the whole `beforeLoad` argument and throws the same access error, using the default behaviour for each reason. It takes no overrides.
|
|
170
183
|
|
|
171
184
|
It also narrows `opts.context.user` for the rest of the handler, where the set names one audience and no other: `User` under `['LOGGED_IN_USERS']`, `null` under `['NON_LOGGED_IN_USERS']`. A set naming both admits either kind of visitor, so it narrows to neither. `guardRoute` cannot, because it hands `beforeLoad` a function rather than running inside one.
|
|
172
185
|
|
|
173
|
-
```
|
|
174
|
-
import { redirect } from '@tanstack/react-router'
|
|
175
|
-
import { assertRouteAccess } from '@softr/sdk'
|
|
186
|
+
```tsx
|
|
187
|
+
import { createFileRoute, redirect } from '@tanstack/react-router'
|
|
188
|
+
import { assertRouteAccess, definePage, pageHead } from '@softr/sdk'
|
|
189
|
+
|
|
190
|
+
const pageSettings = definePage({
|
|
191
|
+
visibility: { userGroupTypes: ['LOGGED_IN_USERS'] },
|
|
192
|
+
title: 'Reports',
|
|
193
|
+
})
|
|
176
194
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
195
|
+
export const Route = createFileRoute('/_app/reports')({
|
|
196
|
+
beforeLoad: (opts) => {
|
|
197
|
+
assertRouteAccess(pageSettings, opts)
|
|
198
|
+
if (opts.context.user.email.endsWith('@example.com')) throw redirect({ to: '/internal' })
|
|
199
|
+
},
|
|
200
|
+
head: pageHead(pageSettings),
|
|
201
|
+
component: Reports,
|
|
202
|
+
})
|
|
181
203
|
```
|
|
182
204
|
|
|
183
205
|
Take `opts` whole. A `context` destructured in the parameter list does not narrow.
|
|
184
206
|
|
|
185
|
-
|
|
207
|
+
`guardRoute` and `assertRouteAccess` throw router redirects, so they belong in a route and nowhere else. That is what `Route` in their names marks.
|
|
208
|
+
|
|
209
|
+
### Check a rule without a throw
|
|
210
|
+
|
|
211
|
+
`isAuthorized(rule, context)` and `checkAccess(rule, context)` answer the same question and throw nothing, so they work anywhere: in a loader, a component or a server function. They take a rule from `defineVisibility`, not a page. Use `isAuthorized` for the same narrowing of `context.user` as `assertRouteAccess`, and `checkAccess` to branch on why: it returns `{ ok: true } | { ok: false, reason }`. All of them are synchronous and make no request.
|
|
212
|
+
|
|
213
|
+
```tsx
|
|
214
|
+
import { defineVisibility, isAuthorized, useCurrentUser } from '@softr/sdk'
|
|
215
|
+
|
|
216
|
+
const staffOnly = defineVisibility({
|
|
217
|
+
userGroupTypes: ['LOGGED_IN_USERS'],
|
|
218
|
+
customGroups: ['00000000-0000-0000-0000-00000000000a'],
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
function StaffNote() {
|
|
222
|
+
const context = { user: useCurrentUser() }
|
|
223
|
+
if (!isAuthorized(staffOnly, context)) return null
|
|
224
|
+
return <p>Signed in as staff: {context.user.email}</p>
|
|
225
|
+
}
|
|
226
|
+
```
|
|
186
227
|
|
|
187
|
-
|
|
228
|
+
To show or hide a part of a page, `Audience` is simpler: see [region-visibility/SKILL.md](../region-visibility/SKILL.md).
|
|
188
229
|
|
|
189
230
|
## References
|
|
190
231
|
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 0.0.
|
|
12
|
+
library_version: 0.0.89-alpha.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.
|