litzjs 0.0.0
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/LICENSE +21 -0
- package/README.md +936 -0
- package/dist/bindings-B1P6pL93.js +21 -0
- package/dist/bindings-BDe-v5i6.mjs +10 -0
- package/dist/chunk-8l464Juk.js +28 -0
- package/dist/client.d.mts +35 -0
- package/dist/client.d.ts +35 -0
- package/dist/client.js +1633 -0
- package/dist/client.mjs +1625 -0
- package/dist/index.d.mts +559 -0
- package/dist/index.d.ts +559 -0
- package/dist/index.js +360 -0
- package/dist/index.mjs +344 -0
- package/dist/internal-transport-DR0r68ff.js +161 -0
- package/dist/internal-transport-dsMykcNK.mjs +114 -0
- package/dist/request-headers-DepZ5tjg.mjs +35 -0
- package/dist/request-headers-ZPR3TQs3.js +46 -0
- package/dist/server.d.mts +74 -0
- package/dist/server.d.ts +74 -0
- package/dist/server.js +316 -0
- package/dist/server.mjs +315 -0
- package/dist/vite.d.mts +10043 -0
- package/dist/vite.d.ts +10043 -0
- package/dist/vite.js +1481 -0
- package/dist/vite.mjs +1474 -0
- package/package.json +90 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_chunk = require("./chunk-8l464Juk.js");
|
|
3
|
+
const require_bindings = require("./bindings-B1P6pL93.js");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
react = require_chunk.__toESM(react);
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
function unimplementedHook(name) {
|
|
8
|
+
throw new Error(`${name} is not available until the Litz runtime is implemented.`);
|
|
9
|
+
}
|
|
10
|
+
function UnimplementedForm() {
|
|
11
|
+
throw new Error("route.Form is not available until the Litz runtime is implemented.");
|
|
12
|
+
}
|
|
13
|
+
function mergeHeaders(current, next) {
|
|
14
|
+
const merged = new Headers(current);
|
|
15
|
+
new Headers(next).forEach((value, key) => {
|
|
16
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
17
|
+
merged.append(key, value);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
merged.set(key, value);
|
|
21
|
+
});
|
|
22
|
+
return merged;
|
|
23
|
+
}
|
|
24
|
+
function getRequiredRouteLocation(path) {
|
|
25
|
+
const bindings = require_bindings.getClientBindings();
|
|
26
|
+
if (!bindings) return unimplementedHook(`Route "${path}" location runtime`);
|
|
27
|
+
return bindings.useRequiredRouteLocation(path);
|
|
28
|
+
}
|
|
29
|
+
function getRequiredRouteStatus(path) {
|
|
30
|
+
const bindings = require_bindings.getClientBindings();
|
|
31
|
+
if (!bindings) return unimplementedHook(`Route "${path}" status runtime`);
|
|
32
|
+
return bindings.useRequiredRouteStatus(path);
|
|
33
|
+
}
|
|
34
|
+
function getRequiredRouteData(path) {
|
|
35
|
+
const bindings = require_bindings.getClientBindings();
|
|
36
|
+
if (!bindings) return unimplementedHook(`Route "${path}" data runtime`);
|
|
37
|
+
return bindings.useRequiredRouteData(path);
|
|
38
|
+
}
|
|
39
|
+
function getRequiredRouteActions(path) {
|
|
40
|
+
const bindings = require_bindings.getClientBindings();
|
|
41
|
+
if (!bindings) return unimplementedHook(`Route "${path}" action runtime`);
|
|
42
|
+
return bindings.useRequiredRouteActions(path);
|
|
43
|
+
}
|
|
44
|
+
function getRequiredResourceLocation(path) {
|
|
45
|
+
const bindings = require_bindings.getClientBindings();
|
|
46
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" location runtime`);
|
|
47
|
+
return bindings.useRequiredResourceLocation(path);
|
|
48
|
+
}
|
|
49
|
+
function getRequiredResourceStatus(path) {
|
|
50
|
+
const bindings = require_bindings.getClientBindings();
|
|
51
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" status runtime`);
|
|
52
|
+
return bindings.useRequiredResourceStatus(path);
|
|
53
|
+
}
|
|
54
|
+
function getRequiredResourceData(path) {
|
|
55
|
+
const bindings = require_bindings.getClientBindings();
|
|
56
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" data runtime`);
|
|
57
|
+
return bindings.useRequiredResourceData(path);
|
|
58
|
+
}
|
|
59
|
+
function getRequiredResourceActions(path) {
|
|
60
|
+
const bindings = require_bindings.getClientBindings();
|
|
61
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" action runtime`);
|
|
62
|
+
return bindings.useRequiredResourceActions(path);
|
|
63
|
+
}
|
|
64
|
+
function defineRoute(path, options) {
|
|
65
|
+
return {
|
|
66
|
+
id: path,
|
|
67
|
+
path,
|
|
68
|
+
component: options.component,
|
|
69
|
+
options,
|
|
70
|
+
useLoaderResult: () => {
|
|
71
|
+
return getRequiredRouteData(path).loaderResult;
|
|
72
|
+
},
|
|
73
|
+
useLoaderData: () => {
|
|
74
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
75
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
76
|
+
},
|
|
77
|
+
useLoaderView: () => {
|
|
78
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
79
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
80
|
+
},
|
|
81
|
+
useData: () => getRequiredRouteData(path).data,
|
|
82
|
+
useView: () => getRequiredRouteData(path).view,
|
|
83
|
+
useError: () => {
|
|
84
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
85
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
86
|
+
},
|
|
87
|
+
useActionResult: () => getRequiredRouteData(path).actionResult,
|
|
88
|
+
useActionData: () => {
|
|
89
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
90
|
+
return actionResult?.kind === "data" ? actionResult.data : null;
|
|
91
|
+
},
|
|
92
|
+
useActionView: () => {
|
|
93
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
94
|
+
return actionResult?.kind === "view" ? actionResult.node : null;
|
|
95
|
+
},
|
|
96
|
+
useActionError: () => {
|
|
97
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
98
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
99
|
+
},
|
|
100
|
+
useInvalid: () => {
|
|
101
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
102
|
+
return actionResult?.kind === "invalid" ? actionResult : null;
|
|
103
|
+
},
|
|
104
|
+
useStatus: () => getRequiredRouteStatus(path).status,
|
|
105
|
+
usePending: () => getRequiredRouteStatus(path).pending,
|
|
106
|
+
useParams: () => getRequiredRouteLocation(path).params,
|
|
107
|
+
useSearch: () => {
|
|
108
|
+
const location = getRequiredRouteLocation(path);
|
|
109
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
110
|
+
},
|
|
111
|
+
useRetry: () => {
|
|
112
|
+
const actions = getRequiredRouteActions(path);
|
|
113
|
+
return () => actions.retry();
|
|
114
|
+
},
|
|
115
|
+
useReload: () => {
|
|
116
|
+
const actions = getRequiredRouteActions(path);
|
|
117
|
+
return () => actions.reload();
|
|
118
|
+
},
|
|
119
|
+
useSubmit: (opts) => {
|
|
120
|
+
const actions = getRequiredRouteActions(path);
|
|
121
|
+
return (payload) => actions.submit(payload, opts);
|
|
122
|
+
},
|
|
123
|
+
Form(props) {
|
|
124
|
+
const bindings = require_bindings.getClientBindings();
|
|
125
|
+
if (!bindings) return react.createElement(UnimplementedForm, props);
|
|
126
|
+
const FormComponent = bindings.createRouteFormComponent(path);
|
|
127
|
+
return react.createElement(FormComponent, props);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function defineLayout(path, options) {
|
|
132
|
+
return {
|
|
133
|
+
id: path,
|
|
134
|
+
path,
|
|
135
|
+
component: options.component,
|
|
136
|
+
options,
|
|
137
|
+
useLoaderResult: () => {
|
|
138
|
+
return getRequiredRouteData(path).loaderResult;
|
|
139
|
+
},
|
|
140
|
+
useLoaderData: () => {
|
|
141
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
142
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
143
|
+
},
|
|
144
|
+
useLoaderView: () => {
|
|
145
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
146
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
147
|
+
},
|
|
148
|
+
useData: () => {
|
|
149
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
150
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
151
|
+
},
|
|
152
|
+
useView: () => getRequiredRouteData(path).view,
|
|
153
|
+
useStatus: () => getRequiredRouteStatus(path).status,
|
|
154
|
+
usePending: () => getRequiredRouteStatus(path).pending,
|
|
155
|
+
useParams: () => getRequiredRouteLocation(path).params,
|
|
156
|
+
useSearch: () => {
|
|
157
|
+
const location = getRequiredRouteLocation(path);
|
|
158
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
159
|
+
},
|
|
160
|
+
useRetry: () => {
|
|
161
|
+
const actions = getRequiredRouteActions(path);
|
|
162
|
+
return () => actions.retry();
|
|
163
|
+
},
|
|
164
|
+
useReload: () => {
|
|
165
|
+
const actions = getRequiredRouteActions(path);
|
|
166
|
+
return () => actions.reload();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function useMatches() {
|
|
171
|
+
const bindings = require_bindings.getClientBindings();
|
|
172
|
+
if (!bindings) return unimplementedHook("useMatches()");
|
|
173
|
+
return bindings.useMatches();
|
|
174
|
+
}
|
|
175
|
+
function usePathname() {
|
|
176
|
+
const bindings = require_bindings.getClientBindings();
|
|
177
|
+
if (!bindings) return unimplementedHook("usePathname()");
|
|
178
|
+
return bindings.usePathname();
|
|
179
|
+
}
|
|
180
|
+
function useLocation() {
|
|
181
|
+
const bindings = require_bindings.getClientBindings();
|
|
182
|
+
if (!bindings) return unimplementedHook("useLocation()");
|
|
183
|
+
return bindings.useLocation();
|
|
184
|
+
}
|
|
185
|
+
function defineApiRoute(path, definition) {
|
|
186
|
+
const { middleware, ...methods } = definition;
|
|
187
|
+
return {
|
|
188
|
+
path,
|
|
189
|
+
middleware,
|
|
190
|
+
methods,
|
|
191
|
+
fetch(...args) {
|
|
192
|
+
const { params, search, method, ...init } = args[0] ?? {};
|
|
193
|
+
const href = buildApiHref(path, params, search);
|
|
194
|
+
return fetch(href, {
|
|
195
|
+
...init,
|
|
196
|
+
method
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function defineResource(path, options) {
|
|
202
|
+
const ResourceComponent = function LitzDefinedResourceComponent(props) {
|
|
203
|
+
const bindings = require_bindings.getClientBindings();
|
|
204
|
+
if (!bindings) return react.createElement(options.component, props);
|
|
205
|
+
const ScopedResourceComponent = bindings.createResourceComponent(path, options.component);
|
|
206
|
+
return react.createElement(ScopedResourceComponent, props);
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
path,
|
|
210
|
+
...options,
|
|
211
|
+
useLoaderResult: () => {
|
|
212
|
+
return getRequiredResourceData(path).loaderResult;
|
|
213
|
+
},
|
|
214
|
+
useLoaderData: () => {
|
|
215
|
+
const loaderResult = getRequiredResourceData(path).loaderResult;
|
|
216
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
217
|
+
},
|
|
218
|
+
useLoaderView: () => {
|
|
219
|
+
const loaderResult = getRequiredResourceData(path).loaderResult;
|
|
220
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
221
|
+
},
|
|
222
|
+
useActionResult: () => getRequiredResourceData(path).actionResult,
|
|
223
|
+
useActionData: () => {
|
|
224
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
225
|
+
return actionResult?.kind === "data" ? actionResult.data : null;
|
|
226
|
+
},
|
|
227
|
+
useActionView: () => {
|
|
228
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
229
|
+
return actionResult?.kind === "view" ? actionResult.node : null;
|
|
230
|
+
},
|
|
231
|
+
useActionError: () => {
|
|
232
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
233
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
234
|
+
},
|
|
235
|
+
useInvalid: () => {
|
|
236
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
237
|
+
return actionResult?.kind === "invalid" ? actionResult : null;
|
|
238
|
+
},
|
|
239
|
+
useData: () => getRequiredResourceData(path).data,
|
|
240
|
+
useView: () => getRequiredResourceData(path).view,
|
|
241
|
+
useError: () => {
|
|
242
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
243
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
244
|
+
},
|
|
245
|
+
useStatus: () => getRequiredResourceStatus(path).status,
|
|
246
|
+
usePending: () => getRequiredResourceStatus(path).pending,
|
|
247
|
+
useParams: () => getRequiredResourceLocation(path).params,
|
|
248
|
+
useSearch: () => {
|
|
249
|
+
const location = getRequiredResourceLocation(path);
|
|
250
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
251
|
+
},
|
|
252
|
+
useRetry: () => {
|
|
253
|
+
const actions = getRequiredResourceActions(path);
|
|
254
|
+
return () => actions.retry();
|
|
255
|
+
},
|
|
256
|
+
useReload: () => {
|
|
257
|
+
const actions = getRequiredResourceActions(path);
|
|
258
|
+
return () => actions.reload();
|
|
259
|
+
},
|
|
260
|
+
useSubmit: (opts) => {
|
|
261
|
+
const actions = getRequiredResourceActions(path);
|
|
262
|
+
return (payload) => actions.submit(payload, opts);
|
|
263
|
+
},
|
|
264
|
+
Form(props) {
|
|
265
|
+
const bindings = require_bindings.getClientBindings();
|
|
266
|
+
if (!bindings) return react.createElement(UnimplementedForm, props);
|
|
267
|
+
const FormComponent = bindings.createResourceFormComponent(path);
|
|
268
|
+
return react.createElement(FormComponent, props);
|
|
269
|
+
},
|
|
270
|
+
Component: ResourceComponent
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function server(handler) {
|
|
274
|
+
return handler;
|
|
275
|
+
}
|
|
276
|
+
function withHeaders(result, headers) {
|
|
277
|
+
if (result instanceof Response) {
|
|
278
|
+
const mergedHeaders = mergeHeaders(result.headers, headers);
|
|
279
|
+
return new Response(result.body, {
|
|
280
|
+
status: result.status,
|
|
281
|
+
statusText: result.statusText,
|
|
282
|
+
headers: mergedHeaders
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
...result,
|
|
287
|
+
headers: mergeHeaders(result.headers, headers)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function data(value, options = {}) {
|
|
291
|
+
return {
|
|
292
|
+
kind: "data",
|
|
293
|
+
data: value,
|
|
294
|
+
headers: options.headers,
|
|
295
|
+
status: options.status,
|
|
296
|
+
revalidate: options.revalidate
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function view(node, options = {}) {
|
|
300
|
+
return {
|
|
301
|
+
kind: "view",
|
|
302
|
+
node,
|
|
303
|
+
headers: options.headers,
|
|
304
|
+
revalidate: options.revalidate
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function invalid(options) {
|
|
308
|
+
return {
|
|
309
|
+
kind: "invalid",
|
|
310
|
+
headers: options.headers,
|
|
311
|
+
status: options.status,
|
|
312
|
+
fields: options.fields,
|
|
313
|
+
formError: options.formError,
|
|
314
|
+
data: options.data
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function redirect(location, options = {}) {
|
|
318
|
+
return {
|
|
319
|
+
kind: "redirect",
|
|
320
|
+
location,
|
|
321
|
+
headers: options.headers,
|
|
322
|
+
status: options.status,
|
|
323
|
+
replace: options.replace,
|
|
324
|
+
revalidate: options.revalidate
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function error(status, message, options = {}) {
|
|
328
|
+
return {
|
|
329
|
+
kind: "error",
|
|
330
|
+
status,
|
|
331
|
+
message,
|
|
332
|
+
headers: options.headers,
|
|
333
|
+
code: options.code,
|
|
334
|
+
data: options.data
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function buildApiHref(pathPattern, params, search) {
|
|
338
|
+
const pathname = pathPattern.replace(/:([A-Za-z0-9_]+)/g, (_, key) => {
|
|
339
|
+
const value = params?.[key];
|
|
340
|
+
if (value === void 0) throw new Error(`Missing required API param "${key}" for path "${pathPattern}".`);
|
|
341
|
+
return encodeURIComponent(value);
|
|
342
|
+
});
|
|
343
|
+
const searchString = (search instanceof URLSearchParams ? new URLSearchParams(search) : new URLSearchParams(search ?? {})).toString();
|
|
344
|
+
return searchString ? `${pathname}?${searchString}` : pathname;
|
|
345
|
+
}
|
|
346
|
+
//#endregion
|
|
347
|
+
exports.data = data;
|
|
348
|
+
exports.defineApiRoute = defineApiRoute;
|
|
349
|
+
exports.defineLayout = defineLayout;
|
|
350
|
+
exports.defineResource = defineResource;
|
|
351
|
+
exports.defineRoute = defineRoute;
|
|
352
|
+
exports.error = error;
|
|
353
|
+
exports.invalid = invalid;
|
|
354
|
+
exports.redirect = redirect;
|
|
355
|
+
exports.server = server;
|
|
356
|
+
exports.useLocation = useLocation;
|
|
357
|
+
exports.useMatches = useMatches;
|
|
358
|
+
exports.usePathname = usePathname;
|
|
359
|
+
exports.view = view;
|
|
360
|
+
exports.withHeaders = withHeaders;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { t as getClientBindings } from "./bindings-BDe-v5i6.mjs";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
function unimplementedHook(name) {
|
|
5
|
+
throw new Error(`${name} is not available until the Litz runtime is implemented.`);
|
|
6
|
+
}
|
|
7
|
+
function UnimplementedForm() {
|
|
8
|
+
throw new Error("route.Form is not available until the Litz runtime is implemented.");
|
|
9
|
+
}
|
|
10
|
+
function mergeHeaders(current, next) {
|
|
11
|
+
const merged = new Headers(current);
|
|
12
|
+
new Headers(next).forEach((value, key) => {
|
|
13
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
14
|
+
merged.append(key, value);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
merged.set(key, value);
|
|
18
|
+
});
|
|
19
|
+
return merged;
|
|
20
|
+
}
|
|
21
|
+
function getRequiredRouteLocation(path) {
|
|
22
|
+
const bindings = getClientBindings();
|
|
23
|
+
if (!bindings) return unimplementedHook(`Route "${path}" location runtime`);
|
|
24
|
+
return bindings.useRequiredRouteLocation(path);
|
|
25
|
+
}
|
|
26
|
+
function getRequiredRouteStatus(path) {
|
|
27
|
+
const bindings = getClientBindings();
|
|
28
|
+
if (!bindings) return unimplementedHook(`Route "${path}" status runtime`);
|
|
29
|
+
return bindings.useRequiredRouteStatus(path);
|
|
30
|
+
}
|
|
31
|
+
function getRequiredRouteData(path) {
|
|
32
|
+
const bindings = getClientBindings();
|
|
33
|
+
if (!bindings) return unimplementedHook(`Route "${path}" data runtime`);
|
|
34
|
+
return bindings.useRequiredRouteData(path);
|
|
35
|
+
}
|
|
36
|
+
function getRequiredRouteActions(path) {
|
|
37
|
+
const bindings = getClientBindings();
|
|
38
|
+
if (!bindings) return unimplementedHook(`Route "${path}" action runtime`);
|
|
39
|
+
return bindings.useRequiredRouteActions(path);
|
|
40
|
+
}
|
|
41
|
+
function getRequiredResourceLocation(path) {
|
|
42
|
+
const bindings = getClientBindings();
|
|
43
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" location runtime`);
|
|
44
|
+
return bindings.useRequiredResourceLocation(path);
|
|
45
|
+
}
|
|
46
|
+
function getRequiredResourceStatus(path) {
|
|
47
|
+
const bindings = getClientBindings();
|
|
48
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" status runtime`);
|
|
49
|
+
return bindings.useRequiredResourceStatus(path);
|
|
50
|
+
}
|
|
51
|
+
function getRequiredResourceData(path) {
|
|
52
|
+
const bindings = getClientBindings();
|
|
53
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" data runtime`);
|
|
54
|
+
return bindings.useRequiredResourceData(path);
|
|
55
|
+
}
|
|
56
|
+
function getRequiredResourceActions(path) {
|
|
57
|
+
const bindings = getClientBindings();
|
|
58
|
+
if (!bindings) return unimplementedHook(`Resource "${path}" action runtime`);
|
|
59
|
+
return bindings.useRequiredResourceActions(path);
|
|
60
|
+
}
|
|
61
|
+
function defineRoute(path, options) {
|
|
62
|
+
return {
|
|
63
|
+
id: path,
|
|
64
|
+
path,
|
|
65
|
+
component: options.component,
|
|
66
|
+
options,
|
|
67
|
+
useLoaderResult: () => {
|
|
68
|
+
return getRequiredRouteData(path).loaderResult;
|
|
69
|
+
},
|
|
70
|
+
useLoaderData: () => {
|
|
71
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
72
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
73
|
+
},
|
|
74
|
+
useLoaderView: () => {
|
|
75
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
76
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
77
|
+
},
|
|
78
|
+
useData: () => getRequiredRouteData(path).data,
|
|
79
|
+
useView: () => getRequiredRouteData(path).view,
|
|
80
|
+
useError: () => {
|
|
81
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
82
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
83
|
+
},
|
|
84
|
+
useActionResult: () => getRequiredRouteData(path).actionResult,
|
|
85
|
+
useActionData: () => {
|
|
86
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
87
|
+
return actionResult?.kind === "data" ? actionResult.data : null;
|
|
88
|
+
},
|
|
89
|
+
useActionView: () => {
|
|
90
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
91
|
+
return actionResult?.kind === "view" ? actionResult.node : null;
|
|
92
|
+
},
|
|
93
|
+
useActionError: () => {
|
|
94
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
95
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
96
|
+
},
|
|
97
|
+
useInvalid: () => {
|
|
98
|
+
const actionResult = getRequiredRouteData(path).actionResult;
|
|
99
|
+
return actionResult?.kind === "invalid" ? actionResult : null;
|
|
100
|
+
},
|
|
101
|
+
useStatus: () => getRequiredRouteStatus(path).status,
|
|
102
|
+
usePending: () => getRequiredRouteStatus(path).pending,
|
|
103
|
+
useParams: () => getRequiredRouteLocation(path).params,
|
|
104
|
+
useSearch: () => {
|
|
105
|
+
const location = getRequiredRouteLocation(path);
|
|
106
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
107
|
+
},
|
|
108
|
+
useRetry: () => {
|
|
109
|
+
const actions = getRequiredRouteActions(path);
|
|
110
|
+
return () => actions.retry();
|
|
111
|
+
},
|
|
112
|
+
useReload: () => {
|
|
113
|
+
const actions = getRequiredRouteActions(path);
|
|
114
|
+
return () => actions.reload();
|
|
115
|
+
},
|
|
116
|
+
useSubmit: (opts) => {
|
|
117
|
+
const actions = getRequiredRouteActions(path);
|
|
118
|
+
return (payload) => actions.submit(payload, opts);
|
|
119
|
+
},
|
|
120
|
+
Form(props) {
|
|
121
|
+
const bindings = getClientBindings();
|
|
122
|
+
if (!bindings) return React.createElement(UnimplementedForm, props);
|
|
123
|
+
const FormComponent = bindings.createRouteFormComponent(path);
|
|
124
|
+
return React.createElement(FormComponent, props);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function defineLayout(path, options) {
|
|
129
|
+
return {
|
|
130
|
+
id: path,
|
|
131
|
+
path,
|
|
132
|
+
component: options.component,
|
|
133
|
+
options,
|
|
134
|
+
useLoaderResult: () => {
|
|
135
|
+
return getRequiredRouteData(path).loaderResult;
|
|
136
|
+
},
|
|
137
|
+
useLoaderData: () => {
|
|
138
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
139
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
140
|
+
},
|
|
141
|
+
useLoaderView: () => {
|
|
142
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
143
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
144
|
+
},
|
|
145
|
+
useData: () => {
|
|
146
|
+
const loaderResult = getRequiredRouteData(path).loaderResult;
|
|
147
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
148
|
+
},
|
|
149
|
+
useView: () => getRequiredRouteData(path).view,
|
|
150
|
+
useStatus: () => getRequiredRouteStatus(path).status,
|
|
151
|
+
usePending: () => getRequiredRouteStatus(path).pending,
|
|
152
|
+
useParams: () => getRequiredRouteLocation(path).params,
|
|
153
|
+
useSearch: () => {
|
|
154
|
+
const location = getRequiredRouteLocation(path);
|
|
155
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
156
|
+
},
|
|
157
|
+
useRetry: () => {
|
|
158
|
+
const actions = getRequiredRouteActions(path);
|
|
159
|
+
return () => actions.retry();
|
|
160
|
+
},
|
|
161
|
+
useReload: () => {
|
|
162
|
+
const actions = getRequiredRouteActions(path);
|
|
163
|
+
return () => actions.reload();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function useMatches() {
|
|
168
|
+
const bindings = getClientBindings();
|
|
169
|
+
if (!bindings) return unimplementedHook("useMatches()");
|
|
170
|
+
return bindings.useMatches();
|
|
171
|
+
}
|
|
172
|
+
function usePathname() {
|
|
173
|
+
const bindings = getClientBindings();
|
|
174
|
+
if (!bindings) return unimplementedHook("usePathname()");
|
|
175
|
+
return bindings.usePathname();
|
|
176
|
+
}
|
|
177
|
+
function useLocation() {
|
|
178
|
+
const bindings = getClientBindings();
|
|
179
|
+
if (!bindings) return unimplementedHook("useLocation()");
|
|
180
|
+
return bindings.useLocation();
|
|
181
|
+
}
|
|
182
|
+
function defineApiRoute(path, definition) {
|
|
183
|
+
const { middleware, ...methods } = definition;
|
|
184
|
+
return {
|
|
185
|
+
path,
|
|
186
|
+
middleware,
|
|
187
|
+
methods,
|
|
188
|
+
fetch(...args) {
|
|
189
|
+
const { params, search, method, ...init } = args[0] ?? {};
|
|
190
|
+
const href = buildApiHref(path, params, search);
|
|
191
|
+
return fetch(href, {
|
|
192
|
+
...init,
|
|
193
|
+
method
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function defineResource(path, options) {
|
|
199
|
+
const ResourceComponent = function LitzDefinedResourceComponent(props) {
|
|
200
|
+
const bindings = getClientBindings();
|
|
201
|
+
if (!bindings) return React.createElement(options.component, props);
|
|
202
|
+
const ScopedResourceComponent = bindings.createResourceComponent(path, options.component);
|
|
203
|
+
return React.createElement(ScopedResourceComponent, props);
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
path,
|
|
207
|
+
...options,
|
|
208
|
+
useLoaderResult: () => {
|
|
209
|
+
return getRequiredResourceData(path).loaderResult;
|
|
210
|
+
},
|
|
211
|
+
useLoaderData: () => {
|
|
212
|
+
const loaderResult = getRequiredResourceData(path).loaderResult;
|
|
213
|
+
return loaderResult?.kind === "data" ? loaderResult.data : null;
|
|
214
|
+
},
|
|
215
|
+
useLoaderView: () => {
|
|
216
|
+
const loaderResult = getRequiredResourceData(path).loaderResult;
|
|
217
|
+
return loaderResult?.kind === "view" ? loaderResult.node : null;
|
|
218
|
+
},
|
|
219
|
+
useActionResult: () => getRequiredResourceData(path).actionResult,
|
|
220
|
+
useActionData: () => {
|
|
221
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
222
|
+
return actionResult?.kind === "data" ? actionResult.data : null;
|
|
223
|
+
},
|
|
224
|
+
useActionView: () => {
|
|
225
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
226
|
+
return actionResult?.kind === "view" ? actionResult.node : null;
|
|
227
|
+
},
|
|
228
|
+
useActionError: () => {
|
|
229
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
230
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
231
|
+
},
|
|
232
|
+
useInvalid: () => {
|
|
233
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
234
|
+
return actionResult?.kind === "invalid" ? actionResult : null;
|
|
235
|
+
},
|
|
236
|
+
useData: () => getRequiredResourceData(path).data,
|
|
237
|
+
useView: () => getRequiredResourceData(path).view,
|
|
238
|
+
useError: () => {
|
|
239
|
+
const actionResult = getRequiredResourceData(path).actionResult;
|
|
240
|
+
return actionResult?.kind === "error" ? actionResult : null;
|
|
241
|
+
},
|
|
242
|
+
useStatus: () => getRequiredResourceStatus(path).status,
|
|
243
|
+
usePending: () => getRequiredResourceStatus(path).pending,
|
|
244
|
+
useParams: () => getRequiredResourceLocation(path).params,
|
|
245
|
+
useSearch: () => {
|
|
246
|
+
const location = getRequiredResourceLocation(path);
|
|
247
|
+
return [location.search, (params, options) => location.setSearch(params, options)];
|
|
248
|
+
},
|
|
249
|
+
useRetry: () => {
|
|
250
|
+
const actions = getRequiredResourceActions(path);
|
|
251
|
+
return () => actions.retry();
|
|
252
|
+
},
|
|
253
|
+
useReload: () => {
|
|
254
|
+
const actions = getRequiredResourceActions(path);
|
|
255
|
+
return () => actions.reload();
|
|
256
|
+
},
|
|
257
|
+
useSubmit: (opts) => {
|
|
258
|
+
const actions = getRequiredResourceActions(path);
|
|
259
|
+
return (payload) => actions.submit(payload, opts);
|
|
260
|
+
},
|
|
261
|
+
Form(props) {
|
|
262
|
+
const bindings = getClientBindings();
|
|
263
|
+
if (!bindings) return React.createElement(UnimplementedForm, props);
|
|
264
|
+
const FormComponent = bindings.createResourceFormComponent(path);
|
|
265
|
+
return React.createElement(FormComponent, props);
|
|
266
|
+
},
|
|
267
|
+
Component: ResourceComponent
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
function server(handler) {
|
|
271
|
+
return handler;
|
|
272
|
+
}
|
|
273
|
+
function withHeaders(result, headers) {
|
|
274
|
+
if (result instanceof Response) {
|
|
275
|
+
const mergedHeaders = mergeHeaders(result.headers, headers);
|
|
276
|
+
return new Response(result.body, {
|
|
277
|
+
status: result.status,
|
|
278
|
+
statusText: result.statusText,
|
|
279
|
+
headers: mergedHeaders
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
...result,
|
|
284
|
+
headers: mergeHeaders(result.headers, headers)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
function data(value, options = {}) {
|
|
288
|
+
return {
|
|
289
|
+
kind: "data",
|
|
290
|
+
data: value,
|
|
291
|
+
headers: options.headers,
|
|
292
|
+
status: options.status,
|
|
293
|
+
revalidate: options.revalidate
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function view(node, options = {}) {
|
|
297
|
+
return {
|
|
298
|
+
kind: "view",
|
|
299
|
+
node,
|
|
300
|
+
headers: options.headers,
|
|
301
|
+
revalidate: options.revalidate
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function invalid(options) {
|
|
305
|
+
return {
|
|
306
|
+
kind: "invalid",
|
|
307
|
+
headers: options.headers,
|
|
308
|
+
status: options.status,
|
|
309
|
+
fields: options.fields,
|
|
310
|
+
formError: options.formError,
|
|
311
|
+
data: options.data
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function redirect(location, options = {}) {
|
|
315
|
+
return {
|
|
316
|
+
kind: "redirect",
|
|
317
|
+
location,
|
|
318
|
+
headers: options.headers,
|
|
319
|
+
status: options.status,
|
|
320
|
+
replace: options.replace,
|
|
321
|
+
revalidate: options.revalidate
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function error(status, message, options = {}) {
|
|
325
|
+
return {
|
|
326
|
+
kind: "error",
|
|
327
|
+
status,
|
|
328
|
+
message,
|
|
329
|
+
headers: options.headers,
|
|
330
|
+
code: options.code,
|
|
331
|
+
data: options.data
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function buildApiHref(pathPattern, params, search) {
|
|
335
|
+
const pathname = pathPattern.replace(/:([A-Za-z0-9_]+)/g, (_, key) => {
|
|
336
|
+
const value = params?.[key];
|
|
337
|
+
if (value === void 0) throw new Error(`Missing required API param "${key}" for path "${pathPattern}".`);
|
|
338
|
+
return encodeURIComponent(value);
|
|
339
|
+
});
|
|
340
|
+
const searchString = (search instanceof URLSearchParams ? new URLSearchParams(search) : new URLSearchParams(search ?? {})).toString();
|
|
341
|
+
return searchString ? `${pathname}?${searchString}` : pathname;
|
|
342
|
+
}
|
|
343
|
+
//#endregion
|
|
344
|
+
export { data, defineApiRoute, defineLayout, defineResource, defineRoute, error, invalid, redirect, server, useLocation, useMatches, usePathname, view, withHeaders };
|