@eskais/app-sdk 0.0.0-stage → 0.2.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 +74 -2
- package/dist/action/action.d.ts +133 -0
- package/dist/action/action.js +224 -0
- package/dist/action/bulk.d.ts +24 -0
- package/dist/action/bulk.js +24 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.js +51 -0
- package/dist/cli/routes.d.ts +17 -0
- package/dist/cli/routes.js +36 -0
- package/dist/core/assume.d.ts +1 -0
- package/dist/core/assume.js +8 -0
- package/dist/core/constants.d.ts +359 -0
- package/dist/core/constants.js +187 -0
- package/dist/core/contracts.d.ts +107 -0
- package/dist/core/contracts.js +3 -0
- package/dist/core/data-labels.d.ts +210 -0
- package/dist/core/data-labels.js +385 -0
- package/dist/core/errors.d.ts +52 -0
- package/dist/core/errors.js +209 -0
- package/dist/core/guards.d.ts +9 -0
- package/dist/core/guards.js +14 -0
- package/dist/core/refusal.d.ts +30 -0
- package/dist/core/refusal.js +53 -0
- package/dist/dialogs/dialogs.d.ts +75 -0
- package/dist/dialogs/dialogs.js +66 -0
- package/dist/draft/draft.d.ts +36 -0
- package/dist/draft/draft.js +87 -0
- package/dist/errors/adapters.d.ts +21 -0
- package/dist/errors/adapters.js +81 -0
- package/dist/errors/define-errors.d.ts +88 -0
- package/dist/errors/define-errors.js +168 -0
- package/dist/errors/idempotency.d.ts +2 -0
- package/dist/errors/idempotency.js +13 -0
- package/dist/export/csv.d.ts +25 -0
- package/dist/export/csv.js +59 -0
- package/dist/i18n/adapters.d.ts +53 -0
- package/dist/i18n/adapters.js +56 -0
- package/dist/i18n/format.d.ts +10 -0
- package/dist/i18n/format.js +273 -0
- package/dist/i18n/labels.d.ts +10 -0
- package/dist/i18n/labels.js +227 -0
- package/dist/i18n/locale.d.ts +73 -0
- package/dist/i18n/locale.js +74 -0
- package/dist/i18n/messages.d.ts +93 -0
- package/dist/i18n/messages.js +16 -0
- package/dist/import/csv-parse.d.ts +11 -0
- package/dist/import/csv-parse.js +70 -0
- package/dist/import/import.d.ts +104 -0
- package/dist/import/import.js +266 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +45 -0
- package/dist/navigation/nav.d.ts +128 -0
- package/dist/navigation/nav.js +301 -0
- package/dist/navigation/router.d.ts +81 -0
- package/dist/navigation/router.js +249 -0
- package/dist/navigation/shortcuts.d.ts +37 -0
- package/dist/navigation/shortcuts.js +93 -0
- package/dist/next/index.d.ts +3 -0
- package/dist/next/index.js +3 -0
- package/dist/next/screen-hydration.d.ts +12 -0
- package/dist/next/screen-hydration.js +26 -0
- package/dist/next/screen.d.ts +25 -0
- package/dist/next/screen.js +44 -0
- package/dist/next/server/handler.d.ts +39 -0
- package/dist/next/server/handler.js +190 -0
- package/dist/next/server/index.d.ts +6 -0
- package/dist/next/server/index.js +6 -0
- package/dist/next/server/page.d.ts +63 -0
- package/dist/next/server/page.js +110 -0
- package/dist/next/server/prefetch.d.ts +29 -0
- package/dist/next/server/prefetch.js +97 -0
- package/dist/next/server/session.d.ts +25 -0
- package/dist/next/server/session.js +23 -0
- package/dist/next/use-next-router.d.ts +21 -0
- package/dist/next/use-next-router.js +104 -0
- package/dist/permissions/permissions.d.ts +134 -0
- package/dist/permissions/permissions.js +121 -0
- package/dist/react/data-layer.d.ts +68 -0
- package/dist/react/data-layer.js +76 -0
- package/dist/react/hooks/use-action-dialog.d.ts +25 -0
- package/dist/react/hooks/use-action-dialog.js +31 -0
- package/dist/react/hooks/use-action.d.ts +37 -0
- package/dist/react/hooks/use-action.js +125 -0
- package/dist/react/hooks/use-better-auth.d.ts +12 -0
- package/dist/react/hooks/use-better-auth.js +11 -0
- package/dist/react/hooks/use-bulk-action.d.ts +20 -0
- package/dist/react/hooks/use-bulk-action.js +24 -0
- package/dist/react/hooks/use-dialogs.d.ts +14 -0
- package/dist/react/hooks/use-dialogs.js +20 -0
- package/dist/react/hooks/use-draft.d.ts +5 -0
- package/dist/react/hooks/use-draft.js +17 -0
- package/dist/react/hooks/use-nav.d.ts +17 -0
- package/dist/react/hooks/use-nav.js +35 -0
- package/dist/react/hooks/use-new-rows.d.ts +22 -0
- package/dist/react/hooks/use-new-rows.js +24 -0
- package/dist/react/hooks/use-permissions.d.ts +13 -0
- package/dist/react/hooks/use-permissions.js +19 -0
- package/dist/react/hooks/use-reauthenticate.d.ts +6 -0
- package/dist/react/hooks/use-reauthenticate.js +11 -0
- package/dist/react/hooks/use-resource.d.ts +92 -0
- package/dist/react/hooks/use-resource.js +255 -0
- package/dist/react/hooks/use-view.d.ts +6 -0
- package/dist/react/hooks/use-view.js +24 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.js +15 -0
- package/dist/react/keys.d.ts +60 -0
- package/dist/react/keys.js +71 -0
- package/dist/react/new-rows.d.ts +22 -0
- package/dist/react/new-rows.js +72 -0
- package/dist/refresh/new-rows.d.ts +13 -0
- package/dist/refresh/new-rows.js +18 -0
- package/dist/resource/form-schema.d.ts +11 -0
- package/dist/resource/form-schema.js +51 -0
- package/dist/resource/relations.d.ts +99 -0
- package/dist/resource/relations.js +194 -0
- package/dist/resource/resource-relations.d.ts +47 -0
- package/dist/resource/resource-relations.js +154 -0
- package/dist/resource/resource-schema.d.ts +9 -0
- package/dist/resource/resource-schema.js +73 -0
- package/dist/resource/resource-texts.d.ts +18 -0
- package/dist/resource/resource-texts.js +13 -0
- package/dist/resource/resource-types.d.ts +420 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/resource-words.d.ts +25 -0
- package/dist/resource/resource-words.js +221 -0
- package/dist/resource/resource.d.ts +15 -0
- package/dist/resource/resource.js +280 -0
- package/dist/resource/schema.d.ts +57 -0
- package/dist/resource/schema.js +115 -0
- package/dist/services/from-page-source.d.ts +15 -0
- package/dist/services/from-page-source.js +27 -0
- package/dist/services/memory-query.d.ts +8 -0
- package/dist/services/memory-query.js +165 -0
- package/dist/services/memory-service.d.ts +33 -0
- package/dist/services/memory-service.js +291 -0
- package/dist/services/read-only-service.d.ts +18 -0
- package/dist/services/read-only-service.js +25 -0
- package/dist/services/rest-service.d.ts +65 -0
- package/dist/services/rest-service.js +222 -0
- package/dist/services/rng.d.ts +9 -0
- package/dist/services/rng.js +22 -0
- package/dist/services/spring-service.d.ts +58 -0
- package/dist/services/spring-service.js +132 -0
- package/dist/session/better-auth.d.ts +83 -0
- package/dist/session/better-auth.js +86 -0
- package/dist/session/session.d.ts +45 -0
- package/dist/session/session.js +36 -0
- package/dist/view/preferences.d.ts +35 -0
- package/dist/view/preferences.js +93 -0
- package/dist/view/view-types.d.ts +124 -0
- package/dist/view/view-types.js +1 -0
- package/dist/view/view-url.d.ts +7 -0
- package/dist/view/view-url.js +54 -0
- package/dist/view/view.d.ts +4 -0
- package/dist/view/view.js +392 -0
- package/locales/en.json +193 -0
- package/locales/es.json +193 -0
- package/package.json +88 -4
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { RouterKind, ValueType } from "../core/constants.js";
|
|
2
|
+
import { isFunction } from "../core/guards.js";
|
|
3
|
+
export function splitPath(path) {
|
|
4
|
+
const hashAt = path.indexOf("#");
|
|
5
|
+
const beforeHash = hashAt === -1 ? path : path.slice(0, hashAt);
|
|
6
|
+
const hash = hashAt === -1 ? "" : path.slice(hashAt);
|
|
7
|
+
const queryAt = beforeHash.indexOf("?");
|
|
8
|
+
const pathname = (queryAt === -1 ? beforeHash : beforeHash.slice(0, queryAt)) || "/";
|
|
9
|
+
const search = queryAt === -1 ? "" : beforeHash.slice(queryAt);
|
|
10
|
+
return { pathname: pathname.startsWith("/") ? pathname : `/${pathname}`, search: search === "?" ? "" : search, hash };
|
|
11
|
+
}
|
|
12
|
+
export function joinPath(location) {
|
|
13
|
+
return `${location.pathname}${location.search}`;
|
|
14
|
+
}
|
|
15
|
+
function cachedLocation(read) {
|
|
16
|
+
let last = "";
|
|
17
|
+
let value = splitPath("/");
|
|
18
|
+
return () => {
|
|
19
|
+
const current = read();
|
|
20
|
+
if (current !== last) {
|
|
21
|
+
last = current;
|
|
22
|
+
value = splitPath(current);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function browserWindow() {
|
|
28
|
+
return typeof window === ValueType.undefined ? undefined : window;
|
|
29
|
+
}
|
|
30
|
+
function listeners() {
|
|
31
|
+
const set = new Set();
|
|
32
|
+
return {
|
|
33
|
+
add(listener) {
|
|
34
|
+
set.add(listener);
|
|
35
|
+
return () => {
|
|
36
|
+
set.delete(listener);
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
notify() {
|
|
40
|
+
for (const listener of set)
|
|
41
|
+
listener();
|
|
42
|
+
},
|
|
43
|
+
get size() {
|
|
44
|
+
return set.size;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function beforeUnloadBlock(target, when) {
|
|
49
|
+
if (!target)
|
|
50
|
+
return () => { };
|
|
51
|
+
const onBeforeUnload = (event) => {
|
|
52
|
+
if (!when())
|
|
53
|
+
return;
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
};
|
|
56
|
+
target.addEventListener("beforeunload", onBeforeUnload);
|
|
57
|
+
return () => target.removeEventListener("beforeunload", onBeforeUnload);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Routes in the fragment (`/#/customers/7`): no server configuration, reload and links work, but the server never
|
|
61
|
+
* sees the route (no server rendering) and it clashes with OAuth's implicit flow.
|
|
62
|
+
*/
|
|
63
|
+
export function hashRouter(options = {}) {
|
|
64
|
+
const prefix = options.prefix ?? "#";
|
|
65
|
+
const target = options.window ?? browserWindow();
|
|
66
|
+
const read = () => {
|
|
67
|
+
const hash = target?.location.hash ?? "";
|
|
68
|
+
return hash.startsWith(prefix) ? hash.slice(prefix.length) || "/" : "/";
|
|
69
|
+
};
|
|
70
|
+
const location = cachedLocation(read);
|
|
71
|
+
const own = listeners();
|
|
72
|
+
const onChange = () => own.notify();
|
|
73
|
+
let pendingGuard;
|
|
74
|
+
return {
|
|
75
|
+
kind: RouterKind.hash,
|
|
76
|
+
location,
|
|
77
|
+
subscribe(listener) {
|
|
78
|
+
if (own.size === 0)
|
|
79
|
+
target?.addEventListener("hashchange", onChange);
|
|
80
|
+
const remove = own.add(listener);
|
|
81
|
+
return () => {
|
|
82
|
+
remove();
|
|
83
|
+
if (own.size === 0)
|
|
84
|
+
target?.removeEventListener("hashchange", onChange);
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
navigate(to, navigateOptions) {
|
|
88
|
+
if (!target)
|
|
89
|
+
return;
|
|
90
|
+
const go = () => {
|
|
91
|
+
const url = `${target.location.pathname}${target.location.search}${prefix}${to}`;
|
|
92
|
+
if (navigateOptions?.replace === true)
|
|
93
|
+
target.history.replaceState(target.history.state, "", url);
|
|
94
|
+
else
|
|
95
|
+
target.history.pushState(null, "", url);
|
|
96
|
+
own.notify();
|
|
97
|
+
};
|
|
98
|
+
if (pendingGuard)
|
|
99
|
+
void pendingGuard().then((leave) => leave && go());
|
|
100
|
+
else
|
|
101
|
+
go();
|
|
102
|
+
},
|
|
103
|
+
href: (to) => `${prefix}${to}`,
|
|
104
|
+
block(when, ask) {
|
|
105
|
+
const guard = async () => (when() ? ask() : true);
|
|
106
|
+
pendingGuard = guard;
|
|
107
|
+
const unload = beforeUnloadBlock(target, when);
|
|
108
|
+
return () => {
|
|
109
|
+
if (pendingGuard === guard)
|
|
110
|
+
pendingGuard = undefined;
|
|
111
|
+
unload();
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Clean URLs with `pushState` and no library. The server must answer every path with `index.html` (an SPA rewrite),
|
|
118
|
+
* or a reload of `/customers/7` is a 404.
|
|
119
|
+
*/
|
|
120
|
+
export function historyRouter(options = {}) {
|
|
121
|
+
const base = (options.base ?? "").replace(/\/$/u, "");
|
|
122
|
+
const target = options.window ?? browserWindow();
|
|
123
|
+
const read = () => {
|
|
124
|
+
if (!target)
|
|
125
|
+
return "/";
|
|
126
|
+
const path = target.location.pathname;
|
|
127
|
+
const inside = base !== "" && path.startsWith(base) ? path.slice(base.length) || "/" : path;
|
|
128
|
+
return `${inside}${target.location.search}`;
|
|
129
|
+
};
|
|
130
|
+
const location = cachedLocation(read);
|
|
131
|
+
const own = listeners();
|
|
132
|
+
const onChange = () => own.notify();
|
|
133
|
+
let pendingGuard;
|
|
134
|
+
return {
|
|
135
|
+
kind: RouterKind.history,
|
|
136
|
+
location,
|
|
137
|
+
subscribe(listener) {
|
|
138
|
+
if (own.size === 0)
|
|
139
|
+
target?.addEventListener("popstate", onChange);
|
|
140
|
+
const remove = own.add(listener);
|
|
141
|
+
return () => {
|
|
142
|
+
remove();
|
|
143
|
+
if (own.size === 0)
|
|
144
|
+
target?.removeEventListener("popstate", onChange);
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
navigate(to, navigateOptions) {
|
|
148
|
+
if (!target)
|
|
149
|
+
return;
|
|
150
|
+
const go = () => {
|
|
151
|
+
if (navigateOptions?.replace === true)
|
|
152
|
+
target.history.replaceState(target.history.state, "", `${base}${to}`);
|
|
153
|
+
else
|
|
154
|
+
target.history.pushState(null, "", `${base}${to}`);
|
|
155
|
+
own.notify();
|
|
156
|
+
};
|
|
157
|
+
if (pendingGuard)
|
|
158
|
+
void pendingGuard().then((leave) => leave && go());
|
|
159
|
+
else
|
|
160
|
+
go();
|
|
161
|
+
},
|
|
162
|
+
href: (to) => `${base}${to}`,
|
|
163
|
+
block(when, ask) {
|
|
164
|
+
const guard = async () => (when() ? ask() : true);
|
|
165
|
+
pendingGuard = guard;
|
|
166
|
+
const unload = beforeUnloadBlock(target, when);
|
|
167
|
+
return () => {
|
|
168
|
+
if (pendingGuard === guard)
|
|
169
|
+
pendingGuard = undefined;
|
|
170
|
+
unload();
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export function memoryRouter(initial = "/") {
|
|
176
|
+
const entries = [initial];
|
|
177
|
+
const location = cachedLocation(() => entries.at(-1) ?? "/");
|
|
178
|
+
const own = listeners();
|
|
179
|
+
let pendingGuard;
|
|
180
|
+
return {
|
|
181
|
+
kind: RouterKind.memory,
|
|
182
|
+
entries,
|
|
183
|
+
location,
|
|
184
|
+
subscribe: (listener) => own.add(listener),
|
|
185
|
+
navigate(to, options) {
|
|
186
|
+
const go = () => {
|
|
187
|
+
if (options?.replace === true)
|
|
188
|
+
entries[entries.length - 1] = to;
|
|
189
|
+
else
|
|
190
|
+
entries.push(to);
|
|
191
|
+
own.notify();
|
|
192
|
+
};
|
|
193
|
+
if (pendingGuard)
|
|
194
|
+
void pendingGuard().then((leave) => leave && go());
|
|
195
|
+
else
|
|
196
|
+
go();
|
|
197
|
+
},
|
|
198
|
+
href: (to) => to,
|
|
199
|
+
block(when, ask) {
|
|
200
|
+
const guard = async () => (when() ? ask() : true);
|
|
201
|
+
pendingGuard = guard;
|
|
202
|
+
return () => {
|
|
203
|
+
if (pendingGuard === guard)
|
|
204
|
+
pendingGuard = undefined;
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
// Workaround: TanStack's hash history (1.170) appends the page's real query to the route's, so `#/x?a=1` under
|
|
210
|
+
// `/?demo=y` reads `?a=1?demo=y`; the route's own query is what comes before the page's.
|
|
211
|
+
function ownSearch(search) {
|
|
212
|
+
const page = typeof window === ValueType.undefined ? "" : window.location.search;
|
|
213
|
+
if (page === "" || search === page || !search.endsWith(page))
|
|
214
|
+
return search;
|
|
215
|
+
return search.slice(0, -page.length);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* TanStack Router as a `RouterAdapter`. Pass the router, or a function that returns it when the router is created
|
|
219
|
+
* after the navigation manifest (its route tree comes from `nav.routes()`).
|
|
220
|
+
*/
|
|
221
|
+
export function tanstackRouter(router) {
|
|
222
|
+
const get = isFunction(router) ? router : () => router;
|
|
223
|
+
const location = cachedLocation(() => {
|
|
224
|
+
const current = get().history.location;
|
|
225
|
+
return `${current.pathname}${ownSearch(current.search)}${current.hash}`;
|
|
226
|
+
});
|
|
227
|
+
return {
|
|
228
|
+
kind: RouterKind.tanstack,
|
|
229
|
+
location,
|
|
230
|
+
subscribe: (listener) => get().history.subscribe(listener),
|
|
231
|
+
navigate(to, options) {
|
|
232
|
+
const instance = get();
|
|
233
|
+
const replace = options?.replace === true;
|
|
234
|
+
if (instance.navigate)
|
|
235
|
+
void instance.navigate({ href: to, replace });
|
|
236
|
+
else if (replace)
|
|
237
|
+
instance.history.replace(to);
|
|
238
|
+
else
|
|
239
|
+
instance.history.push(to);
|
|
240
|
+
},
|
|
241
|
+
href: (to) => get().history.createHref(to),
|
|
242
|
+
block(when, ask) {
|
|
243
|
+
const history = get().history;
|
|
244
|
+
if (!history.block)
|
|
245
|
+
return () => { };
|
|
246
|
+
return history.block({ blockerFn: async () => (when() ? !(await ask()) : false), enableBeforeUnload: when });
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** A registered shortcut: who owns it (`customers`, `customers.block`), where it runs and what it's called. */
|
|
2
|
+
export type ShortcutEntry = {
|
|
3
|
+
keys: string;
|
|
4
|
+
id: string;
|
|
5
|
+
label?: string | undefined;
|
|
6
|
+
/** The screen or region it runs in; none runs everywhere. */
|
|
7
|
+
scope?: string | undefined;
|
|
8
|
+
};
|
|
9
|
+
export type ShortcutConflict = {
|
|
10
|
+
keys: string;
|
|
11
|
+
scope: string | undefined;
|
|
12
|
+
/** The owners whose shortcuts collide, in registration order. */
|
|
13
|
+
ids: readonly string[];
|
|
14
|
+
};
|
|
15
|
+
/** The keys in one spelling: lower case, modifiers in a fixed order, `cmd` as `meta`, steps one space apart. */
|
|
16
|
+
export declare function normalizeShortcut(keys: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Shortcuts that collide where they can both run: the same keys, or a key that starts another's sequence (`g`
|
|
19
|
+
* against `g c`), in the same scope or with one of them global. One owner registering twice is no conflict.
|
|
20
|
+
*/
|
|
21
|
+
export declare function shortcutConflicts(entries: readonly ShortcutEntry[]): ShortcutConflict[];
|
|
22
|
+
export type ShortcutRegistry = {
|
|
23
|
+
/** Registers while the owner is mounted; returns the cleanup. */
|
|
24
|
+
add(entry: ShortcutEntry): () => void;
|
|
25
|
+
entries(): readonly ShortcutEntry[];
|
|
26
|
+
conflicts(): ShortcutConflict[];
|
|
27
|
+
};
|
|
28
|
+
/** Tracks the shortcuts registered now and reports a new one that collides with one already there. */
|
|
29
|
+
export declare function createShortcutRegistry(options?: {
|
|
30
|
+
onConflict?: (conflict: ShortcutConflict) => void;
|
|
31
|
+
}): ShortcutRegistry;
|
|
32
|
+
/** A warning for the developer, in the console, outside production builds. */
|
|
33
|
+
export declare function warnInDevelopment(message: string): void;
|
|
34
|
+
/** Says which owners collide, in the console, outside production builds. */
|
|
35
|
+
export declare function warnShortcutConflict(conflict: ShortcutConflict): void;
|
|
36
|
+
/** The app's registered shortcuts: `NavShell`, `NavCommand` and the resource blocks add theirs here. */
|
|
37
|
+
export declare const appShortcuts: ShortcutRegistry;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isRecord, isString } from "../core/guards.js";
|
|
2
|
+
const modifierOrder = ["mod", "ctrl", "meta", "alt", "shift"];
|
|
3
|
+
/** The keys in one spelling: lower case, modifiers in a fixed order, `cmd` as `meta`, steps one space apart. */
|
|
4
|
+
export function normalizeShortcut(keys) {
|
|
5
|
+
return keys
|
|
6
|
+
.trim()
|
|
7
|
+
.split(/\s+/u)
|
|
8
|
+
.map((step) => {
|
|
9
|
+
const parts = step
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.split("+")
|
|
12
|
+
.filter((part) => part !== "")
|
|
13
|
+
.map((part) => (part === "cmd" || part === "command" ? "meta" : part === "control" ? "ctrl" : part));
|
|
14
|
+
const key = parts.at(-1) ?? "";
|
|
15
|
+
const modifiers = parts.slice(0, -1).toSorted((a, b) => modifierOrder.indexOf(a) - modifierOrder.indexOf(b));
|
|
16
|
+
return [...modifiers, key].join("+");
|
|
17
|
+
})
|
|
18
|
+
.join(" ");
|
|
19
|
+
}
|
|
20
|
+
const overlaps = (a, b) => a === undefined || b === undefined || a === b;
|
|
21
|
+
function collides(a, b) {
|
|
22
|
+
if (a === b)
|
|
23
|
+
return true;
|
|
24
|
+
const [first] = a.split(" ");
|
|
25
|
+
const [other] = b.split(" ");
|
|
26
|
+
return a.includes(" ") !== b.includes(" ") && (a === other || b === first);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Shortcuts that collide where they can both run: the same keys, or a key that starts another's sequence (`g`
|
|
30
|
+
* against `g c`), in the same scope or with one of them global. One owner registering twice is no conflict.
|
|
31
|
+
*/
|
|
32
|
+
export function shortcutConflicts(entries) {
|
|
33
|
+
const conflicts = [];
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
for (const [index, entry] of entries.entries()) {
|
|
36
|
+
const keys = normalizeShortcut(entry.keys);
|
|
37
|
+
const ids = [entry.id];
|
|
38
|
+
for (const other of entries.slice(index + 1)) {
|
|
39
|
+
if (other.id === entry.id || ids.includes(other.id))
|
|
40
|
+
continue;
|
|
41
|
+
if (overlaps(entry.scope, other.scope) && collides(keys, normalizeShortcut(other.keys)))
|
|
42
|
+
ids.push(other.id);
|
|
43
|
+
}
|
|
44
|
+
const key = `${keys}|${entry.scope ?? ""}`;
|
|
45
|
+
if (ids.length > 1 && !seen.has(key)) {
|
|
46
|
+
seen.add(key);
|
|
47
|
+
conflicts.push({ keys, scope: entry.scope, ids });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return conflicts;
|
|
51
|
+
}
|
|
52
|
+
/** Tracks the shortcuts registered now and reports a new one that collides with one already there. */
|
|
53
|
+
export function createShortcutRegistry(options = {}) {
|
|
54
|
+
const current = new Set();
|
|
55
|
+
return {
|
|
56
|
+
add(entry) {
|
|
57
|
+
const own = { ...entry };
|
|
58
|
+
const before = shortcutConflicts([...current]).length;
|
|
59
|
+
current.add(own);
|
|
60
|
+
const found = shortcutConflicts([...current]);
|
|
61
|
+
if (found.length > before) {
|
|
62
|
+
for (const conflict of found)
|
|
63
|
+
if (conflict.ids.includes(own.id))
|
|
64
|
+
options.onConflict?.(conflict);
|
|
65
|
+
}
|
|
66
|
+
return () => {
|
|
67
|
+
current.delete(own);
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
entries: () => [...current],
|
|
71
|
+
conflicts: () => shortcutConflicts([...current]),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function inProduction() {
|
|
75
|
+
const runtime = Reflect.get(globalThis, "process");
|
|
76
|
+
return isRecord(runtime) && isRecord(runtime.env) && isString(runtime.env.NODE_ENV)
|
|
77
|
+
? runtime.env.NODE_ENV === "production"
|
|
78
|
+
: false;
|
|
79
|
+
}
|
|
80
|
+
/** A warning for the developer, in the console, outside production builds. */
|
|
81
|
+
export function warnInDevelopment(message) {
|
|
82
|
+
if (inProduction())
|
|
83
|
+
return;
|
|
84
|
+
// oxlint-disable-next-line no-console -- Spec: conflicts and slow reads are reported to the developer in the console
|
|
85
|
+
console.warn(message);
|
|
86
|
+
}
|
|
87
|
+
/** Says which owners collide, in the console, outside production builds. */
|
|
88
|
+
export function warnShortcutConflict(conflict) {
|
|
89
|
+
const where = conflict.scope === undefined ? "" : ` in ${conflict.scope}`;
|
|
90
|
+
warnInDevelopment(`Shortcut "${conflict.keys}"${where} is taken by ${conflict.ids.map((id) => `"${id}"`).join(" and ")}.`);
|
|
91
|
+
}
|
|
92
|
+
/** The app's registered shortcuts: `NavShell`, `NavCommand` and the resource blocks add theirs here. */
|
|
93
|
+
export const appShortcuts = createShortcutRegistry({ onConflict: warnShortcutConflict });
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { useNextRouter, type NextAppRouter, type NextRouterOptions } from "./use-next-router.js";
|
|
2
|
+
export { isRecordScreen, navScreen, pathOfParams, resourceScreen, type NextScreen, type RouteParams } from "./screen.js";
|
|
3
|
+
export { ScreenHydration, type ScreenHydrationProps } from "./screen-hydration.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type DehydratedState } from "@tanstack/react-query";
|
|
2
|
+
import { type ReactNode } from "react";
|
|
3
|
+
export type ScreenHydrationProps = {
|
|
4
|
+
state: DehydratedState;
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* `HydrationBoundary` that also fills, during render, the queries already in the cache with no data yet. A layout
|
|
9
|
+
* above the page creates them (a breadcrumb asking for the record's title), and TanStack would only fill them in an
|
|
10
|
+
* effect: the server render and the first client render would show a spinner instead of the record.
|
|
11
|
+
*/
|
|
12
|
+
export declare function ScreenHydration({ state, children }: ScreenHydrationProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { hydrate, HydrationBoundary, QueryClientContext } from "@tanstack/react-query";
|
|
4
|
+
import { use, useMemo } from "react";
|
|
5
|
+
/**
|
|
6
|
+
* `HydrationBoundary` that also fills, during render, the queries already in the cache with no data yet. A layout
|
|
7
|
+
* above the page creates them (a breadcrumb asking for the record's title), and TanStack would only fill them in an
|
|
8
|
+
* effect: the server render and the first client render would show a spinner instead of the record.
|
|
9
|
+
*/
|
|
10
|
+
export function ScreenHydration({ state, children }) {
|
|
11
|
+
const client = use(QueryClientContext);
|
|
12
|
+
if (!client)
|
|
13
|
+
throw new Error("A Next.js page from navPage or resourcePage renders inside the DataLayer: put it in a layout above.");
|
|
14
|
+
const ready = useMemo(() => {
|
|
15
|
+
const cache = client.getQueryCache();
|
|
16
|
+
const waiting = state.queries.filter((query) => {
|
|
17
|
+
const existing = cache.get(query.queryHash);
|
|
18
|
+
return existing !== undefined && existing.state.data === undefined;
|
|
19
|
+
});
|
|
20
|
+
// Perf: TanStack's boundary hydrates the new queries; only those it would leave empty until an effect go here.
|
|
21
|
+
if (waiting.length > 0)
|
|
22
|
+
hydrate(client, { mutations: [], queries: waiting });
|
|
23
|
+
return state;
|
|
24
|
+
}, [client, state]);
|
|
25
|
+
return _jsx(HydrationBoundary, { state: ready, children: children });
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ResourceScreen } from "../core/constants.js";
|
|
2
|
+
import type { Nav } from "../navigation/nav.js";
|
|
3
|
+
import type { AnyResource } from "../resource/resource.js";
|
|
4
|
+
/** The screen a path shows: one of a resource's four, with the record's id on a detail or edit path. */
|
|
5
|
+
export type NextScreen = {
|
|
6
|
+
resource: AnyResource;
|
|
7
|
+
screen: ResourceScreen;
|
|
8
|
+
/** The id as the resource parses it (a number for numeric ids). */
|
|
9
|
+
id: unknown;
|
|
10
|
+
/** The path inside the app, without the mount `base`: `/customers/7/edit`. */
|
|
11
|
+
pathname: string;
|
|
12
|
+
/** A record's `hasMany` tab on its path, `orders` in `/customers/7/orders`. */
|
|
13
|
+
tab?: string;
|
|
14
|
+
};
|
|
15
|
+
export type RouteParams = Record<string, string | readonly string[] | undefined>;
|
|
16
|
+
export declare function isSegments(value: string | readonly string[] | undefined): value is readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* The path a catch-all route's params stand for: the first array param (`[[...path]]`, `[[...slug]]`) joined under
|
|
19
|
+
* `prefix`. An empty optional catch-all is no param at all, so the path is the prefix itself.
|
|
20
|
+
*/
|
|
21
|
+
export declare function pathOfParams(params: RouteParams, prefix?: string): string;
|
|
22
|
+
export declare function resourceScreen(resource: AnyResource, pathname: string): NextScreen | undefined;
|
|
23
|
+
/** The resource screen of the manifest a path shows, or undefined when no resource of it has that path. */
|
|
24
|
+
export declare function navScreen<Route>(nav: Nav<Route>, pathname: string): NextScreen | undefined;
|
|
25
|
+
export declare function isRecordScreen(screen: NextScreen): boolean;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ResourceScreen } from "../core/constants.js";
|
|
2
|
+
function trimBase(base) {
|
|
3
|
+
return (base ?? "").replace(/\/+$/u, "");
|
|
4
|
+
}
|
|
5
|
+
export function isSegments(value) {
|
|
6
|
+
return Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function decoded(segment) {
|
|
9
|
+
try {
|
|
10
|
+
return decodeURIComponent(segment);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return segment;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The path a catch-all route's params stand for: the first array param (`[[...path]]`, `[[...slug]]`) joined under
|
|
18
|
+
* `prefix`. An empty optional catch-all is no param at all, so the path is the prefix itself.
|
|
19
|
+
*/
|
|
20
|
+
export function pathOfParams(params, prefix = "") {
|
|
21
|
+
const segments = Object.values(params).find(isSegments) ?? [];
|
|
22
|
+
const joined = segments.map((segment) => encodeURIComponent(decoded(segment))).join("/");
|
|
23
|
+
const path = `${trimBase(prefix)}/${joined}`.replace(/\/+$/u, "");
|
|
24
|
+
return path === "" ? "/" : path;
|
|
25
|
+
}
|
|
26
|
+
export function resourceScreen(resource, pathname) {
|
|
27
|
+
const match = resource.match(pathname);
|
|
28
|
+
if (!match)
|
|
29
|
+
return undefined;
|
|
30
|
+
const id = match.id === undefined ? undefined : resource.parseId(match.id);
|
|
31
|
+
return { resource, screen: match.screen, id, pathname, ...(match.tab === undefined ? {} : { tab: match.tab }) };
|
|
32
|
+
}
|
|
33
|
+
/** The resource screen of the manifest a path shows, or undefined when no resource of it has that path. */
|
|
34
|
+
export function navScreen(nav, pathname) {
|
|
35
|
+
for (const resource of nav.resources()) {
|
|
36
|
+
const screen = resourceScreen(resource, pathname);
|
|
37
|
+
if (screen)
|
|
38
|
+
return screen;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
export function isRecordScreen(screen) {
|
|
43
|
+
return screen.screen === ResourceScreen.detail || screen.screen === ResourceScreen.edit;
|
|
44
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Query } from "../../core/contracts.js";
|
|
2
|
+
import { type ServiceError } from "../../core/errors.js";
|
|
3
|
+
import { type AnyResource } from "../../resource/resource.js";
|
|
4
|
+
import type { Session } from "../../session/session.js";
|
|
5
|
+
import type { RouteParams } from "../screen.js";
|
|
6
|
+
import type { AnyService } from "./prefetch.js";
|
|
7
|
+
/** The second argument Next.js passes a route handler. */
|
|
8
|
+
export type NextRouteContext = {
|
|
9
|
+
params: Promise<RouteParams>;
|
|
10
|
+
};
|
|
11
|
+
export type RouteHandler = (request: Request, context: NextRouteContext) => Promise<Response>;
|
|
12
|
+
export type ResourceHandler = {
|
|
13
|
+
GET: RouteHandler;
|
|
14
|
+
POST: RouteHandler;
|
|
15
|
+
PATCH: RouteHandler;
|
|
16
|
+
PUT: RouteHandler;
|
|
17
|
+
DELETE: RouteHandler;
|
|
18
|
+
};
|
|
19
|
+
export type ResourceHandlerOptions = {
|
|
20
|
+
/** The backend service; the resource's own by default. */
|
|
21
|
+
service?: AnyService;
|
|
22
|
+
/** The request's session, as `nextSession(…)` gives it. */
|
|
23
|
+
session?: () => Session | undefined | Promise<Session | undefined>;
|
|
24
|
+
/** The most rows one page may ask for. Default: the service's `maxPageSize`, or 100. */
|
|
25
|
+
maxPageSize?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* A `ServiceError` as an RFC 9457 problem: the status of its kind, the message as `detail`, field errors as
|
|
29
|
+
* `errors[{ field, message, code }]` and a conflict's `current` row. `restService` reads it back into the same error.
|
|
30
|
+
*/
|
|
31
|
+
export declare function problemResponse(error: ServiceError): Response;
|
|
32
|
+
/** The `Query` a `restService` list request carries: filters by their declared kind, `q`, `sort`, `size`, `page`. */
|
|
33
|
+
export declare function queryOfSearch(resource: AnyResource, params: URLSearchParams, maxPageSize?: number): Query<unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Route handlers (`app/api/customers/[[...id]]/route.ts`) that serve a service the way `restService` calls it: `GET`,
|
|
36
|
+
* `POST`, `PATCH`/`PUT` (`If-Match` is the version), `DELETE`. Each asks the resource's `can` with the request's
|
|
37
|
+
* session first; every error goes back as a problem `restService` reads into the same `ServiceError`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function resourceHandler(resource: AnyResource, options?: ResourceHandlerOptions): ResourceHandler;
|