@jskit-ai/http-web 0.1.96 → 0.2.1

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 (56) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/fixtures/select-field/App.vue +56 -0
  3. package/fixtures/select-field/index.html +12 -0
  4. package/fixtures/select-field/main.js +24 -0
  5. package/fixtures/select-field/vite.config.mjs +15 -0
  6. package/locales/en.json +115 -0
  7. package/package.json +13 -7
  8. package/patterns/crud-screen-set/PATTERN.md +46 -0
  9. package/src/client/components/CrudAddEditScreen.vue +10 -4
  10. package/src/client/components/CrudDeleteAction.vue +10 -7
  11. package/src/client/components/CrudListBulkActionSurface.vue +19 -7
  12. package/src/client/components/CrudListDateFilterControl.vue +10 -5
  13. package/src/client/components/CrudListFilterControls.vue +25 -17
  14. package/src/client/components/CrudListFilterSurface.vue +14 -7
  15. package/src/client/components/CrudListRecordActionMenu.vue +17 -5
  16. package/src/client/components/CrudListScreen.vue +31 -18
  17. package/src/client/components/CrudViewScreen.vue +17 -7
  18. package/src/client/components/SelectField.vue +55 -0
  19. package/src/client/composables/crud/crudLookupFieldRuntime.js +14 -29
  20. package/src/client/composables/internal/crudListFilterLookupSupport.js +7 -4
  21. package/src/client/composables/internal/crudListParentTitleSupport.js +6 -3
  22. package/src/client/composables/records/useAddEdit.js +13 -7
  23. package/src/client/composables/records/useList.js +6 -3
  24. package/src/client/composables/records/useView.js +7 -4
  25. package/src/client/composables/runtime/operationValidationHelpers.js +16 -11
  26. package/src/client/composables/runtime/useAddEditCore.js +9 -6
  27. package/src/client/composables/runtime/useCommandCore.js +8 -5
  28. package/src/client/composables/runtime/useEndpointResource.js +7 -5
  29. package/src/client/composables/runtime/useListCore.js +5 -2
  30. package/src/client/composables/runtime/useUiFeedback.js +1 -1
  31. package/src/client/composables/runtime/useViewCore.js +5 -2
  32. package/src/client/composables/support/errorMessageHelpers.js +24 -8
  33. package/src/client/composables/support/listSearchSupport.js +4 -1
  34. package/src/client/composables/useAccess.js +4 -1
  35. package/src/client/composables/useCommand.js +5 -4
  36. package/src/client/composables/useCrudAddEditScreen.js +5 -2
  37. package/src/client/composables/useCrudDeleteAction.js +6 -3
  38. package/src/client/composables/useCrudListFilterLookups.js +6 -3
  39. package/src/client/composables/useCrudListFilters.js +5 -1
  40. package/src/client/composables/useCrudListParentTitle.js +5 -2
  41. package/src/client/composables/useCrudListScreen.js +8 -5
  42. package/src/client/composables/useCrudViewScreen.js +7 -4
  43. package/src/client/composables/usePagedCollection.js +5 -2
  44. package/src/client/composables/useScopeRuntime.js +7 -1
  45. package/src/client/i18n.js +8 -0
  46. package/src/client/support/crudListDateFilterSupport.js +18 -9
  47. package/src/client/support/selectFieldSupport.js +71 -0
  48. package/test/crudListBulkActionSurface.test.js +1 -1
  49. package/test/crudListFilterSurface.test.js +2 -2
  50. package/test/crudLookupFieldRuntime.test.js +42 -0
  51. package/test/crudScreenComponents.test.js +1 -1
  52. package/test/exportsContract.test.js +4 -1
  53. package/test/i18nCatalog.test.js +16 -0
  54. package/test/selectField.browser.test.js +63 -0
  55. package/test/selectFieldSupport.test.js +68 -0
  56. package/test/translation.test.js +164 -0
@@ -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
+ });