@koordinates/xstate-tree 2.0.11 → 3.0.0-beta.1
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.
- package/lib/routing/createRoute/createRoute.js +107 -135
- package/lib/routing/joinRoutes.js +11 -1
- package/lib/xstate-tree.d.ts +47 -77
- package/package.json +2 -2
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { match, compile } from "path-to-regexp";
|
|
2
2
|
import { parse, stringify } from "query-string";
|
|
3
|
-
import { isNil } from "../../utils";
|
|
4
3
|
import { joinRoutes } from "../joinRoutes";
|
|
5
4
|
/**
|
|
6
5
|
* @public
|
|
@@ -19,163 +18,136 @@ export function buildCreateRoute(history, basePath) {
|
|
|
19
18
|
});
|
|
20
19
|
}
|
|
21
20
|
return {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* `reverse`, a function that is passed params/query objects and turns them into a URL
|
|
34
|
-
*
|
|
35
|
-
* The params and query schemas are ZodSchemas, they both need to be an object (ie Z.object())
|
|
36
|
-
*/
|
|
37
|
-
dynamicRoute: function createDynamicRoute(opts) {
|
|
38
|
-
return ({ event, matches, reverse, }) => {
|
|
39
|
-
return {
|
|
40
|
-
paramsSchema: opts === null || opts === void 0 ? void 0 : opts.params,
|
|
41
|
-
querySchema: opts === null || opts === void 0 ? void 0 : opts.query,
|
|
42
|
-
event,
|
|
43
|
-
history,
|
|
44
|
-
basePath,
|
|
45
|
-
parent: undefined,
|
|
46
|
-
// @ts-ignore the usual
|
|
47
|
-
getEvent({ params, query, meta } = {}) {
|
|
48
|
-
return { type: event, params, query, meta };
|
|
49
|
-
},
|
|
50
|
-
// @ts-ignore not sure how to type this
|
|
51
|
-
matches(url, search) {
|
|
52
|
-
const query = parse(search);
|
|
53
|
-
const match = matches(url, query);
|
|
21
|
+
simpleRoute(baseRoute) {
|
|
22
|
+
return ({ url, paramsSchema, querySchema, ...args }) => {
|
|
23
|
+
const matcher = match(url, { end: false });
|
|
24
|
+
const reverser = compile(url);
|
|
25
|
+
return this.route(baseRoute)({
|
|
26
|
+
...args,
|
|
27
|
+
paramsSchema,
|
|
28
|
+
querySchema,
|
|
29
|
+
// @ts-ignore :cry:
|
|
30
|
+
matcher: (url, query) => {
|
|
31
|
+
const match = matcher(url);
|
|
54
32
|
if (match === false) {
|
|
55
|
-
return
|
|
33
|
+
return false;
|
|
56
34
|
}
|
|
57
|
-
|
|
58
|
-
|
|
35
|
+
const params = match.params;
|
|
36
|
+
if (params && paramsSchema) {
|
|
37
|
+
paramsSchema.parse(params);
|
|
59
38
|
}
|
|
60
|
-
if (
|
|
61
|
-
|
|
39
|
+
if (query && querySchema) {
|
|
40
|
+
querySchema.parse(query);
|
|
62
41
|
}
|
|
63
|
-
return {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return reverse({ params, query });
|
|
42
|
+
return {
|
|
43
|
+
matchLength: match.path.length,
|
|
44
|
+
params,
|
|
45
|
+
query,
|
|
46
|
+
};
|
|
69
47
|
},
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
meta,
|
|
78
|
-
history: this.history,
|
|
79
|
-
});
|
|
48
|
+
// @ts-ignore :cry:
|
|
49
|
+
reverser: (args) => {
|
|
50
|
+
const url = reverser(args.params);
|
|
51
|
+
if (args.query) {
|
|
52
|
+
return `${url}?${stringify(args.query)}`;
|
|
53
|
+
}
|
|
54
|
+
return url;
|
|
80
55
|
},
|
|
81
|
-
};
|
|
56
|
+
});
|
|
82
57
|
};
|
|
83
58
|
},
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* 3. The parents meta type is merged with the routes meta type
|
|
103
|
-
*/
|
|
104
|
-
staticRoute: function createStaticRoute(baseRoute) {
|
|
105
|
-
return (url, event, opts) => {
|
|
106
|
-
if (baseRoute && isNil(baseRoute.url)) {
|
|
107
|
-
throw new Error("Somehow constructing a route with a base route missing a URL, did you pass a dynamic route?");
|
|
59
|
+
route(baseRoute) {
|
|
60
|
+
function getParentArray() {
|
|
61
|
+
const parentRoutes = [];
|
|
62
|
+
let currentParent = baseRoute;
|
|
63
|
+
while (currentParent) {
|
|
64
|
+
parentRoutes.unshift(currentParent);
|
|
65
|
+
currentParent = currentParent.parent;
|
|
66
|
+
}
|
|
67
|
+
return parentRoutes;
|
|
68
|
+
}
|
|
69
|
+
return ({ event, matcher, reverser, paramsSchema, querySchema, }) => {
|
|
70
|
+
let fullParamsSchema = paramsSchema;
|
|
71
|
+
let parentRoute = baseRoute;
|
|
72
|
+
while (fullParamsSchema && parentRoute) {
|
|
73
|
+
if (parentRoute.paramsSchema) {
|
|
74
|
+
fullParamsSchema = fullParamsSchema.merge(parentRoute.paramsSchema);
|
|
75
|
+
}
|
|
76
|
+
parentRoute = parentRoute.parent;
|
|
108
77
|
}
|
|
109
|
-
const urlWithTrailingSlash = url.endsWith("/") ? url : `${url}/`;
|
|
110
|
-
const fullUrl = baseRoute
|
|
111
|
-
? joinRoutes(baseRoute.url, urlWithTrailingSlash)
|
|
112
|
-
: urlWithTrailingSlash;
|
|
113
|
-
const matcher = match(fullUrl, {});
|
|
114
|
-
const reverser = compile(fullUrl);
|
|
115
|
-
const paramsSchema = (baseRoute === null || baseRoute === void 0 ? void 0 : baseRoute.paramsSchema)
|
|
116
|
-
? (opts === null || opts === void 0 ? void 0 : opts.params)
|
|
117
|
-
? baseRoute.paramsSchema.merge(opts.params)
|
|
118
|
-
: baseRoute.paramsSchema
|
|
119
|
-
: (opts === null || opts === void 0 ? void 0 : opts.params)
|
|
120
|
-
? opts.params
|
|
121
|
-
: undefined;
|
|
122
78
|
return {
|
|
123
|
-
|
|
124
|
-
querySchema: opts === null || opts === void 0 ? void 0 : opts.query,
|
|
79
|
+
basePath,
|
|
125
80
|
event,
|
|
126
81
|
history,
|
|
127
|
-
|
|
128
|
-
|
|
82
|
+
paramsSchema,
|
|
83
|
+
querySchema,
|
|
129
84
|
parent: baseRoute,
|
|
130
|
-
|
|
131
|
-
|
|
85
|
+
matcher: matcher,
|
|
86
|
+
reverser: reverser,
|
|
87
|
+
// @ts-ignore :cry:
|
|
88
|
+
getEvent(args) {
|
|
89
|
+
const { params, query, meta } = args !== null && args !== void 0 ? args : {};
|
|
132
90
|
return { type: event, params, query, meta };
|
|
133
91
|
},
|
|
134
|
-
// @ts-ignore
|
|
135
|
-
matches(
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
92
|
+
// @ts-ignore :cry:
|
|
93
|
+
matches(suppliedUrl, search) {
|
|
94
|
+
var _a, _b, _c;
|
|
95
|
+
const fullUrl = suppliedUrl.endsWith("/")
|
|
96
|
+
? suppliedUrl
|
|
97
|
+
: suppliedUrl + "/";
|
|
98
|
+
let url = fullUrl;
|
|
99
|
+
const parentRoutes = getParentArray();
|
|
100
|
+
let params = {};
|
|
101
|
+
while (parentRoutes.length) {
|
|
102
|
+
const parentRoute = parentRoutes.shift();
|
|
103
|
+
const parentMatch = parentRoute.matcher(url, undefined);
|
|
104
|
+
if (parentMatch === false) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
url = url.slice(parentMatch.matchLength);
|
|
108
|
+
// All routes assume the url starts with a /
|
|
109
|
+
// so if the parent route matches the / in the url, which consumes it
|
|
110
|
+
// need to re-add it for the next route to match against
|
|
111
|
+
if (!url.startsWith("/")) {
|
|
112
|
+
url = "/" + url;
|
|
113
|
+
}
|
|
114
|
+
params = { ...params, ...((_a = parentMatch.params) !== null && _a !== void 0 ? _a : {}) };
|
|
140
115
|
}
|
|
141
|
-
const
|
|
142
|
-
if
|
|
143
|
-
|
|
116
|
+
const matches = matcher(url, parse(search));
|
|
117
|
+
// if there is any URL left after matching this route, the last to match
|
|
118
|
+
// that means the match isn't actually a match
|
|
119
|
+
if (matches === false || matches.matchLength !== url.length) {
|
|
120
|
+
return false;
|
|
144
121
|
}
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
122
|
+
const fullParams = {
|
|
123
|
+
...params,
|
|
124
|
+
...((_b = matches.params) !== null && _b !== void 0 ? _b : {}),
|
|
125
|
+
};
|
|
126
|
+
if (fullParamsSchema) {
|
|
127
|
+
fullParamsSchema.parse(fullParams);
|
|
128
|
+
}
|
|
129
|
+
if (querySchema) {
|
|
130
|
+
querySchema.parse(matches.query);
|
|
148
131
|
}
|
|
149
132
|
return {
|
|
150
|
-
type: event,
|
|
151
133
|
originalUrl: `${fullUrl}${search}`,
|
|
152
|
-
|
|
153
|
-
|
|
134
|
+
type: event,
|
|
135
|
+
params: fullParams,
|
|
136
|
+
query: (_c = matches.query) !== null && _c !== void 0 ? _c : {},
|
|
154
137
|
};
|
|
155
138
|
},
|
|
156
|
-
// @ts-ignore
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
else {
|
|
165
|
-
return reverser();
|
|
166
|
-
}
|
|
167
|
-
})();
|
|
168
|
-
if (!isNil(query)) {
|
|
169
|
-
return `${url}?${stringify(query)}`;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
return url;
|
|
173
|
-
}
|
|
139
|
+
// @ts-ignore :cry:
|
|
140
|
+
reverse(args) {
|
|
141
|
+
const { params, query } = args !== null && args !== void 0 ? args : {};
|
|
142
|
+
const parentRoutes = getParentArray();
|
|
143
|
+
const baseUrl = parentRoutes
|
|
144
|
+
.map((route) => route.reverser({ params }))
|
|
145
|
+
.join("");
|
|
146
|
+
return `${joinRoutes(baseUrl, reverser({ params, query }))}`;
|
|
174
147
|
},
|
|
175
|
-
// @ts-ignore
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
// @ts-ignore same problem
|
|
148
|
+
// @ts-ignore :cry:
|
|
149
|
+
navigate(args) {
|
|
150
|
+
const { params, query, meta } = args !== null && args !== void 0 ? args : {};
|
|
179
151
|
const url = this.reverse({ params, query });
|
|
180
152
|
navigate({
|
|
181
153
|
url: joinRoutes(this.basePath, url),
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
export function joinRoutes(base, route) {
|
|
2
2
|
const realBase = base.endsWith("/") ? base.slice(0, -1) : base;
|
|
3
3
|
const realRoute = route.startsWith("/") ? route : `/${route}`;
|
|
4
|
-
|
|
4
|
+
const joinedUrl = realBase + realRoute;
|
|
5
|
+
if (!joinedUrl.endsWith("/")) {
|
|
6
|
+
if (!joinedUrl.includes("?")) {
|
|
7
|
+
return `${joinedUrl}/`;
|
|
8
|
+
}
|
|
9
|
+
if (!joinedUrl.includes("/?")) {
|
|
10
|
+
return joinedUrl.replace("?", "/?");
|
|
11
|
+
}
|
|
12
|
+
return joinedUrl;
|
|
13
|
+
}
|
|
14
|
+
return joinedUrl;
|
|
5
15
|
}
|
package/lib/xstate-tree.d.ts
CHANGED
|
@@ -32,12 +32,13 @@ export declare type AnyRoute = {
|
|
|
32
32
|
navigate: any;
|
|
33
33
|
getEvent: any;
|
|
34
34
|
event: string;
|
|
35
|
-
url?: string;
|
|
36
35
|
basePath: string;
|
|
37
36
|
history: XstateTreeHistory;
|
|
38
37
|
parent?: AnyRoute;
|
|
39
38
|
paramsSchema?: Z.ZodObject<any>;
|
|
40
39
|
querySchema?: Z.ZodObject<any>;
|
|
40
|
+
matcher: (url: string, query: ParsedQuery<string> | undefined) => any;
|
|
41
|
+
reverser: any;
|
|
41
42
|
};
|
|
42
43
|
|
|
43
44
|
/**
|
|
@@ -90,79 +91,50 @@ export declare function buildActions<TMachine extends AnyStateMachine, TActions,
|
|
|
90
91
|
* @param basePath - the base path for this route factory
|
|
91
92
|
*/
|
|
92
93
|
export declare function buildCreateRoute(history: XstateTreeHistory, basePath: string): {
|
|
93
|
-
|
|
94
|
-
* Creates a dynamic Route using the supplied options
|
|
95
|
-
*
|
|
96
|
-
* The return value of dynamicRoute is a function that accepts the routes "dynamic" options
|
|
97
|
-
* The argument to dynamicRoute itself is the params/query/meta schemas defining the route
|
|
98
|
-
*
|
|
99
|
-
* The returned function accepts a singular option object with the following fields
|
|
100
|
-
*
|
|
101
|
-
* `event`, the string constant for the routes event
|
|
102
|
-
* `matches`, a function that is passed a url/query string and determines if the route matches
|
|
103
|
-
* if the route is matched it returns the extracted params/query objects
|
|
104
|
-
* `reverse`, a function that is passed params/query objects and turns them into a URL
|
|
105
|
-
*
|
|
106
|
-
* The params and query schemas are ZodSchemas, they both need to be an object (ie Z.object())
|
|
107
|
-
*/
|
|
108
|
-
dynamicRoute: <TOpts extends Options<Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
109
|
-
[x: string]: any;
|
|
110
|
-
}, {
|
|
111
|
-
[x: string]: any;
|
|
112
|
-
}>, Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
94
|
+
simpleRoute<TBaseRoute extends AnyRoute>(baseRoute?: TBaseRoute | undefined): <TEvent extends string, TParamsSchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
113
95
|
[x: string]: any;
|
|
114
96
|
}, {
|
|
115
97
|
[x: string]: any;
|
|
116
|
-
}
|
|
98
|
+
}> | undefined, TQuerySchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
117
99
|
[x: string]: any;
|
|
118
100
|
}, {
|
|
119
101
|
[x: string]: any;
|
|
120
|
-
}>
|
|
121
|
-
[x: string]: any;
|
|
122
|
-
}, {
|
|
123
|
-
[x: string]: any;
|
|
124
|
-
}> ? Z.TypeOf<TQuerySchema> : undefined, TFullMeta = TMeta extends undefined ? SharedMeta : TMeta & SharedMeta>({ event, matches, reverse, }: {
|
|
102
|
+
}> | undefined, TMeta extends Record<string, unknown>>({ url, paramsSchema, querySchema, ...args }: {
|
|
125
103
|
event: TEvent;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
* The return value of staticRoute is a function that accepts the routes options
|
|
133
|
-
* The only argument to staticRoute itself is an optional parent route
|
|
134
|
-
*
|
|
135
|
-
* The returned function accepts 3 arguments
|
|
136
|
-
*
|
|
137
|
-
* 1. URL of the route
|
|
138
|
-
* 2. The event type of the route
|
|
139
|
-
* 3. The routes options, params schema, query schema and meta type
|
|
140
|
-
*
|
|
141
|
-
* The params and query schemas are ZodSchemas, they both need to be an object (ie Z.object())
|
|
142
|
-
*
|
|
143
|
-
* When creating a route that has a parent route, the following happens
|
|
144
|
-
*
|
|
145
|
-
* 1. The parent routes url is prepended to the routes URL
|
|
146
|
-
* 2. The parents params schema is merged with the routes schema
|
|
147
|
-
* 3. The parents meta type is merged with the routes meta type
|
|
148
|
-
*/
|
|
149
|
-
staticRoute: <TBaseRoute extends AnyRoute | undefined = undefined, TBaseParams = RouteParams<TBaseRoute>, TBaseMeta = RouteMeta<TBaseRoute>>(baseRoute?: TBaseRoute | undefined) => <TOpts_1 extends Options<Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
150
|
-
[x: string]: any;
|
|
151
|
-
}, {
|
|
152
|
-
[x: string]: any;
|
|
153
|
-
}>, Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
154
|
-
[x: string]: any;
|
|
155
|
-
}, {
|
|
156
|
-
[x: string]: any;
|
|
157
|
-
}>, any>, TEvent_1 extends string, TParamsSchema_1 = Params<TOpts_1>, TQuerySchema_1 = Query<TOpts_1>, TMeta_1 = Meta<TOpts_1>, TParams_1 = TParamsSchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
104
|
+
url: string;
|
|
105
|
+
paramsSchema?: TParamsSchema | undefined;
|
|
106
|
+
querySchema?: TQuerySchema | undefined;
|
|
107
|
+
meta?: TMeta | undefined;
|
|
108
|
+
}) => Route<MergeRouteTypes<RouteParams<TBaseRoute>, ResolveZodType<TParamsSchema>>, ResolveZodType<TQuerySchema>, TEvent, MergeRouteTypes<RouteMeta<TBaseRoute>, TMeta> & SharedMeta>;
|
|
109
|
+
route<TBaseRoute_1 extends AnyRoute>(baseRoute?: TBaseRoute_1 | undefined): <TEvent_1 extends string, TParamsSchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
158
110
|
[x: string]: any;
|
|
159
111
|
}, {
|
|
160
112
|
[x: string]: any;
|
|
161
|
-
}>
|
|
113
|
+
}> | undefined, TQuerySchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
162
114
|
[x: string]: any;
|
|
163
115
|
}, {
|
|
164
116
|
[x: string]: any;
|
|
165
|
-
}>
|
|
117
|
+
}> | undefined, TMeta_1 extends Record<string, unknown>>({ event, matcher, reverser, paramsSchema, querySchema, }: {
|
|
118
|
+
event: TEvent_1;
|
|
119
|
+
paramsSchema?: TParamsSchema_1 | undefined;
|
|
120
|
+
querySchema?: TQuerySchema_1 | undefined;
|
|
121
|
+
meta?: TMeta_1 | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Determines if the route matches the given url and query
|
|
124
|
+
*
|
|
125
|
+
* If there is no match, return false
|
|
126
|
+
* If there is a match, return the parsed params and query as well as the length of the matched path in the URL
|
|
127
|
+
*/
|
|
128
|
+
matcher: (url: string, query: ParsedQuery<string> | undefined) => false | (RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>> & {
|
|
129
|
+
matchLength: number;
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* Reverses the route to a URL
|
|
133
|
+
*
|
|
134
|
+
* Supplied with params/query objects and constructs the correct URL based on them
|
|
135
|
+
*/
|
|
136
|
+
reverser: RouteArgumentFunctions<string, MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>, RouteArguments<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1>>>;
|
|
137
|
+
}) => Route<MergeRouteTypes<RouteParams<TBaseRoute_1>, ResolveZodType<TParamsSchema_1>>, ResolveZodType<TQuerySchema_1>, TEvent_1, MergeRouteTypes<RouteMeta<TBaseRoute_1>, TMeta_1> & SharedMeta>;
|
|
166
138
|
};
|
|
167
139
|
|
|
168
140
|
/**
|
|
@@ -311,9 +283,9 @@ declare type InferViewProps<T> = T extends ViewProps<infer TSelectors, infer TAc
|
|
|
311
283
|
inState: (state: Parameters<TMatches>[0]) => TMatches;
|
|
312
284
|
} : never;
|
|
313
285
|
|
|
314
|
-
declare type IsEmptyObject<Obj, ExcludeOptional extends boolean = false> = [
|
|
315
|
-
|
|
316
|
-
]
|
|
286
|
+
declare type IsEmptyObject<Obj, ExcludeOptional extends boolean = false> = undefined extends Obj ? true : [keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)] extends [
|
|
287
|
+
never
|
|
288
|
+
] ? true : false;
|
|
317
289
|
|
|
318
290
|
/**
|
|
319
291
|
* @public
|
|
@@ -325,7 +297,7 @@ keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)
|
|
|
325
297
|
* @param options - configure loading component and context to invoke machine with
|
|
326
298
|
* @returns an xstate-tree machine that wraps the promise, invoking the resulting machine when it resolves
|
|
327
299
|
*/
|
|
328
|
-
export declare function lazy<TMachine extends AnyStateMachine>(factory: () => Promise<TMachine>, { Loader, withContext, }?:
|
|
300
|
+
export declare function lazy<TMachine extends AnyStateMachine>(factory: () => Promise<TMachine>, { Loader, withContext, }?: Options<TMachine["context"]>): StateMachine<Context, any, Events, States, any, any, any>;
|
|
329
301
|
|
|
330
302
|
/**
|
|
331
303
|
* @public
|
|
@@ -372,6 +344,8 @@ export declare type MatchesFrom<T extends AnyStateMachine> = StateFrom<T>["match
|
|
|
372
344
|
*/
|
|
373
345
|
export declare function matchRoute<TRoutes extends Route<any, any, any, any>[]>(routes: TRoutes, basePath: string, path: string, search: string): Return<TRoutes>;
|
|
374
346
|
|
|
347
|
+
declare type MergeRouteTypes<TBase, TSupplied> = undefined extends TBase ? TSupplied : undefined extends TSupplied ? TBase : TBase & TSupplied;
|
|
348
|
+
|
|
375
349
|
/**
|
|
376
350
|
* @public
|
|
377
351
|
*
|
|
@@ -408,16 +382,7 @@ declare type OmitOptional<T> = {
|
|
|
408
382
|
*/
|
|
409
383
|
export declare function onBroadcast(handler: (event: GlobalEvents) => void): () => void;
|
|
410
384
|
|
|
411
|
-
|
|
412
|
-
* @public
|
|
413
|
-
*/
|
|
414
|
-
export declare type Options<TParamsSchema extends Z.ZodObject<any>, TQuerySchema extends Z.ZodObject<any>, TMetaSchema> = {
|
|
415
|
-
params?: TParamsSchema;
|
|
416
|
-
query?: TQuerySchema;
|
|
417
|
-
meta?: TMetaSchema;
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
declare type Options_2<TContext> = {
|
|
385
|
+
declare type Options<TContext> = {
|
|
421
386
|
/**
|
|
422
387
|
* Displayed while the promise is resolving, defaults to returning null
|
|
423
388
|
*/
|
|
@@ -465,6 +430,8 @@ export declare type Query<T> = T extends {
|
|
|
465
430
|
query: infer TQuery;
|
|
466
431
|
} ? TQuery : undefined;
|
|
467
432
|
|
|
433
|
+
declare type ResolveZodType<T extends Z.ZodType<any> | undefined> = undefined extends T ? undefined : Z.TypeOf<Exclude<T, undefined>>;
|
|
434
|
+
|
|
468
435
|
declare type Return<TRoutes extends Route<any, any, any, any>[]> = {
|
|
469
436
|
type: "matched";
|
|
470
437
|
route: TRoutes[number];
|
|
@@ -498,7 +465,7 @@ export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
|
498
465
|
matches: (url: string, search: string) => ({
|
|
499
466
|
type: TEvent;
|
|
500
467
|
originalUrl: string;
|
|
501
|
-
} & RouteArguments<TParams, TQuery, TMeta>) |
|
|
468
|
+
} & RouteArguments<TParams, TQuery, TMeta>) | false;
|
|
502
469
|
/**
|
|
503
470
|
* Takes in query/params objects as required by the route and returns a URL for that route
|
|
504
471
|
*
|
|
@@ -520,11 +487,14 @@ export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
|
520
487
|
getEvent: RouteArgumentFunctions<{
|
|
521
488
|
type: TEvent;
|
|
522
489
|
} & RouteArguments<TParams, TQuery, TMeta>, TParams, TQuery, TMeta>;
|
|
490
|
+
matcher: (url: string, query: ParsedQuery<string> | undefined) => (RouteArguments<TParams, TQuery, TMeta> & {
|
|
491
|
+
matchLength: number;
|
|
492
|
+
}) | false;
|
|
493
|
+
reverser: RouteArgumentFunctions<string, TParams, TQuery, TMeta>;
|
|
523
494
|
/**
|
|
524
495
|
* Event type for this route
|
|
525
496
|
*/
|
|
526
497
|
event: TEvent;
|
|
527
|
-
url?: string;
|
|
528
498
|
history: XstateTreeHistory;
|
|
529
499
|
basePath: string;
|
|
530
500
|
parent?: AnyRoute;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@koordinates/xstate-tree",
|
|
3
3
|
"main": "lib/index.js",
|
|
4
4
|
"types": "lib/xstate-tree.d.ts",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "3.0.0-beta.1",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Build UIs with Actors using xstate and React",
|
|
8
8
|
"keywords": [
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"test-examples": "tsc --noEmit",
|
|
82
82
|
"todomvc": "vite dev",
|
|
83
83
|
"build": "rimraf lib && rimraf out && tsc -p tsconfig.build.json",
|
|
84
|
-
"build:watch": "tsc -p tsconfig.
|
|
84
|
+
"build:watch": "tsc -p tsconfig.json -w",
|
|
85
85
|
"api-extractor": "api-extractor run",
|
|
86
86
|
"release": "semantic-release",
|
|
87
87
|
"commitlint": "commitlint --edit"
|