@eskais/app-sdk 0.1.0 → 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/README.md +17 -11
- package/dist/action/action.d.ts +69 -10
- package/dist/action/action.js +147 -29
- package/dist/action/bulk.d.ts +24 -0
- package/dist/action/bulk.js +24 -0
- package/dist/core/constants.d.ts +57 -0
- package/dist/core/constants.js +31 -0
- package/dist/core/contracts.d.ts +7 -0
- package/dist/core/data-labels.d.ts +14 -0
- package/dist/core/data-labels.js +40 -0
- package/dist/core/errors.d.ts +11 -5
- package/dist/core/errors.js +52 -9
- package/dist/dialogs/dialogs.d.ts +75 -0
- package/dist/dialogs/dialogs.js +66 -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/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/import.d.ts +4 -2
- package/dist/import/import.js +7 -4
- package/dist/index.d.ts +18 -3
- package/dist/index.js +17 -2
- package/dist/navigation/nav.d.ts +14 -5
- package/dist/navigation/nav.js +37 -14
- package/dist/next/server/index.d.ts +2 -1
- package/dist/next/server/index.js +2 -1
- package/dist/next/server/page.d.ts +8 -2
- package/dist/next/server/page.js +14 -7
- package/dist/next/server/session.d.ts +13 -0
- package/dist/next/server/session.js +13 -0
- package/dist/permissions/permissions.d.ts +134 -0
- package/dist/permissions/permissions.js +121 -0
- package/dist/react/data-layer.d.ts +42 -5
- package/dist/react/data-layer.js +49 -4
- 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 +30 -5
- package/dist/react/hooks/use-action.js +85 -27
- 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-nav.js +3 -2
- 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 +28 -5
- package/dist/react/hooks/use-resource.js +63 -26
- package/dist/react/index.d.ts +11 -4
- package/dist/react/index.js +11 -4
- package/dist/react/keys.d.ts +9 -0
- package/dist/react/keys.js +4 -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/resource-relations.d.ts +6 -3
- package/dist/resource/resource-relations.js +26 -13
- package/dist/resource/resource-schema.d.ts +1 -1
- package/dist/resource/resource-schema.js +1 -1
- package/dist/resource/resource-texts.d.ts +18 -0
- package/dist/resource/resource-texts.js +13 -0
- package/dist/resource/resource-types.d.ts +169 -30
- package/dist/resource/resource-words.d.ts +25 -0
- package/dist/resource/resource-words.js +221 -0
- package/dist/resource/resource.d.ts +6 -2
- package/dist/resource/resource.js +99 -173
- 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 +4 -1
- package/dist/services/rest-service.js +2 -0
- package/dist/view/view-types.d.ts +4 -2
- package/locales/en.json +193 -0
- package/locales/es.json +193 -0
- package/package.json +5 -2
|
@@ -1,16 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JsonType, NewRows, RelationKind, ResourceAction, ResourceScreen } from "../core/constants.js";
|
|
2
2
|
import { assumeType } from "../core/assume.js";
|
|
3
3
|
import { isFunction, isRecord, isString, isText } from "../core/guards.js";
|
|
4
4
|
import { valueAt } from "../services/memory-query.js";
|
|
5
5
|
import { firstRefusal, toAllowed } from "../core/refusal.js";
|
|
6
|
-
import {
|
|
7
|
-
import { defined, dependentsOf, inheritedColumn, relationTools, valueLabelsIn } from "./resource-relations.js";
|
|
6
|
+
import { defined, dependentsOf } from "./resource-relations.js";
|
|
8
7
|
import { includeOf, registerResource, resolveRelations, } from "./relations.js";
|
|
8
|
+
import { resourceTexts } from "./resource-texts.js";
|
|
9
|
+
import { resourceWords } from "./resource-words.js";
|
|
9
10
|
import { zodIntrospect } from "./schema.js";
|
|
11
|
+
import { refusalIn } from "../i18n/locale.js";
|
|
10
12
|
export { dependentsOf };
|
|
11
13
|
export const searchKey = "q";
|
|
12
|
-
const
|
|
13
|
-
|
|
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
|
+
}
|
|
14
31
|
function segment(value) {
|
|
15
32
|
return value.replace(/^\/+|\/+$/gu, "");
|
|
16
33
|
}
|
|
@@ -25,13 +42,26 @@ export function defineResource() {
|
|
|
25
42
|
return createResource(assumeType(options));
|
|
26
43
|
};
|
|
27
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
|
+
}
|
|
28
59
|
function createResource(options) {
|
|
29
60
|
let cachedProperties;
|
|
30
61
|
const properties = () => {
|
|
31
62
|
cachedProperties ??= options.schema ? ((options.introspect ?? zodIntrospect)(options.schema) ?? []) : [];
|
|
32
63
|
return cachedProperties;
|
|
33
64
|
};
|
|
34
|
-
const label = (count = 1) => isFunction(options.label) ? options.label(count) : count === 1 ? options.label.one : options.label.other;
|
|
35
65
|
const idOf = (row) => {
|
|
36
66
|
if (options.id)
|
|
37
67
|
return options.id(row);
|
|
@@ -47,9 +77,18 @@ function createResource(options) {
|
|
|
47
77
|
};
|
|
48
78
|
const routes = {
|
|
49
79
|
base: segment(options.routes?.base ?? options.key),
|
|
50
|
-
create: segment(options.routes?.create
|
|
51
|
-
edit: segment(options.routes?.edit
|
|
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 } : {}),
|
|
52
90
|
};
|
|
91
|
+
const detailTabs = new Set((options.detail?.tabs ?? []).map((tab) => tab.id));
|
|
53
92
|
const idKey = (id) => String(id);
|
|
54
93
|
const numericIds = properties().find((property) => property.key === "id");
|
|
55
94
|
const parseId = (raw) => {
|
|
@@ -60,158 +99,7 @@ function createResource(options) {
|
|
|
60
99
|
};
|
|
61
100
|
const auditKeys = new Set(Object.values(options.audit ?? {}).filter(isString));
|
|
62
101
|
const relations = options.relations ?? {};
|
|
63
|
-
const {
|
|
64
|
-
relations,
|
|
65
|
-
properties,
|
|
66
|
-
introspect: options.introspect,
|
|
67
|
-
});
|
|
68
|
-
function declaredKind(key) {
|
|
69
|
-
const declared = (options.fields ?? []).flatMap((entry) => (isGroup(entry) ? entry.fields : [entry]));
|
|
70
|
-
const field = declared.find((entry) => entry.key === key);
|
|
71
|
-
if (field?.as !== undefined)
|
|
72
|
-
return field.as;
|
|
73
|
-
const property = propertyAt(properties(), key);
|
|
74
|
-
return property ? fieldKindOf(property) : undefined;
|
|
75
|
-
}
|
|
76
|
-
const valueLabelsOf = (key) => valueLabelsIn(options, key);
|
|
77
|
-
function columns() {
|
|
78
|
-
const declared = options.columns ??
|
|
79
|
-
properties()
|
|
80
|
-
.filter((property) => property.type !== JsonType.object &&
|
|
81
|
-
!(property.type === JsonType.array && property.items?.type === JsonType.object))
|
|
82
|
-
.filter((property) => property.key !== "id" || !numericIds)
|
|
83
|
-
.map((property) => ({ key: property.key, ...(auditKeys.has(property.key) ? { hidden: true } : {}) }));
|
|
84
|
-
return declared.map((column) => {
|
|
85
|
-
const property = propertyAt(properties(), column.key);
|
|
86
|
-
const sortField = column.sortable === true ? column.key : isString(column.sortable) ? column.sortable : undefined;
|
|
87
|
-
const enumOptions = optionsOfEnum(property, column.valueLabels ?? valueLabelsOf(column.key));
|
|
88
|
-
const field = declaredKind(column.key);
|
|
89
|
-
const reached = relationAt(column.key);
|
|
90
|
-
const related = reached?.relation.target();
|
|
91
|
-
const relatedLabel = reached
|
|
92
|
-
? reached.rest === undefined
|
|
93
|
-
? related?.label(reached.relation.kind === RelationKind.belongsTo ? 1 : 2)
|
|
94
|
-
: `${related?.label(1) ?? reached.relation.name} · ${propertyAt(related?.properties() ?? [], reached.rest)?.title ?? reached.rest}`
|
|
95
|
-
: undefined;
|
|
96
|
-
const sortable = reached === undefined || (reached.rest !== undefined && options.service.capabilities.relationSort === true);
|
|
97
|
-
const inherited = inheritedColumn(related, reached?.rest);
|
|
98
|
-
const tones = column.tones ?? inherited.tones;
|
|
99
|
-
const inheritedKind = inherited.kind(tones !== undefined);
|
|
100
|
-
const align = column.align ?? inherited.align;
|
|
101
|
-
const relatedOptions = inherited.options;
|
|
102
|
-
return {
|
|
103
|
-
key: column.key,
|
|
104
|
-
label: column.label ?? relatedLabel ?? property?.title ?? column.key,
|
|
105
|
-
kind: column.as ??
|
|
106
|
-
(reached
|
|
107
|
-
? (inheritedKind ?? ColumnKind.text)
|
|
108
|
-
: property
|
|
109
|
-
? columnKindOf(property, column.tones !== undefined)
|
|
110
|
-
: ColumnKind.text),
|
|
111
|
-
hidden: column.hidden ?? false,
|
|
112
|
-
...(tones ? { tones } : {}),
|
|
113
|
-
...(relatedOptions && !enumOptions ? { options: relatedOptions } : {}),
|
|
114
|
-
...(column.cell ? { cell: column.cell } : {}),
|
|
115
|
-
...(sortField === undefined || !sortable ? {} : { sortField }),
|
|
116
|
-
...(reached
|
|
117
|
-
? { relation: reached.relation, ...(reached.rest === undefined ? {} : { relationPath: reached.rest }) }
|
|
118
|
-
: {}),
|
|
119
|
-
...(column.width === undefined ? {} : { width: column.width }),
|
|
120
|
-
...(align === undefined ? {} : { align }),
|
|
121
|
-
...(column.mobile === undefined ? {} : { mobile: column.mobile }),
|
|
122
|
-
...(enumOptions ? { options: enumOptions } : {}),
|
|
123
|
-
...(field === undefined ? {} : { field }),
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
function resolveField(field, group) {
|
|
128
|
-
if (field.as === undefined || field.as === FieldKind.resource || field.as === FieldKind.rows) {
|
|
129
|
-
const related = relationField(field, group);
|
|
130
|
-
if (related)
|
|
131
|
-
return related;
|
|
132
|
-
}
|
|
133
|
-
const property = propertyAt(properties(), field.key);
|
|
134
|
-
const readOnly = field.readOnly;
|
|
135
|
-
const hidden = field.hidden;
|
|
136
|
-
const fieldOptions = field.options ?? optionsOfEnum(property, field.valueLabels ?? valueLabelsOf(field.key));
|
|
137
|
-
const description = field.description ?? property?.description;
|
|
138
|
-
return {
|
|
139
|
-
key: field.key,
|
|
140
|
-
label: field.label ?? property?.title ?? field.key,
|
|
141
|
-
kind: field.as ?? (field.resource ? FieldKind.resource : fieldKindOf(property)),
|
|
142
|
-
required: property?.required ?? false,
|
|
143
|
-
multiple: field.multiple ?? property?.type === JsonType.array,
|
|
144
|
-
readOnly: isFunction(readOnly) ? readOnly : readOnly === true || property?.readOnly === true ? always : never,
|
|
145
|
-
hidden: isFunction(hidden) ? hidden : hidden === true ? always : never,
|
|
146
|
-
colSpan: field.colSpan ?? 1,
|
|
147
|
-
...(description === undefined ? {} : { description }),
|
|
148
|
-
...(field.placeholder === undefined ? {} : { placeholder: field.placeholder }),
|
|
149
|
-
...(field.resource ? { resource: field.resource } : {}),
|
|
150
|
-
...(field.create === undefined ? {} : { create: field.create }),
|
|
151
|
-
...(fieldOptions ? { options: fieldOptions } : {}),
|
|
152
|
-
...(field.render ? { render: field.render } : {}),
|
|
153
|
-
...(property?.minimum === undefined ? {} : { min: property.minimum }),
|
|
154
|
-
...(property?.maximum === undefined ? {} : { max: property.maximum }),
|
|
155
|
-
...(property?.type === undefined ? {} : { valueType: property.type }),
|
|
156
|
-
...(group
|
|
157
|
-
? { group: group.group, ...(group.description === undefined ? {} : { groupDescription: group.description }) }
|
|
158
|
-
: {}),
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
function fields(stage) {
|
|
162
|
-
const declared = options.fields ??
|
|
163
|
-
properties()
|
|
164
|
-
.filter((property) => property.key !== "id" &&
|
|
165
|
-
!auditKeys.has(property.key) &&
|
|
166
|
-
property.type !== JsonType.object &&
|
|
167
|
-
!(property.type === JsonType.array && property.items?.type === JsonType.object))
|
|
168
|
-
.map((property) => ({ key: property.key }))
|
|
169
|
-
.concat(Object.entries(relations).flatMap(([name, relation]) => relation.kind === RelationKind.embeds ||
|
|
170
|
-
(relation.kind === RelationKind.hasMany && relation.mode === RowsMode.inline)
|
|
171
|
-
? [{ key: name }]
|
|
172
|
-
: []));
|
|
173
|
-
const flat = declared.flatMap((entry) => isGroup(entry) ? entry.fields.map((field) => resolveField(field, entry)) : [resolveField(entry)]);
|
|
174
|
-
const only = new Map(declared.flatMap((entry) => (isGroup(entry) ? entry.fields : [entry])).map((field) => [field.key, field.only]));
|
|
175
|
-
return flat.filter((field) => {
|
|
176
|
-
const wanted = only.get(field.key);
|
|
177
|
-
if (wanted !== undefined && wanted !== stage)
|
|
178
|
-
return false;
|
|
179
|
-
return !(stage === FormStage.create && propertyAt(properties(), field.key)?.readOnly === true);
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
function filters() {
|
|
183
|
-
return (options.filters ?? []).map((filter) => {
|
|
184
|
-
const reached = relationAt(filter.key);
|
|
185
|
-
if (reached && reached.rest === undefined && reached.relation.kind !== RelationKind.hasMany) {
|
|
186
|
-
const target = reached.relation.target();
|
|
187
|
-
return {
|
|
188
|
-
key: reached.relation.key,
|
|
189
|
-
label: filter.label ?? target.label(1),
|
|
190
|
-
kind: FilterKind.resource,
|
|
191
|
-
resource: reached.relation.target,
|
|
192
|
-
...(filter.default === undefined ? {} : { default: filter.default }),
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
const property = propertyAt(properties(), filter.key);
|
|
196
|
-
const kind = filter.as ??
|
|
197
|
-
(filter.resource
|
|
198
|
-
? FilterKind.resource
|
|
199
|
-
: property?.enum
|
|
200
|
-
? FilterKind.select
|
|
201
|
-
: property?.type === JsonType.boolean
|
|
202
|
-
? FilterKind.boolean
|
|
203
|
-
: FilterKind.search);
|
|
204
|
-
const filterOptions = filter.options ?? optionsOfEnum(property, valueLabelsOf(filter.key));
|
|
205
|
-
return {
|
|
206
|
-
key: filter.key,
|
|
207
|
-
label: filter.label ?? property?.title ?? filter.key,
|
|
208
|
-
kind,
|
|
209
|
-
...(filterOptions ? { options: filterOptions } : {}),
|
|
210
|
-
...(filter.resource ? { resource: filter.resource } : {}),
|
|
211
|
-
...(filter.default === undefined ? {} : { default: filter.default }),
|
|
212
|
-
};
|
|
213
|
-
});
|
|
214
|
-
}
|
|
102
|
+
const words = resourceWords({ options, properties, relations, auditKeys, numericIds });
|
|
215
103
|
// Workaround: a filter that crossed a boundary (URL, agent, table) is a record the service validates as its `F`.
|
|
216
104
|
const filterOf = (value) => assumeType(isRecord(value) ? value : {});
|
|
217
105
|
const softDelete = options.softDelete ?? options.service.capabilities.restore === true;
|
|
@@ -276,21 +164,60 @@ function createResource(options) {
|
|
|
276
164
|
sources.set(session, source);
|
|
277
165
|
return source;
|
|
278
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
|
+
}
|
|
279
196
|
const resource = {
|
|
280
197
|
key: options.key,
|
|
281
198
|
options,
|
|
199
|
+
locale: undefined,
|
|
200
|
+
in: inLocale,
|
|
201
|
+
gender: base.gender,
|
|
282
202
|
service: options.service,
|
|
283
203
|
schema: options.schema,
|
|
284
204
|
icon: options.icon,
|
|
285
|
-
staleTime: options.staleTime ?? 30_000,
|
|
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
|
+
},
|
|
286
213
|
exportLimit: options.exportLimit ?? 10_000,
|
|
287
214
|
shortcuts: options.shortcuts ?? {},
|
|
288
215
|
searchable: options.searchable ?? true,
|
|
289
216
|
softDelete,
|
|
290
217
|
defaultSort: options.defaultSort ?? [],
|
|
291
218
|
queryKey,
|
|
292
|
-
label,
|
|
293
|
-
createLabel:
|
|
219
|
+
label: base.label,
|
|
220
|
+
createLabel: base.createLabel,
|
|
294
221
|
title,
|
|
295
222
|
titleField: firstText?.key,
|
|
296
223
|
// Workaround: the title field is one of `T`'s text properties, so a text in it is part of a `T`.
|
|
@@ -302,19 +229,18 @@ function createResource(options) {
|
|
|
302
229
|
source: sourceFor(undefined),
|
|
303
230
|
sourceFor,
|
|
304
231
|
listWith,
|
|
305
|
-
columns,
|
|
306
|
-
|
|
307
|
-
|
|
232
|
+
columns: base.columns,
|
|
233
|
+
fieldLabel: base.fieldLabel,
|
|
234
|
+
fields: base.fields,
|
|
235
|
+
filters: base.filters,
|
|
308
236
|
properties,
|
|
309
237
|
filterOf,
|
|
310
238
|
// Workaround: as above, the values passed the resource's schema before they reach here.
|
|
311
239
|
inputOf: (value) => assumeType(isRecord(value) ? value : {}),
|
|
312
|
-
check(
|
|
313
|
-
if (action === ResourceAction.restore && !softDelete)
|
|
314
|
-
return { allowed: false, reason: "this resource has no soft delete" };
|
|
315
|
-
return firstRefusal(toAllowed(options.can?.(action, row), notAllowed), toAllowed(session?.can(action, { resource: options.key, row }), notAllowed));
|
|
316
|
-
},
|
|
240
|
+
check: checkIn(undefined),
|
|
317
241
|
routes,
|
|
242
|
+
screens,
|
|
243
|
+
detail: base.detail,
|
|
318
244
|
paths: {
|
|
319
245
|
list: () => `/${routes.base}`,
|
|
320
246
|
create: () => `/${routes.base}/${routes.create}`,
|
|
@@ -340,11 +266,11 @@ function createResource(options) {
|
|
|
340
266
|
if (second === routes.edit)
|
|
341
267
|
return { screen: ResourceScreen.edit, id: decodeURIComponent(first) };
|
|
342
268
|
const tab = decodeURIComponent(second);
|
|
343
|
-
if (relations[tab]?.kind === RelationKind.hasMany)
|
|
269
|
+
if (relations[tab]?.kind === RelationKind.hasMany || detailTabs.has(tab))
|
|
344
270
|
return { screen: ResourceScreen.detail, id: decodeURIComponent(first), tab };
|
|
345
271
|
return undefined;
|
|
346
272
|
},
|
|
347
|
-
actions:
|
|
273
|
+
actions: base.actions,
|
|
348
274
|
audit: options.audit,
|
|
349
275
|
};
|
|
350
276
|
if (Object.keys(relations).length > 0)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type FileFormat } from "../core/constants.js";
|
|
2
|
+
import type { CallContext, ExportResult, Page, Query, ResourceCapabilities, ResourceService, Version } from "../core/contracts.js";
|
|
3
|
+
/** The reading half of a service: what a third-party API, a report or a log usually offers. */
|
|
4
|
+
export type ReadService<T, Id = string, F = Record<string, unknown>> = {
|
|
5
|
+
capabilities: ResourceCapabilities;
|
|
6
|
+
list(query: Query<F>, context: CallContext): Promise<Page<T>>;
|
|
7
|
+
get(id: Id, context: CallContext): Promise<T>;
|
|
8
|
+
count?(filter: F, context: CallContext): Promise<number>;
|
|
9
|
+
getMany?(ids: readonly Id[], context: CallContext): Promise<T[]>;
|
|
10
|
+
export?(query: Query<F>, format: FileFormat, context: CallContext): Promise<ExportResult>;
|
|
11
|
+
versionOf?(row: T): Version | undefined;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A service that only reads. Its resource refuses `create`, `update`, `remove`, `restore` and `import` with the reason
|
|
15
|
+
* (`capabilities.readOnly`): their buttons don't show and an agent's step fails saying so. A write that reaches the
|
|
16
|
+
* service anyway (app code) throws a `forbidden` `ServiceError`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readOnlyService<T, Id = string, F = Record<string, unknown>>(service: ReadService<T, Id, F>): ResourceService<T, Id, F>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ServiceErrorKind } from "../core/constants.js";
|
|
2
|
+
import { ServiceFailure, serviceError } from "../core/errors.js";
|
|
3
|
+
function refused(operation) {
|
|
4
|
+
return Promise.reject(new ServiceFailure(serviceError(ServiceErrorKind.forbidden, `This service is read-only: it has no ${operation}.`)));
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A service that only reads. Its resource refuses `create`, `update`, `remove`, `restore` and `import` with the reason
|
|
8
|
+
* (`capabilities.readOnly`): their buttons don't show and an agent's step fails saying so. A write that reaches the
|
|
9
|
+
* service anyway (app code) throws a `forbidden` `ServiceError`.
|
|
10
|
+
*/
|
|
11
|
+
export function readOnlyService(service) {
|
|
12
|
+
const { count, getMany, export: exportRows, versionOf } = service;
|
|
13
|
+
return {
|
|
14
|
+
capabilities: { ...service.capabilities, readOnly: true, restore: false },
|
|
15
|
+
list: (query, context) => service.list(query, context),
|
|
16
|
+
get: (id, context) => service.get(id, context),
|
|
17
|
+
...(count ? { count: count.bind(service) } : {}),
|
|
18
|
+
...(getMany ? { getMany: getMany.bind(service) } : {}),
|
|
19
|
+
...(exportRows ? { export: exportRows.bind(service) } : {}),
|
|
20
|
+
...(versionOf ? { versionOf: versionOf.bind(service) } : {}),
|
|
21
|
+
create: () => refused("create"),
|
|
22
|
+
update: () => refused("update"),
|
|
23
|
+
remove: () => refused("remove"),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -11,7 +11,10 @@ export declare const HttpMethod: {
|
|
|
11
11
|
export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
|
|
12
12
|
export type RestServiceOptions<T, F> = {
|
|
13
13
|
fetch?: typeof fetch;
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Headers for every call; `session.token()` becomes `Authorization: Bearer …` unless you set it, and a write's
|
|
16
|
+
* `idempotencyKey` `Idempotency-Key`.
|
|
17
|
+
*/
|
|
15
18
|
headers?: (context: CallContext) => Record<string, string> | Promise<Record<string, string>>;
|
|
16
19
|
position?: PagePositionKind;
|
|
17
20
|
/** The first page's number: 1 (default) or 0. */
|
|
@@ -75,6 +75,8 @@ export function restService(base, options = {}) {
|
|
|
75
75
|
headers.authorization = `Bearer ${token}`;
|
|
76
76
|
if (body !== undefined)
|
|
77
77
|
headers["content-type"] = "application/json";
|
|
78
|
+
if (context.idempotencyKey !== undefined && method !== HttpMethod.get)
|
|
79
|
+
headers["idempotency-key"] = context.idempotencyKey;
|
|
78
80
|
const fail = (error) => new ServiceFailure(options.mapError ? options.mapError(error) : error);
|
|
79
81
|
let response;
|
|
80
82
|
try {
|
|
@@ -5,6 +5,7 @@ import type { PreferencesStore, SavedView, ViewStore } from "./preferences.js";
|
|
|
5
5
|
import type { Allowed } from "../core/refusal.js";
|
|
6
6
|
import type { Resource } from "../resource/resource.js";
|
|
7
7
|
import type { RouterAdapter } from "../navigation/router.js";
|
|
8
|
+
import type { Text } from "../i18n/locale.js";
|
|
8
9
|
export type ViewState<F> = {
|
|
9
10
|
filter: Partial<F>;
|
|
10
11
|
search: string;
|
|
@@ -17,15 +18,16 @@ export type ViewState<F> = {
|
|
|
17
18
|
/** The saved view or preset applied; it stays when the state moves away from it, which `views.modified()` tells. */
|
|
18
19
|
view?: string | undefined;
|
|
19
20
|
};
|
|
21
|
+
/** A tab over the list; its label shows with `resolveText`, so it may be a message. */
|
|
20
22
|
export type ViewScope<F> = {
|
|
21
23
|
id: string;
|
|
22
|
-
label:
|
|
24
|
+
label: Text;
|
|
23
25
|
filter: Partial<F>;
|
|
24
26
|
count?: boolean;
|
|
25
27
|
};
|
|
26
28
|
export type ViewPreset<F> = {
|
|
27
29
|
id: string;
|
|
28
|
-
label:
|
|
30
|
+
label: Text;
|
|
29
31
|
state: Partial<ViewState<F>>;
|
|
30
32
|
};
|
|
31
33
|
export type ViewOptions<F> = {
|
package/locales/en.json
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
{
|
|
2
|
+
"create": "New {one}",
|
|
3
|
+
"edit": "Edit",
|
|
4
|
+
"editRecord": "Edit {title}",
|
|
5
|
+
"remove": "Delete",
|
|
6
|
+
"restore": "Restore",
|
|
7
|
+
"save": "Save",
|
|
8
|
+
"cancel": "Cancel",
|
|
9
|
+
"undo": "Undo",
|
|
10
|
+
"open": "Open",
|
|
11
|
+
"back": "Back",
|
|
12
|
+
"retry": "Try again",
|
|
13
|
+
"records": "{Other} records",
|
|
14
|
+
"listTool": "{Other} list",
|
|
15
|
+
"removeRecords": "Remove {other}",
|
|
16
|
+
"rowActions": "Actions for {title}",
|
|
17
|
+
"quickCreate": "Quick create {one}",
|
|
18
|
+
"createOption": "Create “{text}”",
|
|
19
|
+
"creatingOption": "Creating “{text}”…",
|
|
20
|
+
"empty": "No {other}",
|
|
21
|
+
"errorTitle": "Couldn't load {other}",
|
|
22
|
+
"notFound": "This {one} doesn't exist",
|
|
23
|
+
"forbidden": "You can't see this {one}",
|
|
24
|
+
"notAllowed": "You don't have permission",
|
|
25
|
+
"forbiddenPage": "You don't have access to this page",
|
|
26
|
+
"newRows": "Show {count} new {count, plural, one {row} other {rows}}",
|
|
27
|
+
"confirm": "Confirm",
|
|
28
|
+
"ok": "OK",
|
|
29
|
+
"range": "{shape, select, partial {Showing {from}–{to}} estimate {Showing {from}–{to} of about {total}} other {Showing {from}–{to} of {total}}}",
|
|
30
|
+
"loading": "Loading…",
|
|
31
|
+
"search": "Search {other}",
|
|
32
|
+
"filters": "{Other} filters",
|
|
33
|
+
"pageSize": "Rows per page",
|
|
34
|
+
"addFilter": "Add filter",
|
|
35
|
+
"resetFilters": "Reset filters",
|
|
36
|
+
"removeFilter": "Remove {field} filter",
|
|
37
|
+
"previousPage": "Previous page",
|
|
38
|
+
"nextPage": "Next page",
|
|
39
|
+
"page": "Page {page}",
|
|
40
|
+
"pagination": "{Other} pages",
|
|
41
|
+
"loadMore": "Load more",
|
|
42
|
+
"showDeleted": "Show deleted",
|
|
43
|
+
"columns": "Columns",
|
|
44
|
+
"views": "{Other} views",
|
|
45
|
+
"allRows": "All",
|
|
46
|
+
"saveView": "Save view",
|
|
47
|
+
"viewName": "View name",
|
|
48
|
+
"removeView": "Delete view {label}",
|
|
49
|
+
"removeViews": "Delete {other} views",
|
|
50
|
+
"confirmRemoveView": "Delete the view {label}?",
|
|
51
|
+
"viewOptions": "Options for view {label}",
|
|
52
|
+
"viewModified": "Modified",
|
|
53
|
+
"saveViewChanges": "Save changes",
|
|
54
|
+
"renameView": "Rename",
|
|
55
|
+
"defaultView": "Default",
|
|
56
|
+
"setDefaultView": "Make default",
|
|
57
|
+
"unsetDefaultView": "Stop using as default",
|
|
58
|
+
"onlyThisBrowser": "Only in this browser",
|
|
59
|
+
"selected": "{count} selected",
|
|
60
|
+
"selectAllMatching": "Select all {count} {other}",
|
|
61
|
+
"allMatchingSelected": "All {count} {other} that match are selected",
|
|
62
|
+
"clearSelection": "Clear selection",
|
|
63
|
+
"exportCsv": "Export CSV",
|
|
64
|
+
"importCsv": "Import CSV",
|
|
65
|
+
"importTitle": "Import {other}",
|
|
66
|
+
"importTool": "{Other} import",
|
|
67
|
+
"importNoRows": "The file has no rows under its header.",
|
|
68
|
+
"columnField": "Field for {column}",
|
|
69
|
+
"skipColumn": "Don't import",
|
|
70
|
+
"next": "Next",
|
|
71
|
+
"close": "Close",
|
|
72
|
+
"importReady": "{valid} {valid, plural, one {row} other {rows}} ready, {invalid} with errors",
|
|
73
|
+
"importRows": "Import {count} {count, plural, one {row} other {rows}}",
|
|
74
|
+
"downloadErrors": "Download rows with errors",
|
|
75
|
+
"importing": "Imported {done} of {total}",
|
|
76
|
+
"stop": "Stop",
|
|
77
|
+
"importSummary": "{cancelled, select, yes {Stopped. } other {}}{created} created, {updated} updated, {failed} failed.",
|
|
78
|
+
"line": "Row",
|
|
79
|
+
"errorColumn": "Error",
|
|
80
|
+
"exportTooLarge": "{count} rows exceed the {limit}-row limit of a browser export",
|
|
81
|
+
"confirmRemove": "Delete {target}?",
|
|
82
|
+
"confirmRemoveDescription": "This can't be undone.",
|
|
83
|
+
"removeAgentConfirm": "Deletes {target}",
|
|
84
|
+
"removed": "{count} {other} deleted",
|
|
85
|
+
"created": "{title} created",
|
|
86
|
+
"saved": "{title} saved",
|
|
87
|
+
"restored": "{count} restored",
|
|
88
|
+
"failed": "Something went wrong",
|
|
89
|
+
"unsavedChanges": "Discard your unsaved changes?",
|
|
90
|
+
"discard": "Discard",
|
|
91
|
+
"keepEditing": "Keep editing",
|
|
92
|
+
"draftFound": "You have unsaved changes from {date, date, medium}, {date, time, short}.",
|
|
93
|
+
"draft": "{Title} draft",
|
|
94
|
+
"restoreDraft": "Restore",
|
|
95
|
+
"discardDraft": "Discard",
|
|
96
|
+
"favorites": "Favorites",
|
|
97
|
+
"recents": "Recent",
|
|
98
|
+
"pin": "Pin {label}",
|
|
99
|
+
"unpin": "Unpin {label}",
|
|
100
|
+
"goTo": "Go to",
|
|
101
|
+
"pages": "Pages",
|
|
102
|
+
"actions": "Actions",
|
|
103
|
+
"breadcrumb": "Breadcrumb",
|
|
104
|
+
"audit": {
|
|
105
|
+
"createdAt": "Created",
|
|
106
|
+
"createdBy": "Created by",
|
|
107
|
+
"updatedAt": "Updated",
|
|
108
|
+
"updatedBy": "Updated by"
|
|
109
|
+
},
|
|
110
|
+
"yes": "Yes",
|
|
111
|
+
"no": "No",
|
|
112
|
+
"notSet": "Not set",
|
|
113
|
+
"dropFile": "Drop a file here",
|
|
114
|
+
"browseFiles": "Browse files",
|
|
115
|
+
"controls": {
|
|
116
|
+
"refreshing": "Refreshing",
|
|
117
|
+
"filters": {
|
|
118
|
+
"chip": "{empty, select, yes {{field}} other {{field}: {summary}}}",
|
|
119
|
+
"selected": "{count} selected",
|
|
120
|
+
"any": "Any",
|
|
121
|
+
"from": "From",
|
|
122
|
+
"to": "To",
|
|
123
|
+
"min": "Minimum",
|
|
124
|
+
"max": "Maximum",
|
|
125
|
+
"value": "Value",
|
|
126
|
+
"comparison": "Comparison",
|
|
127
|
+
"operators": {
|
|
128
|
+
"eq": "Equals",
|
|
129
|
+
"neq": "Is not",
|
|
130
|
+
"lt": "Less than",
|
|
131
|
+
"lte": "At most",
|
|
132
|
+
"gt": "More than",
|
|
133
|
+
"gte": "At least",
|
|
134
|
+
"between": "Between"
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
"combobox": {
|
|
138
|
+
"trigger": "Show options",
|
|
139
|
+
"clear": "Clear",
|
|
140
|
+
"remove": "Remove {label}",
|
|
141
|
+
"error": "Couldn't load options",
|
|
142
|
+
"empty": "{empty, select, yes {No options} other {Nothing matches “{query}”}}",
|
|
143
|
+
"count": "{count}{more, select, yes {+} other {}} {count, plural, one {option} other {options}}"
|
|
144
|
+
},
|
|
145
|
+
"files": {
|
|
146
|
+
"list": "Files",
|
|
147
|
+
"remove": "Remove {name}",
|
|
148
|
+
"rejected": "{name} can't be added"
|
|
149
|
+
},
|
|
150
|
+
"date": {
|
|
151
|
+
"clear": "Clear date",
|
|
152
|
+
"previousMonth": "Previous month",
|
|
153
|
+
"nextMonth": "Next month",
|
|
154
|
+
"start": "Start date",
|
|
155
|
+
"end": "End date"
|
|
156
|
+
},
|
|
157
|
+
"time": "Time",
|
|
158
|
+
"colorValue": "Colour value"
|
|
159
|
+
},
|
|
160
|
+
"issues": {
|
|
161
|
+
"required": "This field is required.",
|
|
162
|
+
"minLength": "Use at least {min} {min, plural, one {character} other {characters}}.",
|
|
163
|
+
"maxLength": "Use at most {max} {max, plural, one {character} other {characters}}.",
|
|
164
|
+
"minItems": "Choose at least {min}.",
|
|
165
|
+
"maxItems": "Choose at most {max}.",
|
|
166
|
+
"min": "Enter {min} or more.",
|
|
167
|
+
"max": "Enter {max} or less.",
|
|
168
|
+
"email": "Enter a valid email address.",
|
|
169
|
+
"url": "Enter a valid URL.",
|
|
170
|
+
"pattern": "Use the expected format.",
|
|
171
|
+
"invalid": "Enter a valid value."
|
|
172
|
+
},
|
|
173
|
+
"missingRecord": "No record with the id {id}",
|
|
174
|
+
"addRow": "Add row",
|
|
175
|
+
"removeRow": "Remove row {row}",
|
|
176
|
+
"invalidDuration": "Enter a duration, like 1:30",
|
|
177
|
+
"errors": {
|
|
178
|
+
"network": "Couldn't reach the server. Check your connection and try again.",
|
|
179
|
+
"timeout": "The server took too long to answer. Try again.",
|
|
180
|
+
"unauthorized": "Sign in to continue.",
|
|
181
|
+
"session-expired": "Your session has expired. Sign in again.",
|
|
182
|
+
"forbidden": "You don't have permission to do this.",
|
|
183
|
+
"not-found": "It doesn't exist anymore.",
|
|
184
|
+
"conflict": "Someone changed this in the meantime. Review it and try again.",
|
|
185
|
+
"validation": "Check the highlighted fields.",
|
|
186
|
+
"rate-limited": "Too many requests. Wait a moment and try again.",
|
|
187
|
+
"server": "The server ran into a problem. Try again later.",
|
|
188
|
+
"unknown": "Something went wrong.",
|
|
189
|
+
"aborted": "Cancelled."
|
|
190
|
+
},
|
|
191
|
+
"bulkResult": "{succeeded} done, {failed} failed",
|
|
192
|
+
"retryFailed": "Retry failed ({count})"
|
|
193
|
+
}
|