@jskit-ai/http-web 0.1.95 → 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 (49) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/locales/en.json +112 -0
  3. package/package.json +8 -7
  4. package/src/client/components/CrudAddEditScreen.vue +10 -4
  5. package/src/client/components/CrudDeleteAction.vue +10 -7
  6. package/src/client/components/CrudListBulkActionSurface.vue +19 -7
  7. package/src/client/components/CrudListDateFilterControl.vue +10 -5
  8. package/src/client/components/CrudListFilterControls.vue +25 -17
  9. package/src/client/components/CrudListFilterSurface.vue +14 -7
  10. package/src/client/components/CrudListRecordActionMenu.vue +17 -5
  11. package/src/client/components/CrudListScreen.vue +31 -18
  12. package/src/client/components/CrudViewScreen.vue +17 -7
  13. package/src/client/composables/crud/crudLookupFieldRuntime.js +4 -1
  14. package/src/client/composables/internal/crudListFilterLookupSupport.js +7 -4
  15. package/src/client/composables/internal/crudListParentTitleSupport.js +6 -3
  16. package/src/client/composables/records/useAddEdit.js +13 -7
  17. package/src/client/composables/records/useCrudList.js +6 -0
  18. package/src/client/composables/records/useCrudView.js +6 -0
  19. package/src/client/composables/records/useList.js +6 -3
  20. package/src/client/composables/records/useView.js +7 -4
  21. package/src/client/composables/runtime/operationValidationHelpers.js +16 -11
  22. package/src/client/composables/runtime/useAddEditCore.js +9 -6
  23. package/src/client/composables/runtime/useCommandCore.js +8 -5
  24. package/src/client/composables/runtime/useEndpointResource.js +7 -5
  25. package/src/client/composables/runtime/useListCore.js +5 -2
  26. package/src/client/composables/runtime/useUiFeedback.js +1 -1
  27. package/src/client/composables/runtime/useViewCore.js +5 -2
  28. package/src/client/composables/support/errorMessageHelpers.js +24 -8
  29. package/src/client/composables/support/listSearchSupport.js +4 -1
  30. package/src/client/composables/useAccess.js +4 -1
  31. package/src/client/composables/useCommand.js +5 -4
  32. package/src/client/composables/useCrudAddEditScreen.js +5 -2
  33. package/src/client/composables/useCrudDeleteAction.js +6 -3
  34. package/src/client/composables/useCrudListFilterLookups.js +6 -3
  35. package/src/client/composables/useCrudListFilters.js +5 -1
  36. package/src/client/composables/useCrudListParentTitle.js +5 -2
  37. package/src/client/composables/useCrudListScreen.js +8 -5
  38. package/src/client/composables/useCrudViewScreen.js +7 -4
  39. package/src/client/composables/usePagedCollection.js +5 -2
  40. package/src/client/composables/useScopeRuntime.js +7 -1
  41. package/src/client/i18n.js +8 -0
  42. package/src/client/support/crudListDateFilterSupport.js +18 -9
  43. package/test/crudListBulkActionSurface.test.js +1 -1
  44. package/test/crudListFilterSurface.test.js +2 -2
  45. package/test/crudScreenComponents.test.js +1 -1
  46. package/test/exportsContract.test.js +3 -1
  47. package/test/i18nCatalog.test.js +16 -0
  48. package/test/translation.test.js +164 -0
  49. package/test/useCrudListScreen.test.js +3 -1
@@ -1,4 +1,12 @@
1
+ import { useJskitText } from "../i18n.js";
2
+
3
+ const { t, text, formatDate } = useJskitText();
1
4
  const DATE_ONLY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/u;
5
+ const DATE_DISPLAY_OPTIONS = Object.freeze({
6
+ year: "numeric",
7
+ month: "short",
8
+ day: "numeric"
9
+ });
2
10
 
3
11
  function parseDateOnlyValue(value = "") {
4
12
  const match = DATE_ONLY_PATTERN.exec(String(value || "").trim());
@@ -51,21 +59,22 @@ function formatDateOnlyDisplay(value = "", { locale = undefined } = {}) {
51
59
  return "";
52
60
  }
53
61
 
62
+ // An explicit locale (the date picker's) wins; otherwise the app's active
63
+ // language, or the browser default when the app has no vue-i18n.
54
64
  try {
55
- return new Intl.DateTimeFormat(locale || undefined, {
56
- year: "numeric",
57
- month: "short",
58
- day: "numeric"
59
- }).format(date);
65
+ return locale
66
+ ? new Intl.DateTimeFormat(locale, DATE_DISPLAY_OPTIONS).format(date)
67
+ : formatDate(date, DATE_DISPLAY_OPTIONS);
60
68
  } catch {
61
69
  return formatDateOnlyValue(date);
62
70
  }
63
71
  }
64
72
 
65
73
  function formatCrudListDateFilterChipLabel(filter = {}, rawValue, { locale = undefined } = {}) {
74
+ const label = text(filter.label);
66
75
  if (filter.type === "date") {
67
76
  const dateLabel = formatDateOnlyDisplay(rawValue, { locale }) || String(rawValue || "");
68
- return dateLabel ? `${filter.label}: ${dateLabel}` : "";
77
+ return dateLabel ? t("jskit.http.crud.filters.chipDate", { label, date: dateLabel }) : "";
69
78
  }
70
79
 
71
80
  if (filter.type !== "dateRange") {
@@ -75,12 +84,12 @@ function formatCrudListDateFilterChipLabel(filter = {}, rawValue, { locale = und
75
84
  const from = formatDateOnlyDisplay(rawValue?.from, { locale }) || String(rawValue?.from || "");
76
85
  const to = formatDateOnlyDisplay(rawValue?.to, { locale }) || String(rawValue?.to || "");
77
86
  if (from && to) {
78
- return `${filter.label}: ${from} to ${to}`;
87
+ return t("jskit.http.crud.filters.chipRange", { label, from, to });
79
88
  }
80
89
  if (from) {
81
- return `${filter.label}: from ${from}`;
90
+ return t("jskit.http.crud.filters.chipFrom", { label, from });
82
91
  }
83
- return to ? `${filter.label}: to ${to}` : "";
92
+ return to ? t("jskit.http.crud.filters.chipTo", { label, to }) : "";
84
93
  }
85
94
 
86
95
  export {
@@ -20,7 +20,7 @@ test("CrudListBulkActionSurface stays adaptive and runtime-owned", async () => {
20
20
  assert.match(source, /hasActions/);
21
21
  assert.match(source, /hasSelection/);
22
22
  assert.match(source, /v-menu/);
23
- assert.match(source, /Bulk actions/);
23
+ assert.match(source, /jskit\.http\.crud\.bulk\.actions/);
24
24
  assert.match(source, /min-height:\s*48px/);
25
25
  assert.doesNotMatch(source, /useCrudList\(/);
26
26
  assert.doesNotMatch(source, /apiSuffix|repository|server/);
@@ -71,8 +71,8 @@ test("CrudListDateFilterControl composes stable Vuetify picker components access
71
71
  assert.match(source, /v-bind="\{ \.\.\.attrs, \.\.\.activatorProps \}"/);
72
72
  assert.match(source, /\["Enter", " ", "ArrowDown"\]/);
73
73
  assert.match(source, /role="dialog"/);
74
- assert.match(source, />\s*Clear\s*</u);
75
- assert.match(source, />\s*Close\s*</u);
74
+ assert.match(source, />\s*\{\{ t\("jskit\.http\.actions\.clear"\) \}\}\s*</u);
75
+ assert.match(source, />\s*\{\{ t\("jskit\.http\.actions\.close"\) \}\}\s*</u);
76
76
  assert.doesNotMatch(source, /VDateInput|vuetify\/labs|type="date"/);
77
77
 
78
78
  const { descriptor, errors: parseErrors } = parse(source, { filename: componentPath });
@@ -95,7 +95,7 @@ test("CRUD screen components own list/view/form chrome centrally", async () => {
95
95
  listSource,
96
96
  /@media \(min-width: 960px\)[\s\S]*\.crud-list-cards\s*\{\s*display:\s*none;[\s\S]*\.crud-list-table\s*\{\s*display:\s*block;[\s\S]*\.crud-list-fab\s*\{\s*display:\s*none;/
97
97
  );
98
- assert.match(listSource, /button-label="More"/);
98
+ assert.match(listSource, /:button-label="t\('jskit\.http\.actions\.more'\)"/);
99
99
  assert.match(listSource, /selectableRows/);
100
100
  assert.match(
101
101
  listSource,
@@ -49,7 +49,9 @@ test("http-web exports are explicit and aligned with production and generated us
49
49
  "./client/crudHttpClient",
50
50
  "./client/lib/httpClient",
51
51
  "./client/lib/permissions",
52
- "./client/support/contractGuards"
52
+ "./client/support/contractGuards",
53
+ // The generated client bootstrap registers every package's English catalog.
54
+ "./locales/en.json"
53
55
  ]
54
56
  });
55
57
 
@@ -0,0 +1,16 @@
1
+ import assert from "node:assert/strict";
2
+ import path from "node:path";
3
+ import test from "node:test";
4
+ import { fileURLToPath } from "node:url";
5
+ import { evaluateI18nCatalogContract } from "../../../tooling/test-support/i18nCatalogContract.mjs";
6
+
7
+ const TEST_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
8
+ const REPO_ROOT = path.resolve(TEST_DIRECTORY, "..", "..", "..");
9
+
10
+ test("http-web English catalog matches its source", () => {
11
+ const { problems } = evaluateI18nCatalogContract({
12
+ repoRoot: REPO_ROOT,
13
+ packageDir: path.join(REPO_ROOT, "packages", "http-web")
14
+ });
15
+ assert.deepEqual(problems, []);
16
+ });
@@ -0,0 +1,164 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { registerHooks } from "node:module";
4
+ import test from "node:test";
5
+ import { compileScript, parse } from "@vue/compiler-sfc";
6
+ import { createSSRApp, defineComponent, h } from "vue";
7
+ import { renderToString } from "vue/server-renderer";
8
+ import { createMemoryHistory, createRouter } from "vue-router";
9
+ import { createI18n } from "vue-i18n";
10
+ import { registerJskitMessages, setJskitI18n } from "@jskit-ai/kernel/client/i18n";
11
+ import { flattenCatalog } from "@jskit-ai/kernel/shared/i18n";
12
+ import en from "../locales/en.json" with { type: "json" };
13
+ import { toUiErrorMessage, toUiFieldErrors } from "../src/client/composables/support/errorMessageHelpers.js";
14
+ import {
15
+ formatCrudListDateFilterChipLabel,
16
+ formatDateOnlyDisplay
17
+ } from "../src/client/support/crudListDateFilterSupport.js";
18
+
19
+ // A server package's catalog, as the generated bootstrap registers it.
20
+ const SERVER_CATALOG = {
21
+ jskit: {
22
+ users: {
23
+ username: {
24
+ taken: "Username {username} is already taken."
25
+ }
26
+ }
27
+ }
28
+ };
29
+ registerJskitMessages(SERVER_CATALOG);
30
+
31
+ // A test language with every http-web key and the server codes above translated.
32
+ function testLanguageMessages() {
33
+ const messages = {};
34
+ const flat = {
35
+ ...flattenCatalog(en),
36
+ ...flattenCatalog(SERVER_CATALOG),
37
+ "jskit.errors.not_found": "Not found."
38
+ };
39
+ for (const [key, message] of Object.entries(flat)) {
40
+ let target = messages;
41
+ const segments = key.split(".");
42
+ for (const segment of segments.slice(0, -1)) {
43
+ target = target[segment] ||= {};
44
+ }
45
+ target[segments.at(-1)] = `[xx] ${message}`;
46
+ }
47
+ return messages;
48
+ }
49
+
50
+ async function withTestLanguage(run, locale = "xx") {
51
+ setJskitI18n(createI18n({ legacy: false, locale, messages: { [locale]: testLanguageMessages() } }));
52
+ try {
53
+ return await run();
54
+ } finally {
55
+ setJskitI18n(null);
56
+ }
57
+ }
58
+
59
+ async function importCrudListScreen() {
60
+ const componentUrl = new URL("../src/client/components/CrudListScreen.vue", import.meta.url).href;
61
+ const hooks = registerHooks({
62
+ load(url, context, nextLoad) {
63
+ if (!url.endsWith(".vue")) return nextLoad(url, context);
64
+ if (url !== componentUrl) {
65
+ return { format: "module", shortCircuit: true, source: "export default { render: () => null };" };
66
+ }
67
+ const { descriptor } = parse(readFileSync(new URL(url), "utf8"));
68
+ return {
69
+ format: "module", shortCircuit: true,
70
+ source: compileScript(descriptor, { id: "crud-list-translation", inlineTemplate: true }).content
71
+ };
72
+ }
73
+ });
74
+ try {
75
+ return (await import(componentUrl)).default;
76
+ } finally {
77
+ hooks.deregister();
78
+ }
79
+ }
80
+
81
+ async function renderCrudListScreen(props) {
82
+ const CrudListScreen = await importCrudListScreen();
83
+ const app = createSSRApp({ render: () => h(CrudListScreen, props) });
84
+ const router = createRouter({
85
+ history: createMemoryHistory(), routes: [{ path: "/books", component: { render: () => null } }]
86
+ });
87
+ await router.push("/books");
88
+ app.use(router);
89
+ for (const name of ["VBtn", "VSheet", "VTable", "VTextField", "VSkeletonLoader", "VCheckboxBtn"]) {
90
+ app.component(name, defineComponent({ setup: (_props, { slots }) => () => h("div", slots.default?.()) }));
91
+ }
92
+ return renderToString(app);
93
+ }
94
+
95
+ test("API errors render through their code only when the server sent JSKIT's English", async () => {
96
+ const coded = { status: 409, message: "Username ada is already taken.", code: "users.username.taken", params: { username: "ada" } };
97
+ const appSentence = { status: 404, message: "Contact not found.", code: "not_found", params: {} };
98
+ const generic = { status: 404, message: "Not found.", code: "not_found", params: {} };
99
+
100
+ assert.equal(toUiErrorMessage(coded, "Unable to save."), "Username ada is already taken.");
101
+ await withTestLanguage(() => {
102
+ assert.equal(toUiErrorMessage(coded, "Unable to save."), "[xx] Username ada is already taken.");
103
+ assert.equal(toUiErrorMessage(appSentence, "Unable to load."), "Contact not found.");
104
+ assert.equal(toUiErrorMessage(generic), "[xx] Not found.");
105
+ // Specific fallback copy still wins over generic transport wording.
106
+ assert.equal(
107
+ toUiErrorMessage({ status: 500, message: "Request failed with status 500." }, "Unable to save."),
108
+ "Unable to save."
109
+ );
110
+ assert.equal(toUiErrorMessage({}), "[xx] Request failed.");
111
+ });
112
+ });
113
+
114
+ test("field errors render through their codes field by field", async () => {
115
+ const error = {
116
+ fieldErrors: { username: "Username ada is already taken.", email: "Use your work email." },
117
+ fieldErrorCodes: {
118
+ username: { code: "users.username.taken", params: { username: "ada" } },
119
+ email: { code: "users.username.taken", params: { username: "ada" } }
120
+ }
121
+ };
122
+
123
+ assert.deepEqual(toUiFieldErrors(error), error.fieldErrors);
124
+ await withTestLanguage(() => {
125
+ assert.deepEqual(toUiFieldErrors(error), {
126
+ username: "[xx] Username ada is already taken.",
127
+ email: "Use your work email."
128
+ });
129
+ });
130
+ });
131
+
132
+ test("CRUD list chrome is English by default and follows the active language", async () => {
133
+ const screen = {
134
+ records: { hasMore: true, loadError: "" },
135
+ listPrimaryAction: "/books/new",
136
+ displayRows: [{ key: "1", recordKey: "1", record: { title: "Kindred" } }],
137
+ resolveRecordTitle: (record) => record.title
138
+ };
139
+
140
+ const english = await renderCrudListScreen({ screen });
141
+ for (const text of ["Refresh", "New record", "Load more"]) {
142
+ assert.match(english, new RegExp(`>${text}<`, "u"));
143
+ }
144
+
145
+ await withTestLanguage(async () => {
146
+ const translated = await renderCrudListScreen({ screen });
147
+ for (const text of ["Refresh", "New record", "Load more"]) {
148
+ assert.match(translated, new RegExp(`>\\[xx\\] ${text}<`, "u"));
149
+ }
150
+ const appKeyed = await renderCrudListScreen({ screen, createLabel: "jskit.http.crud.record" });
151
+ assert.match(appKeyed, />\[xx\] Record</u);
152
+ });
153
+ });
154
+
155
+ test("date filters format with the app's active language", async () => {
156
+ const filter = { type: "dateRange", label: "jskit.http.crud.records" };
157
+ await withTestLanguage(() => {
158
+ assert.equal(formatDateOnlyDisplay("2026-04-18"), "18 Apr 2026");
159
+ assert.equal(
160
+ formatCrudListDateFilterChipLabel(filter, { from: "2026-05-04", to: "2026-05-10" }),
161
+ "[xx] [xx] Records: 4 May 2026 to 10 May 2026"
162
+ );
163
+ }, "en-GB");
164
+ });
@@ -40,6 +40,8 @@ test("list screen link availability is the list runtime's single answer", async
40
40
  assert.equal(linked.hasViewUrl, true);
41
41
  assert.equal(linked.hasEditUrl, true);
42
42
  assert.equal(Object.hasOwn(linked.records, "actionColumnCount"), false);
43
- assert.equal(Object.hasOwn(linked.records, "resolveFieldDisplay"), false);
43
+ // Application templates read field and title display from the list runtime.
44
+ assert.equal(linked.records.resolveFieldDisplay({ title: "Dune" }, "title"), "Dune");
45
+ assert.equal(linked.records.resolveRecordTitle({}, { fallbackKey: "title", defaultValue: "Book" }), "Book");
44
46
  assert.equal(linked.resolveRecordTitle({ name: "Dune" }), "Dune");
45
47
  });