@softr/sdk 0.0.3 → 0.0.4
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/index.d.ts +35 -82
- package/dist/index.js +152 -56
- package/dist/tanstack.d.ts +1 -1
- package/dist/types-DCiIbAw_.d.ts +237 -0
- package/package.json +6 -4
- package/dist/types-BLLUI6MI.d.ts +0 -75
package/dist/index.d.ts
CHANGED
|
@@ -1,84 +1,37 @@
|
|
|
1
|
-
import { a as ListResult, c as SoftrRecord, d as WorkflowAPI, i as ListOptions, l as SoftrSDK, n as DatasourceAPI, o as SoftrMe, r as DatasourcePermissions, s as SoftrPermissions, t as AuthAPI, u as SoftrUser } from "./types-
|
|
1
|
+
import { C as isAccessError, E as isUnauthenticatedError, S as defineVisibility, T as isForbiddenError, _ as SoftrContext, a as ListResult, b as User, c as SoftrRecord, d as WorkflowAPI, f as AccessBehaviour, g as RouteVisibility, h as AccessReason, i as ListOptions, l as SoftrSDK, m as AccessError, n as DatasourceAPI, o as SoftrMe, p as AccessCheck, r as DatasourcePermissions, s as SoftrPermissions, t as AuthAPI, u as SoftrUser, v as SoftrUserGroupId, w as isAlreadySignedInError, x as VisibilityHandle, y as SoftrUserGroups } from "./types-DCiIbAw_.js";
|
|
2
|
+
import * as _tanstack_react_start0 from "@tanstack/react-start";
|
|
2
3
|
|
|
3
|
-
//#region src/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
readonly reason: RefusalReason;
|
|
36
|
-
readonly requiredGroupIds: Array<SoftrUserGroupId> | undefined;
|
|
37
|
-
constructor(reason: RefusalReason, requiredGroupIds?: Array<SoftrUserGroupId>);
|
|
38
|
-
}
|
|
39
|
-
interface VisibilityHandle {
|
|
40
|
-
/** Structured check — returns { ok: true } or { ok: false, reason }. */
|
|
41
|
-
check(context: SoftrContext): CheckResult;
|
|
42
|
-
/** Returns true if the visitor passes the rule. */
|
|
43
|
-
isAuthorized(context: SoftrContext): boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Throws a VisibilityError if not authorized. Acts as a TypeScript assertion,
|
|
46
|
-
* narrowing context.user to non-null for code that follows.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* beforeLoad: ({ context }) => {
|
|
50
|
-
* visibility.assert(context)
|
|
51
|
-
* if (context.user.trialExpired) throw redirect({ to: '/renew' })
|
|
52
|
-
* }
|
|
53
|
-
*/
|
|
54
|
-
assert(context: SoftrContext): asserts context is SoftrContext & {
|
|
55
|
-
user: SoftrUser;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Returns a TanStack beforeLoad handler. On refusal, throws a VisibilityError
|
|
59
|
-
* caught by the route's errorComponent.
|
|
60
|
-
*
|
|
61
|
-
* For redirect-on-refusal, use isAuthorized or check directly and throw
|
|
62
|
-
* TanStack's redirect() yourself.
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* beforeLoad: visibility.guard()
|
|
66
|
-
* errorComponent: ({ error }) => isVisibilityError(error) ? <NoAccess /> : <AppError />
|
|
67
|
-
*/
|
|
68
|
-
guard(overrides?: GuardOverrides): (params: {
|
|
69
|
-
context: SoftrContext;
|
|
70
|
-
}) => void;
|
|
71
|
-
}
|
|
72
|
-
declare function defineVisibility(rule: RouteVisibility): VisibilityHandle;
|
|
73
|
-
declare function isVisibilityError(error: unknown): error is VisibilityError;
|
|
74
|
-
declare function isUnauthenticatedError(error: unknown): error is VisibilityError & {
|
|
75
|
-
reason: "unauthenticated";
|
|
76
|
-
};
|
|
77
|
-
declare function isForbiddenError(error: unknown): error is VisibilityError & {
|
|
78
|
-
reason: "forbidden";
|
|
79
|
-
};
|
|
80
|
-
declare function isAlreadySignedInError(error: unknown): error is VisibilityError & {
|
|
81
|
-
reason: "alreadySignedIn";
|
|
82
|
-
};
|
|
4
|
+
//#region src/auth/session.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns the signed-in visitor, or `null` when nobody is signed in.
|
|
8
|
+
*
|
|
9
|
+
* Await it in the root route's `beforeLoad` to publish the visitor as route
|
|
10
|
+
* context, which every `defineVisibility` guard then reads. It requires
|
|
11
|
+
* {@link softrAuthMiddleware} to be registered.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* export const Route = createRootRouteWithContext<SoftrContext>()({
|
|
15
|
+
* beforeLoad: async () => ({ user: await getSoftrUser() }),
|
|
16
|
+
* })
|
|
17
|
+
*/
|
|
18
|
+
declare const getSoftrUser: _tanstack_react_start0.OptionalFetcher<undefined, undefined, Promise<User | null>>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/auth/middleware.d.ts
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the Softr visitor for every request, so that `getSoftrUser` and the
|
|
23
|
+
* route guards have someone to check.
|
|
24
|
+
*
|
|
25
|
+
* Register it once, as a global request middleware in the app's `src/start.ts`.
|
|
26
|
+
* It touches nothing but the visitor.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* export const startInstance = createStart(() => ({
|
|
30
|
+
* requestMiddleware: [softrAuthMiddleware()],
|
|
31
|
+
* }))
|
|
32
|
+
*/
|
|
33
|
+
declare function softrAuthMiddleware(): _tanstack_react_start0.RequestMiddlewareAfterServer<{}, undefined, {
|
|
34
|
+
user: User | null;
|
|
35
|
+
}>;
|
|
83
36
|
//#endregion
|
|
84
|
-
export { type
|
|
37
|
+
export { type AccessBehaviour, type AccessCheck, type AccessError, type AccessReason, type AuthAPI, type DatasourceAPI, type DatasourcePermissions, type ListOptions, type ListResult, type RouteVisibility, type SoftrContext, type SoftrMe, type SoftrPermissions, type SoftrRecord, type SoftrSDK, type SoftrUser, type SoftrUserGroupId, type SoftrUserGroups, type User, type VisibilityHandle, type WorkflowAPI, defineVisibility, getSoftrUser, isAccessError, isAlreadySignedInError, isForbiddenError, isUnauthenticatedError, softrAuthMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -1,73 +1,169 @@
|
|
|
1
|
+
import { redirect } from "@tanstack/react-router";
|
|
2
|
+
import { createMiddleware, createServerFn, getGlobalStartContext } from "@tanstack/react-start";
|
|
3
|
+
|
|
1
4
|
//#region src/routes/visibility.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Why a visitor was denied.
|
|
7
|
+
*
|
|
8
|
+
* - `unauthenticated` — the route needs a signed-in visitor and there is none.
|
|
9
|
+
* - `forbidden` — signed in, but in none of the groups the route names.
|
|
10
|
+
* - `alreadySignedIn` — the route is for signed-out visitors only.
|
|
11
|
+
*/
|
|
12
|
+
const ACCESS_REASONS = [
|
|
13
|
+
"unauthenticated",
|
|
14
|
+
"forbidden",
|
|
15
|
+
"alreadySignedIn"
|
|
16
|
+
];
|
|
17
|
+
/** Decides whether a visitor passes a visibility rule, without throwing. */
|
|
18
|
+
function checkAccess(rule, context) {
|
|
13
19
|
const { user } = context;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
ok:
|
|
17
|
-
reason: "alreadySignedIn"
|
|
18
|
-
} : { ok: true };
|
|
19
|
-
if (!user) return {
|
|
20
|
-
ok: false,
|
|
21
|
-
reason: "unauthenticated"
|
|
22
|
-
};
|
|
23
|
-
const { userGroupIds } = rule;
|
|
24
|
-
if (userGroupIds?.length) {
|
|
25
|
-
if (!userGroupIds.some((id) => user.userGroupIds.includes(id))) return {
|
|
20
|
+
switch (rule.userGroup) {
|
|
21
|
+
case "ALL_USERS": return { ok: true };
|
|
22
|
+
case "NON_LOGGED_IN_USERS": return user === null ? { ok: true } : {
|
|
26
23
|
ok: false,
|
|
27
|
-
reason: "
|
|
28
|
-
requiredGroupIds: userGroupIds
|
|
24
|
+
reason: "alreadySignedIn"
|
|
29
25
|
};
|
|
26
|
+
case "LOGGED_IN_USERS": {
|
|
27
|
+
if (user === null) return {
|
|
28
|
+
ok: false,
|
|
29
|
+
reason: "unauthenticated"
|
|
30
|
+
};
|
|
31
|
+
const required = rule.userGroupIds ?? [];
|
|
32
|
+
if (required.length === 0) return { ok: true };
|
|
33
|
+
return required.some((id) => user.userGroupIds.includes(id)) ? { ok: true } : {
|
|
34
|
+
ok: false,
|
|
35
|
+
reason: "forbidden"
|
|
36
|
+
};
|
|
37
|
+
}
|
|
30
38
|
}
|
|
31
|
-
return { ok: true };
|
|
32
39
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
throw new VisibilityError(result.reason, result.reason === "forbidden" ? result.requiredGroupIds : void 0);
|
|
40
|
+
function isAccessReason(value) {
|
|
41
|
+
return ACCESS_REASONS.some((reason) => reason === value);
|
|
36
42
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return ({ context }) => {
|
|
49
|
-
throwIfDenied(runCheck(rule, context));
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
check,
|
|
54
|
-
isAuthorized,
|
|
55
|
-
assert,
|
|
56
|
-
guard
|
|
57
|
-
};
|
|
43
|
+
/**
|
|
44
|
+
* True for any access error thrown by a guard, whatever its reason.
|
|
45
|
+
*
|
|
46
|
+
* An access error arrives as `unknown` — hydrated from the page, or thrown by
|
|
47
|
+
* anything else at all — so every field is checked before it is narrowed.
|
|
48
|
+
*/
|
|
49
|
+
function isAccessError(error) {
|
|
50
|
+
if (typeof error !== "object" || error === null) return false;
|
|
51
|
+
const candidate = error;
|
|
52
|
+
if (candidate.type !== "access_error") return false;
|
|
53
|
+
return isAccessReason(candidate.reason);
|
|
58
54
|
}
|
|
59
|
-
function
|
|
60
|
-
return error
|
|
55
|
+
function isReason(error, reason) {
|
|
56
|
+
return isAccessError(error) && error.reason === reason;
|
|
61
57
|
}
|
|
58
|
+
/** True for an access error thrown because nobody was signed in. */
|
|
62
59
|
function isUnauthenticatedError(error) {
|
|
63
|
-
return error
|
|
60
|
+
return isReason(error, "unauthenticated");
|
|
64
61
|
}
|
|
62
|
+
/** True for an access error thrown because the visitor was in none of the groups. */
|
|
65
63
|
function isForbiddenError(error) {
|
|
66
|
-
return error
|
|
64
|
+
return isReason(error, "forbidden");
|
|
67
65
|
}
|
|
66
|
+
/** True for an access error thrown because a signed-out-only route saw a user. */
|
|
68
67
|
function isAlreadySignedInError(error) {
|
|
69
|
-
return error
|
|
68
|
+
return isReason(error, "alreadySignedIn");
|
|
69
|
+
}
|
|
70
|
+
const DEFAULT_ACCESS_BEHAVIOUR = {
|
|
71
|
+
unauthenticated: {
|
|
72
|
+
strategy: "redirect",
|
|
73
|
+
to: "/login"
|
|
74
|
+
},
|
|
75
|
+
forbidden: { strategy: "throw" },
|
|
76
|
+
alreadySignedIn: {
|
|
77
|
+
strategy: "redirect",
|
|
78
|
+
to: "/"
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/** What a guard throws for a reason: a redirect, or an {@link AccessError}. */
|
|
82
|
+
function thrownFor(reason, overrides) {
|
|
83
|
+
const behaviour = overrides?.[reason] ?? DEFAULT_ACCESS_BEHAVIOUR[reason];
|
|
84
|
+
const resolved = typeof behaviour === "string" ? {
|
|
85
|
+
strategy: "redirect",
|
|
86
|
+
to: behaviour
|
|
87
|
+
} : behaviour;
|
|
88
|
+
if (resolved.strategy === "redirect") return redirect({ to: resolved.to });
|
|
89
|
+
return {
|
|
90
|
+
type: "access_error",
|
|
91
|
+
reason
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Declares who may see a route, and returns the handle that enforces it.
|
|
96
|
+
*
|
|
97
|
+
* One call per route file, at module scope: the build reads these declarations
|
|
98
|
+
* to publish the app's route visibility, so the rule must be a literal.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const visibility = defineVisibility({ userGroup: "LOGGED_IN_USERS" })
|
|
102
|
+
*
|
|
103
|
+
* export const Route = createFileRoute("/about")({
|
|
104
|
+
* beforeLoad: visibility.guard(),
|
|
105
|
+
* component: About,
|
|
106
|
+
* })
|
|
107
|
+
*/
|
|
108
|
+
function defineVisibility(rule) {
|
|
109
|
+
return {
|
|
110
|
+
guard: (overrides) => ({ context }) => {
|
|
111
|
+
const result = checkAccess(rule, context);
|
|
112
|
+
if (result.ok) return;
|
|
113
|
+
throw thrownFor(result.reason, overrides);
|
|
114
|
+
},
|
|
115
|
+
assert: (context) => {
|
|
116
|
+
const result = checkAccess(rule, context);
|
|
117
|
+
if (result.ok) return;
|
|
118
|
+
throw thrownFor(result.reason, void 0);
|
|
119
|
+
},
|
|
120
|
+
isAuthorized: (context) => checkAccess(rule, context).ok,
|
|
121
|
+
check: (context) => checkAccess(rule, context)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/auth/session.ts
|
|
127
|
+
async function resolveVisitor(_request) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
function visitorFromContext(context) {
|
|
131
|
+
if (typeof context !== "object" || context === null || !("user" in context)) throw new Error("softrAuthMiddleware has not run: the Start global context carries no `user`. Register it as a global request middleware, e.g. createStart(() => ({ requestMiddleware: [softrAuthMiddleware()] })).");
|
|
132
|
+
return context.user ?? null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns the signed-in visitor, or `null` when nobody is signed in.
|
|
136
|
+
*
|
|
137
|
+
* Await it in the root route's `beforeLoad` to publish the visitor as route
|
|
138
|
+
* context, which every `defineVisibility` guard then reads. It requires
|
|
139
|
+
* {@link softrAuthMiddleware} to be registered.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* export const Route = createRootRouteWithContext<SoftrContext>()({
|
|
143
|
+
* beforeLoad: async () => ({ user: await getSoftrUser() }),
|
|
144
|
+
* })
|
|
145
|
+
*/
|
|
146
|
+
const getSoftrUser = createServerFn({ method: "GET" }).handler(async () => visitorFromContext(getGlobalStartContext()));
|
|
147
|
+
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/auth/middleware.ts
|
|
150
|
+
/**
|
|
151
|
+
* Resolves the Softr visitor for every request, so that `getSoftrUser` and the
|
|
152
|
+
* route guards have someone to check.
|
|
153
|
+
*
|
|
154
|
+
* Register it once, as a global request middleware in the app's `src/start.ts`.
|
|
155
|
+
* It touches nothing but the visitor.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* export const startInstance = createStart(() => ({
|
|
159
|
+
* requestMiddleware: [softrAuthMiddleware()],
|
|
160
|
+
* }))
|
|
161
|
+
*/
|
|
162
|
+
function softrAuthMiddleware() {
|
|
163
|
+
return createMiddleware({ type: "request" }).server(async ({ request, next }) => {
|
|
164
|
+
return next({ context: { user: await resolveVisitor(request) } });
|
|
165
|
+
});
|
|
70
166
|
}
|
|
71
167
|
|
|
72
168
|
//#endregion
|
|
73
|
-
export {
|
|
169
|
+
export { defineVisibility, getSoftrUser, isAccessError, isAlreadySignedInError, isForbiddenError, isUnauthenticatedError, softrAuthMiddleware };
|
package/dist/tanstack.d.ts
CHANGED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
//#region src/routes/visibility.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Declare this interface in your app to narrow every group id the SDK takes and
|
|
4
|
+
* returns. Without a declaration, a group id is a plain `string`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* declare module "@softr/sdk" {
|
|
8
|
+
* interface SoftrUserGroups {
|
|
9
|
+
* ids:
|
|
10
|
+
* | "b1f2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"
|
|
11
|
+
* | "f9e8d7c6-5b4a-4938-8271-605f4e3d2c1b"
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
interface SoftrUserGroups {}
|
|
16
|
+
/** A Softr user group id, narrowed to your own ids if you declare {@link SoftrUserGroups}. */
|
|
17
|
+
type SoftrUserGroupId = SoftrUserGroups extends {
|
|
18
|
+
ids: infer Id extends string;
|
|
19
|
+
} ? Id : string;
|
|
20
|
+
/**
|
|
21
|
+
* Who may see a route.
|
|
22
|
+
*
|
|
23
|
+
* - `ALL_USERS` — everyone, signed in or not.
|
|
24
|
+
* - `NON_LOGGED_IN_USERS` — signed-out visitors only.
|
|
25
|
+
* - `LOGGED_IN_USERS` — signed-in visitors; with `userGroupIds`, only members of
|
|
26
|
+
* at least one of those groups.
|
|
27
|
+
*/
|
|
28
|
+
type RouteVisibility = {
|
|
29
|
+
userGroup: "ALL_USERS";
|
|
30
|
+
} | {
|
|
31
|
+
userGroup: "NON_LOGGED_IN_USERS";
|
|
32
|
+
} | {
|
|
33
|
+
userGroup: "LOGGED_IN_USERS";
|
|
34
|
+
userGroupIds?: Array<SoftrUserGroupId>;
|
|
35
|
+
};
|
|
36
|
+
/** The signed-in visitor. */
|
|
37
|
+
type User = {
|
|
38
|
+
id: string;
|
|
39
|
+
email: string;
|
|
40
|
+
name: string;
|
|
41
|
+
userGroupIds: Array<SoftrUserGroupId>;
|
|
42
|
+
};
|
|
43
|
+
/** The route context the root route publishes: the visitor, or `null` when signed out. */
|
|
44
|
+
type SoftrContext = {
|
|
45
|
+
user: User | null;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Why a visitor was denied.
|
|
49
|
+
*
|
|
50
|
+
* - `unauthenticated` — the route needs a signed-in visitor and there is none.
|
|
51
|
+
* - `forbidden` — signed in, but in none of the groups the route names.
|
|
52
|
+
* - `alreadySignedIn` — the route is for signed-out visitors only.
|
|
53
|
+
*/
|
|
54
|
+
declare const ACCESS_REASONS: readonly ["unauthenticated", "forbidden", "alreadySignedIn"];
|
|
55
|
+
type AccessReason = (typeof ACCESS_REASONS)[number];
|
|
56
|
+
/**
|
|
57
|
+
* What a guard does when it refuses a visitor: redirect to a path, or throw an
|
|
58
|
+
* {@link AccessError} for the route's `errorComponent` to render. A bare string
|
|
59
|
+
* is shorthand for redirecting there.
|
|
60
|
+
*/
|
|
61
|
+
type AccessBehaviour = string | {
|
|
62
|
+
strategy: "redirect";
|
|
63
|
+
to: string;
|
|
64
|
+
} | {
|
|
65
|
+
strategy: "throw";
|
|
66
|
+
};
|
|
67
|
+
/** Per-reason overrides of the default {@link AccessBehaviour}. */
|
|
68
|
+
type GuardOverrides = Partial<Record<AccessReason, AccessBehaviour>>;
|
|
69
|
+
/** The result of {@link VisibilityHandle.check}: passed, or denied with a reason. */
|
|
70
|
+
type AccessCheck = {
|
|
71
|
+
ok: true;
|
|
72
|
+
} | {
|
|
73
|
+
ok: false;
|
|
74
|
+
reason: AccessReason;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* The access error a guard throws. It is plain data, not an `Error` subclass, so
|
|
78
|
+
* take it as `unknown` in an `errorComponent` and narrow it with
|
|
79
|
+
* {@link isAccessError}. It carries the reason and nothing else: the error is
|
|
80
|
+
* serialized into the page, so it never names the groups the route wanted.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* errorComponent: ({ error }: { error: unknown }) =>
|
|
84
|
+
* isAccessError(error) ? <NoAccess /> : <AppError />
|
|
85
|
+
*/
|
|
86
|
+
type AccessError = {
|
|
87
|
+
type: "access_error";
|
|
88
|
+
reason: AccessReason;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* True for any access error thrown by a guard, whatever its reason.
|
|
92
|
+
*
|
|
93
|
+
* An access error arrives as `unknown` — hydrated from the page, or thrown by
|
|
94
|
+
* anything else at all — so every field is checked before it is narrowed.
|
|
95
|
+
*/
|
|
96
|
+
declare function isAccessError(error: unknown): error is AccessError;
|
|
97
|
+
/** True for an access error thrown because nobody was signed in. */
|
|
98
|
+
declare function isUnauthenticatedError(error: unknown): error is AccessError;
|
|
99
|
+
/** True for an access error thrown because the visitor was in none of the groups. */
|
|
100
|
+
declare function isForbiddenError(error: unknown): error is AccessError;
|
|
101
|
+
/** True for an access error thrown because a signed-out-only route saw a user. */
|
|
102
|
+
declare function isAlreadySignedInError(error: unknown): error is AccessError;
|
|
103
|
+
type Narrowed<R extends RouteVisibility> = R extends {
|
|
104
|
+
userGroup: "LOGGED_IN_USERS";
|
|
105
|
+
} ? SoftrContext & {
|
|
106
|
+
user: User;
|
|
107
|
+
} : R extends {
|
|
108
|
+
userGroup: "NON_LOGGED_IN_USERS";
|
|
109
|
+
} ? SoftrContext & {
|
|
110
|
+
user: null;
|
|
111
|
+
} : SoftrContext;
|
|
112
|
+
/** The four ways to apply a rule, returned by {@link defineVisibility}. */
|
|
113
|
+
type VisibilityHandle<R extends RouteVisibility = RouteVisibility> = {
|
|
114
|
+
/**
|
|
115
|
+
* A `beforeLoad` handler that denies a visitor who fails the rule.
|
|
116
|
+
*
|
|
117
|
+
* By default an unauthenticated visitor is redirected to `/login`, an already
|
|
118
|
+
* signed-in one to `/`, and a forbidden one gets an {@link AccessError} thrown
|
|
119
|
+
* in place, for the route's `errorComponent` to render. Pass `overrides` to
|
|
120
|
+
* change what any one reason does.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* beforeLoad: visibility.guard({ unauthenticated: "/sign-in" })
|
|
124
|
+
*/
|
|
125
|
+
guard(overrides?: GuardOverrides): (opts: {
|
|
126
|
+
context: SoftrContext;
|
|
127
|
+
}) => void;
|
|
128
|
+
/**
|
|
129
|
+
* Throws the same access error or redirect `guard()` would, for code that
|
|
130
|
+
* needs to run its own checks after the rule.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* beforeLoad: ({ context }) => {
|
|
134
|
+
* visibility.assert(context)
|
|
135
|
+
* if (context.user.trialExpired) throw redirect({ to: "/renew" })
|
|
136
|
+
* }
|
|
137
|
+
*/
|
|
138
|
+
assert(context: SoftrContext): void;
|
|
139
|
+
/**
|
|
140
|
+
* Whether the visitor passes the rule. Narrows `context.user` where the rule
|
|
141
|
+
* allows it.
|
|
142
|
+
*/
|
|
143
|
+
isAuthorized(context: SoftrContext): context is Narrowed<R>;
|
|
144
|
+
/** The decision and, when a visitor is refused, the reason. Throws nothing. */
|
|
145
|
+
check(context: SoftrContext): AccessCheck;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Declares who may see a route, and returns the handle that enforces it.
|
|
149
|
+
*
|
|
150
|
+
* One call per route file, at module scope: the build reads these declarations
|
|
151
|
+
* to publish the app's route visibility, so the rule must be a literal.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* const visibility = defineVisibility({ userGroup: "LOGGED_IN_USERS" })
|
|
155
|
+
*
|
|
156
|
+
* export const Route = createFileRoute("/about")({
|
|
157
|
+
* beforeLoad: visibility.guard(),
|
|
158
|
+
* component: About,
|
|
159
|
+
* })
|
|
160
|
+
*/
|
|
161
|
+
declare function defineVisibility<const R extends RouteVisibility>(rule: R): VisibilityHandle<R>;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/types.d.ts
|
|
164
|
+
interface SoftrUser {
|
|
165
|
+
id: string;
|
|
166
|
+
email: string;
|
|
167
|
+
name: string;
|
|
168
|
+
userGroupIds: Array<SoftrUserGroupId>;
|
|
169
|
+
}
|
|
170
|
+
interface DatasourcePermissions {
|
|
171
|
+
read: boolean;
|
|
172
|
+
create: boolean;
|
|
173
|
+
update: boolean;
|
|
174
|
+
delete: boolean;
|
|
175
|
+
}
|
|
176
|
+
interface SoftrPermissions {
|
|
177
|
+
datasources: Record<string, DatasourcePermissions>;
|
|
178
|
+
workflows: Record<string, boolean>;
|
|
179
|
+
}
|
|
180
|
+
interface SoftrMe {
|
|
181
|
+
user: SoftrUser | null;
|
|
182
|
+
permissions: SoftrPermissions;
|
|
183
|
+
}
|
|
184
|
+
interface ListOptions {
|
|
185
|
+
page?: number;
|
|
186
|
+
limit?: number;
|
|
187
|
+
filters?: Array<{
|
|
188
|
+
field: string;
|
|
189
|
+
operator: string;
|
|
190
|
+
value: unknown;
|
|
191
|
+
}>;
|
|
192
|
+
sort?: Array<{
|
|
193
|
+
field: string;
|
|
194
|
+
direction: 'asc' | 'desc';
|
|
195
|
+
}>;
|
|
196
|
+
}
|
|
197
|
+
interface ListResult<T = SoftrRecord> {
|
|
198
|
+
records: T[];
|
|
199
|
+
total: number;
|
|
200
|
+
page: number;
|
|
201
|
+
limit: number;
|
|
202
|
+
}
|
|
203
|
+
interface SoftrRecord {
|
|
204
|
+
id: string;
|
|
205
|
+
[field: string]: unknown;
|
|
206
|
+
}
|
|
207
|
+
interface DatasourceAPI {
|
|
208
|
+
list(options?: ListOptions): Promise<ListResult>;
|
|
209
|
+
get(recordId: string): Promise<SoftrRecord>;
|
|
210
|
+
create(data: Record<string, unknown>): Promise<SoftrRecord>;
|
|
211
|
+
update(recordId: string, data: Record<string, unknown>): Promise<SoftrRecord>;
|
|
212
|
+
delete(recordId: string): Promise<void>;
|
|
213
|
+
}
|
|
214
|
+
interface WorkflowAPI {
|
|
215
|
+
trigger(payload?: Record<string, unknown>): Promise<void>;
|
|
216
|
+
}
|
|
217
|
+
interface AuthAPI {
|
|
218
|
+
/** Sign in with email and password. Clears the cached me() result. */
|
|
219
|
+
login(credentials: {
|
|
220
|
+
email: string;
|
|
221
|
+
password: string;
|
|
222
|
+
}): Promise<void>;
|
|
223
|
+
/** Sign out the current user. Clears the cached me() result. */
|
|
224
|
+
logout(): Promise<void>;
|
|
225
|
+
}
|
|
226
|
+
interface SoftrSDK {
|
|
227
|
+
/**
|
|
228
|
+
* Fetch the current user and their permissions from /me.
|
|
229
|
+
* Cached for the lifetime of the instance — create a new instance per request on the server.
|
|
230
|
+
*/
|
|
231
|
+
me(): Promise<SoftrMe>;
|
|
232
|
+
auth: AuthAPI;
|
|
233
|
+
datasource(id: string): DatasourceAPI;
|
|
234
|
+
workflow(id: string): WorkflowAPI;
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
export { isAccessError as C, isUnauthenticatedError as E, defineVisibility as S, isForbiddenError as T, SoftrContext as _, ListResult as a, User as b, SoftrRecord as c, WorkflowAPI as d, AccessBehaviour as f, RouteVisibility as g, AccessReason as h, ListOptions as i, SoftrSDK as l, AccessError as m, DatasourceAPI as n, SoftrMe as o, AccessCheck as p, DatasourcePermissions as r, SoftrPermissions as s, AuthAPI as t, SoftrUser as u, SoftrUserGroupId as v, isAlreadySignedInError as w, VisibilityHandle as x, SoftrUserGroups as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softr/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
"@tanstack/router-generator": "1.167.23"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
+
"@tanstack/react-router": "^1.168.0",
|
|
25
|
+
"@tanstack/react-start": "^1.168.0",
|
|
24
26
|
"@types/babel__traverse": "^7.28.0",
|
|
25
27
|
"@types/node": "^24.3.0",
|
|
26
28
|
"tsdown": "^0.15.0",
|
|
@@ -28,11 +30,11 @@
|
|
|
28
30
|
"vite": "^8.0.0"
|
|
29
31
|
},
|
|
30
32
|
"peerDependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
+
"vite": "^8.0.0",
|
|
34
|
+
"@tanstack/react-router": "^1.168.0",
|
|
35
|
+
"@tanstack/react-start": "^1.168.0"
|
|
33
36
|
},
|
|
34
37
|
"peerDependenciesMeta": {
|
|
35
|
-
"@tanstack/start": { "optional": true },
|
|
36
38
|
"vite": { "optional": true }
|
|
37
39
|
}
|
|
38
40
|
}
|
package/dist/types-BLLUI6MI.d.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
|
-
interface SoftrUser {
|
|
3
|
-
id: string;
|
|
4
|
-
email: string;
|
|
5
|
-
name: string;
|
|
6
|
-
userGroupIds: Array<SoftrUserGroupId>;
|
|
7
|
-
}
|
|
8
|
-
interface DatasourcePermissions {
|
|
9
|
-
read: boolean;
|
|
10
|
-
create: boolean;
|
|
11
|
-
update: boolean;
|
|
12
|
-
delete: boolean;
|
|
13
|
-
}
|
|
14
|
-
interface SoftrPermissions {
|
|
15
|
-
datasources: Record<string, DatasourcePermissions>;
|
|
16
|
-
workflows: Record<string, boolean>;
|
|
17
|
-
}
|
|
18
|
-
interface SoftrMe {
|
|
19
|
-
user: SoftrUser | null;
|
|
20
|
-
permissions: SoftrPermissions;
|
|
21
|
-
}
|
|
22
|
-
interface ListOptions {
|
|
23
|
-
page?: number;
|
|
24
|
-
limit?: number;
|
|
25
|
-
filters?: Array<{
|
|
26
|
-
field: string;
|
|
27
|
-
operator: string;
|
|
28
|
-
value: unknown;
|
|
29
|
-
}>;
|
|
30
|
-
sort?: Array<{
|
|
31
|
-
field: string;
|
|
32
|
-
direction: 'asc' | 'desc';
|
|
33
|
-
}>;
|
|
34
|
-
}
|
|
35
|
-
interface ListResult<T = SoftrRecord> {
|
|
36
|
-
records: T[];
|
|
37
|
-
total: number;
|
|
38
|
-
page: number;
|
|
39
|
-
limit: number;
|
|
40
|
-
}
|
|
41
|
-
interface SoftrRecord {
|
|
42
|
-
id: string;
|
|
43
|
-
[field: string]: unknown;
|
|
44
|
-
}
|
|
45
|
-
interface DatasourceAPI {
|
|
46
|
-
list(options?: ListOptions): Promise<ListResult>;
|
|
47
|
-
get(recordId: string): Promise<SoftrRecord>;
|
|
48
|
-
create(data: Record<string, unknown>): Promise<SoftrRecord>;
|
|
49
|
-
update(recordId: string, data: Record<string, unknown>): Promise<SoftrRecord>;
|
|
50
|
-
delete(recordId: string): Promise<void>;
|
|
51
|
-
}
|
|
52
|
-
interface WorkflowAPI {
|
|
53
|
-
trigger(payload?: Record<string, unknown>): Promise<void>;
|
|
54
|
-
}
|
|
55
|
-
interface AuthAPI {
|
|
56
|
-
/** Sign in with email and password. Clears the cached me() result. */
|
|
57
|
-
login(credentials: {
|
|
58
|
-
email: string;
|
|
59
|
-
password: string;
|
|
60
|
-
}): Promise<void>;
|
|
61
|
-
/** Sign out the current user. Clears the cached me() result. */
|
|
62
|
-
logout(): Promise<void>;
|
|
63
|
-
}
|
|
64
|
-
interface SoftrSDK {
|
|
65
|
-
/**
|
|
66
|
-
* Fetch the current user and their permissions from /me.
|
|
67
|
-
* Cached for the lifetime of the instance — create a new instance per request on the server.
|
|
68
|
-
*/
|
|
69
|
-
me(): Promise<SoftrMe>;
|
|
70
|
-
auth: AuthAPI;
|
|
71
|
-
datasource(id: string): DatasourceAPI;
|
|
72
|
-
workflow(id: string): WorkflowAPI;
|
|
73
|
-
}
|
|
74
|
-
//#endregion
|
|
75
|
-
export { ListResult as a, SoftrRecord as c, WorkflowAPI as d, ListOptions as i, SoftrSDK as l, DatasourceAPI as n, SoftrMe as o, DatasourcePermissions as r, SoftrPermissions as s, AuthAPI as t, SoftrUser as u };
|