@flattype/models 1.3.0 → 1.4.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 (2) hide show
  1. package/README.md +45 -5
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -17,6 +17,7 @@
17
17
  <a href="https://www.npmjs.com/package/@flattype/models"><img src="https://img.shields.io/npm/v/@flattype/models?label=%40flattype%2Fmodels&color=8b6cff" alt="npm @flattype/models"></a>
18
18
  <a href="https://www.npmjs.com/package/@flattype/http"><img src="https://img.shields.io/npm/v/@flattype/http?label=%40flattype%2Fhttp&color=8b6cff" alt="npm @flattype/http"></a>
19
19
  <a href="https://www.npmjs.com/package/@flattype/react"><img src="https://img.shields.io/npm/v/@flattype/react?label=%40flattype%2Freact&color=8b6cff" alt="npm @flattype/react"></a>
20
+ <a href="https://www.npmjs.com/package/@flattype/vue"><img src="https://img.shields.io/npm/v/@flattype/vue?label=%40flattype%2Fvue&color=8b6cff" alt="npm @flattype/vue"></a>
20
21
  <a href="https://github.com/karammm/flattype/actions/workflows/ci.yml"><img src="https://github.com/karammm/flattype/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
21
22
  <img src="https://img.shields.io/badge/core-3.7_KB_min%2Bgz-2fd4b2" alt="3.7 KB min+gz">
22
23
  <img src="https://img.shields.io/badge/dependencies-0-2fd4b2" alt="No third-party dependencies">
@@ -29,6 +30,7 @@
29
30
  <a href="#typed-models"><b>Models</b></a> ·
30
31
  <a href="#http"><b>HTTP</b></a> ·
31
32
  <a href="#react"><b>React</b></a> ·
33
+ <a href="#vue"><b>Vue</b></a> ·
32
34
  <a href="#benchmarks"><b>Benchmarks</b></a> ·
33
35
  <a href="https://github.com/karammm/flattype/blob/main/CHANGELOG.md"><b>Changelog</b></a> ·
34
36
  <a href="https://github.com/karammm/flattype/blob/main/ROADMAP.md"><b>Roadmap</b></a>
@@ -37,10 +39,10 @@
37
39
  <p align="center">
38
40
  <a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">
39
41
  <img src="https://raw.githubusercontent.com/karammm/flattype/main/docs/assets/flattype-intro.gif" width="820"
40
- alt="FlatType.js in 62 seconds: an API response encoded to Flat Notation, how tables and dot paths keep it small, typed models mapped from snake_case JSON, an Express server answering in Flat Notation or JSON, React hooks in a real app, and the size comparison with JSON and TOON">
42
+ alt="FlatType.js in 66 seconds: an API response encoded to Flat Notation, how tables and dot paths keep it small, typed models mapped from snake_case JSON, an Express server answering in Flat Notation or JSON, React hooks and Vue composables in real apps, and the size comparison with JSON and TOON">
41
43
  </a>
42
44
  <br>
43
- <sub>▶ <a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">Watch the 62-second intro in full quality</a>. Every result shown is real output, generated by <a href="https://github.com/karammm/flattype/blob/main/scripts/video/record.mjs"><code>scripts/video/record.mjs</code></a>.</sub>
45
+ <sub>▶ <a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">Watch the 66-second intro in full quality</a>. Every result shown is real output, generated by <a href="https://github.com/karammm/flattype/blob/main/scripts/video/record.mjs"><code>scripts/video/record.mjs</code></a>.</sub>
44
46
  </p>
45
47
 
46
48
  ---
@@ -99,10 +101,10 @@ app.use(flatExpress()); // server: res.json() answers in Flat Notation when the
99
101
  const users = await flatFetch("/api/users", { model: User, list: true }); // client: User[]
100
102
  ```
101
103
 
102
- And in React:
104
+ And in React or Vue:
103
105
 
104
106
  ```tsx
105
- import { useFlat } from "@flattype/react";
107
+ import { useFlat } from "@flattype/react"; // or "@flattype/vue": the same API, with refs
106
108
 
107
109
  const { data: users, loading, error } = useFlat("/api/users", { model: User, list: true }); // User[] | undefined
108
110
  ```
@@ -124,9 +126,10 @@ const { data: users, loading, error } = useFlat("/api/users", { model: User, lis
124
126
  | [`@flattype/models`](https://github.com/karammm/flattype/blob/main/packages/models) | `defineModel`, `infer`, `toSource`: typed models from JSON or Flat Notation | +3.2 KB |
125
127
  | [`@flattype/http`](https://github.com/karammm/flattype/blob/main/packages/http) | `flatFetch`, `createClient`, `flatAxios`, `streamRows`, and `flatExpress` / `flatFastify` / `flatHono` | +2.3 KB |
126
128
  | [`@flattype/react`](https://github.com/karammm/flattype/blob/main/packages/react) | `useFlat`, `useFlatSuspense`, `useFlatStream`, `useModel`, `FlatProvider` | +1.4 KB |
129
+ | [`@flattype/vue`](https://github.com/karammm/flattype/blob/main/packages/vue) | The same composables for Vue 3, with reactive URLs, plus the `createFlat()` plugin | +1.1 KB |
127
130
  | [`@flattype/shared`](https://github.com/karammm/flattype/blob/main/packages/shared) | Shared TypeScript types | types only |
128
131
 
129
- Vue, Angular and Svelte bindings, Claude helpers and a CLI are planned. See the [roadmap](https://github.com/karammm/flattype/blob/main/ROADMAP.md).
132
+ Angular and Svelte bindings, Claude helpers and a CLI are planned. See the [roadmap](https://github.com/karammm/flattype/blob/main/ROADMAP.md).
130
133
 
131
134
  ## The format
132
135
 
@@ -293,6 +296,43 @@ function Export() {
293
296
 
294
297
  Works with React 18 and 19, in StrictMode and with any server. A JSON-only API works too. A runnable app with Playwright tests in three browsers is in [`examples/react-app`](https://github.com/karammm/flattype/blob/main/examples/react-app).
295
298
 
299
+ ## Vue
300
+
301
+ ```bash
302
+ npm install @flattype/vue @flattype/models
303
+ ```
304
+
305
+ ```ts
306
+ // main.ts
307
+ app.use(createFlat({ baseURL: "/api", headers: () => ({ authorization: `Bearer ${token.value}` }) }));
308
+ ```
309
+
310
+ ```vue
311
+ <script setup lang="ts">
312
+ import { useFlat, useFlatStream, useFlatSuspense } from "@flattype/vue";
313
+
314
+ const props = defineProps<{ id: number }>();
315
+
316
+ // The URL can be a ref or a getter: changing it aborts the old request and loads the new one.
317
+ const { data: user, error, loading, refetch } = useFlat(() => `/users/${props.id}`, { model: User }); // Ref<User | undefined>
318
+
319
+ // Inside <Suspense>, `await` the first response so `data` is always set.
320
+ const { data: me } = await useFlatSuspense("/me", { model: User }); // Ref<User>
321
+
322
+ // Big lists render while they download.
323
+ const { rows, done } = useFlatStream("/export", { model: Row, table: "rows" });
324
+ </script>
325
+ ```
326
+
327
+ | Composable | Returns | Notes |
328
+ | --- | --- | --- |
329
+ | `useFlat(url, options)` | `{ data, error, loading, refetch }` refs | `url` and `enabled` can be refs or getters. Aborts on change and on unmount |
330
+ | `useFlatSuspense(url, options)` | `Promise<{ data, error, loading, refetch }>` | For `async setup()` in `<Suspense>`. The first error goes to `onErrorCaptured` |
331
+ | `useFlatStream(url, options)` | `{ rows, done, error, restart }` refs | One update per network chunk. `table` picks one table by path |
332
+ | `useModel(source, Model, { list })` | a computed ref | For props, Pinia stores and sockets |
333
+
334
+ Vue 3.3+. A runnable app with Playwright tests in three browsers is in [`examples/vue-app`](https://github.com/karammm/flattype/blob/main/examples/vue-app).
335
+
296
336
  ## Benchmarks
297
337
 
298
338
  `pnpm compare` on the fixtures in [`fixtures/`](https://github.com/karammm/flattype/blob/main/fixtures/index.mjs) (Node 24, Apple Silicon):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flattype/models",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Typed models for API data: key mapping, coercion and defaults, from JSON or Flat Notation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,8 +33,8 @@
33
33
  "flattype"
34
34
  ],
35
35
  "dependencies": {
36
- "@flattype/core": "1.3.0",
37
- "@flattype/shared": "1.3.0"
36
+ "@flattype/core": "1.4.1",
37
+ "@flattype/shared": "1.4.1"
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public"