@jskit-ai/crud-core 0.2.32 → 0.2.33

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 CRUD list's 400 for a query key it does not take (`crud.query.undeclaredKeys`) tells the person "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; if it keeps happening, tell support.", which is also the code's catalog sentence, so a client shows it in the person's language. Before, it sent the developer's text ("This list does not take …: … add it to the feature's listFilterQueryValidator …"), which a list screen showed as its notice. That text now goes to the error's `details` (`reason`, with the `unknown` and `known` keys) and to the server log as a warning; the error has no `params`. A list screen offers no Retry for it, as for every refused load.
5
6
  - Depends on json-rest-api 3.1.7 exactly (from 3.1.6): an aggregate over one table, with no filter or row policy joining another, reads only the rows it groups, through the filters' index, instead of the table's rows whose ids its filtered query selects (details in `@jskit-ai/resource-runtime`'s changelog). Results are unchanged.
6
7
  - Depends on json-rest-api 3.1.6 exactly (from 3.1.5): within one transaction a link check, or a response's check of a to-one link, of a row already checked in the same scope under a lock at least as strong is answered without reading the row again, until the transaction changes what the check read (the counts are in `@jskit-ai/resource-runtime`'s changelog).
7
8
  - Depends on json-rest-api 3.1.5 exactly (from 3.1.4), from the registry with its published integrity: a read-only builder (a `scopedQuery()`, a join source, a row policy's `ownTable()`) that json-rest-api's own query compiles as a subquery keeps its joins, where 3.1.4 dropped them and the subquery matched more rows than it should (a row policy or filter narrowing with `whereExists` over a joined subquery widened), and `lockByIds`/`lockWhere` take `lockMode: 'share'`.
package/locales/en.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "fieldsKeysInvalid": "fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}.",
12
12
  "fieldsKeysInvalidWithAliases": "fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}. Relationship aliases are invalid keys; {aliasGuidance}.",
13
13
  "fieldsNotShown": "fields[{type}] may name only what this read's includes show of {type}: {shown}. It names {fields}.",
14
- "undeclaredKeys": "This list does not take {unknown}: an undeclared query key would be dropped, and the list would answer as if it had not been sent. It takes {known}. To filter on a field, add it to the feature's listFilterQueryValidator and the resource's searchSchema; to search, send filter[q] and give the resource searchSchema.q."
14
+ "undeclaredKeys": "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; if it keeps happening, tell support."
15
15
  }
16
16
  }
17
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/crud-core",
3
- "version": "0.2.32",
3
+ "version": "0.2.33",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
@@ -9,7 +9,7 @@
9
9
  "./shared": "./src/shared/index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@jskit-ai/crud-core": "0.2.32",
12
+ "@jskit-ai/crud-core": "0.2.33",
13
13
  "@jskit-ai/resource-core": "0.1.213",
14
14
  "@jskit-ai/resource-crud-core": "0.1.215"
15
15
  },
@@ -23,7 +23,9 @@ function buildScopeInput(scopeInput, request) {
23
23
  * Refuses a list request that names a query key its route does not take. Fastify's validation drops an undeclared
24
24
  * query key without a word, so a filter the list does not know (`filter[status]`) would answer every row, as if it
25
25
  * were never sent: the keys the request names that validation left out of request.query are the undeclared ones.
26
- * @param {{ url?: string, query?: object, routeOptions?: { schema?: { querystring?: { properties?: object } } } }} request
26
+ * The key comes from the page's code, not from anything the person did, so the refusal tells them only that nothing
27
+ * was loaded; what a developer needs to fix it goes to the error's details and the server log.
28
+ * @param {{ url?: string, query?: object, routeOptions?: { schema?: { querystring?: { properties?: object } } }, log: { warn: (fields: object, message: string) => void } }} request
27
29
  */
28
30
  function refuseUndeclaredListQuery(request) {
29
31
  const url = String(request.url || "");
@@ -40,12 +42,16 @@ function refuseUndeclaredListQuery(request) {
40
42
  // A sparse fieldset key is taken by pattern, one per JSON:API type: `fields[<type>]`.
41
43
  const patterns = Object.keys(querystring.patternProperties || {}).map((pattern) => (pattern.startsWith("^fields") ? "fields[<type>]" : pattern));
42
44
  const known = [...Object.keys(querystring.properties || {}), ...patterns].join(", ") || "none";
43
- const params = { unknown: unknown.join(", "), known };
45
+ const reason =
46
+ `This list does not take ${unknown.join(", ")}: an undeclared query key would be dropped, and the list would answer ` +
47
+ `as if it had not been sent. It takes ${known}. To filter on a field, add it to the feature's ` +
48
+ "listFilterQueryValidator and the resource's searchSchema; to search, send filter[q] and give the resource " +
49
+ "searchSchema.q.";
50
+ request.log.warn({ code: "crud.query.undeclaredKeys", unknown, known }, reason);
44
51
  throw new AppError(400,
45
- `This list does not take ${params.unknown}: an undeclared query key would be dropped, and the list would answer as ` +
46
- `if it had not been sent. It takes ${known}. To filter on a field, add it to the feature's listFilterQueryValidator ` +
47
- "and the resource's searchSchema; to search, send filter[q] and give the resource searchSchema.q.",
48
- { code: "crud.query.undeclaredKeys", params });
52
+ "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; if it keeps " +
53
+ "happening, tell support.",
54
+ { code: "crud.query.undeclaredKeys", details: { reason, unknown, known } });
49
55
  }
50
56
 
51
57
  function buildCustomActionInput(scopeInput, route, request) {
@@ -36,7 +36,9 @@ async function listApp(t) {
36
36
  access: "public",
37
37
  operations: ["list"]
38
38
  }));
39
- const app = Fastify();
39
+ // The server's warnings, as the lines its logger writes.
40
+ const warnings = [];
41
+ const app = Fastify({ logger: { level: "warn", stream: { write: (line) => warnings.push(JSON.parse(line)) } } });
40
42
  t.after(() => app.close());
41
43
  registerApiErrorHandler(app);
42
44
  const listed = [];
@@ -50,11 +52,11 @@ async function listApp(t) {
50
52
  }
51
53
  }
52
54
  });
53
- return { app, listed };
55
+ return { app, listed, warnings };
54
56
  }
55
57
 
56
58
  test("a CRUD list refuses a query key it does not take, which Fastify's validation would drop unseen", async (t) => {
57
- const { app, listed } = await listApp(t);
59
+ const { app, listed, warnings } = await listApp(t);
58
60
 
59
61
  const searched = await app.inject({ method: "GET", url: "/api/notes?filter%5Bq%5D=Ada" });
60
62
  assert.equal(searched.statusCode, 200, searched.body);
@@ -69,16 +71,26 @@ test("a CRUD list refuses a query key it does not take, which Fastify's validati
69
71
  assert.equal(listed.length, 2);
70
72
 
71
73
  // Before, both answered every row: validation dropped the filter and the list never saw it.
72
- for (const url of ["/api/notes?filter%5Bstatus%5D=open", "/api/notes?filter%5Bq%5D=Ada&status=open"]) {
74
+ const english = mergeCatalogs(kernelEnglish, crudEnglish);
75
+ for (const [url, unknownKey] of [["/api/notes?filter%5Bstatus%5D=open", "filter[status]"], ["/api/notes?filter%5Bq%5D=Ada&status=open", "status"]]) {
76
+ warnings.length = 0;
73
77
  const response = await app.inject({ method: "GET", url });
74
78
  assert.equal(response.statusCode, 400, url);
75
79
  const [error] = response.json().errors;
76
80
  assert.equal(error.code, "crud.query.undeclaredKeys", url);
77
- assert.match(error.title, /does not take (filter\[status\]|status):/u, url);
78
- assert.match(error.title, /It takes .*filter\[q\].*fields\[<type>\]/u, url);
79
- // The client shows the code's sentence in the user's language: the English sent is the catalog's.
80
- const english = mergeCatalogs(kernelEnglish, crudEnglish);
81
- assert.equal(formatMessage(catalogMessage(english, messageKeyForCode(error.code)), error.meta.params), error.title, url);
81
+ // The person reads a plain sentence; the client shows the code's sentence in their language, as the English sent
82
+ // is the catalog's. Before, it was the developer's text below, which a list screen showed as its notice.
83
+ assert.equal(error.title,
84
+ "This page asked for something the server doesn't recognise, so nothing was loaded. Reload the page; if it " +
85
+ "keeps happening, tell support.", url);
86
+ assert.equal(formatMessage(catalogMessage(english, messageKeyForCode(error.code)), {}), error.title, url);
87
+ assert.doesNotMatch(response.body, /listFilterQueryValidator|does not take/u, url);
88
+ // What a developer needs to fix it is in the server log.
89
+ assert.equal(warnings.length, 1, url);
90
+ assert.equal(warnings[0].code, "crud.query.undeclaredKeys", url);
91
+ assert.deepEqual(warnings[0].unknown, [unknownKey], url);
92
+ assert.match(warnings[0].msg, new RegExp(`does not take ${unknownKey.replace(/[[\]]/gu, "\\$&")}:`, "u"), url);
93
+ assert.match(warnings[0].msg, /It takes .*filter\[q\].*fields\[<type>\].*listFilterQueryValidator/u, url);
82
94
  }
83
95
  assert.equal(listed.length, 2);
84
96
  });