@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.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -2
  3. package/dist/action/action.d.ts +133 -0
  4. package/dist/action/action.js +224 -0
  5. package/dist/action/bulk.d.ts +24 -0
  6. package/dist/action/bulk.js +24 -0
  7. package/dist/cli/cli.d.ts +2 -0
  8. package/dist/cli/cli.js +51 -0
  9. package/dist/cli/routes.d.ts +17 -0
  10. package/dist/cli/routes.js +36 -0
  11. package/dist/core/assume.d.ts +1 -0
  12. package/dist/core/assume.js +8 -0
  13. package/dist/core/constants.d.ts +359 -0
  14. package/dist/core/constants.js +187 -0
  15. package/dist/core/contracts.d.ts +107 -0
  16. package/dist/core/contracts.js +3 -0
  17. package/dist/core/data-labels.d.ts +210 -0
  18. package/dist/core/data-labels.js +385 -0
  19. package/dist/core/errors.d.ts +52 -0
  20. package/dist/core/errors.js +209 -0
  21. package/dist/core/guards.d.ts +9 -0
  22. package/dist/core/guards.js +14 -0
  23. package/dist/core/refusal.d.ts +30 -0
  24. package/dist/core/refusal.js +53 -0
  25. package/dist/dialogs/dialogs.d.ts +75 -0
  26. package/dist/dialogs/dialogs.js +66 -0
  27. package/dist/draft/draft.d.ts +36 -0
  28. package/dist/draft/draft.js +87 -0
  29. package/dist/errors/adapters.d.ts +21 -0
  30. package/dist/errors/adapters.js +81 -0
  31. package/dist/errors/define-errors.d.ts +88 -0
  32. package/dist/errors/define-errors.js +168 -0
  33. package/dist/errors/idempotency.d.ts +2 -0
  34. package/dist/errors/idempotency.js +13 -0
  35. package/dist/export/csv.d.ts +25 -0
  36. package/dist/export/csv.js +59 -0
  37. package/dist/i18n/adapters.d.ts +53 -0
  38. package/dist/i18n/adapters.js +56 -0
  39. package/dist/i18n/format.d.ts +10 -0
  40. package/dist/i18n/format.js +273 -0
  41. package/dist/i18n/labels.d.ts +10 -0
  42. package/dist/i18n/labels.js +227 -0
  43. package/dist/i18n/locale.d.ts +73 -0
  44. package/dist/i18n/locale.js +74 -0
  45. package/dist/i18n/messages.d.ts +93 -0
  46. package/dist/i18n/messages.js +16 -0
  47. package/dist/import/csv-parse.d.ts +11 -0
  48. package/dist/import/csv-parse.js +70 -0
  49. package/dist/import/import.d.ts +104 -0
  50. package/dist/import/import.js +266 -0
  51. package/dist/index.d.ts +46 -0
  52. package/dist/index.js +45 -0
  53. package/dist/navigation/nav.d.ts +128 -0
  54. package/dist/navigation/nav.js +301 -0
  55. package/dist/navigation/router.d.ts +81 -0
  56. package/dist/navigation/router.js +249 -0
  57. package/dist/navigation/shortcuts.d.ts +37 -0
  58. package/dist/navigation/shortcuts.js +93 -0
  59. package/dist/next/index.d.ts +3 -0
  60. package/dist/next/index.js +3 -0
  61. package/dist/next/screen-hydration.d.ts +12 -0
  62. package/dist/next/screen-hydration.js +26 -0
  63. package/dist/next/screen.d.ts +25 -0
  64. package/dist/next/screen.js +44 -0
  65. package/dist/next/server/handler.d.ts +39 -0
  66. package/dist/next/server/handler.js +190 -0
  67. package/dist/next/server/index.d.ts +6 -0
  68. package/dist/next/server/index.js +6 -0
  69. package/dist/next/server/page.d.ts +63 -0
  70. package/dist/next/server/page.js +110 -0
  71. package/dist/next/server/prefetch.d.ts +29 -0
  72. package/dist/next/server/prefetch.js +97 -0
  73. package/dist/next/server/session.d.ts +25 -0
  74. package/dist/next/server/session.js +23 -0
  75. package/dist/next/use-next-router.d.ts +21 -0
  76. package/dist/next/use-next-router.js +104 -0
  77. package/dist/permissions/permissions.d.ts +134 -0
  78. package/dist/permissions/permissions.js +121 -0
  79. package/dist/react/data-layer.d.ts +68 -0
  80. package/dist/react/data-layer.js +76 -0
  81. package/dist/react/hooks/use-action-dialog.d.ts +25 -0
  82. package/dist/react/hooks/use-action-dialog.js +31 -0
  83. package/dist/react/hooks/use-action.d.ts +37 -0
  84. package/dist/react/hooks/use-action.js +125 -0
  85. package/dist/react/hooks/use-better-auth.d.ts +12 -0
  86. package/dist/react/hooks/use-better-auth.js +11 -0
  87. package/dist/react/hooks/use-bulk-action.d.ts +20 -0
  88. package/dist/react/hooks/use-bulk-action.js +24 -0
  89. package/dist/react/hooks/use-dialogs.d.ts +14 -0
  90. package/dist/react/hooks/use-dialogs.js +20 -0
  91. package/dist/react/hooks/use-draft.d.ts +5 -0
  92. package/dist/react/hooks/use-draft.js +17 -0
  93. package/dist/react/hooks/use-nav.d.ts +17 -0
  94. package/dist/react/hooks/use-nav.js +35 -0
  95. package/dist/react/hooks/use-new-rows.d.ts +22 -0
  96. package/dist/react/hooks/use-new-rows.js +24 -0
  97. package/dist/react/hooks/use-permissions.d.ts +13 -0
  98. package/dist/react/hooks/use-permissions.js +19 -0
  99. package/dist/react/hooks/use-reauthenticate.d.ts +6 -0
  100. package/dist/react/hooks/use-reauthenticate.js +11 -0
  101. package/dist/react/hooks/use-resource.d.ts +92 -0
  102. package/dist/react/hooks/use-resource.js +255 -0
  103. package/dist/react/hooks/use-view.d.ts +6 -0
  104. package/dist/react/hooks/use-view.js +24 -0
  105. package/dist/react/index.d.ts +15 -0
  106. package/dist/react/index.js +15 -0
  107. package/dist/react/keys.d.ts +60 -0
  108. package/dist/react/keys.js +71 -0
  109. package/dist/react/new-rows.d.ts +22 -0
  110. package/dist/react/new-rows.js +72 -0
  111. package/dist/refresh/new-rows.d.ts +13 -0
  112. package/dist/refresh/new-rows.js +18 -0
  113. package/dist/resource/form-schema.d.ts +11 -0
  114. package/dist/resource/form-schema.js +51 -0
  115. package/dist/resource/relations.d.ts +99 -0
  116. package/dist/resource/relations.js +194 -0
  117. package/dist/resource/resource-relations.d.ts +47 -0
  118. package/dist/resource/resource-relations.js +154 -0
  119. package/dist/resource/resource-schema.d.ts +9 -0
  120. package/dist/resource/resource-schema.js +73 -0
  121. package/dist/resource/resource-texts.d.ts +18 -0
  122. package/dist/resource/resource-texts.js +13 -0
  123. package/dist/resource/resource-types.d.ts +420 -0
  124. package/dist/resource/resource-types.js +1 -0
  125. package/dist/resource/resource-words.d.ts +25 -0
  126. package/dist/resource/resource-words.js +221 -0
  127. package/dist/resource/resource.d.ts +15 -0
  128. package/dist/resource/resource.js +280 -0
  129. package/dist/resource/schema.d.ts +57 -0
  130. package/dist/resource/schema.js +115 -0
  131. package/dist/services/from-page-source.d.ts +15 -0
  132. package/dist/services/from-page-source.js +27 -0
  133. package/dist/services/memory-query.d.ts +8 -0
  134. package/dist/services/memory-query.js +165 -0
  135. package/dist/services/memory-service.d.ts +33 -0
  136. package/dist/services/memory-service.js +291 -0
  137. package/dist/services/read-only-service.d.ts +18 -0
  138. package/dist/services/read-only-service.js +25 -0
  139. package/dist/services/rest-service.d.ts +65 -0
  140. package/dist/services/rest-service.js +222 -0
  141. package/dist/services/rng.d.ts +9 -0
  142. package/dist/services/rng.js +22 -0
  143. package/dist/services/spring-service.d.ts +58 -0
  144. package/dist/services/spring-service.js +132 -0
  145. package/dist/session/better-auth.d.ts +83 -0
  146. package/dist/session/better-auth.js +86 -0
  147. package/dist/session/session.d.ts +45 -0
  148. package/dist/session/session.js +36 -0
  149. package/dist/view/preferences.d.ts +35 -0
  150. package/dist/view/preferences.js +93 -0
  151. package/dist/view/view-types.d.ts +124 -0
  152. package/dist/view/view-types.js +1 -0
  153. package/dist/view/view-url.d.ts +7 -0
  154. package/dist/view/view-url.js +54 -0
  155. package/dist/view/view.d.ts +4 -0
  156. package/dist/view/view.js +392 -0
  157. package/locales/en.json +193 -0
  158. package/locales/es.json +193 -0
  159. package/package.json +88 -4
@@ -0,0 +1,273 @@
1
+ import { isNumber, isOneOf, isRecord, isString, valuesOf } from "../core/guards.js";
2
+ const PartKind = {
3
+ argument: "argument",
4
+ number: "number",
5
+ date: "date",
6
+ time: "time",
7
+ plural: "plural",
8
+ selectordinal: "selectordinal",
9
+ select: "select",
10
+ pound: "pound",
11
+ };
12
+ const Other = "other";
13
+ const Offset = "offset:";
14
+ const Exact = "=";
15
+ const formatKinds = new Map([
16
+ [PartKind.number, PartKind.number],
17
+ [PartKind.date, PartKind.date],
18
+ [PartKind.time, PartKind.time],
19
+ [PartKind.plural, PartKind.plural],
20
+ [PartKind.selectordinal, PartKind.selectordinal],
21
+ [PartKind.select, PartKind.select],
22
+ ]);
23
+ const Brace = { open: "{", close: "}" };
24
+ const Quote = "'";
25
+ const Pound = "#";
26
+ const Comma = ",";
27
+ class Parser {
28
+ source;
29
+ at = 0;
30
+ constructor(source) {
31
+ this.source = source;
32
+ }
33
+ fail(what) {
34
+ throw new SyntaxError(`${what} at ${String(this.at)} in the message "${this.source}".`);
35
+ }
36
+ spaces() {
37
+ while (/\s/u.test(this.source[this.at] ?? ""))
38
+ this.at += 1;
39
+ }
40
+ word() {
41
+ this.spaces();
42
+ const start = this.at;
43
+ while (this.at < this.source.length && !/[\s{},]/u.test(this.source[this.at]))
44
+ this.at += 1;
45
+ if (start === this.at)
46
+ this.fail("Expected a name");
47
+ return this.source.slice(start, this.at);
48
+ }
49
+ expect(char) {
50
+ this.spaces();
51
+ if (this.source[this.at] !== char)
52
+ this.fail(`Expected "${char}"`);
53
+ this.at += 1;
54
+ }
55
+ quoted(inPlural) {
56
+ const next = this.source[this.at + 1];
57
+ if (next === Quote) {
58
+ this.at += 2;
59
+ return Quote;
60
+ }
61
+ const special = next === Brace.open || next === Brace.close || (inPlural && next === Pound);
62
+ if (!special)
63
+ return undefined;
64
+ const close = this.source.indexOf(Quote, this.at + 1);
65
+ const end = close === -1 ? this.source.length : close;
66
+ const text = this.source.slice(this.at + 1, end);
67
+ this.at = end + 1;
68
+ return text;
69
+ }
70
+ message(inPlural, nested) {
71
+ const parts = [];
72
+ let text = "";
73
+ const flush = () => {
74
+ if (text !== "")
75
+ parts.push(text);
76
+ text = "";
77
+ };
78
+ while (this.at < this.source.length) {
79
+ const char = this.source[this.at];
80
+ if (char === Brace.close) {
81
+ if (nested)
82
+ break;
83
+ this.fail("Unexpected }");
84
+ }
85
+ if (char === Brace.open) {
86
+ flush();
87
+ parts.push(this.argument(inPlural));
88
+ continue;
89
+ }
90
+ if (char === Pound && inPlural) {
91
+ flush();
92
+ parts.push({ kind: PartKind.pound });
93
+ this.at += 1;
94
+ continue;
95
+ }
96
+ if (char === Quote) {
97
+ const literal = this.quoted(inPlural);
98
+ if (literal !== undefined) {
99
+ text += literal;
100
+ continue;
101
+ }
102
+ }
103
+ text += char;
104
+ this.at += 1;
105
+ }
106
+ flush();
107
+ return parts;
108
+ }
109
+ branches(inPlural) {
110
+ const found = {};
111
+ this.spaces();
112
+ while (this.source[this.at] !== Brace.close) {
113
+ const key = this.word();
114
+ this.expect(Brace.open);
115
+ found[key] = this.message(inPlural, true);
116
+ this.expect(Brace.close);
117
+ this.spaces();
118
+ if (this.at >= this.source.length)
119
+ this.fail("Expected }");
120
+ }
121
+ if (!(Other in found))
122
+ this.fail("Expected an other branch");
123
+ return found;
124
+ }
125
+ argument(inPlural) {
126
+ this.expect(Brace.open);
127
+ const name = this.word();
128
+ this.spaces();
129
+ if (this.source[this.at] === Brace.close) {
130
+ this.at += 1;
131
+ return { kind: PartKind.argument, name };
132
+ }
133
+ this.expect(Comma);
134
+ const type = this.word();
135
+ const kind = formatKinds.get(type) ?? this.fail(`Unknown format "${type}"`);
136
+ this.spaces();
137
+ if (kind === PartKind.select || kind === PartKind.plural || kind === PartKind.selectordinal) {
138
+ this.expect(Comma);
139
+ this.spaces();
140
+ let offset = 0;
141
+ if (this.source.startsWith(Offset, this.at)) {
142
+ this.at += Offset.length;
143
+ offset = Number(this.word());
144
+ }
145
+ const branches = this.branches(kind === PartKind.select ? inPlural : true);
146
+ this.expect(Brace.close);
147
+ return kind === PartKind.select ? { kind, name, branches } : { kind, name, offset, branches };
148
+ }
149
+ if (kind !== PartKind.number && kind !== PartKind.date && kind !== PartKind.time)
150
+ this.fail("Unexpected format");
151
+ let style = "";
152
+ if (this.source[this.at] === Comma) {
153
+ this.at += 1;
154
+ style = this.word();
155
+ }
156
+ this.expect(Brace.close);
157
+ return { kind, name, style };
158
+ }
159
+ }
160
+ const parsed = new Map();
161
+ function parse(source) {
162
+ const known = parsed.get(source);
163
+ if (known)
164
+ return known;
165
+ const parts = source.includes(Brace.open) || source.includes(Quote) ? new Parser(source).message(false, false) : [source];
166
+ parsed.set(source, parts);
167
+ return parts;
168
+ }
169
+ const intl = new Map();
170
+ function cached(key, make, is) {
171
+ const known = intl.get(key);
172
+ if (is(known))
173
+ return known;
174
+ const made = make();
175
+ intl.set(key, made);
176
+ return made;
177
+ }
178
+ const isNumberFormat = (value) => value instanceof Intl.NumberFormat;
179
+ const isDateFormat = (value) => value instanceof Intl.DateTimeFormat;
180
+ const isPluralRules = (value) => value instanceof Intl.PluralRules;
181
+ const NumberStyle = { integer: "integer", percent: "percent" };
182
+ const DateStyle = { short: "short", medium: "medium", long: "long", full: "full" };
183
+ const dateStyles = valuesOf(DateStyle);
184
+ const PluralType = { cardinal: "cardinal", ordinal: "ordinal" };
185
+ function numberFormat(code, style) {
186
+ const options = style === NumberStyle.integer
187
+ ? { maximumFractionDigits: 0 }
188
+ : style === NumberStyle.percent
189
+ ? { style: NumberStyle.percent }
190
+ : {};
191
+ return cached(`n|${code}|${style}`, () => new Intl.NumberFormat(code, options), isNumberFormat);
192
+ }
193
+ function dateFormat(code, kind, style) {
194
+ const chosen = isOneOf(dateStyles, style) ? style : DateStyle.medium;
195
+ const options = kind === PartKind.date ? { dateStyle: chosen } : { timeStyle: chosen };
196
+ return cached(`${kind}|${code}|${chosen}`, () => new Intl.DateTimeFormat(code, options), isDateFormat);
197
+ }
198
+ function plurals(code, ordinal) {
199
+ const type = ordinal ? PluralType.ordinal : PluralType.cardinal;
200
+ return cached(`p|${code}|${type}`, () => new Intl.PluralRules(code, { type }), isPluralRules);
201
+ }
202
+ function toDate(value) {
203
+ if (value instanceof Date)
204
+ return value;
205
+ if (isNumber(value) || isString(value)) {
206
+ const date = new Date(value);
207
+ return Number.isNaN(date.getTime()) ? undefined : date;
208
+ }
209
+ return undefined;
210
+ }
211
+ function plain(value, code) {
212
+ if (isNumber(value))
213
+ return numberFormat(code, "").format(value);
214
+ if (value instanceof Date)
215
+ return dateFormat(code, PartKind.date, DateStyle.medium).format(value);
216
+ if (value === undefined || value === null)
217
+ return "";
218
+ if (isString(value))
219
+ return value;
220
+ if (isRecord(value) || Array.isArray(value))
221
+ return JSON.stringify(value);
222
+ return String(value);
223
+ }
224
+ function render(parts, values, code, pound) {
225
+ let out = "";
226
+ for (const part of parts) {
227
+ if (isString(part)) {
228
+ out += part;
229
+ continue;
230
+ }
231
+ if (part.kind === PartKind.pound) {
232
+ out += pound === undefined ? Pound : numberFormat(code, "").format(pound);
233
+ continue;
234
+ }
235
+ const value = values[part.name];
236
+ if (part.kind === PartKind.argument)
237
+ out += plain(value, code);
238
+ else if (part.kind === PartKind.number || part.kind === PartKind.date || part.kind === PartKind.time) {
239
+ const date = part.kind === PartKind.number ? undefined : toDate(value);
240
+ if (part.kind === PartKind.number)
241
+ out += isNumber(value) ? numberFormat(code, part.style).format(value) : plain(value, code);
242
+ else
243
+ out += date ? dateFormat(code, part.kind, part.style).format(date) : plain(value, code);
244
+ }
245
+ else if (part.kind === PartKind.select) {
246
+ const branch = part.branches[plain(value, code)] ?? part.branches[Other] ?? [];
247
+ out += render(branch, values, code, pound);
248
+ }
249
+ else {
250
+ const count = Number(value);
251
+ const exact = part.branches[`${Exact}${String(count)}`];
252
+ const shifted = count - part.offset;
253
+ const branch = exact ??
254
+ part.branches[plurals(code, part.kind === PartKind.selectordinal).select(shifted)] ??
255
+ part.branches[Other] ??
256
+ [];
257
+ out += render(branch, values, code, shifted);
258
+ }
259
+ }
260
+ return out;
261
+ }
262
+ /**
263
+ * Formats an ICU MessageFormat message in `code`'s language: `{name}`, `{n, number}` (`integer`, `percent`),
264
+ * `{d, date, short}` and `{d, time, short}`, `{n, plural, =0 {…} one {# item} other {# items}}` (with `offset:`),
265
+ * `{n, selectordinal, …}` and `{gender, select, f {…} other {…}}`. Quotes escape as in ICU: `''` and `'{literal}'`.
266
+ */
267
+ export function formatMessage(message, values = {}, code = "en") {
268
+ return render(parse(message), values, code, undefined);
269
+ }
270
+ /** Throws a `SyntaxError` that says where, when `message` isn't valid ICU MessageFormat. */
271
+ export function checkMessage(message) {
272
+ parse(message);
273
+ }
@@ -0,0 +1,10 @@
1
+ import type { DataLabels } from "../core/data-labels.js";
2
+ import { Gender } from "./messages.js";
3
+ import { type Locale } from "./locale.js";
4
+ /**
5
+ * The `DataLabels` the blocks read, from a locale's messages. Messages about a resource get its `gender` too
6
+ * (`Gender.feminine`, `"f"`), for a `select`; `{one}` and `{other}` are its name in lower case, `{One}` and `{Other}`
7
+ * as written.
8
+ */
9
+ export declare function dataLabelsOf(locale: Locale, gender?: string): DataLabels;
10
+ export { Gender };
@@ -0,0 +1,227 @@
1
+ import { ErrorReason } from "../core/constants.js";
2
+ import { Gender } from "./messages.js";
3
+ import { lookup } from "./locale.js";
4
+ const Flag = { yes: "yes", no: "no" };
5
+ const RangeShape = { partial: "partial", estimate: "estimate", exact: "exact" };
6
+ const NoGender = "other";
7
+ const flag = (value) => (value ? Flag.yes : Flag.no);
8
+ function controlsOf(t) {
9
+ const c = (key, values) => t(`controls.${key}`, values);
10
+ return {
11
+ refreshing: c("refreshing"),
12
+ filters: {
13
+ chip: (field, summary) => c("filters.chip", { field, summary, empty: flag(summary === "") }),
14
+ selected: (count) => c("filters.selected", { count }),
15
+ any: c("filters.any"),
16
+ from: c("filters.from"),
17
+ to: c("filters.to"),
18
+ min: c("filters.min"),
19
+ max: c("filters.max"),
20
+ value: c("filters.value"),
21
+ comparison: c("filters.comparison"),
22
+ operators: {
23
+ eq: c("filters.operators.eq"),
24
+ neq: c("filters.operators.neq"),
25
+ lt: c("filters.operators.lt"),
26
+ lte: c("filters.operators.lte"),
27
+ gt: c("filters.operators.gt"),
28
+ gte: c("filters.operators.gte"),
29
+ between: c("filters.operators.between"),
30
+ },
31
+ },
32
+ combobox: {
33
+ trigger: c("combobox.trigger"),
34
+ clear: c("combobox.clear"),
35
+ remove: (label) => c("combobox.remove", { label }),
36
+ error: c("combobox.error"),
37
+ empty: (query) => c("combobox.empty", { query, empty: flag(query === "") }),
38
+ count: (count, hasMore) => c("combobox.count", { count, more: flag(hasMore) }),
39
+ },
40
+ files: {
41
+ list: c("files.list"),
42
+ remove: (name) => c("files.remove", { name }),
43
+ rejected: (name) => c("files.rejected", { name }),
44
+ },
45
+ date: {
46
+ clear: c("date.clear"),
47
+ previousMonth: c("date.previousMonth"),
48
+ nextMonth: c("date.nextMonth"),
49
+ start: c("date.start"),
50
+ end: c("date.end"),
51
+ },
52
+ time: c("time"),
53
+ colorValue: c("colorValue"),
54
+ };
55
+ }
56
+ function errorsOf(t) {
57
+ const e = (reason) => t(`errors.${reason}`);
58
+ return {
59
+ [ErrorReason.network]: e(ErrorReason.network),
60
+ [ErrorReason.timeout]: e(ErrorReason.timeout),
61
+ [ErrorReason.unauthorized]: e(ErrorReason.unauthorized),
62
+ [ErrorReason.sessionExpired]: e(ErrorReason.sessionExpired),
63
+ [ErrorReason.forbidden]: e(ErrorReason.forbidden),
64
+ [ErrorReason.notFound]: e(ErrorReason.notFound),
65
+ [ErrorReason.conflict]: e(ErrorReason.conflict),
66
+ [ErrorReason.validation]: e(ErrorReason.validation),
67
+ [ErrorReason.rateLimited]: e(ErrorReason.rateLimited),
68
+ [ErrorReason.server]: e(ErrorReason.server),
69
+ [ErrorReason.unknown]: e(ErrorReason.unknown),
70
+ [ErrorReason.aborted]: e(ErrorReason.aborted),
71
+ };
72
+ }
73
+ function build(locale, gender) {
74
+ const { t, code } = locale;
75
+ const lower = (text) => text.toLocaleLowerCase(code);
76
+ const one = (text) => ({ one: lower(text), One: text, gender });
77
+ const other = (text) => ({ other: lower(text), Other: text, gender });
78
+ return {
79
+ create: (name) => t("create", one(name)),
80
+ edit: t("edit"),
81
+ editRecord: (title) => t("editRecord", { title, gender }),
82
+ remove: t("remove"),
83
+ restore: t("restore"),
84
+ save: t("save"),
85
+ cancel: t("cancel"),
86
+ undo: t("undo"),
87
+ open: t("open"),
88
+ back: t("back"),
89
+ retry: t("retry"),
90
+ records: (name) => t("records", other(name)),
91
+ listTool: (name) => t("listTool", other(name)),
92
+ removeRecords: (name) => t("removeRecords", other(name)),
93
+ rowActions: (title) => t("rowActions", { title, gender }),
94
+ quickCreate: (name) => t("quickCreate", one(name)),
95
+ createOption: (text) => t("createOption", { text, gender }),
96
+ creatingOption: (text) => t("creatingOption", { text, gender }),
97
+ empty: (name) => t("empty", other(name)),
98
+ errorTitle: (name) => t("errorTitle", other(name)),
99
+ notFound: (name) => t("notFound", one(name)),
100
+ forbidden: (name) => t("forbidden", one(name)),
101
+ notAllowed: t("notAllowed"),
102
+ forbiddenPage: t("forbiddenPage"),
103
+ newRows: (count) => t("newRows", { count }),
104
+ confirm: t("confirm"),
105
+ ok: t("ok"),
106
+ range: ({ from, to, total, estimate }) => t("range", {
107
+ from,
108
+ to,
109
+ total,
110
+ shape: total === undefined ? RangeShape.partial : estimate === true ? RangeShape.estimate : RangeShape.exact,
111
+ }),
112
+ loading: t("loading"),
113
+ search: (name) => t("search", other(name)),
114
+ filters: (name) => t("filters", other(name)),
115
+ pageSize: t("pageSize"),
116
+ addFilter: t("addFilter"),
117
+ resetFilters: t("resetFilters"),
118
+ removeFilter: (field) => t("removeFilter", { field }),
119
+ previousPage: t("previousPage"),
120
+ nextPage: t("nextPage"),
121
+ page: (page) => t("page", { page }),
122
+ pagination: (name) => t("pagination", other(name)),
123
+ loadMore: t("loadMore"),
124
+ showDeleted: t("showDeleted"),
125
+ columns: t("columns"),
126
+ views: (name) => t("views", other(name)),
127
+ allRows: t("allRows"),
128
+ saveView: t("saveView"),
129
+ viewName: t("viewName"),
130
+ removeView: (label) => t("removeView", { label }),
131
+ removeViews: (name) => t("removeViews", other(name)),
132
+ confirmRemoveView: (label) => t("confirmRemoveView", { label }),
133
+ viewOptions: (label) => t("viewOptions", { label }),
134
+ viewModified: t("viewModified"),
135
+ saveViewChanges: t("saveViewChanges"),
136
+ renameView: t("renameView"),
137
+ defaultView: t("defaultView"),
138
+ setDefaultView: t("setDefaultView"),
139
+ unsetDefaultView: t("unsetDefaultView"),
140
+ onlyThisBrowser: t("onlyThisBrowser"),
141
+ selected: (count) => t("selected", { count, gender }),
142
+ selectAllMatching: (count, name) => t("selectAllMatching", { count, ...other(name) }),
143
+ allMatchingSelected: (count, name) => t("allMatchingSelected", { count, ...other(name) }),
144
+ clearSelection: t("clearSelection"),
145
+ exportCsv: t("exportCsv"),
146
+ importCsv: t("importCsv"),
147
+ importTitle: (name) => t("importTitle", other(name)),
148
+ importTool: (name) => t("importTool", other(name)),
149
+ importNoRows: t("importNoRows"),
150
+ columnField: (column) => t("columnField", { column }),
151
+ skipColumn: t("skipColumn"),
152
+ next: t("next"),
153
+ close: t("close"),
154
+ importReady: (valid, invalid) => t("importReady", { valid, invalid }),
155
+ importRows: (count) => t("importRows", { count }),
156
+ downloadErrors: t("downloadErrors"),
157
+ importing: (done, total) => t("importing", { done, total }),
158
+ stop: t("stop"),
159
+ importSummary: ({ created, updated, failed, cancelled }) => t("importSummary", { created, updated, failed, cancelled: flag(cancelled) }),
160
+ line: t("line"),
161
+ errorColumn: t("errorColumn"),
162
+ exportTooLarge: (count, limit) => t("exportTooLarge", { count, limit }),
163
+ confirmRemove: (target) => t("confirmRemove", { target, gender }),
164
+ confirmRemoveDescription: t("confirmRemoveDescription"),
165
+ removeAgentConfirm: (target) => t("removeAgentConfirm", { target, gender }),
166
+ removed: (count, name) => t("removed", { count, ...other(name) }),
167
+ created: (title) => t("created", { title, gender }),
168
+ saved: (title) => t("saved", { title, gender }),
169
+ restored: (count) => t("restored", { count, gender }),
170
+ failed: t("failed"),
171
+ unsavedChanges: t("unsavedChanges"),
172
+ discard: t("discard"),
173
+ keepEditing: t("keepEditing"),
174
+ draftFound: (date) => t("draftFound", { date }),
175
+ draft: (title) => t("draft", { title: lower(title), Title: title, gender }),
176
+ restoreDraft: t("restoreDraft"),
177
+ discardDraft: t("discardDraft"),
178
+ favorites: t("favorites"),
179
+ recents: t("recents"),
180
+ pin: (label) => t("pin", { label }),
181
+ unpin: (label) => t("unpin", { label }),
182
+ goTo: t("goTo"),
183
+ pages: t("pages"),
184
+ actions: t("actions"),
185
+ breadcrumb: t("breadcrumb"),
186
+ audit: {
187
+ createdAt: t("audit.createdAt"),
188
+ createdBy: t("audit.createdBy"),
189
+ updatedAt: t("audit.updatedAt"),
190
+ updatedBy: t("audit.updatedBy"),
191
+ },
192
+ yes: t("yes"),
193
+ no: t("no"),
194
+ notSet: t("notSet"),
195
+ dropFile: t("dropFile"),
196
+ browseFiles: t("browseFiles"),
197
+ controls: controlsOf(t),
198
+ issues: ({ code: issue, params, label }) => lookup(locale, `issues.${issue}`, { ...params, ...(label === undefined ? {} : { label }) }),
199
+ missingRecord: (id) => t("missingRecord", { id }),
200
+ addRow: t("addRow"),
201
+ removeRow: (row) => t("removeRow", { row }),
202
+ invalidDuration: t("invalidDuration"),
203
+ errors: errorsOf(t),
204
+ bulkResult: (succeeded, failed) => t("bulkResult", { succeeded, failed }),
205
+ retryFailed: (count) => t("retryFailed", { count }),
206
+ };
207
+ }
208
+ const built = new WeakMap();
209
+ /**
210
+ * The `DataLabels` the blocks read, from a locale's messages. Messages about a resource get its `gender` too
211
+ * (`Gender.feminine`, `"f"`), for a `select`; `{one}` and `{other}` are its name in lower case, `{One}` and `{Other}`
212
+ * as written.
213
+ */
214
+ export function dataLabelsOf(locale, gender = NoGender) {
215
+ let byGender = built.get(locale);
216
+ if (!byGender) {
217
+ byGender = new Map();
218
+ built.set(locale, byGender);
219
+ }
220
+ const known = byGender.get(gender);
221
+ if (known)
222
+ return known;
223
+ const labels = build(locale, gender);
224
+ byGender.set(gender, labels);
225
+ return labels;
226
+ }
227
+ export { Gender };
@@ -0,0 +1,73 @@
1
+ import { type MessageValues } from "./format.js";
2
+ import type { Messages, MessagesInput, StrictMessages } from "./messages.js";
3
+ import type { AnyResource } from "../resource/resource-types.js";
4
+ export type { MessageValues };
5
+ /** Formats the message under `key` with `values`; the key itself when no catalog has it. */
6
+ export type Translate = (key: string, values?: MessageValues) => string;
7
+ /**
8
+ * The language the App SDK speaks: a BCP 47 `code` for `Intl`, and the messages its blocks and the app's resources
9
+ * read. `defineLocale` makes one from JSON catalogs; `fromNextIntl`, `fromI18next` and `fromIntl` from the app's own
10
+ * library.
11
+ */
12
+ export type Locale = {
13
+ readonly code: string;
14
+ readonly t: Translate;
15
+ readonly has: (key: string) => boolean;
16
+ };
17
+ declare const messageRef: unique symbol;
18
+ /** A message of the app's catalog, by key: `msg("orders.cancel")`, resolved with the `DataLayer`'s locale. */
19
+ export type MessageRef = {
20
+ readonly [messageRef]: true;
21
+ readonly key: string;
22
+ readonly values?: MessageValues;
23
+ };
24
+ /**
25
+ * Any user-visible string of a `define…`: the text itself, a message by key (`msg("orders.cancel")`) or a function
26
+ * of the locale's `t`, called at render with the `DataLayer`'s locale.
27
+ */
28
+ export type Text = string | MessageRef | ((t: Translate, locale: Locale | undefined) => string);
29
+ export declare function msg(key: string, values?: MessageValues): MessageRef;
30
+ export declare function isMessageRef(value: unknown): value is MessageRef;
31
+ /** A `Text` as a string in `locale`; without one, a message by key shows its key. */
32
+ export declare function resolveText(text: Text, locale: Locale | undefined): string;
33
+ export declare function resolveText(text: Text | undefined, locale: Locale | undefined): string | undefined;
34
+ /** A refusal whose reason a person reads, so it may be a message: `{ allowed: false, reason: msg("orders.paid") }`. */
35
+ export type TextRefusal = {
36
+ allowed: false;
37
+ reason: Text;
38
+ };
39
+ /** A `can`'s answer with its reason in `locale`. */
40
+ export declare function refusalIn(answer: boolean | TextRefusal | undefined, locale: Locale | undefined): boolean | {
41
+ allowed: false;
42
+ reason: string;
43
+ } | undefined;
44
+ /** The message under `key` when `locale` has it, else `undefined`: how a convention key stays optional. */
45
+ export declare function lookup(locale: Locale | undefined, key: string, values?: MessageValues): string | undefined;
46
+ /** A catalog as JSON holds it: messages nested by key, `{ "controls": { "date": { "clear": "…" } } }`. */
47
+ export type Catalog = {
48
+ readonly [key: string]: string | Catalog;
49
+ };
50
+ /**
51
+ * A nested catalog as flat keys, for a library whose keys are flat (FormatJS):
52
+ * `{ ...flattenCatalog(es, "eskais"), ...mine }`.
53
+ */
54
+ export declare function flattenCatalog(catalog: Catalog, prefix?: string): Record<string, string>;
55
+ export type LocaleOptions = {
56
+ /** Where a key `messages` leaves out comes from: a shipped catalog (`@eskais/app-sdk/locales/es.json`) or a locale. */
57
+ base?: Locale | Catalog;
58
+ messages?: MessagesInput;
59
+ };
60
+ declare function defineLocaleLoose(code: string, options?: LocaleOptions): Locale;
61
+ declare function defineLocaleStrict<const Resources extends readonly AnyResource[]>(code: string, options: {
62
+ resources: Resources;
63
+ messages: StrictMessages<Resources>;
64
+ base?: Locale | Catalog;
65
+ }): Locale;
66
+ /**
67
+ * A locale from ICU catalogs; `messages` override `base` key by key and are checked here. `defineLocale.strict`
68
+ * requires every key by type: each message, and each name, field and enum value of its `resources`.
69
+ */
70
+ export declare const defineLocale: typeof defineLocaleLoose & {
71
+ strict: typeof defineLocaleStrict;
72
+ };
73
+ export type { Messages, MessagesInput, StrictMessages };
@@ -0,0 +1,74 @@
1
+ import { isFunction, isRecord, isString } from "../core/guards.js";
2
+ import { checkMessage, formatMessage } from "./format.js";
3
+ const messageRef = Symbol.for("@eskais/app-sdk/message");
4
+ export function msg(key, values) {
5
+ return Object.freeze({ [messageRef]: true, key, ...(values === undefined ? {} : { values }) });
6
+ }
7
+ export function isMessageRef(value) {
8
+ return isRecord(value) && messageRef in value;
9
+ }
10
+ const keyOnly = (key) => key;
11
+ export function resolveText(text, locale) {
12
+ if (text === undefined || isString(text))
13
+ return text;
14
+ if (isFunction(text))
15
+ return text(locale?.t ?? keyOnly, locale);
16
+ return locale ? locale.t(text.key, text.values) : text.key;
17
+ }
18
+ /** A `can`'s answer with its reason in `locale`. */
19
+ export function refusalIn(answer, locale) {
20
+ if (answer === undefined || answer === true || answer === false)
21
+ return answer;
22
+ return { allowed: false, reason: resolveText(answer.reason, locale) };
23
+ }
24
+ /** The message under `key` when `locale` has it, else `undefined`: how a convention key stays optional. */
25
+ export function lookup(locale, key, values) {
26
+ return locale?.has(key) === true ? locale.t(key, values) : undefined;
27
+ }
28
+ function flatten(catalog, prefix, into) {
29
+ if (!isRecord(catalog))
30
+ return into;
31
+ for (const [key, value] of Object.entries(catalog)) {
32
+ const path = prefix === "" ? key : `${prefix}.${key}`;
33
+ if (isString(value))
34
+ into.set(path, value);
35
+ else
36
+ flatten(value, path, into);
37
+ }
38
+ return into;
39
+ }
40
+ /**
41
+ * A nested catalog as flat keys, for a library whose keys are flat (FormatJS):
42
+ * `{ ...flattenCatalog(es, "eskais"), ...mine }`.
43
+ */
44
+ export function flattenCatalog(catalog, prefix = "") {
45
+ return Object.fromEntries(flatten(catalog, prefix, new Map()));
46
+ }
47
+ function isLocale(value) {
48
+ return isRecord(value) && isString(value.code) && isFunction(value.t) && isFunction(value.has);
49
+ }
50
+ function createLocale(code, messages, from) {
51
+ const own = flatten(messages, "", new Map());
52
+ for (const message of own.values())
53
+ checkMessage(message);
54
+ const base = from === undefined ? undefined : isLocale(from) ? from : createLocale(code, from, undefined);
55
+ const has = (key) => own.has(key) || base?.has(key) === true;
56
+ const t = (key, values) => {
57
+ const message = own.get(key);
58
+ if (message !== undefined)
59
+ return formatMessage(message, values, code);
60
+ return base ? base.t(key, values) : key;
61
+ };
62
+ return Object.freeze({ code, t, has });
63
+ }
64
+ function defineLocaleLoose(code, options = {}) {
65
+ return createLocale(code, options.messages, options.base);
66
+ }
67
+ function defineLocaleStrict(code, options) {
68
+ return createLocale(code, options.messages, options.base);
69
+ }
70
+ /**
71
+ * A locale from ICU catalogs; `messages` override `base` key by key and are checked here. `defineLocale.strict`
72
+ * requires every key by type: each message, and each name, field and enum value of its `resources`.
73
+ */
74
+ export const defineLocale = Object.assign(defineLocaleLoose, { strict: defineLocaleStrict });