@umituz/web-hono 1.20.1 → 1.21.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 +1 -1
- package/src/client.ts +11 -3
- package/src/crud.ts +46 -4
- package/src/index.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.21.0",
|
|
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), 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",
|
package/src/client.ts
CHANGED
|
@@ -17,11 +17,19 @@ export class ApiClientError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export async function apiFetch<T>(
|
|
20
|
+
export async function apiFetch<T>(
|
|
21
|
+
path: string,
|
|
22
|
+
init: { method?: 'POST' | 'PATCH' | 'PUT' | 'DELETE'; json?: unknown; form?: FormData } = {},
|
|
23
|
+
): Promise<T> {
|
|
21
24
|
const res = await fetch(path, {
|
|
22
25
|
method: init.method ?? 'GET',
|
|
23
|
-
headers:
|
|
24
|
-
|
|
26
|
+
headers:
|
|
27
|
+
init.form !== undefined || init.json !== undefined
|
|
28
|
+
? init.form !== undefined
|
|
29
|
+
? { accept: 'application/json' }
|
|
30
|
+
: { 'content-type': 'application/json', accept: 'application/json' }
|
|
31
|
+
: { accept: 'application/json' },
|
|
32
|
+
body: init.form !== undefined ? init.form : init.json !== undefined ? JSON.stringify(init.json) : undefined,
|
|
25
33
|
});
|
|
26
34
|
if (!res.ok) {
|
|
27
35
|
const body = (await res.json().catch(() => null)) as { error?: string } | null;
|
package/src/crud.ts
CHANGED
|
@@ -30,9 +30,16 @@ export interface CreateSpec {
|
|
|
30
30
|
pre?: (c: RequestContext, body: Record<string, unknown>) => Promise<Response | void>;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
export interface ThroughSpec {
|
|
34
|
+
column: PgColumn;
|
|
35
|
+
table: PgTable & { id: PgColumn };
|
|
36
|
+
farmColumn: PgColumn;
|
|
37
|
+
}
|
|
38
|
+
|
|
33
39
|
export interface OrgScopeSpec {
|
|
34
40
|
column?: PgColumn;
|
|
35
41
|
farmColumn?: PgColumn;
|
|
42
|
+
through?: ThroughSpec;
|
|
36
43
|
farms?: PgTable & { id: PgColumn; orgId: PgColumn };
|
|
37
44
|
resolve: (c: RequestContext) => Promise<string | null | undefined>;
|
|
38
45
|
}
|
|
@@ -55,15 +62,30 @@ export interface CrudSpec extends ListSpec {
|
|
|
55
62
|
const MAX_LIST_LIMIT = 500;
|
|
56
63
|
|
|
57
64
|
function assertOrgScope(scope: OrgScopeSpec): void {
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
if (
|
|
65
|
+
const modes = [scope.column, scope.farmColumn, scope.through].filter(Boolean).length;
|
|
66
|
+
if (modes > 1) throw new Error('orgScope: pass exactly one of column, farmColumn or through');
|
|
67
|
+
if (modes === 0) throw new Error('orgScope: column, farmColumn or through is required');
|
|
68
|
+
if ((scope.farmColumn || scope.through) && !scope.farms) throw new Error('orgScope: farms table is required with farmColumn/through');
|
|
61
69
|
}
|
|
62
70
|
|
|
63
71
|
function orgScopeCondition(scope: OrgScopeSpec, db: CrudDb, orgId: string): SQL {
|
|
64
72
|
if (scope.column) return eq(scope.column, orgId) as SQL;
|
|
65
73
|
const farms = scope.farms!;
|
|
66
|
-
|
|
74
|
+
const orgFarmIds = db.select({ id: farms.id }).from(farms).where(eq(farms.orgId, orgId));
|
|
75
|
+
if (scope.through) {
|
|
76
|
+
const t = scope.through;
|
|
77
|
+
return inArray(
|
|
78
|
+
t.column,
|
|
79
|
+
db.select({ id: t.table.id }).from(t.table).where(inArray(t.farmColumn, orgFarmIds)),
|
|
80
|
+
) as SQL;
|
|
81
|
+
}
|
|
82
|
+
return inArray(scope.farmColumn!, orgFarmIds) as SQL;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function scopeColumnKeys(spec: ListSpec, scope: OrgScopeSpec): string[] {
|
|
86
|
+
const cols = getTableColumns(spec.table) as unknown as Record<string, PgColumn>;
|
|
87
|
+
const targets = new Set([scope.column, scope.farmColumn, scope.through?.column].filter(Boolean) as PgColumn[]);
|
|
88
|
+
return Object.keys(cols).filter((k) => cols[k] !== undefined && targets.has(cols[k]!));
|
|
67
89
|
}
|
|
68
90
|
|
|
69
91
|
async function resolveScopedOrg(spec: ListSpec, c: RequestContext): Promise<string | null | undefined> {
|
|
@@ -154,6 +176,7 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
154
176
|
const orgId = await resolveScopedOrg(spec, c);
|
|
155
177
|
if (!orgId) return c.json({ error: 'Sign in to an organization to update this resource' }, 401);
|
|
156
178
|
conds.push(orgScopeCondition(spec.orgScope, db, orgId));
|
|
179
|
+
for (const key of scopeColumnKeys(spec, spec.orgScope)) delete (patch as Record<string, unknown>)[key];
|
|
157
180
|
}
|
|
158
181
|
const [row] = await db
|
|
159
182
|
.update(table)
|
|
@@ -188,6 +211,25 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
188
211
|
if (scope.column) {
|
|
189
212
|
const key = orgColumnKey(spec, scope.column);
|
|
190
213
|
if (key) stamped[key] = orgId;
|
|
214
|
+
} else if (scope.through) {
|
|
215
|
+
const t = scope.through;
|
|
216
|
+
const key = orgColumnKey(spec, t.column);
|
|
217
|
+
const parentId = key ? (body[key] as string | undefined) : undefined;
|
|
218
|
+
if (parentId) {
|
|
219
|
+
const farms = scope.farms!;
|
|
220
|
+
const [parent] = await db
|
|
221
|
+
.select({ farm: t.farmColumn })
|
|
222
|
+
.from(t.table)
|
|
223
|
+
.where(eq(t.table.id, parentId))
|
|
224
|
+
.limit(1);
|
|
225
|
+
if (!parent) return c.json({ error: 'Linked record not found' }, 400);
|
|
226
|
+
const [farm] = await db
|
|
227
|
+
.select({ id: farms.id })
|
|
228
|
+
.from(farms)
|
|
229
|
+
.where(and(eq(farms.id, parent.farm), eq(farms.orgId, orgId)))
|
|
230
|
+
.limit(1);
|
|
231
|
+
if (!farm) return c.json({ error: 'Linked record belongs to another organization' }, 400);
|
|
232
|
+
}
|
|
191
233
|
} else {
|
|
192
234
|
const key = orgColumnKey(spec, scope.farmColumn!);
|
|
193
235
|
const farmId = key ? (body[key] as string | undefined) : undefined;
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type { ApiConfig } from './api';
|
|
|
3
3
|
export { healthRouter } from './health';
|
|
4
4
|
export { ApiError, apiOnError, isUniqueViolation } from './errors';
|
|
5
5
|
export { crudRoutes, listRoutes } from './crud';
|
|
6
|
-
export type { CrudSpec, CrudTable, CrudDb, CrudDbFactory, CreateSpec, ListSpec, OrgScopeSpec } from './crud';
|
|
6
|
+
export type { CrudSpec, CrudTable, CrudDb, CrudDbFactory, CreateSpec, ListSpec, OrgScopeSpec, ThroughSpec } from './crud';
|
|
7
7
|
export { docStore, canTransition, epoch, WorkflowError, UnknownCollectionError } from './docstore';
|
|
8
8
|
export type { Doc, DocInput, DocTable, DocCollectionSpec, DocStoreSpec, DocValidateFn, DocAfterWriteFn, DocScopeSpec, DocWriteHookArgs, DocPatchHookArgs } from './docstore';
|
|
9
9
|
export { getDb } from './db';
|