@umituz/web-hono 1.16.0 → 1.18.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/package.json +4 -2
- package/src/index.ts +1 -0
- package/src/resource-react.ts +39 -0
- package/src/sync.ts +64 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
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), better-auth server integration (tables/getter/mount/session helpers), weak-compare If-None-Match matcher (matchesEtag), cookie channels + request scope/ETag/R2-stream server helpers, TTL resource cache, and db lifecycle scripts (reset / snapshot / seed)",
|
|
3
|
+
"version": "1.18.0",
|
|
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), doc-sync diff/snapshot/push machine (snapshotDocs/diffOps/pushOps), better-auth server integration (tables/getter/mount/session helpers), weak-compare If-None-Match matcher (matchesEtag), cookie channels + request scope/ETag/R2-stream server helpers, TTL resource cache, and db lifecycle scripts (reset / snapshot / seed)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
7
7
|
"types": "./src/index.ts",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"./dates": "./src/dates.ts",
|
|
15
15
|
"./store": "./src/store.ts",
|
|
16
16
|
"./store/react": "./src/store-react.ts",
|
|
17
|
+
"./sync": "./src/sync.ts",
|
|
17
18
|
"./lifecycle": "./src/lifecycle.ts",
|
|
18
19
|
"./auth": "./src/auth.ts",
|
|
19
20
|
"./mail": "./src/mail.ts",
|
|
@@ -22,6 +23,7 @@
|
|
|
22
23
|
"./http": "./src/http.ts",
|
|
23
24
|
"./r2": "./src/r2.ts",
|
|
24
25
|
"./resource": "./src/resource.ts",
|
|
26
|
+
"./resource/react": "./src/resource-react.ts",
|
|
25
27
|
"./package.json": "./package.json"
|
|
26
28
|
},
|
|
27
29
|
"scripts": {
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* `/client` apiFetch / apiWrite — the same-origin API client
|
|
19
19
|
* `/store` createStore — the fetch-backed reactive store
|
|
20
20
|
* `/store/react` makeStoreHooks — useQuery/useDbValue/useCollection/useHydration
|
|
21
|
+
* `/sync` snapshotDocs / diffOps / pushOps — the doc-sync diff machine
|
|
21
22
|
* `/lifecycle` reset / snapshot / seed — the shared-Neon db lifecycle
|
|
22
23
|
* `/cookies` createCookieChannel — one named browser cookie, read/write/clear
|
|
23
24
|
* `/http` requestScopeOf / respondWithEtag / internalError / CACHE_CONTROLS
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/* React binding for a createResource instance. The resource is
|
|
2
|
+
framework-free; apps that don't use React never import this entry.
|
|
3
|
+
Binding pattern:
|
|
4
|
+
|
|
5
|
+
const plans = createResource({ load, storageKey: null, ttlMs: RESOURCE_TTL_MS });
|
|
6
|
+
const { value, loading } = useResource(plans); */
|
|
7
|
+
|
|
8
|
+
import { useEffect, useState, useSyncExternalStore } from 'react';
|
|
9
|
+
import type { Resource } from './resource';
|
|
10
|
+
|
|
11
|
+
export interface ResourceResult<T> {
|
|
12
|
+
value: T | null;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function useResource<T>(resource: Resource<T>): ResourceResult<T> {
|
|
17
|
+
const value = useSyncExternalStore(
|
|
18
|
+
(onChange) => resource.subscribe(onChange),
|
|
19
|
+
() => resource.read(),
|
|
20
|
+
() => resource.read(),
|
|
21
|
+
);
|
|
22
|
+
const [loading, setLoading] = useState(resource.read() === null);
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (resource.read() !== null) setLoading(false);
|
|
26
|
+
let alive = true;
|
|
27
|
+
resource
|
|
28
|
+
.ensure()
|
|
29
|
+
.catch(() => null)
|
|
30
|
+
.finally(() => {
|
|
31
|
+
if (alive) setLoading(false);
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
alive = false;
|
|
35
|
+
};
|
|
36
|
+
}, [resource]);
|
|
37
|
+
|
|
38
|
+
return { value, loading };
|
|
39
|
+
}
|
package/src/sync.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { apiFetch } from './client';
|
|
2
|
+
|
|
3
|
+
export type SyncDoc = Record<string, unknown> & { id: string };
|
|
4
|
+
|
|
5
|
+
export type SyncCollections = Record<string, readonly SyncDoc[]>;
|
|
6
|
+
|
|
7
|
+
export type SyncPutOp = { op: 'put'; collection: string; doc: SyncDoc };
|
|
8
|
+
|
|
9
|
+
export type SyncDeleteOp = { op: 'del'; collection: string; id: string };
|
|
10
|
+
|
|
11
|
+
export type SyncOp = SyncPutOp | SyncDeleteOp;
|
|
12
|
+
|
|
13
|
+
export type StripFields = Record<string, readonly string[]>;
|
|
14
|
+
|
|
15
|
+
export function snapshotDocs(collections: SyncCollections, stripFields?: StripFields): Map<string, string> {
|
|
16
|
+
const snap = new Map<string, string>();
|
|
17
|
+
for (const [collection, docs] of Object.entries(collections)) {
|
|
18
|
+
for (const doc of docs) {
|
|
19
|
+
const fields = stripFields?.[collection];
|
|
20
|
+
const wire = fields
|
|
21
|
+
? Object.fromEntries(Object.entries(doc).filter(([field]) => !fields.includes(field)))
|
|
22
|
+
: doc;
|
|
23
|
+
snap.set(`${collection}:${doc.id}`, JSON.stringify(wire));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return snap;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function diffOps(last: Map<string, string>, next: SyncCollections, stripFields?: StripFields): SyncOp[] {
|
|
30
|
+
const current = snapshotDocs(next, stripFields);
|
|
31
|
+
const ops: SyncOp[] = [];
|
|
32
|
+
for (const [key, json] of current) {
|
|
33
|
+
if (last.get(key) === json) continue;
|
|
34
|
+
const separator = key.indexOf(':');
|
|
35
|
+
const collection = key.slice(0, separator);
|
|
36
|
+
const doc = next[collection]?.find((entry) => entry.id === key.slice(separator + 1));
|
|
37
|
+
if (doc) ops.push({ op: 'put', collection, doc });
|
|
38
|
+
}
|
|
39
|
+
for (const key of last.keys()) {
|
|
40
|
+
if (current.has(key)) continue;
|
|
41
|
+
const separator = key.indexOf(':');
|
|
42
|
+
ops.push({ op: 'del', collection: key.slice(0, separator), id: key.slice(separator + 1) });
|
|
43
|
+
}
|
|
44
|
+
return ops;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function pushOps(endpoint: string, ops: readonly SyncOp[], chunk: number): Promise<void> {
|
|
48
|
+
for (let index = 0; index < ops.length; index += chunk) {
|
|
49
|
+
await apiFetch(endpoint, { method: 'POST', json: { ops: ops.slice(index, index + chunk) } });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function pushOpsKeepalive(endpoint: string, ops: readonly SyncOp[], chunk: number, bodyLimit: number): void {
|
|
54
|
+
for (let index = 0; index < ops.length; index += chunk) {
|
|
55
|
+
const body = JSON.stringify({ ops: ops.slice(index, index + chunk) });
|
|
56
|
+
if (body.length > bodyLimit) continue;
|
|
57
|
+
void fetch(endpoint, {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: { 'content-type': 'application/json' },
|
|
60
|
+
body,
|
|
61
|
+
keepalive: true,
|
|
62
|
+
}).catch(() => {});
|
|
63
|
+
}
|
|
64
|
+
}
|