@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,15 @@
|
|
|
1
|
+
import { dependentsOf } from "./resource-relations.js";
|
|
2
|
+
import { type RelationRows, type Relations } from "./relations.js";
|
|
3
|
+
import type { DefineResourceOptions, Resource, RefreshPolicy, ResolvedRefresh } from "./resource-types.js";
|
|
4
|
+
export type * from "./resource-types.js";
|
|
5
|
+
export { dependentsOf };
|
|
6
|
+
export declare const searchKey = "q";
|
|
7
|
+
/** A resource's refresh policy with a list's own over it. */
|
|
8
|
+
export declare function refreshPolicy(resource: {
|
|
9
|
+
readonly refresh: ResolvedRefresh;
|
|
10
|
+
}, overrides?: RefreshPolicy): ResolvedRefresh;
|
|
11
|
+
/**
|
|
12
|
+
* Declares a resource. Called twice, `defineResource<Customer>()({ … })`, so `T` is given by hand and the rest is
|
|
13
|
+
* inferred. Columns, fields and filters left out come from the schema (Zod 4 first).
|
|
14
|
+
*/
|
|
15
|
+
export declare function defineResource<T>(): <Id = string, F = Record<string, unknown>, const R extends Relations = {}, const Key extends string = string>(options: DefineResourceOptions<T, Id, F, R, Key>) => Resource<T & RelationRows<R>, Id, F, Key>;
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { JsonType, NewRows, RelationKind, ResourceAction, ResourceScreen } from "../core/constants.js";
|
|
2
|
+
import { assumeType } from "../core/assume.js";
|
|
3
|
+
import { isFunction, isRecord, isString, isText } from "../core/guards.js";
|
|
4
|
+
import { valueAt } from "../services/memory-query.js";
|
|
5
|
+
import { firstRefusal, toAllowed } from "../core/refusal.js";
|
|
6
|
+
import { defined, dependentsOf } from "./resource-relations.js";
|
|
7
|
+
import { includeOf, registerResource, resolveRelations, } from "./relations.js";
|
|
8
|
+
import { resourceTexts } from "./resource-texts.js";
|
|
9
|
+
import { resourceWords } from "./resource-words.js";
|
|
10
|
+
import { zodIntrospect } from "./schema.js";
|
|
11
|
+
import { refusalIn } from "../i18n/locale.js";
|
|
12
|
+
export { dependentsOf };
|
|
13
|
+
export const searchKey = "q";
|
|
14
|
+
const writes = new Set([
|
|
15
|
+
ResourceAction.create,
|
|
16
|
+
ResourceAction.update,
|
|
17
|
+
ResourceAction.remove,
|
|
18
|
+
ResourceAction.restore,
|
|
19
|
+
ResourceAction.import,
|
|
20
|
+
]);
|
|
21
|
+
/** A resource's refresh policy with a list's own over it. */
|
|
22
|
+
export function refreshPolicy(resource, overrides = {}) {
|
|
23
|
+
return {
|
|
24
|
+
onFocus: overrides.onFocus ?? resource.refresh.onFocus,
|
|
25
|
+
onReconnect: overrides.onReconnect ?? resource.refresh.onReconnect,
|
|
26
|
+
interval: overrides.interval ?? resource.refresh.interval,
|
|
27
|
+
staleTime: overrides.staleTime ?? resource.refresh.staleTime,
|
|
28
|
+
newRows: overrides.newRows ?? resource.refresh.newRows,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function segment(value) {
|
|
32
|
+
return value.replace(/^\/+|\/+$/gu, "");
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Declares a resource. Called twice, `defineResource<Customer>()({ … })`, so `T` is given by hand and the rest is
|
|
36
|
+
* inferred. Columns, fields and filters left out come from the schema (Zod 4 first).
|
|
37
|
+
*/
|
|
38
|
+
export function defineResource() {
|
|
39
|
+
return function define(options) {
|
|
40
|
+
// Workaround: a row read with its relations is `T` with each relation's record added under its name; the
|
|
41
|
+
// options describe `T`, which every such row still is.
|
|
42
|
+
return createResource(assumeType(options));
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function routedSegment(route, fallback) {
|
|
46
|
+
if (isString(route))
|
|
47
|
+
return route;
|
|
48
|
+
if (isRecord(route) && isString(route.segment))
|
|
49
|
+
return route.segment;
|
|
50
|
+
return fallback;
|
|
51
|
+
}
|
|
52
|
+
function routedScreen(route) {
|
|
53
|
+
if (isFunction(route))
|
|
54
|
+
return route;
|
|
55
|
+
if (isRecord(route) && isFunction(route.screen))
|
|
56
|
+
return route.screen;
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
function createResource(options) {
|
|
60
|
+
let cachedProperties;
|
|
61
|
+
const properties = () => {
|
|
62
|
+
cachedProperties ??= options.schema ? ((options.introspect ?? zodIntrospect)(options.schema) ?? []) : [];
|
|
63
|
+
return cachedProperties;
|
|
64
|
+
};
|
|
65
|
+
const idOf = (row) => {
|
|
66
|
+
if (options.id)
|
|
67
|
+
return options.id(row);
|
|
68
|
+
// Workaround: without `id`, a row's id is its `id` property, whose type is the service's `Id`.
|
|
69
|
+
return assumeType(valueAt(row, "id"));
|
|
70
|
+
};
|
|
71
|
+
const firstText = properties().find((property) => property.type === JsonType.string && property.key !== "id");
|
|
72
|
+
const title = (row) => {
|
|
73
|
+
if (options.title)
|
|
74
|
+
return options.title(row);
|
|
75
|
+
const value = firstText ? valueAt(row, firstText.key) : undefined;
|
|
76
|
+
return isText(value) ? value : String(idOf(row));
|
|
77
|
+
};
|
|
78
|
+
const routes = {
|
|
79
|
+
base: segment(options.routes?.base ?? options.key),
|
|
80
|
+
create: segment(routedSegment(options.routes?.create, "new")),
|
|
81
|
+
edit: segment(routedSegment(options.routes?.edit, "edit")),
|
|
82
|
+
};
|
|
83
|
+
const createScreen = routedScreen(options.routes?.create);
|
|
84
|
+
const editScreen = routedScreen(options.routes?.edit);
|
|
85
|
+
const screens = {
|
|
86
|
+
...(options.routes?.list ? { list: options.routes.list } : {}),
|
|
87
|
+
...(createScreen ? { create: createScreen } : {}),
|
|
88
|
+
...(options.routes?.detail ? { detail: options.routes.detail } : {}),
|
|
89
|
+
...(editScreen ? { edit: editScreen } : {}),
|
|
90
|
+
};
|
|
91
|
+
const detailTabs = new Set((options.detail?.tabs ?? []).map((tab) => tab.id));
|
|
92
|
+
const idKey = (id) => String(id);
|
|
93
|
+
const numericIds = properties().find((property) => property.key === "id");
|
|
94
|
+
const parseId = (raw) => {
|
|
95
|
+
const decoded = decodeURIComponent(raw);
|
|
96
|
+
const asNumber = numericIds?.type === JsonType.number || numericIds?.type === JsonType.integer;
|
|
97
|
+
// Workaround: an id read from a URL is text; the schema says whether the service's `Id` is a number.
|
|
98
|
+
return assumeType(asNumber && decoded.trim() !== "" && !Number.isNaN(Number(decoded)) ? Number(decoded) : decoded);
|
|
99
|
+
};
|
|
100
|
+
const auditKeys = new Set(Object.values(options.audit ?? {}).filter(isString));
|
|
101
|
+
const relations = options.relations ?? {};
|
|
102
|
+
const words = resourceWords({ options, properties, relations, auditKeys, numericIds });
|
|
103
|
+
// Workaround: a filter that crossed a boundary (URL, agent, table) is a record the service validates as its `F`.
|
|
104
|
+
const filterOf = (value) => assumeType(isRecord(value) ? value : {});
|
|
105
|
+
const softDelete = options.softDelete ?? options.service.capabilities.restore === true;
|
|
106
|
+
const queryKey = ["app-sdk", options.key];
|
|
107
|
+
const sources = new WeakMap();
|
|
108
|
+
async function listWith(query, context, paths = []) {
|
|
109
|
+
const include = includeOf(relations, paths);
|
|
110
|
+
const page = await options.service.list(include.length > 0 ? { ...query, include } : query, context);
|
|
111
|
+
if (include.length === 0)
|
|
112
|
+
return page;
|
|
113
|
+
return { ...page, items: await resolveRelations(resource, page.items, paths, context) };
|
|
114
|
+
}
|
|
115
|
+
function sourceFor(session, reading) {
|
|
116
|
+
if (reading && includeOf(relations, reading.paths).length > 0) {
|
|
117
|
+
const plain = sourceFor(session);
|
|
118
|
+
return {
|
|
119
|
+
...plain,
|
|
120
|
+
key: [...queryKey, "with", includeOf(relations, reading.paths).toSorted().join(",")],
|
|
121
|
+
fetchPage(request, { signal }) {
|
|
122
|
+
const { [searchKey]: search, ...rest } = isRecord(request.filter) ? request.filter : {};
|
|
123
|
+
return listWith({
|
|
124
|
+
filter: filterOf(rest),
|
|
125
|
+
search: isString(search) ? search : undefined,
|
|
126
|
+
sort: request.sort,
|
|
127
|
+
size: request.size,
|
|
128
|
+
position: request.position,
|
|
129
|
+
}, { ...reading.context, signal, session }, reading.paths);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (session) {
|
|
134
|
+
const known = sources.get(session);
|
|
135
|
+
if (known)
|
|
136
|
+
return known;
|
|
137
|
+
}
|
|
138
|
+
const context = (signal) => ({ signal, session });
|
|
139
|
+
const source = {
|
|
140
|
+
key: queryKey,
|
|
141
|
+
getId: (row) => idKey(idOf(row)),
|
|
142
|
+
capabilities: options.service.capabilities,
|
|
143
|
+
fetchPage(request, { signal }) {
|
|
144
|
+
const { [searchKey]: search, ...rest } = isRecord(request.filter) ? request.filter : {};
|
|
145
|
+
const filter = filterOf(rest);
|
|
146
|
+
return options.service.list({
|
|
147
|
+
filter,
|
|
148
|
+
search: isString(search) ? search : undefined,
|
|
149
|
+
sort: request.sort,
|
|
150
|
+
size: request.size,
|
|
151
|
+
position: request.position,
|
|
152
|
+
}, context(signal));
|
|
153
|
+
},
|
|
154
|
+
...(options.service.count
|
|
155
|
+
? {
|
|
156
|
+
fetchTotal(filter, { signal }) {
|
|
157
|
+
const { [searchKey]: _search, ...rest } = isRecord(filter) ? filter : {};
|
|
158
|
+
return options.service.count(filterOf(rest), context(signal));
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
: {}),
|
|
162
|
+
};
|
|
163
|
+
if (session)
|
|
164
|
+
sources.set(session, source);
|
|
165
|
+
return source;
|
|
166
|
+
}
|
|
167
|
+
const base = words(resourceTexts(options.key, undefined));
|
|
168
|
+
const checkIn = (locale) => (action, row, session, notAllowed = "") => {
|
|
169
|
+
if (options.service.capabilities.readOnly === true && writes.has(action))
|
|
170
|
+
return { allowed: false, reason: "this resource is read-only" };
|
|
171
|
+
if (action === ResourceAction.restore && !softDelete)
|
|
172
|
+
return { allowed: false, reason: "this resource has no soft delete" };
|
|
173
|
+
return firstRefusal(toAllowed(refusalIn(options.can?.(action, row), locale), notAllowed), toAllowed(session?.can(action, { resource: options.key, row }), notAllowed));
|
|
174
|
+
};
|
|
175
|
+
const localized = new WeakMap();
|
|
176
|
+
const origins = new WeakMap();
|
|
177
|
+
function inLocale(locale) {
|
|
178
|
+
const from = origins.get(this) ?? this;
|
|
179
|
+
if (!locale)
|
|
180
|
+
return from;
|
|
181
|
+
const byLocale = localized.get(from) ?? new WeakMap();
|
|
182
|
+
localized.set(from, byLocale);
|
|
183
|
+
const known = byLocale.get(locale);
|
|
184
|
+
if (known)
|
|
185
|
+
return known;
|
|
186
|
+
const made = {
|
|
187
|
+
...from,
|
|
188
|
+
...words(resourceTexts(options.key, locale)),
|
|
189
|
+
check: checkIn(locale),
|
|
190
|
+
locale,
|
|
191
|
+
};
|
|
192
|
+
origins.set(made, from);
|
|
193
|
+
byLocale.set(locale, made);
|
|
194
|
+
return made;
|
|
195
|
+
}
|
|
196
|
+
const resource = {
|
|
197
|
+
key: options.key,
|
|
198
|
+
options,
|
|
199
|
+
locale: undefined,
|
|
200
|
+
in: inLocale,
|
|
201
|
+
gender: base.gender,
|
|
202
|
+
service: options.service,
|
|
203
|
+
schema: options.schema,
|
|
204
|
+
icon: options.icon,
|
|
205
|
+
staleTime: options.refresh?.staleTime ?? options.staleTime ?? 30_000,
|
|
206
|
+
refresh: {
|
|
207
|
+
onFocus: options.refresh?.onFocus ?? false,
|
|
208
|
+
onReconnect: options.refresh?.onReconnect ?? true,
|
|
209
|
+
interval: options.refresh?.interval ?? false,
|
|
210
|
+
staleTime: options.refresh?.staleTime ?? options.staleTime ?? 30_000,
|
|
211
|
+
newRows: options.refresh?.newRows ?? NewRows.hold,
|
|
212
|
+
},
|
|
213
|
+
exportLimit: options.exportLimit ?? 10_000,
|
|
214
|
+
shortcuts: options.shortcuts ?? {},
|
|
215
|
+
searchable: options.searchable ?? true,
|
|
216
|
+
softDelete,
|
|
217
|
+
defaultSort: options.defaultSort ?? [],
|
|
218
|
+
queryKey,
|
|
219
|
+
label: base.label,
|
|
220
|
+
createLabel: base.createLabel,
|
|
221
|
+
title,
|
|
222
|
+
titleField: firstText?.key,
|
|
223
|
+
// Workaround: the title field is one of `T`'s text properties, so a text in it is part of a `T`.
|
|
224
|
+
inputFromText: (text) => (firstText ? assumeType({ [firstText.key]: text }) : undefined),
|
|
225
|
+
description: (row) => options.description?.(row),
|
|
226
|
+
idOf,
|
|
227
|
+
idKey,
|
|
228
|
+
parseId,
|
|
229
|
+
source: sourceFor(undefined),
|
|
230
|
+
sourceFor,
|
|
231
|
+
listWith,
|
|
232
|
+
columns: base.columns,
|
|
233
|
+
fieldLabel: base.fieldLabel,
|
|
234
|
+
fields: base.fields,
|
|
235
|
+
filters: base.filters,
|
|
236
|
+
properties,
|
|
237
|
+
filterOf,
|
|
238
|
+
// Workaround: as above, the values passed the resource's schema before they reach here.
|
|
239
|
+
inputOf: (value) => assumeType(isRecord(value) ? value : {}),
|
|
240
|
+
check: checkIn(undefined),
|
|
241
|
+
routes,
|
|
242
|
+
screens,
|
|
243
|
+
detail: base.detail,
|
|
244
|
+
paths: {
|
|
245
|
+
list: () => `/${routes.base}`,
|
|
246
|
+
create: () => `/${routes.base}/${routes.create}`,
|
|
247
|
+
detail: (id) => `/${routes.base}/${encodeURIComponent(idKey(id))}`,
|
|
248
|
+
edit: (id) => `/${routes.base}/${encodeURIComponent(idKey(id))}/${routes.edit}`,
|
|
249
|
+
tab: (id, relation) => `/${routes.base}/${encodeURIComponent(idKey(id))}/${encodeURIComponent(relation)}`,
|
|
250
|
+
},
|
|
251
|
+
relations,
|
|
252
|
+
match(pathname) {
|
|
253
|
+
const parts = segment(pathname).split("/").filter(Boolean);
|
|
254
|
+
const baseParts = routes.base.split("/").filter(Boolean);
|
|
255
|
+
if (baseParts.some((part, index) => parts[index] !== part))
|
|
256
|
+
return undefined;
|
|
257
|
+
const [first, second, ...extra] = parts.slice(baseParts.length);
|
|
258
|
+
if (extra.length > 0)
|
|
259
|
+
return undefined;
|
|
260
|
+
if (first === undefined)
|
|
261
|
+
return { screen: ResourceScreen.list };
|
|
262
|
+
if (first === routes.create && second === undefined)
|
|
263
|
+
return { screen: ResourceScreen.create };
|
|
264
|
+
if (second === undefined)
|
|
265
|
+
return { screen: ResourceScreen.detail, id: decodeURIComponent(first) };
|
|
266
|
+
if (second === routes.edit)
|
|
267
|
+
return { screen: ResourceScreen.edit, id: decodeURIComponent(first) };
|
|
268
|
+
const tab = decodeURIComponent(second);
|
|
269
|
+
if (relations[tab]?.kind === RelationKind.hasMany || detailTabs.has(tab))
|
|
270
|
+
return { screen: ResourceScreen.detail, id: decodeURIComponent(first), tab };
|
|
271
|
+
return undefined;
|
|
272
|
+
},
|
|
273
|
+
actions: base.actions,
|
|
274
|
+
audit: options.audit,
|
|
275
|
+
};
|
|
276
|
+
if (Object.keys(relations).length > 0)
|
|
277
|
+
defined.add(resource);
|
|
278
|
+
registerResource(resource);
|
|
279
|
+
return resource;
|
|
280
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { JsonType } from "../core/constants.js";
|
|
2
|
+
export type StandardSchema<Input = unknown, Output = Input> = {
|
|
3
|
+
readonly "~standard": {
|
|
4
|
+
readonly version: 1;
|
|
5
|
+
readonly vendor: string;
|
|
6
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
7
|
+
readonly types?: {
|
|
8
|
+
readonly input: Input;
|
|
9
|
+
readonly output: Output;
|
|
10
|
+
} | undefined;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type StandardIssue = {
|
|
14
|
+
readonly message: string;
|
|
15
|
+
readonly path?: readonly (PropertyKey | {
|
|
16
|
+
readonly key: PropertyKey;
|
|
17
|
+
})[] | undefined;
|
|
18
|
+
};
|
|
19
|
+
export type StandardResult<Output> = {
|
|
20
|
+
readonly value: Output;
|
|
21
|
+
readonly issues?: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
readonly issues: readonly StandardIssue[];
|
|
24
|
+
};
|
|
25
|
+
export type InferOutput<Schema> = Schema extends StandardSchema<unknown, infer Output> ? Output : never;
|
|
26
|
+
export type SchemaProperty = {
|
|
27
|
+
key: string;
|
|
28
|
+
type?: JsonType;
|
|
29
|
+
format?: string;
|
|
30
|
+
enum?: readonly string[];
|
|
31
|
+
title?: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
required: boolean;
|
|
34
|
+
readOnly: boolean;
|
|
35
|
+
minLength?: number;
|
|
36
|
+
maxLength?: number;
|
|
37
|
+
minimum?: number;
|
|
38
|
+
maximum?: number;
|
|
39
|
+
pattern?: string;
|
|
40
|
+
items?: SchemaProperty;
|
|
41
|
+
properties?: readonly SchemaProperty[];
|
|
42
|
+
};
|
|
43
|
+
export type Introspect = (schema: unknown) => readonly SchemaProperty[] | undefined;
|
|
44
|
+
export declare function jsonSchemaProperties(jsonSchema: unknown): SchemaProperty[] | undefined;
|
|
45
|
+
/** Whether a Zod schema (or one with Standard JSON Schema) is a list: `z.array(line)`. */
|
|
46
|
+
export declare function isListSchema(schema: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Reads a Zod 4 schema through its own JSON Schema (`schema.toJSONSchema()`), or any schema that implements Standard
|
|
49
|
+
* JSON Schema. A schema it can't read still validates; only the deduced columns and fields need declaring by hand.
|
|
50
|
+
*/
|
|
51
|
+
export declare const zodIntrospect: Introspect;
|
|
52
|
+
export declare function isStandardSchema(value: unknown): value is StandardSchema;
|
|
53
|
+
export declare function issuePath(issue: StandardIssue): string;
|
|
54
|
+
export declare function validate<Output>(schema: StandardSchema<unknown, Output>, value: unknown): Promise<StandardResult<Output>>;
|
|
55
|
+
export declare function isValidationFailure<Output>(result: StandardResult<Output>): result is {
|
|
56
|
+
readonly issues: readonly StandardIssue[];
|
|
57
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { JsonType } from "../core/constants.js";
|
|
2
|
+
import { isFunction, isNumber, isRecord, isString, isText, valuesOf } from "../core/guards.js";
|
|
3
|
+
const jsonTypes = valuesOf(JsonType);
|
|
4
|
+
function typeOf(node) {
|
|
5
|
+
const raw = node.type;
|
|
6
|
+
const list = Array.isArray(raw) ? raw : [raw];
|
|
7
|
+
return jsonTypes.find((type) => type !== JsonType.null && list.includes(type));
|
|
8
|
+
}
|
|
9
|
+
function unwrap(node) {
|
|
10
|
+
if (!isRecord(node))
|
|
11
|
+
return undefined;
|
|
12
|
+
for (const key of ["anyOf", "oneOf", "allOf"]) {
|
|
13
|
+
const list = node[key];
|
|
14
|
+
if (!Array.isArray(list))
|
|
15
|
+
continue;
|
|
16
|
+
const meaningful = list.filter((entry) => !(isRecord(entry) && entry.type === JsonType.null));
|
|
17
|
+
if (meaningful.length === 1)
|
|
18
|
+
return { ...unwrap(meaningful[0]), ...withoutCombinators(node) };
|
|
19
|
+
}
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
function withoutCombinators(node) {
|
|
23
|
+
const { anyOf: _anyOf, oneOf: _oneOf, allOf: _allOf, ...rest } = node;
|
|
24
|
+
return rest;
|
|
25
|
+
}
|
|
26
|
+
function propertyOf(key, raw, required) {
|
|
27
|
+
const node = unwrap(raw) ?? {};
|
|
28
|
+
const type = typeOf(node);
|
|
29
|
+
const enumValues = Array.isArray(node.enum) ? node.enum.filter(isString) : undefined;
|
|
30
|
+
const items = node.items === undefined ? undefined : propertyOf("", node.items, true);
|
|
31
|
+
const properties = type === JsonType.object ? propertiesOf(node) : undefined;
|
|
32
|
+
return {
|
|
33
|
+
key,
|
|
34
|
+
required,
|
|
35
|
+
readOnly: node.readOnly === true,
|
|
36
|
+
...(type ? { type } : {}),
|
|
37
|
+
...(isText(node.format) ? { format: node.format } : {}),
|
|
38
|
+
...(enumValues && enumValues.length > 0 ? { enum: enumValues } : {}),
|
|
39
|
+
...(isText(node.title) ? { title: node.title } : {}),
|
|
40
|
+
...(isText(node.description) ? { description: node.description } : {}),
|
|
41
|
+
...(isNumber(node.minLength) ? { minLength: node.minLength } : {}),
|
|
42
|
+
...(isNumber(node.maxLength) ? { maxLength: node.maxLength } : {}),
|
|
43
|
+
...(isNumber(node.minimum) ? { minimum: node.minimum } : {}),
|
|
44
|
+
...(isNumber(node.maximum) ? { maximum: node.maximum } : {}),
|
|
45
|
+
...(isText(node.pattern) ? { pattern: node.pattern } : {}),
|
|
46
|
+
...(items ? { items } : {}),
|
|
47
|
+
...(properties ? { properties } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function propertiesOf(node) {
|
|
51
|
+
if (!isRecord(node.properties))
|
|
52
|
+
return undefined;
|
|
53
|
+
const required = new Set(Array.isArray(node.required) ? node.required.filter(isString) : []);
|
|
54
|
+
return Object.entries(node.properties).map(([key, value]) => propertyOf(key, value, required.has(key)));
|
|
55
|
+
}
|
|
56
|
+
export function jsonSchemaProperties(jsonSchema) {
|
|
57
|
+
const node = unwrap(jsonSchema);
|
|
58
|
+
if (node && typeOf(node) === JsonType.array) {
|
|
59
|
+
const items = unwrap(node.items);
|
|
60
|
+
return items ? propertiesOf(items) : undefined;
|
|
61
|
+
}
|
|
62
|
+
return node ? propertiesOf(node) : undefined;
|
|
63
|
+
}
|
|
64
|
+
/** Whether a Zod schema (or one with Standard JSON Schema) is a list: `z.array(line)`. */
|
|
65
|
+
export function isListSchema(schema) {
|
|
66
|
+
try {
|
|
67
|
+
const node = unwrap(zodJsonSchema(schema));
|
|
68
|
+
return node !== undefined && typeOf(node) === JsonType.array;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function zodJsonSchema(schema) {
|
|
75
|
+
if (!isRecord(schema))
|
|
76
|
+
return undefined;
|
|
77
|
+
const method = schema.toJSONSchema;
|
|
78
|
+
if (isFunction(method)) {
|
|
79
|
+
const call = Reflect.apply(method, schema, [{ io: "input", unrepresentable: "any" }]);
|
|
80
|
+
return call;
|
|
81
|
+
}
|
|
82
|
+
const standard = schema["~standard"];
|
|
83
|
+
if (isRecord(standard) && isRecord(standard.jsonSchema) && isFunction(standard.jsonSchema.input)) {
|
|
84
|
+
const call = Reflect.apply(standard.jsonSchema.input, standard.jsonSchema, [{ target: "draft-2020-12" }]);
|
|
85
|
+
return call;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Reads a Zod 4 schema through its own JSON Schema (`schema.toJSONSchema()`), or any schema that implements Standard
|
|
91
|
+
* JSON Schema. A schema it can't read still validates; only the deduced columns and fields need declaring by hand.
|
|
92
|
+
*/
|
|
93
|
+
export const zodIntrospect = (schema) => {
|
|
94
|
+
try {
|
|
95
|
+
return jsonSchemaProperties(zodJsonSchema(schema));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export function isStandardSchema(value) {
|
|
102
|
+
return isRecord(value) && isRecord(value["~standard"]) && isFunction(value["~standard"].validate);
|
|
103
|
+
}
|
|
104
|
+
function pathKey(segment) {
|
|
105
|
+
return String(isRecord(segment) ? segment.key : segment);
|
|
106
|
+
}
|
|
107
|
+
export function issuePath(issue) {
|
|
108
|
+
return (issue.path ?? []).map(pathKey).join(".");
|
|
109
|
+
}
|
|
110
|
+
export async function validate(schema, value) {
|
|
111
|
+
return schema["~standard"].validate(value);
|
|
112
|
+
}
|
|
113
|
+
export function isValidationFailure(result) {
|
|
114
|
+
return result.issues !== undefined;
|
|
115
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CallContext, PageSourceLike, ResourceService, Target, Version } from "../core/contracts.js";
|
|
2
|
+
export type PageSourceCrud<T, Id, F> = {
|
|
3
|
+
get(id: Id, context: CallContext): Promise<T>;
|
|
4
|
+
create?(input: Partial<T>, context: CallContext): Promise<T>;
|
|
5
|
+
update?(id: Id, patch: Partial<T>, context: CallContext & {
|
|
6
|
+
version?: Version | undefined;
|
|
7
|
+
}): Promise<T>;
|
|
8
|
+
remove?(target: Target<Id, F>, context: CallContext): Promise<void>;
|
|
9
|
+
restore?(target: Target<Id, F>, context: CallContext): Promise<void>;
|
|
10
|
+
getMany?(ids: readonly Id[], context: CallContext): Promise<T[]>;
|
|
11
|
+
versionOf?(row: T): Version | undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare function fromPageSource<T, Id = string, F = Record<string, unknown>>(source: PageSourceLike<T, F & {
|
|
14
|
+
q?: string | undefined;
|
|
15
|
+
}>, crud: PageSourceCrud<T, Id, F>): ResourceService<T, Id, F>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ServiceErrorKind } from "../core/constants.js";
|
|
2
|
+
import { ServiceFailure, serviceError } from "../core/errors.js";
|
|
3
|
+
import { searchKey } from "../resource/resource.js";
|
|
4
|
+
function unsupported(operation) {
|
|
5
|
+
throw new ServiceFailure(serviceError(ServiceErrorKind.forbidden, `This service has no ${operation}.`));
|
|
6
|
+
}
|
|
7
|
+
export function fromPageSource(source, crud) {
|
|
8
|
+
return {
|
|
9
|
+
capabilities: { ...source.capabilities, ...(crud.restore ? { restore: true } : {}) },
|
|
10
|
+
list(query, { signal }) {
|
|
11
|
+
const filter = { ...query.filter, [searchKey]: query.search };
|
|
12
|
+
return source.fetchPage({ filter, sort: query.sort, size: query.size, position: query.position }, { signal });
|
|
13
|
+
},
|
|
14
|
+
...(source.fetchTotal
|
|
15
|
+
? {
|
|
16
|
+
count: (filter, { signal }) => source.fetchTotal({ ...filter, [searchKey]: undefined }, { signal }),
|
|
17
|
+
}
|
|
18
|
+
: {}),
|
|
19
|
+
get: (id, context) => crud.get(id, context),
|
|
20
|
+
create: (input, context) => (crud.create ? crud.create(input, context) : unsupported("create")),
|
|
21
|
+
update: (id, patch, context) => (crud.update ? crud.update(id, patch, context) : unsupported("update")),
|
|
22
|
+
remove: (target, context) => (crud.remove ? crud.remove(target, context) : unsupported("remove")),
|
|
23
|
+
...(crud.restore ? { restore: crud.restore } : {}),
|
|
24
|
+
...(crud.getMany ? { getMany: crud.getMany } : {}),
|
|
25
|
+
...(crud.versionOf ? { versionOf: crud.versionOf } : {}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Page, type Query, type SortSpec } from "../core/contracts.js";
|
|
2
|
+
export declare function valueAt(row: unknown, path: string): unknown;
|
|
3
|
+
export declare function sortRows<T>(rows: readonly T[], sort: readonly SortSpec[]): T[];
|
|
4
|
+
export declare function isActive(value: unknown): boolean;
|
|
5
|
+
export declare function matchesValue(actual: unknown, expected: unknown): boolean;
|
|
6
|
+
export declare function matchesFilter(row: unknown, filter: unknown): boolean;
|
|
7
|
+
export declare function matchesSearch(row: unknown, search: string | undefined): boolean;
|
|
8
|
+
export declare function applyQuery<T>(rows: readonly T[], query: Query<unknown>): Page<T>;
|