@umituz/web-hono 1.20.0 → 1.20.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-hono",
3
- "version": "1.20.0",
3
+ "version": "1.20.2",
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>(path: string, init: { method?: 'POST' | 'PATCH' | 'PUT' | 'DELETE'; json?: unknown } = {}): Promise<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: init.json !== undefined ? { 'content-type': 'application/json', accept: 'application/json' } : { accept: 'application/json' },
24
- body: init.json !== undefined ? JSON.stringify(init.json) : undefined,
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
@@ -27,6 +27,7 @@ export interface CreateSpec {
27
27
  schema: z.ZodType;
28
28
  prefix: string;
29
29
  defaults?: (c: RequestContext) => Record<string, unknown>;
30
+ pre?: (c: RequestContext, body: Record<string, unknown>) => Promise<Response | void>;
30
31
  }
31
32
 
32
33
  export interface OrgScopeSpec {
@@ -163,7 +164,7 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
163
164
  });
164
165
 
165
166
  if (spec.create) {
166
- const { schema, prefix, defaults } = spec.create;
167
+ const { schema, prefix, defaults, pre } = spec.create;
167
168
  router.post(path, async (c) => {
168
169
  const raw = await c.req.json().catch(() => undefined);
169
170
  if (raw === undefined || typeof raw !== 'object' || raw === null) {
@@ -174,6 +175,10 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
174
175
  return c.json({ error: 'Invalid request body', details: parsed.error.flatten().fieldErrors }, 400);
175
176
  }
176
177
  const body = normalizeRow(table, parsed.data as Record<string, unknown>);
178
+ if (pre) {
179
+ const gate = await pre(c, body);
180
+ if (gate) return gate;
181
+ }
177
182
  const db = getDb(c);
178
183
  const stamped: Record<string, unknown> = {};
179
184
  if (spec.orgScope) {