@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
88 lines (86 loc) • 3.31 kB
TypeScript
import { AccessContext, AccessReason, Narrowed, VisibilityRule } from "./visibility.js";
import { Page, PageSettings } from "./page.js";
//#region src/route/route.d.ts
/**
* What a guard does when it denies a visitor: redirect to a path, or throw an
* {@link AccessError} for the route's `errorComponent` to render. A bare string
* is shorthand for redirecting there.
*/
type AccessBehaviour = string | {
strategy: "redirect";
to: string;
} | {
strategy: "throw";
};
/** Per-reason overrides of the default {@link AccessBehaviour}. */
type GuardOverrides = Partial<Record<AccessReason, AccessBehaviour>>;
/**
* The access error a guard throws. It is plain data, not an `Error` subclass, so
* take it as `unknown` in an `errorComponent` and narrow it with
* {@link isAccessError}. It carries the reason and nothing else: the error is
* serialized into the page, so it never names the groups the route wanted.
*
* @example
* errorComponent: ({ error }: { error: unknown }) =>
* isAccessError(error) ? <NoAccess /> : <AppError />
*/
type AccessError = {
type: "access_error";
reason: AccessReason;
};
/**
* True for any access error thrown by a guard, whatever its reason.
*
* An access error arrives as `unknown` — hydrated from the page, or thrown by
* anything else at all — so every field is checked before it is narrowed.
*/
declare function isAccessError(error: unknown): error is AccessError;
/** True for an access error thrown because nobody was signed in. */
declare function isUnauthenticatedError(error: unknown): error is AccessError;
/** True for an access error thrown because the visitor was in none of the groups. */
declare function isForbiddenError(error: unknown): error is AccessError;
/** True for an access error thrown because a signed-out-only route saw a user. */
declare function isAlreadyAuthenticatedError(error: unknown): error is AccessError;
type GuardedPage = Page<PageSettings & {
visibility: VisibilityRule;
}>;
/**
* A `beforeLoad` handler that denies a visitor the page's rule does not admit.
*
* Each reason falls back to the app's configured behaviour. Pass `overrides` to
* change what any one reason does. A redirected signed-out visitor carries the
* page they wanted, for `toReturnPath` to read back.
*
* Throws a router redirect, so it belongs in `beforeLoad` and nowhere else.
*
* @example
* beforeLoad: guardRoute(pageSettings, { unauthenticated: "/sign-in" })
*/
declare function guardRoute(page: GuardedPage, overrides?: GuardOverrides): (opts: {
context: AccessContext;
location: {
href: string;
};
}) => void;
/**
* Throws what {@link guardRoute} would, for a `beforeLoad` that runs its own
* checks after the rule. It takes no overrides.
*
* @example
* beforeLoad: (opts) => {
* assertRouteAccess(pageSettings, opts)
* if (opts.params.projectId === "demo") throw redirect({ to: "/projects" })
* }
*/
declare function assertRouteAccess<R extends VisibilityRule, O extends {
context: AccessContext;
location: {
href: string;
};
}>(page: Page<PageSettings & {
visibility: R;
}>, opts: O): asserts opts is O & {
context: Narrowed<R>;
};
//#endregion
export { assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError };