@flattype/models 1.1.0 → 1.2.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.
- package/README.md +56 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
<p align="center">
|
|
16
16
|
<a href="https://www.npmjs.com/package/@flattype/core"><img src="https://img.shields.io/npm/v/@flattype/core?label=%40flattype%2Fcore&color=8b6cff" alt="npm @flattype/core"></a>
|
|
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
|
+
<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>
|
|
18
19
|
<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>
|
|
19
|
-
<img src="https://img.shields.io/badge/core-3.
|
|
20
|
+
<img src="https://img.shields.io/badge/core-3.7_KB_min%2Bgz-2fd4b2" alt="3.7 KB min+gz">
|
|
20
21
|
<img src="https://img.shields.io/badge/dependencies-0-2fd4b2" alt="No third-party dependencies">
|
|
21
22
|
<a href="https://github.com/karammm/flattype/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license"></a>
|
|
22
23
|
</p>
|
|
@@ -25,6 +26,7 @@
|
|
|
25
26
|
<a href="#quick-start"><b>Quick start</b></a> ·
|
|
26
27
|
<a href="#the-format"><b>Format</b></a> ·
|
|
27
28
|
<a href="#typed-models"><b>Models</b></a> ·
|
|
29
|
+
<a href="#http"><b>HTTP</b></a> ·
|
|
28
30
|
<a href="#benchmarks"><b>Benchmarks</b></a> ·
|
|
29
31
|
<a href="https://github.com/karammm/flattype/blob/main/CHANGELOG.md"><b>Changelog</b></a> ·
|
|
30
32
|
<a href="https://github.com/karammm/flattype/blob/main/ROADMAP.md"><b>Roadmap</b></a>
|
|
@@ -33,10 +35,10 @@
|
|
|
33
35
|
<p align="center">
|
|
34
36
|
<a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">
|
|
35
37
|
<img src="https://raw.githubusercontent.com/karammm/flattype/main/docs/assets/flattype-intro.gif" width="820"
|
|
36
|
-
alt="FlatType.js in
|
|
38
|
+
alt="FlatType.js in 52 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, and the size comparison with JSON and TOON">
|
|
37
39
|
</a>
|
|
38
40
|
<br>
|
|
39
|
-
<sub>▶ <a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">Watch the
|
|
41
|
+
<sub>▶ <a href="https://github.com/karammm/flattype/blob/main/docs/assets/flattype-intro.mp4">Watch the 52-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>
|
|
40
42
|
</p>
|
|
41
43
|
|
|
42
44
|
---
|
|
@@ -44,7 +46,7 @@
|
|
|
44
46
|
## Quick start
|
|
45
47
|
|
|
46
48
|
```bash
|
|
47
|
-
npm install @flattype/core @flattype/models
|
|
49
|
+
npm install @flattype/core @flattype/models @flattype/http
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
```ts
|
|
@@ -85,23 +87,35 @@ type User = Infer<typeof User>; // { id: number; fullName: string; createdAt: Da
|
|
|
85
87
|
const users = User.parseList(await res.json()); // full_name → fullName, ISO string → Date
|
|
86
88
|
```
|
|
87
89
|
|
|
90
|
+
Or let both ends speak Flat Notation, with JSON for every other client:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { flatExpress, flatFetch } from "@flattype/http";
|
|
94
|
+
|
|
95
|
+
app.use(flatExpress()); // server: res.json() answers in Flat Notation when the client asks
|
|
96
|
+
|
|
97
|
+
const users = await flatFetch("/api/users", { model: User, list: true }); // client: User[]
|
|
98
|
+
```
|
|
99
|
+
|
|
88
100
|
## Why FlatType
|
|
89
101
|
|
|
90
102
|
- **About half the size on list payloads.** A list of 1,000 users is 45% smaller than minified JSON and 50% smaller than TOON, because keys are written once and nested fields are flattened into the table header.
|
|
91
103
|
- **Made for LLM context.** You pay for every character you send to Claude or another model. Flat Notation is plain `key: value` text that both models and people read easily, and `decode(text, { strict: false })` recovers data from model replies.
|
|
92
104
|
- **Typed models without boilerplate.** Declare the fields you want and get full TypeScript types. snake_case keys, numeric strings, ISO dates, defaults and nullable fields are handled for you, and every problem is reported with its path.
|
|
105
|
+
- **Drop-in for your API.** One middleware for Express, Fastify, Hono or any fetch-API server. Clients that ask get Flat Notation, everyone else still gets JSON, and big lists can be streamed row by row.
|
|
93
106
|
- **Lossless.** `decode(encode(x))` always equals `JSON.parse(JSON.stringify(x))`. Thousands of random values check this on every test run.
|
|
94
|
-
- **Small and safe.** The core is 3.
|
|
107
|
+
- **Small and safe.** The core is 3.7 KB min+gz, with no third-party dependencies. It's ESM + CJS with full types, works in the browser and Node 18+, and guards against `__proto__` pollution.
|
|
95
108
|
|
|
96
109
|
## Packages
|
|
97
110
|
|
|
98
111
|
| Package | What it does | Size (min+gz) |
|
|
99
112
|
| --- | --- | ---: |
|
|
100
|
-
| [`@flattype/core`](https://github.com/karammm/flattype/blob/main/packages/core) | `encode`, `decode`, `flatten`, `unflatten`, `stats` | 3.
|
|
113
|
+
| [`@flattype/core`](https://github.com/karammm/flattype/blob/main/packages/core) | `encode`, `decode`, `createDecoder`, `flatten`, `unflatten`, `stats` | 3.7 KB |
|
|
101
114
|
| [`@flattype/models`](https://github.com/karammm/flattype/blob/main/packages/models) | `defineModel`, `infer`, `toSource`: typed models from JSON or Flat Notation | +3.2 KB |
|
|
115
|
+
| [`@flattype/http`](https://github.com/karammm/flattype/blob/main/packages/http) | `flatFetch`, `createClient`, `flatAxios`, `streamRows`, and `flatExpress` / `flatFastify` / `flatHono` | +2.3 KB |
|
|
102
116
|
| [`@flattype/shared`](https://github.com/karammm/flattype/blob/main/packages/shared) | Shared TypeScript types | types only |
|
|
103
117
|
|
|
104
|
-
React, Vue, Angular and Svelte bindings,
|
|
118
|
+
React, Vue, Angular and Svelte bindings, Claude helpers and a CLI are planned. See the [roadmap](https://github.com/karammm/flattype/blob/main/ROADMAP.md).
|
|
105
119
|
|
|
106
120
|
## The format
|
|
107
121
|
|
|
@@ -193,6 +207,41 @@ export const UsersPage = defineModel({
|
|
|
193
207
|
export type UsersPage = Infer<typeof UsersPage>;
|
|
194
208
|
```
|
|
195
209
|
|
|
210
|
+
## HTTP
|
|
211
|
+
|
|
212
|
+
`@flattype/http` adds Flat Notation to an existing API without breaking anyone. A response is re-encoded only when the request says `Accept: text/x-flat`, and `Vary: Accept` keeps caches correct.
|
|
213
|
+
|
|
214
|
+
**Server**
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { flatExpress, flatFastify, flatHono, negotiate } from "@flattype/http";
|
|
218
|
+
|
|
219
|
+
expressApp.use(flatExpress()); // Express: res.json(...)
|
|
220
|
+
await fastifyApp.register(flatFastify); // Fastify: object replies
|
|
221
|
+
honoApp.use(flatHono()); // Hono: c.json(...)
|
|
222
|
+
return negotiate(request, Response.json(data)); // Bun, Deno, Next.js route handlers, Workers
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
**Client**
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import { createClient, flatAxios, flatFetch, streamRows } from "@flattype/http";
|
|
229
|
+
|
|
230
|
+
const user = await flatFetch("/api/users/7", { model: User }); // typed, throws HttpError on 4xx/5xx
|
|
231
|
+
|
|
232
|
+
const api = createClient({ baseURL: "https://api.example.com/v1", headers: { authorization } });
|
|
233
|
+
const page = await api.get("/users", { query: { page: 2 } });
|
|
234
|
+
await api.post("/users", { name: "Chen" }); // JSON request body
|
|
235
|
+
|
|
236
|
+
const axiosApi = flatAxios(axios.create({ baseURL: "/api" })); // axios: response.data is decoded
|
|
237
|
+
|
|
238
|
+
for await (const { path, index, value } of streamRows("/api/export")) { // rows while still downloading
|
|
239
|
+
table.append(value);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
A JSON-only server works with every client helper: the response is just read as JSON.
|
|
244
|
+
|
|
196
245
|
## Benchmarks
|
|
197
246
|
|
|
198
247
|
`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
|
+
"version": "1.2.0",
|
|
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/
|
|
37
|
-
"@flattype/
|
|
36
|
+
"@flattype/shared": "1.2.0",
|
|
37
|
+
"@flattype/core": "1.2.0"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|