@teardown/navigation 2.0.80 → 2.0.82

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.
@@ -2,8 +2,13 @@
2
2
  * Router module exports
3
3
  */
4
4
 
5
- export type { ExtractRoutePaths, ExtractScreens, TeardownRouterOptions } from "./create-router";
6
- export { createTeardownRouter } from "./create-router";
5
+ export type {
6
+ ExtractRoutePaths,
7
+ ExtractScreens,
8
+ RouteTreeRouterOptions,
9
+ TeardownRouterOptions,
10
+ } from "./create-router";
11
+ export { createTeardownRouter, createTeardownRouterFromTree } from "./create-router";
7
12
  // Legacy type aliases for backwards compatibility
8
13
  export type {
9
14
  DrawerScreenOptions,
@@ -2,6 +2,18 @@
2
2
  * Type utilities and definitions for @teardown/navigation
3
3
  */
4
4
 
5
+ // Route builder types
6
+ export type {
7
+ AccumulateParams,
8
+ AnyRouteDefinition,
9
+ ComputeFullPath,
10
+ ExtractRouteParams,
11
+ ExtractRoutePaths,
12
+ MergeRouteParams,
13
+ ParamsForPath,
14
+ RouteDefinitionBase,
15
+ StripRouteGroups,
16
+ } from "./route-builder-types";
5
17
  // Route type definitions
6
18
  export type {
7
19
  NavigationState,
@@ -22,7 +34,9 @@ export type {
22
34
  InferParams,
23
35
  IsCatchAllSegment,
24
36
  IsDynamicSegment,
37
+ IsEmptyParams,
25
38
  IsOptionalSegment,
26
39
  NavigateArgs,
27
40
  Simplify,
41
+ ToReactNavigationPath,
28
42
  } from "./type-utils";
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Route builder type utilities for @teardown/navigation
3
+ * Enables compile-time path computation and param accumulation
4
+ */
5
+
6
+ import type { InferParams, Simplify } from "./type-utils";
7
+
8
+ /**
9
+ * Compute full path from parent chain
10
+ * Handles route groups by stripping (groupName) segments
11
+ */
12
+ export type ComputeFullPath<TPath extends string, TParent> = TParent extends {
13
+ fullPath: infer P extends string;
14
+ }
15
+ ? P extends "/"
16
+ ? `/${StripRouteGroups<TPath>}`
17
+ : StripRouteGroups<TPath> extends ""
18
+ ? P
19
+ : `${P}/${StripRouteGroups<TPath>}`
20
+ : StripRouteGroups<TPath> extends ""
21
+ ? "/"
22
+ : `/${StripRouteGroups<TPath>}`;
23
+
24
+ /**
25
+ * Strip route group segments from path
26
+ * (auth)/login -> login
27
+ * (auth) -> ""
28
+ */
29
+ export type StripRouteGroups<TPath extends string> = TPath extends `(${string})/${infer Rest}`
30
+ ? StripRouteGroups<Rest>
31
+ : TPath extends `(${string})`
32
+ ? ""
33
+ : TPath;
34
+
35
+ /**
36
+ * Accumulate params from parent chain + this route
37
+ */
38
+ export type AccumulateParams<TPath extends string, TParent> = Simplify<
39
+ InferParams<TPath> & (TParent extends { allParams: infer P } ? P : {})
40
+ >;
41
+
42
+ /**
43
+ * Base interface for all route definitions (screens and layouts)
44
+ */
45
+ export interface RouteDefinitionBase<
46
+ TPath extends string = string,
47
+ TFullPath extends string = string,
48
+ TParams = unknown,
49
+ TAllParams = unknown,
50
+ TContext = unknown,
51
+ > {
52
+ /** This route segment path */
53
+ readonly path: TPath;
54
+ /** Full path from root */
55
+ readonly fullPath: TFullPath;
56
+ /** Params from this route only */
57
+ readonly params: TParams;
58
+ /** Accumulated params from parent chain */
59
+ readonly allParams: TAllParams;
60
+ /** Loader context type */
61
+ readonly context: TContext;
62
+ }
63
+
64
+ /**
65
+ * Type for any route definition (screen or layout)
66
+ */
67
+ export type AnyRouteDefinition = RouteDefinitionBase<string, string, unknown, unknown, unknown>;
68
+
69
+ /**
70
+ * Extract all route paths from a route tree
71
+ */
72
+ export type ExtractRoutePaths<T> = T extends { fullPath: infer P extends string }
73
+ ?
74
+ | P
75
+ | (T extends { children: infer C }
76
+ ? C extends readonly (infer Child)[]
77
+ ? ExtractRoutePaths<Child>
78
+ : never
79
+ : never)
80
+ : never;
81
+
82
+ /**
83
+ * Extract route params map from a route tree
84
+ */
85
+ export type ExtractRouteParams<T> = T extends { fullPath: infer P extends string; allParams: infer Params }
86
+ ? { [K in P]: Params } & (T extends { children: infer C }
87
+ ? C extends readonly (infer Child)[]
88
+ ? ExtractRouteParams<Child>
89
+ : {}
90
+ : {})
91
+ : {};
92
+
93
+ /**
94
+ * Merge two route param maps
95
+ */
96
+ export type MergeRouteParams<A, B> = Simplify<A & B>;
97
+
98
+ /**
99
+ * Get params for a specific path from a route tree
100
+ */
101
+ export type ParamsForPath<TTree, TPath extends string> = TPath extends keyof ExtractRouteParams<TTree>
102
+ ? ExtractRouteParams<TTree>[TPath]
103
+ : never;
@@ -11,19 +11,46 @@ export type Simplify<T> = { [K in keyof T]: T[K] } & {};
11
11
 
12
12
  /**
13
13
  * Extracts parameter from a single path segment
14
+ * Supports both [param] and :param syntax
14
15
  *
15
- * - [userId] -> { userId: string }
16
- * - [...slug] -> { slug: string[] }
17
- * - [section]? -> { section?: string }
16
+ * - [userId] or :userId -> { userId: string }
17
+ * - [...slug] or *slug -> { slug: string[] }
18
+ * - [section]? or :section? -> { section?: string }
18
19
  * - about -> Record<string, never>
19
20
  */
20
- export type ExtractParam<Segment extends string> = Segment extends `[...${infer Rest}]`
21
- ? { [K in Rest]: string[] }
22
- : Segment extends `[${infer Param}]?`
23
- ? { [K in Param]?: string }
24
- : Segment extends `[${infer Param}]`
25
- ? { [K in Param]: string }
26
- : Record<string, never>;
21
+ export type ExtractParam<Segment extends string> =
22
+ // Catch-all: [...slug] or *slug
23
+ Segment extends `[...${infer Rest}]`
24
+ ? { [K in Rest]: string[] }
25
+ : Segment extends `*${infer Rest}`
26
+ ? { [K in Rest]: string[] }
27
+ : // Optional: [param]? or :param?
28
+ Segment extends `[${infer Param}]?`
29
+ ? { [K in Param]?: string }
30
+ : Segment extends `:${infer Param}?`
31
+ ? { [K in Param]?: string }
32
+ : // Required: [param] or :param
33
+ Segment extends `[${infer Param}]`
34
+ ? { [K in Param]: string }
35
+ : Segment extends `:${infer Param}`
36
+ ? { [K in Param]: string }
37
+ : // Static segment
38
+ Record<string, never>;
39
+
40
+ /**
41
+ * Check if params object is empty (no required params)
42
+ */
43
+ export type IsEmptyParams<T> = keyof T extends never ? true : false;
44
+
45
+ /**
46
+ * Convert [param] syntax to :param syntax for React Navigation
47
+ */
48
+ export type ToReactNavigationPath<TPath extends string> =
49
+ TPath extends `${infer Before}[...${infer Param}]${infer After}`
50
+ ? ToReactNavigationPath<`${Before}*${Param}${After}`>
51
+ : TPath extends `${infer Before}[${infer Param}]${infer After}`
52
+ ? ToReactNavigationPath<`${Before}:${Param}${After}`>
53
+ : TPath;
27
54
 
28
55
  /**
29
56
  * Recursively extracts all params from a path