rutter
Version:
Framework-agnostic, lightweight router.
185 lines (178 loc) • 7.09 kB
TypeScript
import * as urlpattern_polyfill_dist_types from 'urlpattern-polyfill/dist/types';
type Value$1 = string | number | bigint | boolean | undefined;
type Data$1 = {
[key: string]: Value$1;
};
type URLBuilderOptions = {
params?: Data$1;
hash?: string;
queryParams?: Data$1;
};
declare const buildURL: (pathname: string, options?: URLBuilderOptions) => URL;
type SetRequired<V extends object, T extends keyof V> = Omit<V, T> & Pick<Required<V>, T>;
/** Unique route name */
type RouteName = string;
/** Default value */
type MetaValue = unknown;
type OptionalPatternFields = Partial<Pick<URLPattern, 'search' | 'hash'>>;
type RequiredPatternFields = Pick<URLPattern, 'pathname'>;
type PatternFields = OptionalPatternFields & RequiredPatternFields;
type Fields<Meta extends MetaValue> = {
/**
* Normalize patterns.
*
* Default: `true`.
* - Handles unspecified `hash` by accepting `*` wildcard.
* - Handles unspecified `search` query by accepting `*` wildcard.
* - Handles `pathname` with unspecified trailing slashes by adding `{/}?` pattern at the end.
*/
normalize?: boolean;
/**
* **Usage**: Mark as placeholder route or URL grouping segment.
*
* **Example**: In `/auth/:method`, `auth` isn't a route, just a URL grouping path. Since it's not a route, it's considered `404` page when accessed.
*/
ignore?: boolean;
/**
* Custom meta data
*/
meta?: Meta;
};
type Value<FieldsMeta extends MetaValue> = Fields<FieldsMeta>;
type Data<Name extends RouteName, FieldsMeta extends MetaValue> = Record<Name, SetRequired<Partial<PatternFields>, 'pathname'> & Value<FieldsMeta>>;
type RedirectMode = 'pushState' | 'replaceState';
type RedirectOptions = URLBuilderOptions & {
replace?: boolean;
};
type MainRedirectOptions = RedirectOptions | ((currentRouteState: URLBuilderOptions) => RedirectOptions);
type Options<RN extends RouteName, FieldsMeta extends MetaValue> = {
/** Register your routes here. */
routes: Data<RN, FieldsMeta>;
/** Replace with ponyfill. */
URLPattern?: unknown;
};
declare class CreateHistory<RN extends RouteName, FieldsMeta = MetaValue> {
#private;
/** History API based router. */
constructor({ routes, URLPattern }: Options<RN, FieldsMeta>);
/** Overall detail */
get summaryState(): {
url: URL;
routeData: Data<RN, FieldsMeta>;
withPattern: Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>;
details: Record<RN, Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>[RN] & {
/** `Matched against current route` */
isMatch: boolean;
/** `Matched against current route. In details` */
detail: urlpattern_polyfill_dist_types.URLPatternResult | null;
}>;
route: {
name: RN | undefined;
is404: boolean;
params: {
[x: string]: string | undefined;
};
queryParams: {
[k: string]: string;
};
hash: string;
info: ({
name: RN;
} & Record<RN, Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>[RN] & {
/** `Matched against current route` */
isMatch: boolean;
/** `Matched against current route. In details` */
detail: urlpattern_polyfill_dist_types.URLPatternResult | null;
}>[RN]) | undefined;
};
current: RN | undefined;
};
/** Current route detail */
get routeState(): {
name: RN | undefined;
is404: boolean;
params: {
[x: string]: string | undefined;
};
queryParams: {
[k: string]: string;
};
hash: string;
info: ({
name: RN;
} & Record<RN, Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>[RN] & {
/** `Matched against current route` */
isMatch: boolean;
/** `Matched against current route. In details` */
detail: urlpattern_polyfill_dist_types.URLPatternResult | null;
}>[RN]) | undefined;
};
getDetail: (name: RN) => Record<RN, Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>[RN] & {
/** `Matched against current route` */
isMatch: boolean;
/** `Matched against current route. In details` */
detail: urlpattern_polyfill_dist_types.URLPatternResult | null;
}>[RN];
getCurrentDetail: () => ({
name: RN;
} & Record<RN, Record<RN, {
pattern: URLPattern;
normalize: boolean;
} & Omit<Data<RN, FieldsMeta>[RN], "hash" | "search" | "pathname" | "normalize">>[RN] & {
/** `Matched against current route` */
isMatch: boolean;
/** `Matched against current route. In details` */
detail: urlpattern_polyfill_dist_types.URLPatternResult | null;
}>[RN]) | undefined;
/** Refresh route information */
update: () => void;
/** Check current route name */
on: (name: RN) => boolean;
/**
* Same as `on()` but accepts multiple route names.
*
* Returns `true` if one of them matches.
*/
onOneOf: (names: RN[]) => boolean;
/**
* Similar to `on()` except this only check for route pattern.
*
* Whereas `on` consider options such as `ignore`.
*/
onRouteMatch: (name: RN) => boolean;
/** Jump between routes. */
redirect: (name: RN, options?: MainRedirectOptions) => void;
/** Watch: `summaryState` */
watchSummaryState: (callback: (summaryState: CreateHistory<RN, FieldsMeta>['summaryState']) => void) => () => void;
/** Watch: `routeState` */
watchRouteState: (callback: (routeState: CreateHistory<RN, FieldsMeta>['routeState']) => void) => () => void;
/** De-register events, watchers */
destroy: () => void;
}
declare const mapValues: <Value extends object, Result extends object>(original: Value, iteratee: (value: Value[keyof Value], key: keyof Value, original: Value) => Result) => Record<keyof Value, Result>;
type index_URLBuilderOptions = URLBuilderOptions;
declare const index_buildURL: typeof buildURL;
declare const index_mapValues: typeof mapValues;
declare namespace index {
export {
index_URLBuilderOptions as URLBuilderOptions,
index_buildURL as buildURL,
index_mapValues as mapValues,
};
}
export { CreateHistory, Data, MainRedirectOptions, MetaValue, RedirectMode, RouteName, index as utility };