UNPKG

@prezly/theme-kit-core

Version:

Data layer and utility library for developing Prezly themes with JavaScript

38 lines (37 loc) 1.56 kB
import type { Locale } from '@prezly/theme-kit-intl'; import UrlPattern from 'url-pattern'; import type { ExtractPathParams } from './types'; type Awaitable<T> = T | Promise<T>; export type Route<Pattern = string, Match = unknown> = { /** * Route match pattern. */ pattern: Pattern; /** * Match incoming request path and search params against the route definition. */ match(path: string, searchParams: URLSearchParams): Match | undefined; /** * Low-level route URL generator. * Unaware of newsroom locales and how to shorten locale slugs. */ generate(params: Match): `/${string}`; /** * Rewrite external route URL to the internal unambiguous pages layout URL. */ rewrite(params: Match): string; /** * Manually resolve locale code from the request parameters, * if the stock logic is not suitable for the current route. */ resolveLocale?(params: Match): Awaitable<Locale.Code | undefined>; }; export declare namespace Route { interface Options<Pattern extends string, Match> { check?(match: Match, searchParams: URLSearchParams): boolean; generate?(pattern: UrlPattern, params: ExtractPathParams<Pattern>): `/${string}`; resolveLocale?: Route<Pattern, Match>['resolveLocale']; } function create<Pattern extends `/${string}` | `(/:${string})/${string}`, Match extends ExtractPathParams<Pattern>>(pattern: Pattern, rewrite: string, { check, generate, resolveLocale }?: Options<Pattern, Match>): Route<Pattern, Match>; } export {};