sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
303 lines • 8.04 kB
TypeScript
/**
* @public
*/
interface RouteSegment {
/**
* The name of the segment.
*/
name: string;
/**
* The type of the segment.
* Can be either "dir" or "param".
*/
type: 'dir' | 'param';
}
/**
* @public
*/
interface RouteTransform<T> {
/**
* Converts a path string to a state object.
*/
toState: (value: string) => T;
/**
* Converts a state object to a path string.
*/
toPath: (value: T) => string;
}
/**
* @public
*/
interface Route {
/**
* The raw string representation of the route.
*/
raw: string;
/**
* An array of route segments that make up the route.
* See {@link RouteSegment}
*/
segments: RouteSegment[];
/**
* An optional object containing route transforms.
* See {@link RouteTransform} and {@link RouterState}
*/
transform?: {
[key: string]: RouteTransform<RouterState>;
};
}
/**
* @public
*/
type RouteChildren = RouterNode[] | ((state: RouterState) => Router | RouterNode | RouterNode[] | undefined | false);
/**
* @public
*/
interface RouterNode {
/**
* The route information for this node. See {@link Route}
*/
route: Route;
/**
* An optional scope for this node.
*/
scope?: string;
/**
* Optionally disable scoping of search params
* Scoped search params will be represented as scope[param]=value in the url
* Disabling this will still scope search params based on any parent scope unless the parent scope also has disabled search params scoping
* Caution: enabling this can cause conflicts with multiple plugins defining search params with the same name
*/
__unsafe_disableScopedSearchParams?: boolean;
/**
* An optional object containing transforms to apply to this node.
* See {@link RouteTransform} and {@link RouterState}
*/
transform?: {
[key: string]: RouteTransform<RouterState>;
};
/**
* The child nodes of this node. See {@link RouteChildren}
*/
children: RouteChildren;
}
/**
* @public
*/
interface Router extends RouterNode {
/**
* Indicates whether this router is a route.
* @internal
*/
_isRoute: boolean;
/**
* Encodes the specified router state into a path string.
* See {@link RouterState}
*
*/
encode: (state: RouterState) => string;
/**
* Decodes the specified path string into a router state.
* See {@link RouterState}
*/
decode: (path: string) => RouterState | null;
/**
* Determines whether the specified path is not found.
*/
isNotFound: (path: string) => boolean;
/**
* Gets the base path of this router.
*/
getBasePath: () => string;
/**
* Gets the redirect base of this router.
*/
getRedirectBase: (pathname: string) => string | null;
/**
* Determines whether the specified path is the root path.
*/
isRoot: (path: string) => boolean;
}
/** @internal */
type InternalSearchParam = [scopedPath: string[], value: string];
/** @internal */
interface MatchOk {
type: 'ok';
node: RouterNode;
matchedState: Record<string, string>;
searchParams: InternalSearchParam[];
child: MatchOk | undefined;
}
/** @internal */
interface MatchError {
type: 'error';
node: RouterNode;
/**
* Parameters found in the route string but not provided as a key in the state object
*/
missingKeys: string[];
/**
* These are keys found in the state object but not in the route definition (and can't be mapped to a child route)
*/
unmappableStateKeys: string[];
}
/** @internal */
type MatchResult = MatchError | MatchOk;
/**
* @public
*/
interface NavigateBaseOptions {
replace?: boolean;
}
/**
* @public
*/
interface NavigateOptions extends NavigateBaseOptions {
stickyParams?: Record<string, string | undefined | null>;
}
/**
* @public
*/
interface NavigateOptionsWithState extends NavigateOptions {
state?: RouterState | null;
}
/**
* @public
*/
interface RouterContextValue {
/**
* Resolves the path from the given router state. See {@link RouterState}
*
* When state is null, it will resolve the path from the current state
* and navigate to the root path.
*/
resolvePathFromState: (state: RouterState | null) => string;
/**
* Resolves the intent link for the given intent name and parameters.
* See {@link IntentParameters}
*/
resolveIntentLink: (intentName: string, params?: IntentParameters, searchParams?: SearchParam[]) => string;
/**
* Navigates to the given URL.
* The function requires an object that has a path and an optional replace property.
*/
navigateUrl: (opts: {
path: string;
replace?: boolean;
}) => void;
/**
* @deprecated Use `navigate({stickyParams: params, ...options})` instead
*/
navigateStickyParams: (params: NavigateOptions['stickyParams'], options?: NavigateBaseOptions) => void;
/**
* Updates the router state and navigates to a new path.
* Allows specifying new state values and optionally merging sticky parameters.
*
* See {@link RouterState} and {@link NavigateOptions}
*
* @public
*
* @example Navigate with sticky params only, staying on the current path
* ```tsx
* router.navigate({stickyParams: {baz: 'qux'}})
* ```
* @remarks `null` sticky parameter value will remove the sticky parameter from the url
*
* @example Navigate with state and sticky params
* ```tsx
* router.navigate({stickyParams: {baz: 'qux'}, state: {foo: 'bar'}})
* ```
*
* @example Navigate to root path
* ```tsx
* router.navigate({stickyParams: {baz: 'qux'}, state: null})
* ```
*/
navigate: {
(nextState: RouterState, options?: NavigateOptions): void;
(options: NavigateOptions & {
state?: RouterState | null;
}): void;
};
/**
* Navigates to the given intent.
* See {@link RouterState} and {@link NavigateBaseOptions}
*/
navigateIntent: (intentName: string, params?: IntentParameters, options?: NavigateBaseOptions) => void;
/**
* The current router state. See {@link RouterState}
*/
state: RouterState;
/**
* The current router state. See {@link RouterState}
*/
stickyParams: Record<string, string | undefined | null>;
}
/**
* Base intent parameters
*
* @public
* @todo dedupe with core/structure
*/
interface BaseIntentParams {
/**
* Document schema type name to create/edit.
* Required for `create` intents, optional for `edit` (but encouraged, safer and faster)
*/
type?: string;
/**
* ID of the document to create/edit.
* Required for `edit` intents, optional for `create`.
*/
id?: string;
template?: string;
/**
* Experimental field path
*
* @beta
* @experimental
* @hidden
*/
path?: string;
/**
* Optional "mode" to use for edit intent.
* Known modes are `structure` and `presentation`.
*/
mode?: string;
/**
* Arbitrary/custom parameters are generally discouraged - try to keep them to a minimum,
* or use `payload` (arbitrary JSON-serializable object) instead.
*/
[key: string]: string | undefined;
}
/**
* Intent parameters (json)
*
* @public
*/
type IntentJsonParams = {
[key: string]: any;
};
/**
* @public
* @todo dedupe with intent types in core
*/
type IntentParameters = BaseIntentParams | [BaseIntentParams, IntentJsonParams];
/**
* @public
*/
type SearchParam = [key: string, value: string];
/**
* @public
*/
type RouterState = Record<string, unknown> & {
_searchParams?: SearchParam[];
};
/**
* Type representing either a new router state or navigation options with an optional state.
* @internal
*/
type NextStateOrOptions = RouterState | (NavigateOptions & {
state?: RouterState | null;
});
export { RouterContextValue as _, MatchError as a, SearchParam as b, NavigateBaseOptions as c, NextStateOrOptions as d, Route as f, Router as g, RouteTransform as h, InternalSearchParam as i, NavigateOptions as l, RouteSegment as m, IntentJsonParams as n, MatchOk as o, RouteChildren as p, IntentParameters as r, MatchResult as s, BaseIntentParams as t, NavigateOptionsWithState as u, RouterNode as v, RouterState as y };