@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
95 lines (94 loc) • 3.59 kB
TypeScript
import { VisibilityRule } from "./visibility.js";
import { AnyRouteMatch } from "@tanstack/react-router";
//#region src/route/page.d.ts
/** The part of TanStack's `head` context a page setting can read. */
type HeadContext = {
match: {
id: string;
};
matches: ReadonlyArray<{
id: string;
}>;
loaderData?: AnyRouteMatch["loaderData"];
};
type HeadSetting<T> = T | ((ctx: HeadContext) => T | undefined);
type PageSettings = {
/** Studio's label for the page, separate from its title. */
name?: string;
visibility?: VisibilityRule;
title?: HeadSetting<string>;
description?: HeadSetting<string>;
searchEngineIndexing?: HeadSetting<boolean>;
canonicalUrl?: HeadSetting<string>;
socialTitle?: HeadSetting<string>;
socialDescription?: HeadSetting<string>;
socialImage?: HeadSetting<string>;
};
declare const pageBrand: unique symbol;
/** The settings {@link definePage} has produced. */
type Page<S extends PageSettings = PageSettings> = S & {
readonly [pageBrand]: "page";
};
/**
* Declares a route's settings: its name in Studio, who may open it, its title and its SEO tags.
*
* One call per route file, at module scope, with an object literal. Studio
* reads and rewrites each setting written out as a literal; a computed one is
* changed in the code. A head setting that depends on the route's data is a
* function of its head context. The route's `beforeLoad` and `head` apply it.
*
* @example
* const pageSettings = definePage({
* visibility: { userGroupTypes: ["LOGGED_IN_USERS"] },
* title: ({ loaderData }) => loaderData?.name,
* })
*
* export const Route = createFileRoute("/_app/projects/$projectId")({
* beforeLoad: guardRoute(pageSettings),
* loader: ({ params }) => loadProject({ data: params.projectId }),
* head: pageHead(pageSettings),
* component: Project,
* })
*/
declare function definePage<const S extends PageSettings>(settings: S & { [K in Exclude<keyof S, keyof PageSettings>]: never }): Page<S>;
type Meta = NonNullable<AnyRouteMatch["meta"]>;
type Links = NonNullable<AnyRouteMatch["links"]>;
/**
* The router keeps the deepest route's tag for each title, name and property,
* so a layout's settings reach every page under it that does not state its own.
* A canonical URL names one page, so only the page the visitor is on renders
* its own.
*
* With `seo.enabled` in softr.json, a page also gets what search engines and
* link previews expect when it states nothing else: a canonical link to its own
* address, `og:url`, `og:title` and `og:description` from its title and
* description, and an absolute `og:image`.
*
* Returned rather than called with the route's context: a `head` written as
* `(ctx) => …` before `loader` stops TypeScript inferring the loader's types.
*
* @example
* head: pageHead(pageSettings)
*/
declare function pageHead(page: Page): (ctx: HeadContext) => {
meta: Meta;
links: Links;
};
/** A `head` script entry, in the shape TanStack's `scripts` array takes. */
type JsonLdScript = {
type: "application/ld+json";
children: string;
};
/**
* Structured data for search engines, as a `head` script entry. Record text is
* safe in it: a value such as `</script>` cannot close the tag.
*
* @example
* head: (ctx) => ({
* ...pageHead(pageSettings)(ctx),
* scripts: [jsonLdScript({ "@context": "https://schema.org", "@type": "Product", name: ctx.loaderData?.fields.name })],
* })
*/
declare function jsonLdScript(data: Record<string, unknown>): JsonLdScript;
//#endregion
export { Page, PageSettings, definePage, jsonLdScript, pageHead };