@umituz/web-hono 1.8.2 → 1.8.4
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/docstore.ts +26 -10
- package/src/errors.ts +1 -1
- package/src/index.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.4",
|
|
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/docstore.ts
CHANGED
|
@@ -20,8 +20,8 @@ import type { ApiEnv } from './env';
|
|
|
20
20
|
* One call returns the router (GET list / GET-PUT-PATCH-DELETE by id /
|
|
21
21
|
* POST /batch) AND the motor functions (putDoc/patchDoc/softDeleteDoc/
|
|
22
22
|
* listDocs/getDoc) that seed scripts and server-side workflows call
|
|
23
|
-
* directly. Status machines ride along as declarative `transitions` maps
|
|
24
|
-
* violations surface as WorkflowError → 400.
|
|
23
|
+
* directly. Status machines ride along as declarative `transitions` maps and
|
|
24
|
+
* write guards as `validate` hooks; violations surface as WorkflowError → 400.
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
type RequestContext = Context<{ Bindings: ApiEnv }>;
|
|
@@ -30,6 +30,16 @@ export type Doc = Record<string, unknown> & { id: string };
|
|
|
30
30
|
|
|
31
31
|
export type DocTable = PgTable & { id: PgColumn; data: PgColumn; deletedAt: PgColumn };
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Cross-cutting write guard: runs inside putDoc before the write, covering
|
|
35
|
+
* every path that funnels through it (PUT, PATCH-merged, batch puts,
|
|
36
|
+
* server-side calls) but never putDocs seeds. Receives the incoming doc,
|
|
37
|
+
* the current row when one exists, and the db for cross-collection reads
|
|
38
|
+
* (booking-conflict style rules). Throw WorkflowError (or ApiError) to
|
|
39
|
+
* reject the write.
|
|
40
|
+
*/
|
|
41
|
+
export type DocValidateFn = (args: { db: CrudDb; doc: Doc; existing?: Doc }) => void | Promise<void>;
|
|
42
|
+
|
|
33
43
|
export interface DocCollectionSpec {
|
|
34
44
|
/** Wire collection name — the URL segment and the batch op key. */
|
|
35
45
|
key: string;
|
|
@@ -42,6 +52,8 @@ export interface DocCollectionSpec {
|
|
|
42
52
|
transitions?: Record<string, string[]>;
|
|
43
53
|
/** Doc field the transitions guard reads. Default 'status' (fuelora: state, franchisia: stage). */
|
|
44
54
|
statusKey?: string;
|
|
55
|
+
/** Write guard — see DocValidateFn. */
|
|
56
|
+
validate?: DocValidateFn;
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
export interface DocStoreSpec {
|
|
@@ -102,20 +114,24 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
102
114
|
throw new WorkflowError(`Singleton collection '${key}' must use id '${col.singletonId}'`);
|
|
103
115
|
}
|
|
104
116
|
const guard = col.transitions;
|
|
105
|
-
if (guard) {
|
|
106
|
-
const
|
|
107
|
-
const [existing] = await db
|
|
117
|
+
if (guard || col.validate) {
|
|
118
|
+
const [existingRow] = await db
|
|
108
119
|
.select({ data: col.table.data })
|
|
109
120
|
.from(col.table)
|
|
110
121
|
.where(and(eq(col.table.id, String(doc.id)), isNull(col.table.deletedAt)))
|
|
111
122
|
.limit(1);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
116
|
-
|
|
123
|
+
const existing = existingRow?.data as Doc | undefined;
|
|
124
|
+
if (guard) {
|
|
125
|
+
const statusKey = col.statusKey ?? 'status';
|
|
126
|
+
if (existing) {
|
|
127
|
+
const from = existing[statusKey];
|
|
128
|
+
const to = doc[statusKey];
|
|
129
|
+
if (typeof from === 'string' && typeof to === 'string' && !canTransition(guard, from, to)) {
|
|
130
|
+
throw new WorkflowError(`Invalid ${key} transition ${from} → ${to}`);
|
|
131
|
+
}
|
|
117
132
|
}
|
|
118
133
|
}
|
|
134
|
+
await col.validate?.({ db, doc, existing });
|
|
119
135
|
}
|
|
120
136
|
const values = rowValues(col, doc, Date.now());
|
|
121
137
|
await db
|
package/src/errors.ts
CHANGED
|
@@ -48,7 +48,7 @@ export class ApiError extends Error {
|
|
|
48
48
|
* node-postgres) or nested in a driver cause-chain (neon-http). Depth-capped
|
|
49
49
|
* walk so malformed causes can't loop.
|
|
50
50
|
*/
|
|
51
|
-
function isUniqueViolation(err: unknown): boolean {
|
|
51
|
+
export function isUniqueViolation(err: unknown): boolean {
|
|
52
52
|
let e: unknown = err;
|
|
53
53
|
for (let depth = 0; e !== null && typeof e === 'object' && depth < 3; depth += 1) {
|
|
54
54
|
if ((e as { code?: unknown }).code === '23505') return true;
|
package/src/index.ts
CHANGED
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
export { createApi } from './api';
|
|
23
23
|
export type { ApiConfig } from './api';
|
|
24
24
|
export { healthRouter } from './health';
|
|
25
|
-
export { ApiError, apiOnError } from './errors';
|
|
25
|
+
export { ApiError, apiOnError, isUniqueViolation } from './errors';
|
|
26
26
|
export { crudRoutes, listRoutes } from './crud';
|
|
27
27
|
export type { CrudSpec, CrudTable, CrudDb, CrudDbFactory, CreateSpec, ListSpec } from './crud';
|
|
28
28
|
export { docStore, canTransition, epoch, WorkflowError, UnknownCollectionError } from './docstore';
|
|
29
|
-
export type { Doc, DocTable, DocCollectionSpec, DocStoreSpec } from './docstore';
|
|
29
|
+
export type { Doc, DocTable, DocCollectionSpec, DocStoreSpec, DocValidateFn } from './docstore';
|
|
30
30
|
export { getDb } from './db';
|
|
31
31
|
export { uid, pickPatch, patchTypeErrors, normalizeRow, stampUpdate, trio, actor } from './lib';
|
|
32
32
|
export { resolveOrg, orgHasMode, nextNumber } from './org';
|