@kitbag/router 0.22.4 → 0.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { History, Listener } from 'history';
1
+ import { History, Listener } from './history';
2
2
  type NavigationPushOptions = {
3
3
  replace?: boolean;
4
4
  state?: unknown;
@@ -1,4 +1,7 @@
1
1
  import { ResolvedRoute } from '../types/resolved';
2
2
  import { Routes } from '../types/route';
3
3
  import { RouterResolveOptions } from '../types/routerResolve';
4
- export declare function getMatchForUrl(routes: Routes, url: string, options?: RouterResolveOptions): ResolvedRoute | undefined;
4
+ import { ParseUrlOptions } from '../types/url';
5
+ type MatchOptions = RouterResolveOptions & ParseUrlOptions;
6
+ export declare function getMatchForUrl(routes: Routes, url: string, options?: MatchOptions): ResolvedRoute | undefined;
7
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ export type Action = 'POP' | 'PUSH' | 'REPLACE';
2
+ export type Pathname = string;
3
+ export type Search = string;
4
+ export type Hash = string;
5
+ export type Key = string;
6
+ export interface Path {
7
+ pathname: Pathname;
8
+ search: Search;
9
+ hash: Hash;
10
+ }
11
+ export interface Location extends Path {
12
+ state: unknown;
13
+ key: Key;
14
+ }
15
+ export interface Update {
16
+ action: Action;
17
+ location: Location;
18
+ }
19
+ export type Listener = (update: Update) => void;
20
+ export interface Transition extends Update {
21
+ retry: () => void;
22
+ }
23
+ export type Blocker = (tx: Transition) => void;
24
+ export type To = string | Partial<Path>;
25
+ export interface History {
26
+ readonly action: Action;
27
+ readonly location: Location;
28
+ createHref: (to: To) => string;
29
+ push: (to: To, state?: unknown) => void;
30
+ replace: (to: To, state?: unknown) => void;
31
+ go: (delta: number) => void;
32
+ back: () => void;
33
+ forward: () => void;
34
+ listen: (listener: Listener) => () => void;
35
+ block: (blocker: Blocker) => () => void;
36
+ }
37
+ export interface BrowserHistory extends History {
38
+ }
39
+ export interface HashHistory extends History {
40
+ }
41
+ export interface MemoryHistory extends History {
42
+ readonly index: number;
43
+ }
44
+ export type BrowserHistoryOptions = {
45
+ window?: Window;
46
+ };
47
+ export type HashHistoryOptions = {
48
+ window?: Window;
49
+ };
50
+ export type InitialEntry = string | Partial<Location>;
51
+ export type MemoryHistoryOptions = {
52
+ initialEntries?: InitialEntry[];
53
+ initialIndex?: number;
54
+ };
55
+ export declare function createPath({ pathname, search, hash }: Partial<Path>): string;
56
+ export declare function parsePath(path: string): Partial<Path>;
57
+ export declare function createBrowserHistory(options?: BrowserHistoryOptions): BrowserHistory;
58
+ export declare function createHashHistory(options?: HashHistoryOptions): HashHistory;
59
+ export declare function createMemoryHistory(options?: MemoryHistoryOptions): MemoryHistory;
@@ -1,19 +1,19 @@
1
- import { ExtractParamName, ParamEnd, ParamIsGreedy, ParamIsOptional, ParamStart } from '../types/params';
1
+ import { ExtractParamName, ParamEnd, ParamIsGreedy, ParamIsOptionalOrHasDefault, ParamStart } from '../types/params';
2
2
  import { Param } from '../types/paramTypes';
3
3
  import { Identity } from '../types/utilities';
4
4
  import { MakeOptional } from '../utilities/makeOptional';
5
5
  type WithParamsParamsInput<TValue extends string> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? Record<ExtractParamName<TParam>, Param | undefined> & WithParamsParamsInput<Rest> : {};
6
6
  type WithParamsParamsOutput<TValue extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? ExtractParamName<TParam> extends keyof TParams ? TParams[ExtractParamName<TParam>] extends Param ? Record<ExtractParamName<TParam>, {
7
7
  param: TParams[ExtractParamName<TParam>];
8
- isOptional: ParamIsOptional<TParam>;
8
+ isOptional: ParamIsOptionalOrHasDefault<TParam, TParams[ExtractParamName<TParam>]>;
9
9
  isGreedy: ParamIsGreedy<TParam>;
10
10
  }> & WithParamsParamsOutput<Rest, TParams> : Record<ExtractParamName<TParam>, {
11
11
  param: StringConstructor;
12
- isOptional: ParamIsOptional<TParam>;
12
+ isOptional: ParamIsOptionalOrHasDefault<TParam, TParams[ExtractParamName<TParam>]>;
13
13
  isGreedy: ParamIsGreedy<TParam>;
14
14
  }> & WithParamsParamsOutput<Rest, TParams> : Record<ExtractParamName<TParam>, {
15
15
  param: StringConstructor;
16
- isOptional: ParamIsOptional<TParam>;
16
+ isOptional: ParamIsOptionalOrHasDefault<TParam, TParams[ExtractParamName<TParam>]>;
17
17
  isGreedy: ParamIsGreedy<TParam>;
18
18
  }> & WithParamsParamsOutput<Rest, TParams> : {};
19
19
  declare const UrlPartsWithParamsSymbol: unique symbol;
@@ -57,13 +57,13 @@ export type ToUrlQueryPart<T extends UrlQueryPart | string | undefined> = T exte
57
57
  type QueryRecordToUrlPart<T extends Record<string, Param>> = {
58
58
  [K in keyof T as T[K] extends string ? never : ExtractParamName<K & string>]: {
59
59
  param: T[K];
60
- isOptional: ParamIsOptional<K & string>;
60
+ isOptional: ParamIsOptionalOrHasDefault<K & string, T[K]>;
61
61
  isGreedy: false;
62
62
  };
63
63
  };
64
64
  type QueryArrayToUrlPart<T extends [string, string | Param][]> = T extends [infer First extends [string, string | Param], ...infer Rest extends [string, string | Param][]] ? First extends [string, string] ? {} : First extends [infer TKey extends string, infer TValue extends Param] ? Identity<Record<ExtractParamName<TKey>, {
65
65
  param: TValue;
66
- isOptional: ParamIsOptional<TKey>;
66
+ isOptional: ParamIsOptionalOrHasDefault<TKey, TValue>;
67
67
  isGreedy: false;
68
68
  }> & QueryArrayToUrlPart<Rest>> : never : {};
69
69
  export declare function toUrlQueryPart<T extends UrlQueryPart | string | undefined>(querySource: T): ToUrlQueryPart<T>;
@@ -102,8 +102,9 @@ type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions,
102
102
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends false ? K : never]: PropsGetter<TOptions, TComponents[K]>;
103
103
  };
104
104
  export type CreateRouteProps<TOptions extends CreateRouteOptions = CreateRouteOptions> = TOptions['component'] extends Component ? PropsGetter<TOptions, TOptions['component']> : TOptions['components'] extends Record<string, Component> ? RoutePropsRecord<TOptions, TOptions['components']> : RouterViewPropsGetter<TOptions>;
105
- type ToMatch<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = Omit<TOptions, 'props' | 'meta'> & {
105
+ type ToMatch<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = Omit<TOptions, 'props' | 'meta' | 'name'> & {
106
106
  id: string;
107
+ name: ToName<TOptions['name']>;
107
108
  props: TProps;
108
109
  /**
109
110
  * Represents additional metadata associated with a route. Always present, defaults to empty object.
@@ -36,3 +36,4 @@ export type ExtractParamName<TParam extends PropertyKey> = TParam extends string
36
36
  export type ExtractParamType<TParam extends Param> = Param extends TParam ? unknown : TParam extends ParamGetSet<infer Type> ? Type : TParam extends ParamGetter ? ReturnType<TParam> : TParam extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TParam> : TParam extends LiteralParam ? TParam : string;
37
37
  export type ParamIsOptional<TParam extends string> = TParam extends `?${string}` ? true : false;
38
38
  export type ParamIsGreedy<TParam extends string> = TParam extends `${string}*` ? true : false;
39
+ export type ParamIsOptionalOrHasDefault<TParamName extends string, TParam extends Param | undefined> = ParamIsOptional<TParamName> extends true ? true : TParam extends Required<ParamGetSet> ? true : false;
@@ -163,10 +163,11 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
163
163
  hasDevtools: boolean;
164
164
  };
165
165
  /**
166
- * This type is the same as `RouterRoute<ResolvedRoute<TRoutes[number]>>` while remaining distributive
166
+ * This type is the same as `RouterRoute<ResolvedRoute<TRoutes[number]>>` while remaining distributive.
167
+ * Routes without a name (empty string) are excluded so that router.route.name is never ''.
167
168
  */
168
169
  export type RouterRouteUnion<TRoutes extends Routes> = {
169
- [K in keyof TRoutes]: RouterRoute<ResolvedRoute<TRoutes[K]>>;
170
+ [K in keyof TRoutes]: TRoutes[K]['name'] extends '' ? never : RouterRoute<ResolvedRoute<TRoutes[K]>>;
170
171
  }[number];
171
172
  export type RouterRoutes<TRouter extends Router> = TRouter extends Router<infer TRoutes extends Routes> ? TRoutes : Routes;
172
173
  export type RouterRejections<TRouter extends Router> = TRouter extends Router<any, infer TOptions extends RouterOptions, infer TPlugins extends RouterPlugin> ? ExtractRejections<TOptions> | ExtractRejections<TPlugins> : [];
@@ -19,6 +19,13 @@ export type ToUrl<TOptions extends CreateUrlOptions> = Url<Identity<ToUrlPart<TO
19
19
  export declare function isUrlWithSchema(url: unknown): url is Url & {
20
20
  schema: Record<string, UrlPart>;
21
21
  };
22
+ export type ParseUrlOptions = {
23
+ /**
24
+ * Whether to remove trailing slashes from the path. When true, trailing slashes will be removed from the path.
25
+ * @default true
26
+ */
27
+ removeTrailingSlashes?: boolean;
28
+ };
22
29
  /**
23
30
  * Represents the structure of a url parts. Can be used to create a url with support for params.
24
31
  */
@@ -35,11 +42,11 @@ export type Url<TParams extends UrlParams = UrlParams> = {
35
42
  /**
36
43
  * Parses the url supplied and returns any params found.
37
44
  */
38
- parse(url: string): ToUrlParamsReading<TParams>;
45
+ parse(url: string, options?: ParseUrlOptions): ToUrlParamsReading<TParams>;
39
46
  /**
40
47
  * Parses the url supplied and returns any params found.
41
48
  */
42
- tryParse(url: string): {
49
+ tryParse(url: string, options?: ParseUrlOptions): {
43
50
  success: true;
44
51
  params: ToUrlParamsReading<TParams>;
45
52
  } | {
@@ -1,5 +1,6 @@
1
1
  export declare function isDefined<T>(value: T | undefined): value is T;
2
2
  export declare function isRecord(value: unknown): value is Record<PropertyKey, unknown>;
3
+ export declare function isPropertyKey(value: unknown): value is PropertyKey;
3
4
  export declare function hasProperty<TSource extends Record<PropertyKey, unknown>, TProperty extends PropertyKey, TType extends () => unknown>(value: TSource, key: TProperty, type?: TType): value is TSource & Record<TProperty, ReturnType<TType>>;
4
5
  export declare function stringHasValue(value: string | undefined): value is string;
5
6
  export type StringHasValue<T> = string extends T ? true : '' extends T ? false : T extends string ? true : false;