@umituz/web-hono 1.9.0 → 1.10.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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/api.ts +30 -0
- package/src/docstore.ts +13 -7
- package/src/store.ts +0 -0
package/README.md
CHANGED
|
@@ -153,7 +153,7 @@ const { status, error, retry } = useHydration();
|
|
|
153
153
|
const orders = useCollection('orders'); // typed, re-renders on its channel
|
|
154
154
|
```
|
|
155
155
|
|
|
156
|
-
The machine: first read hydrates every collection in
|
|
156
|
+
The machine: first read hydrates every collection through `GET /api/bootstrap` in small sequential batches (default 12 paths per request — the server replays its own list routes in-process and answers one bundle; collections emit progressively as their batch lands, so the database wakes once instead of once per collection; APIs predating the route answer 404 and hydration falls back to the 1.x parallel fan-out; `bootstrap: false` pins the legacy mode); repositories read synchronously via `getDb()` or `store.read(fn)`; writes go through `store.apiWrite(path, init, keys)` / `store.http.write` which refresh only the touched collections on success and leave the cache untouched on failure; per-collection pub/sub (plus the `__refetch_all__` wildcard) drives the hooks; `view(keys, selector)` gives change-detected derived reads.
|
|
157
157
|
|
|
158
158
|
**7. Lifecycle scripts** (`scripts/`) — keep only your `TABLE_SPECS` registry and thin wrappers:
|
|
159
159
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
4
4
|
"description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), and db lifecycle scripts (reset / snapshot / seed)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
package/src/api.ts
CHANGED
|
@@ -34,6 +34,11 @@ export interface ApiConfig<E extends ApiEnv = ApiEnv> {
|
|
|
34
34
|
middleware?: ReadonlyArray<MiddlewareHandler<{ Bindings: E }>>;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/** Cap on replayed paths per bootstrap call — keeps one Worker request
|
|
38
|
+
* inside the free-tier subrequest budget even when a replayed route runs
|
|
39
|
+
* a couple of queries of its own. */
|
|
40
|
+
const BOOTSTRAP_MAX_PATHS = 40;
|
|
41
|
+
|
|
37
42
|
export function createApi<E extends ApiEnv = ApiEnv>(config: ApiConfig<E>): Hono<{ Bindings: E }> {
|
|
38
43
|
const api = new Hono<{ Bindings: E }>();
|
|
39
44
|
|
|
@@ -46,6 +51,31 @@ export function createApi<E extends ApiEnv = ApiEnv>(config: ApiConfig<E>): Hono
|
|
|
46
51
|
// Widening ApiEnv→E is safe at runtime (c.env: E ⊇ ApiEnv); hono's Context
|
|
47
52
|
// invariance just refuses the direct cast.
|
|
48
53
|
app.onError(apiOnError as unknown as ErrorHandler<{ Bindings: E }>);
|
|
54
|
+
|
|
55
|
+
// GET /api/bootstrap?paths=/api/a,/api/b — replays the app's own list
|
|
56
|
+
// routes in-process (no network) and bundles their JSON bodies under one
|
|
57
|
+
// response, so a hydrating client pays one round trip per chunk instead
|
|
58
|
+
// of one per collection. Only /api paths are replayed; anything else the
|
|
59
|
+
// router would have answered stays answerable only the normal way.
|
|
60
|
+
app.get('/api/bootstrap', async (c) => {
|
|
61
|
+
const raw = c.req.query('paths') ?? '';
|
|
62
|
+
const paths = [...new Set(raw.split(',').map((p) => p.trim()).filter((p) => p.startsWith('/api/') && p !== '/api/bootstrap'))]
|
|
63
|
+
.slice(0, BOOTSTRAP_MAX_PATHS);
|
|
64
|
+
const results: Record<string, { status: number; body?: unknown }> = {};
|
|
65
|
+
await Promise.all(
|
|
66
|
+
paths.map(async (p) => {
|
|
67
|
+
const res = await app.request(p, { headers: c.req.raw.headers }, c.env);
|
|
68
|
+
if (res.status !== 200) {
|
|
69
|
+
results[p] = { status: res.status };
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const body = await res.json().catch(() => undefined);
|
|
73
|
+
results[p] = body === undefined ? { status: res.status } : { status: res.status, body };
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
return c.json({ results });
|
|
77
|
+
});
|
|
78
|
+
|
|
49
79
|
app.all('/api/*', apiNotFound);
|
|
50
80
|
app.get('*', spaFallback);
|
|
51
81
|
return app;
|
package/src/docstore.ts
CHANGED
|
@@ -83,9 +83,13 @@ export function canTransition(map: Record<string, string[]>, from: string, to: s
|
|
|
83
83
|
return from === to || (map[from] ?? []).includes(to);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/** ISO string → epoch ms; anything unparsable falls back to now.
|
|
86
|
+
/** ISO string → epoch ms; anything unparsable falls back to now. The
|
|
87
|
+
* epoch-0 instant (1970-01-01T00:00:00Z) parses to 0 and must stay 0 —
|
|
88
|
+
* a truthiness check would silently rewrite it to "now". */
|
|
87
89
|
export function epoch(iso: unknown): number {
|
|
88
|
-
|
|
90
|
+
if (typeof iso !== 'string') return Date.now();
|
|
91
|
+
const ms = Date.parse(iso);
|
|
92
|
+
return Number.isNaN(ms) ? Date.now() : ms;
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
function defaultDb(c: RequestContext): CrudDb {
|
|
@@ -145,12 +149,14 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
145
149
|
return and(...conds);
|
|
146
150
|
}
|
|
147
151
|
|
|
148
|
-
function rowValues(col: DocCollectionSpec, doc: DocInput
|
|
152
|
+
function rowValues(col: DocCollectionSpec, doc: DocInput): Record<string, unknown> {
|
|
149
153
|
const id = docIdOf(col, doc);
|
|
150
154
|
const values: Record<string, unknown> = {
|
|
151
155
|
id,
|
|
152
|
-
|
|
153
|
-
|
|
156
|
+
// epoch() already falls back to now for unparsable input; the extra
|
|
157
|
+
// `|| now` would rewrite a legitimate 0 (epoch-0 instant) to now.
|
|
158
|
+
createdAt: epoch(doc.createdAt),
|
|
159
|
+
updatedAt: epoch(doc.updatedAt),
|
|
154
160
|
deletedAt: null as number | null,
|
|
155
161
|
data: doc,
|
|
156
162
|
...col.columns?.(doc),
|
|
@@ -185,7 +191,7 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
185
191
|
}
|
|
186
192
|
await col.validate?.({ db, doc, existing });
|
|
187
193
|
}
|
|
188
|
-
const values = rowValues(col, doc
|
|
194
|
+
const values = rowValues(col, doc);
|
|
189
195
|
await db
|
|
190
196
|
.insert(col.table)
|
|
191
197
|
.values(values as never)
|
|
@@ -203,7 +209,7 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
203
209
|
const col = requireSpec(key);
|
|
204
210
|
let count = 0;
|
|
205
211
|
for (let i = 0; i < docs.length; i += chunkSize) {
|
|
206
|
-
const chunk = docs.slice(i, i + chunkSize).map((doc) => rowValues(col, doc
|
|
212
|
+
const chunk = docs.slice(i, i + chunkSize).map((doc) => rowValues(col, doc));
|
|
207
213
|
await db
|
|
208
214
|
.insert(col.table)
|
|
209
215
|
.values(chunk as never)
|
package/src/store.ts
CHANGED
|
Binary file
|