@xeplr/ui-utils 1.1.3 → 1.1.4

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/README.md CHANGED
@@ -521,6 +521,7 @@ await sources.fetchSuggestions('procedureParams', { connectionId, procedure })
521
521
  ```
522
522
 
523
523
  - **A kind nobody configured** is `has() === false`: show a text box. An empty dropdown looks broken.
524
+ - **`where`** keeps only rows with those values, e.g. `{ connectionType: 'api' }`: sent to the server as query values and checked again in the browser.
524
525
  - **`key`** names the list in a hand-written endpoint's answer (default `dataArray`). Guessing it wrong would show an empty list and no error.
525
526
  - **Pure, no React, no login of its own:** the host passes its own fetch.
526
527
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-utils",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "UI utility components: file upload with progress, snackbar, static data (countries/states)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -17,9 +17,19 @@
17
17
  // (@xeplr/ui-account's authFetch), so this package never learns about logins.
18
18
 
19
19
  /**
20
- * @param {object} sources { kind: { url, value?='id', label?, key?='dataArray' } }
20
+ * @param {object} sources { kind: { url, value?='id', label?, key?='dataArray', where? } }
21
21
  * @param {Function} fetchJson (path) → Promise of the parsed answer
22
22
  */
23
+ // `where`: only rows with these values, e.g. { connectionType: 'api' } so a
24
+ // step that calls an API lists API connections and not database ones. Sent as
25
+ // query values, and checked again here for an endpoint that ignores them.
26
+ function whereQuery(src) {
27
+ return Object.entries(src.where || {}).map(([k, v]) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('')
28
+ }
29
+ function matchesWhere(src, row) {
30
+ return Object.entries(src.where || {}).every(([k, v]) => row[k] === v)
31
+ }
32
+
23
33
  export function createOptionSources(sources, fetchJson) {
24
34
  const all = sources || {}
25
35
 
@@ -35,13 +45,14 @@ export function createOptionSources(sources, fetchJson) {
35
45
  async function fetchOptions(kind, filter) {
36
46
  const src = all[kind]
37
47
  if (!src) return []
38
- const q = filter && filter.value ? `&${encodeURIComponent(filter.name)}=${encodeURIComponent(filter.value)}` : ''
48
+ const q = (filter && filter.value ? `&${encodeURIComponent(filter.name)}=${encodeURIComponent(filter.value)}` : '') + whereQuery(src)
39
49
  const res = await fetchJson(`${src.url}?limit=0${q}`)
40
50
  // `dataArray` is what a generic route answers; a hand-written endpoint
41
51
  // answers with its own key, which the host names. Guessing wrong would
42
52
  // give an empty list and no error, which reads as "you have none".
43
53
  const rows = (res && res[src.key || 'dataArray']) || []
44
54
  return rows
55
+ .filter((r) => matchesWhere(src, r))
45
56
  .filter((r) => (filter && filter.clientKey ? r[filter.clientKey] === filter.value : true))
46
57
  .map((r) => ({ value: r[src.value || 'id'], label: r[src.label] || r[src.value || 'id'] }))
47
58
  }