@talelk/newui-data 0.0.0-stage → 1.0.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 (47) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +54 -2
  3. package/package.json +54 -3
  4. package/skills/newui-data/SKILL.md +116 -0
  5. package/src/download.test.ts +14 -0
  6. package/src/download.ts +39 -0
  7. package/src/exports.test.ts +105 -0
  8. package/src/form/form.test.tsx +102 -0
  9. package/src/form/form.tsx +85 -0
  10. package/src/form/index.ts +1 -0
  11. package/src/format.test.ts +22 -0
  12. package/src/format.ts +36 -0
  13. package/src/index.ts +4 -0
  14. package/src/query/client.tsx +53 -0
  15. package/src/query/collection.test.tsx +151 -0
  16. package/src/query/collection.ts +192 -0
  17. package/src/query/index.ts +3 -0
  18. package/src/query/repository.ts +30 -0
  19. package/src/resource/client.test.ts +173 -0
  20. package/src/resource/client.ts +307 -0
  21. package/src/resource/document.test.ts +151 -0
  22. package/src/resource/document.ts +198 -0
  23. package/src/resource/idb.test.ts +169 -0
  24. package/src/resource/idb.ts +167 -0
  25. package/src/resource/index.ts +70 -0
  26. package/src/resource/list.test.ts +150 -0
  27. package/src/resource/list.ts +230 -0
  28. package/src/resource/local.test.ts +111 -0
  29. package/src/resource/local.ts +155 -0
  30. package/src/resource/realtime.test.ts +331 -0
  31. package/src/resource/realtime.ts +287 -0
  32. package/src/resource/resource.test.ts +186 -0
  33. package/src/resource/resource.ts +213 -0
  34. package/src/resource/useCall.test.ts +60 -0
  35. package/src/resource/useCall.ts +74 -0
  36. package/src/resource/useDoc.test.ts +31 -0
  37. package/src/resource/useDoc.ts +23 -0
  38. package/src/resource/useDoctype.test.ts +104 -0
  39. package/src/resource/useDoctype.ts +128 -0
  40. package/src/resource/useList.test.ts +32 -0
  41. package/src/resource/useList.ts +22 -0
  42. package/src/resource/useNewDoc.test.ts +67 -0
  43. package/src/resource/useNewDoc.ts +78 -0
  44. package/src/select.test.ts +13 -0
  45. package/src/select.ts +19 -0
  46. package/src/undo.test.ts +24 -0
  47. package/src/undo.ts +37 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,55 @@
1
- # Temporary Holding Version
1
+ # @talelk/newui-data
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The logic layer: the things every app does the same way and no two agree on
4
+ when they write them themselves.
5
+
6
+ ```ts
7
+ import { createUndo, downloadCsv, usd, optionsOf } from '@talelk/newui-data'
8
+ ```
9
+
10
+ The package is **headless** — it imports `solid-js` for signals and nothing from
11
+ the visual packages, so a page can use it without paying for a component, and a
12
+ component can never reach up into it. What renders stays in the atomic packages;
13
+ this level only answers *how it behaves*.
14
+
15
+ The first four surfaces are the ones the examples had already copied:
16
+
17
+ | surface | what it owns |
18
+ | --- | --- |
19
+ | `format` | `usd`, `usdK`, `count`, `bytes`, `isEmail` — one `Intl` formatter each, built once |
20
+ | `select` | `optionsOf`, `first` — the `{ label, value }` plumbing and the empty-array unwrap |
21
+ | `undo` | `createUndo` — a snapshot, a restore and a clear, without the page's notice copy |
22
+ | `download` | `downloadBlob`, `downloadText`, `downloadCsv`, `csvText` — the Blob/anchor/revoke dance and RFC-shaped quoting |
23
+
24
+ ## Query
25
+
26
+ ```tsx
27
+ import { DataProvider, createCollection } from '@talelk/newui-data/query'
28
+
29
+ const tasks = createCollection(() => ({
30
+ key: ['tasks'],
31
+ repository, // list / create / update / remove
32
+ }))
33
+
34
+ tasks.query.rows()
35
+ tasks.update.mutate({ id, patch }) // optimistic, reversible
36
+ ```
37
+
38
+ The query surface is its own entry point, so importing `usd` never pulls a
39
+ cache in. `DataProvider` owns the client (and clears it on unmount);
40
+ `createCollection` returns one list query and four mutations, each refreshing
41
+ the list on settle so a mutation and the Refresh button cannot disagree.
42
+
43
+ TanStack Query and Form are dependencies of this package; the app does not
44
+ depend on them directly. Rendered `DataTable` and `VirtualList` are organisms
45
+ and `ScrollTable` is a molecule. This package supplies their headless data
46
+ behavior, not their rendered surface; `spec/data.md` is the contract for what
47
+ has shipped.
48
+
49
+ ## Resource — `@talelk/newui-data/resource`
50
+
51
+ The Frappe DocType client: `request`/`frappeRequest` transport with
52
+ `setConfig`, document and list hooks (`useDoc`, `useDoctype`, `useList`,
53
+ `useNewDoc`, `useCall`), an IndexedDB cache, and a shared WebSocket realtime
54
+ multiplexer. It lives behind its own entry so importing `usd` never pulls a
55
+ fetch stack, and it is app-specific — generic query needs belong on `/query`.
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@talelk/newui-data",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "The logic layer: formatting, selection, undo and downloads — no components.",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/talelk/newui.git",
9
+ "directory": "packages/data"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./src/index.ts",
14
+ "default": "./src/index.ts"
15
+ },
16
+ "./query": {
17
+ "types": "./src/query/index.ts",
18
+ "default": "./src/query/index.ts"
19
+ },
20
+ "./form": {
21
+ "types": "./src/form/index.ts",
22
+ "default": "./src/form/index.ts"
23
+ },
24
+ "./resource": {
25
+ "types": "./src/resource/index.ts",
26
+ "default": "./src/resource/index.ts"
27
+ }
28
+ },
29
+ "files": [
30
+ "src",
31
+ "skills"
32
+ ],
33
+ "keywords": [
34
+ "tanstack-intent",
35
+ "agent-skills",
36
+ "solidjs",
37
+ "data"
38
+ ],
39
+ "peerDependencies": {
40
+ "solid-js": "^1.9.0"
41
+ },
42
+ "devDependencies": {
43
+ "solid-js": "^1.9.0"
44
+ },
45
+ "sideEffects": false,
46
+ "dependencies": {
47
+ "@tanstack/solid-form": "^1.33.5",
48
+ "@tanstack/solid-query": "^5.104.3"
49
+ },
50
+ "license": "Apache-2.0",
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "scripts": {
55
+ "typecheck": "tsc --noEmit -p tsconfig.json"
56
+ }
6
57
  }
@@ -0,0 +1,116 @@
1
+ ---
2
+ name: newui-data
3
+ description: Use the headless newui data layer for SolidJS collections,
4
+ repository queries, optimistic mutations, TanStack Query caching and form
5
+ submit lifecycles. Use when a page needs to load a list, create/update/delete
6
+ records, share a cache, or wire TanStack Form through the kit's Form and
7
+ field-error contract.
8
+ metadata:
9
+ type: core
10
+ domain: data
11
+ library: '@talelk/newui-data'
12
+ library_version: '1.0.0'
13
+ sources:
14
+ - talelk/newui:spec/data.md
15
+ - talelk/newui:skills/README.md
16
+ - talelk/newui:packages/data/README.md
17
+ - talelk/newui:packages/data/src/query/client.tsx
18
+ - talelk/newui:packages/data/src/query/collection.ts
19
+ - talelk/newui:packages/data/src/query/repository.ts
20
+ - talelk/newui:packages/data/src/form/form.tsx
21
+ ---
22
+
23
+ # newui-data
24
+
25
+ The headless data layer: one query client, one collection abstraction and one
26
+ form surface. It renders nothing. Load the `newui` skill as well when the page
27
+ needs components.
28
+
29
+ ## Wire the client once
30
+
31
+ ```tsx no-check
32
+ import { DataProvider } from '@talelk/newui-data/query'
33
+
34
+ export function App() {
35
+ return <DataProvider>{/* router and pages */}</DataProvider>
36
+ }
37
+ ```
38
+
39
+ `DataProvider` owns a `QueryClient` and clears it on unmount. Pass `client` to
40
+ share one across panes. The defaults are deliberate: no refetch on window focus,
41
+ one retry, and errors are data the page renders rather than exceptions.
42
+
43
+ ## Collections
44
+
45
+ A repository is the only place that knows where records come from. A collection
46
+ turns it into one cached list and four optimistic mutations.
47
+
48
+ ```tsx
49
+ import { createCollection } from '@talelk/newui-data/query'
50
+
51
+ const projects = createCollection<Project, NewProject>(() => ({
52
+ key: ['projects'],
53
+ repository: projectRepository,
54
+ onError: (error) => setNotice(error.message),
55
+ }))
56
+
57
+ projects.query.rows()
58
+ projects.query.isPending()
59
+ projects.query.isError()
60
+ projects.create.mutateAsync(input)
61
+ projects.update.mutateAsync({ id, patch })
62
+ projects.updateMany.mutate({ ids, patch })
63
+ projects.remove.mutateAsync({ ids })
64
+ ```
65
+
66
+ - Read `collection.query.rows()`; never reach into the cache from a page.
67
+ - `create` is not optimistic because the repository owns the new id and defaults.
68
+ - `update`, `updateMany` and `remove` land immediately and roll back on failure.
69
+ - Mutations resolve after the list refresh settles, so a page can close its
70
+ dialog in the same `await`.
71
+ - Repositories receive an `AbortSignal`; honour it or superseded list requests
72
+ can resurrect stale rows.
73
+
74
+ ## Forms
75
+
76
+ ```tsx no-check
77
+ import { createAppForm, fieldError, Form } from '@talelk/newui-data/form'
78
+
79
+ const { form, submitting } = createAppForm({
80
+ defaultValues: { name: '' },
81
+ onSubmit: async (value) => {
82
+ await projects.create.mutateAsync({ name: value.name })
83
+ },
84
+ })
85
+
86
+ <Form form={form}>
87
+ <form.Field
88
+ name="name"
89
+ validators={{
90
+ onChange: ({ value }) =>
91
+ value.trim().length < 2 ? 'Give the project a name.' : undefined,
92
+ }}
93
+ >
94
+ {(field) => (
95
+ <TextInput
96
+ label="Name"
97
+ required
98
+ value={field().state.value}
99
+ error={fieldError(field())}
100
+ onInput={(event) => field().handleChange(event.currentTarget.value)}
101
+ />
102
+ )}
103
+ </form.Field>
104
+ <Button type="submit" loading={submitting()}>
105
+ Create
106
+ </Button>
107
+ </Form>
108
+ ```
109
+
110
+ - `createAppForm` owns TanStack Form and exposes `submitting()` for the button.
111
+ - `Form` sets `noValidate` and prevents the native submit; field messages are the
112
+ error UI.
113
+ - `fieldError` maps TanStack's error list to the single string `Field` takes and
114
+ de-duplicates the same validator message.
115
+ - Pass the same message to `onChange` and `onSubmit` when the field should
116
+ validate on both; `fieldError` will not print it twice.
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { csvText } from './download'
3
+
4
+ describe('download', () => {
5
+ it('quotes every cell and doubles inner quotes', () => {
6
+ expect(
7
+ csvText([
8
+ ['Name', 'Note'],
9
+ ['Acme, Inc', 'Said "yes"'],
10
+ ['Globex', 42],
11
+ ]),
12
+ ).toBe('"Name","Note"\r\n"Acme, Inc","Said ""yes"""\r\n"Globex","42"')
13
+ })
14
+ })
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Saving a file, without the Blob/anchor/revoke dance every page wrote.
3
+ *
4
+ * `downloadCsv` quotes every cell and doubles inner quotes, which is the part
5
+ * that is easy to get subtly wrong.
6
+ */
7
+
8
+ /** Save a Blob (or a local `File`) under `name`. */
9
+ export function downloadBlob(name: string, blob: Blob): void {
10
+ const url = URL.createObjectURL(blob)
11
+ const link = document.createElement('a')
12
+ link.href = url
13
+ link.download = name
14
+ link.click()
15
+ URL.revokeObjectURL(url)
16
+ }
17
+
18
+ /** Save text under `name`; plain text unless the caller names a type. */
19
+ export function downloadText(
20
+ name: string,
21
+ text: string,
22
+ mime = 'text/plain;charset=utf-8',
23
+ ): void {
24
+ downloadBlob(name, new Blob([text], { type: mime }))
25
+ }
26
+
27
+ /** Rows → CSV text, every cell quoted, quotes doubled. */
28
+ export function csvText(rows: (string | number)[][]): string {
29
+ return rows
30
+ .map((row) =>
31
+ row.map((value) => `"${String(value).replaceAll('"', '""')}"`).join(','),
32
+ )
33
+ .join('\r\n')
34
+ }
35
+
36
+ /** Save rows as a CSV file under `name`. */
37
+ export function downloadCsv(name: string, rows: (string | number)[][]): void {
38
+ downloadText(name, csvText(rows), 'text/csv;charset=utf-8')
39
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * The logic layer's public surface, asserted by name and by entry point.
3
+ *
4
+ * The data package has four entries (`.`, `/query`, `/form`, `/resource`),
5
+ * which is exactly
6
+ * the shape an accidental `export *` or a stray
7
+ * re-export slips into. An entry's cost is part of its contract — 0.77 kB for
8
+ * the helpers, 15.5 for the query — so a new name on an entry is a decision, not
9
+ * a detail.
10
+ */
11
+ import { readFileSync } from 'node:fs'
12
+ import path from 'node:path'
13
+ import { describe, expect, it } from 'vitest'
14
+ import * as form from './form'
15
+ import * as root from './index'
16
+ import * as query from './query'
17
+ import * as resource from './resource'
18
+
19
+ const ENTRIES = {
20
+ '.': root,
21
+ './query': query,
22
+ './form': form,
23
+ './resource': resource,
24
+ } as const
25
+
26
+ const SURFACE: Record<keyof typeof ENTRIES, string[]> = {
27
+ // Formatting, selection, undo, downloads — and nothing that renders.
28
+ '.': [
29
+ 'bytes',
30
+ 'count',
31
+ 'createUndo',
32
+ 'csvText',
33
+ 'downloadBlob',
34
+ 'downloadCsv',
35
+ 'downloadText',
36
+ 'first',
37
+ 'isEmail',
38
+ 'optionsOf',
39
+ 'usd',
40
+ 'usdK',
41
+ ],
42
+ './query': [
43
+ 'DataProvider',
44
+ 'createCollection',
45
+ 'createDataClient',
46
+ 'createListQuery',
47
+ ],
48
+ './form': ['Form', 'createAppForm', 'fieldError'],
49
+ './resource': [
50
+ 'RequestError',
51
+ 'createDocumentResource',
52
+ 'createIdbStore',
53
+ 'createListResource',
54
+ 'createLocalStore',
55
+ 'createRealtime',
56
+ 'createResource',
57
+ 'getConfig',
58
+ 'isAbortError',
59
+ 'normalizeError',
60
+ 'request',
61
+ 'resetConfig',
62
+ 'setConfig',
63
+ 'useCall',
64
+ 'useDoc',
65
+ 'useDoctype',
66
+ 'useList',
67
+ 'useNewDoc',
68
+ ],
69
+ }
70
+
71
+ const manifest = JSON.parse(
72
+ readFileSync(path.join(process.cwd(), 'packages/data/package.json'), 'utf8'),
73
+ ) as { exports: Record<string, unknown> }
74
+
75
+ describe('@talelk/newui-data', () => {
76
+ it('publishes exactly the entries this test pins', () => {
77
+ expect(Object.keys(manifest.exports).sort()).toEqual(
78
+ Object.keys(ENTRIES).sort(),
79
+ )
80
+ })
81
+
82
+ for (const [entry, module] of Object.entries(ENTRIES)) {
83
+ it(`${entry} exports exactly its surface`, () => {
84
+ expect(Object.keys(module).sort()).toEqual(
85
+ SURFACE[entry as keyof typeof ENTRIES],
86
+ )
87
+ })
88
+ }
89
+
90
+ it('keeps the root entry headless', () => {
91
+ // The root is the only entry with no component, no query client and no
92
+ // validator; every value it exports is a function over plain data.
93
+ for (const value of Object.values(root)) {
94
+ expect(typeof value).toBe('function')
95
+ }
96
+ })
97
+
98
+ it('keeps the Frappe resource client off the root', () => {
99
+ // The DocType fetching layer is app-specific cost (fetch + IndexedDB +
100
+ // realtime multiplexer); it must stay behind its earned subpath.
101
+ for (const name of ['useDoc', 'useDoctype', 'frappeRequest', 'request']) {
102
+ expect(name in root).toBe(false)
103
+ }
104
+ })
105
+ })