@jskit-ai/http-web 0.2.37 → 0.2.39
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- A `useCommand()` run refused because the person lacks its `runPermissions` (`canRun` false) is no longer silent: it shows, through the command's feedback (its `message` and `messageType`, and the shell's action feedback), the notice the server's 403 for the same request shows (`ACTION_PERMISSION_DENIED`, "Forbidden." in the person's language). It still sends nothing and resolves `null`, so callers are unchanged, and `onRunError` does not run, as nothing was sent. Before, `run()` answered null with no request and no notice, so a tap did nothing visible. A run while the command is already running still answers null without a notice.
|
|
5
6
|
- A list's, a record's or a form's load notice offers Retry only when trying again can help: after a network failure (no status), a timeout (408), a rate limit (429) or a server error (5xx). Before, it offered Retry on every failed load, also when the server had refused it (401, 403, 404, 422 and the other 4xx), where Retry only failed again; such a notice now has no action. One helper decides it for `useView()`, `useList()` and `useAddEdit()` (whose notice still offers nothing while `readEnabled` is false, and whose `canRetryLoad` and `refresh()` are unchanged).
|
|
6
7
|
- `CrudListScreen`, `CrudViewScreen` and `CrudAddEditScreen` show their own Retry button under the same rule, so a refused load shows no Retry anywhere on the screen, and the three say the same thing about a refusal: the server's own message when the error carries one, otherwise a neutral text for its kind ("Sign in again to see this." for a 401, "You don't have access to this." for a 403, "This wasn't found." for a 404, "This couldn't be loaded." for another 4xx). Before, the button showed after every failed load, also next to a notice that offered none; the list screen said "Check the connection and try again." after a refusal too, and the record and form screens said "Unable to load resource." when the server sent no message. A failure trying again can help keeps its text: the list screen's `loadErrorBody` (by default "Check the connection and try again."), now shown only then, and the record and form screens' `loadError`. A runtime without `errorStatus`, such as one a page builds itself, keeps its Retry button and text as before; the list screen shows the `loadError` of a refusal from a runtime without `loadRefusalMessage`.
|
|
7
8
|
- `useList()`, and so `useCrudList()`, and `useAddEdit()` expose the failed load's HTTP status as `errorStatus`, 0 when it has none (a network failure), as `useView()` already did, so a screen can tell a refusal from a network failure. The three runtimes also expose `loadRefusalMessage`, the text above for a refused load and empty when trying again can help, which the CRUD screens show. `loadError` and the load notice's text are unchanged.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/http-web",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.39",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"./locales/en.json": "./locales/en.json"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
50
|
+
"@jskit-ai/resource-crud-core": "0.1.216"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
54
|
-
"@jskit-ai/kernel": "0.2.
|
|
55
|
-
"@jskit-ai/realtime": "0.2.
|
|
56
|
-
"@jskit-ai/shell-web": "0.2.
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.23",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.23",
|
|
55
|
+
"@jskit-ai/realtime": "0.2.35",
|
|
56
|
+
"@jskit-ai/shell-web": "0.2.32",
|
|
57
57
|
"@tanstack/vue-query": "^5.90.5",
|
|
58
58
|
"vue": "^3.5.13",
|
|
59
59
|
"vue-router": "^5.0.4",
|
|
@@ -2,10 +2,18 @@ import { useQueryClient } from "@tanstack/vue-query";
|
|
|
2
2
|
import { validateOperationInput } from "./operationValidationHelpers.js";
|
|
3
3
|
import { toUiFieldErrors } from "../support/errorMessageHelpers.js";
|
|
4
4
|
import { emptyRecord } from "../support/scopeHelpers.js";
|
|
5
|
-
import { useJskitText } from "../../i18n.js";
|
|
5
|
+
import { createTextError, useJskitText } from "../../i18n.js";
|
|
6
6
|
|
|
7
7
|
const { t, text } = useJskitText();
|
|
8
8
|
|
|
9
|
+
// The error the server answers a request the person's permissions do not allow, for a run refused before it sends one.
|
|
10
|
+
function permissionRefusal() {
|
|
11
|
+
return Object.assign(createTextError("jskit.errors.ACTION_PERMISSION_DENIED"), {
|
|
12
|
+
status: 403,
|
|
13
|
+
code: "ACTION_PERMISSION_DENIED"
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
/** @param {import("../../types.js").CommandCoreOptions} options */
|
|
10
18
|
function useCommandCore({
|
|
11
19
|
model,
|
|
@@ -36,13 +44,20 @@ function useCommandCore({
|
|
|
36
44
|
const messageType = feedback.messageType;
|
|
37
45
|
|
|
38
46
|
async function run(context = {}) {
|
|
39
|
-
if (
|
|
47
|
+
if (running.value) {
|
|
40
48
|
return null;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
feedback.clear();
|
|
44
52
|
fieldBag.clear();
|
|
45
53
|
|
|
54
|
+
// The person's permissions do not allow the command, so it sends nothing and says so through its feedback, as the
|
|
55
|
+
// server's refusal of the request would. It still answers null, and onRunError does not run: nothing was sent.
|
|
56
|
+
if (!canRun.value) {
|
|
57
|
+
feedback.error(permissionRefusal(), text(messages.error) || t("jskit.http.feedback.actionFailed"));
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
46
61
|
const rawPayload = readRawPayload(model, {
|
|
47
62
|
queryClient,
|
|
48
63
|
resource,
|
package/src/client/types.js
CHANGED
|
@@ -311,7 +311,9 @@
|
|
|
311
311
|
* @property {string} [surfaceId]
|
|
312
312
|
* @property {string} [access]
|
|
313
313
|
* @property {ApiSuffix} [apiSuffix]
|
|
314
|
-
* @property {string | string[]} [runPermissions]
|
|
314
|
+
* @property {string | string[]} [runPermissions] The permissions, any one of which lets the person run the command.
|
|
315
|
+
* Without one, `canRun` is false and `run()` sends nothing: it shows the server's 403 notice through the command's
|
|
316
|
+
* feedback and resolves null.
|
|
315
317
|
* @property {string} [writeMethod]
|
|
316
318
|
* @property {HttpClient} [client]
|
|
317
319
|
* @property {CrudResource | null} [resource]
|
|
@@ -203,3 +203,13 @@ test("CrudListScreen gives a refusal from a runtime without loadRefusalMessage i
|
|
|
203
203
|
const { text } = await renderLoadError(SCREENS.list(403), screens.list);
|
|
204
204
|
assert.equal(text, "Unable to load books.");
|
|
205
205
|
});
|
|
206
|
+
|
|
207
|
+
// crud-core's 400 for a query key the list does not take (crud.query.undeclaredKeys): the list shows the server's
|
|
208
|
+
// plain sentence, which useCrudList gives as its loadRefusalMessage, and no Retry button.
|
|
209
|
+
test("CrudListScreen shows the plain sentence and no Retry after a 400 for a query key the list does not take", async () => {
|
|
210
|
+
const sentence = "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; " +
|
|
211
|
+
"if it keeps happening, tell support.";
|
|
212
|
+
const { text, retry } = await renderLoadError(SCREENS.list(400, { loadRefusalMessage: sentence }), screens.list);
|
|
213
|
+
assert.equal(text, sentence);
|
|
214
|
+
assert.equal(retry, false);
|
|
215
|
+
});
|
|
@@ -58,9 +58,12 @@ async function waitFor(check, message) {
|
|
|
58
58
|
assert.fail(message);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
// Mounts one runtime whose every read fails with `status` (and `message`, by default one of the server's own
|
|
62
|
-
// records what it reports to the shell.
|
|
63
|
-
async function mountFailingLoad(t, status, useRuntime, {
|
|
61
|
+
// Mounts one runtime whose every read fails with `status` (and `message`, by default one of the server's own, and the
|
|
62
|
+
// server's `code` when given), and records what it reports to the shell.
|
|
63
|
+
async function mountFailingLoad(t, status, useRuntime, {
|
|
64
|
+
message = `Contacts refused the read (${describeStatus(status)}).`,
|
|
65
|
+
code
|
|
66
|
+
} = {}) {
|
|
64
67
|
const requests = [];
|
|
65
68
|
const client = {
|
|
66
69
|
async request(path) {
|
|
@@ -69,6 +72,9 @@ async function mountFailingLoad(t, status, useRuntime, { message = `Contacts ref
|
|
|
69
72
|
if (status !== undefined) {
|
|
70
73
|
error.status = status;
|
|
71
74
|
}
|
|
75
|
+
if (code) {
|
|
76
|
+
error.code = code;
|
|
77
|
+
}
|
|
72
78
|
throw error;
|
|
73
79
|
}
|
|
74
80
|
};
|
|
@@ -247,3 +253,18 @@ for (const [name, useRuntime] of Object.entries(REFUSAL_RUNTIMES)) {
|
|
|
247
253
|
});
|
|
248
254
|
}
|
|
249
255
|
}
|
|
256
|
+
|
|
257
|
+
// A list that asks for a query key it does not take (crud-core's 400 crud.query.undeclaredKeys): the person reads the
|
|
258
|
+
// server's plain sentence and is offered no Retry, since the same page asks for the same key again.
|
|
259
|
+
test("useCrudList: a 400 for a query key the list does not take says the server's plain sentence and offers no Retry", async (t) => {
|
|
260
|
+
const sentence = "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; " +
|
|
261
|
+
"if it keeps happening, tell support.";
|
|
262
|
+
const mounted = await mountFailingLoad(t, 400, (client) => useCrudList(listOptions(client)), {
|
|
263
|
+
message: sentence,
|
|
264
|
+
code: "crud.query.undeclaredKeys"
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
assertRetryOffer(mounted, { status: 400, retry: false });
|
|
268
|
+
assert.equal(mounted.runtime.errorStatus, 400);
|
|
269
|
+
assert.equal(mounted.runtime.loadRefusalMessage, sentence);
|
|
270
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
|
|
4
|
+
import { createSSRApp, h } from "vue";
|
|
5
|
+
import { renderToString } from "vue/server-renderer";
|
|
6
|
+
import { routeLocationKey } from "vue-router";
|
|
7
|
+
import { SHELL_WEB_ERROR_RUNTIME_KEY } from "@jskit-ai/shell-web/client/error";
|
|
8
|
+
import { SHELL_WEB_PLACEMENT_RUNTIME_KEY } from "@jskit-ai/shell-web/client/placement";
|
|
9
|
+
import { useCommand } from "../src/client/composables/useCommand.js";
|
|
10
|
+
import { configureHttpWebClient } from "../src/client/lib/httpClient.js";
|
|
11
|
+
|
|
12
|
+
test.afterEach(() => {
|
|
13
|
+
configureHttpWebClient();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// A command that needs orders.pay, on a page whose placement context grants `permissions`, against a server that
|
|
17
|
+
// refuses every request it is sent as an action permission denial. Answers the command, the requests it sent and the
|
|
18
|
+
// notices its feedback showed through the shell.
|
|
19
|
+
async function payOrderCommand(permissions) {
|
|
20
|
+
const requests = [];
|
|
21
|
+
const notices = [];
|
|
22
|
+
configureHttpWebClient({
|
|
23
|
+
fetchImpl: async (url) => {
|
|
24
|
+
requests.push(url);
|
|
25
|
+
return {
|
|
26
|
+
ok: false,
|
|
27
|
+
status: 403,
|
|
28
|
+
headers: { get: (name) => (String(name).toLowerCase() === "content-type" ? "application/json" : "") },
|
|
29
|
+
json: async () => ({ error: "Forbidden.", code: "ACTION_PERMISSION_DENIED", params: {} })
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const placementContext = { permissions };
|
|
34
|
+
let command = null;
|
|
35
|
+
const app = createSSRApp({
|
|
36
|
+
setup() {
|
|
37
|
+
command = useCommand({
|
|
38
|
+
access: "always",
|
|
39
|
+
apiSuffix: "/orders/7/pay",
|
|
40
|
+
runPermissions: ["orders.pay"],
|
|
41
|
+
resource: { apiAccess: "public" },
|
|
42
|
+
messages: { error: "Unable to pay the order." }
|
|
43
|
+
});
|
|
44
|
+
return () => h("div");
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
app.provide(routeLocationKey, { path: "/" });
|
|
48
|
+
app.provide(SHELL_WEB_PLACEMENT_RUNTIME_KEY, {
|
|
49
|
+
getContext: () => placementContext,
|
|
50
|
+
getPlacements: () => [],
|
|
51
|
+
setContext: () => placementContext,
|
|
52
|
+
subscribe: () => () => {},
|
|
53
|
+
getRevision: () => 0
|
|
54
|
+
});
|
|
55
|
+
app.provide(SHELL_WEB_ERROR_RUNTIME_KEY, {
|
|
56
|
+
report({ source, message, intent, severity }) {
|
|
57
|
+
notices.push({ source, message, intent, severity });
|
|
58
|
+
return {};
|
|
59
|
+
},
|
|
60
|
+
dismiss() {}
|
|
61
|
+
});
|
|
62
|
+
app.use(VueQueryPlugin, { queryClient: new QueryClient() });
|
|
63
|
+
await renderToString(app);
|
|
64
|
+
return { command, requests, notices };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Before, run() answered null with no request and no notice, so a tap on the command did nothing visible.
|
|
68
|
+
test("a command the person's permissions do not allow sends nothing and shows the notice the server's 403 shows", async () => {
|
|
69
|
+
const refused = await payOrderCommand(["orders.view"]);
|
|
70
|
+
assert.equal(refused.command.canRun, false);
|
|
71
|
+
assert.equal(await refused.command.run(), null, "A refused run still answers null.");
|
|
72
|
+
assert.deepEqual(refused.requests, [], "Nothing is sent.");
|
|
73
|
+
|
|
74
|
+
const answered = await payOrderCommand(["orders.pay"]);
|
|
75
|
+
await assert.rejects(answered.command.run(), (error) => error.status === 403);
|
|
76
|
+
assert.equal(answered.requests.length, 1);
|
|
77
|
+
|
|
78
|
+
assert.deepEqual(refused.notices, [{
|
|
79
|
+
source: "http-web.command.feedback",
|
|
80
|
+
message: "Forbidden.",
|
|
81
|
+
intent: "action-feedback",
|
|
82
|
+
severity: "error"
|
|
83
|
+
}]);
|
|
84
|
+
assert.deepEqual(refused.notices, answered.notices);
|
|
85
|
+
assert.equal(refused.command.messageType, "error");
|
|
86
|
+
assert.equal(refused.command.message, answered.command.message);
|
|
87
|
+
});
|