@umituz/web-hono 1.21.0 → 1.22.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/crud.ts +17 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.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/crud.ts
CHANGED
|
@@ -44,6 +44,10 @@ export interface OrgScopeSpec {
|
|
|
44
44
|
resolve: (c: RequestContext) => Promise<string | null | undefined>;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
export interface AuthSpec {
|
|
48
|
+
resolve: (c: RequestContext) => Promise<string | null | undefined>;
|
|
49
|
+
}
|
|
50
|
+
|
|
47
51
|
export interface ListSpec {
|
|
48
52
|
path: string;
|
|
49
53
|
table: PgTable & { deletedAt: PgColumn };
|
|
@@ -57,6 +61,7 @@ export interface CrudSpec extends ListSpec {
|
|
|
57
61
|
label: string;
|
|
58
62
|
create?: CreateSpec;
|
|
59
63
|
remove?: boolean;
|
|
64
|
+
auth?: AuthSpec;
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
const MAX_LIST_LIMIT = 500;
|
|
@@ -93,6 +98,12 @@ async function resolveScopedOrg(spec: ListSpec, c: RequestContext): Promise<stri
|
|
|
93
98
|
return spec.orgScope?.resolve(c);
|
|
94
99
|
}
|
|
95
100
|
|
|
101
|
+
async function requireWriteAuth(spec: CrudSpec, c: RequestContext): Promise<Response | void> {
|
|
102
|
+
if (spec.orgScope || !spec.auth) return;
|
|
103
|
+
const who = await spec.auth.resolve(c);
|
|
104
|
+
if (!who) return c.json({ error: 'Sign in to write to this resource' }, 401);
|
|
105
|
+
}
|
|
106
|
+
|
|
96
107
|
function orgColumnKey(spec: ListSpec, column: PgColumn): string | undefined {
|
|
97
108
|
const cols = getTableColumns(spec.table) as unknown as Record<string, PgColumn>;
|
|
98
109
|
return Object.keys(cols).find((k) => cols[k] === column);
|
|
@@ -164,6 +175,8 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
164
175
|
});
|
|
165
176
|
|
|
166
177
|
router.patch(`${path}/:id`, async (c) => {
|
|
178
|
+
const gate = await requireWriteAuth(spec, c);
|
|
179
|
+
if (gate) return gate;
|
|
167
180
|
const raw = await c.req.json().catch(() => undefined);
|
|
168
181
|
if (raw === undefined) return c.json({ error: 'Malformed JSON in request body' }, 400);
|
|
169
182
|
const patch = pickPatch(table, raw);
|
|
@@ -189,6 +202,8 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
189
202
|
if (spec.create) {
|
|
190
203
|
const { schema, prefix, defaults, pre } = spec.create;
|
|
191
204
|
router.post(path, async (c) => {
|
|
205
|
+
const gate = await requireWriteAuth(spec, c);
|
|
206
|
+
if (gate) return gate;
|
|
192
207
|
const raw = await c.req.json().catch(() => undefined);
|
|
193
208
|
if (raw === undefined || typeof raw !== 'object' || raw === null) {
|
|
194
209
|
return c.json({ error: 'Malformed JSON in request body' }, 400);
|
|
@@ -254,6 +269,8 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
254
269
|
|
|
255
270
|
if (spec.remove) {
|
|
256
271
|
router.delete(`${path}/:id`, async (c) => {
|
|
272
|
+
const gate = await requireWriteAuth(spec, c);
|
|
273
|
+
if (gate) return gate;
|
|
257
274
|
const db = getDb(c);
|
|
258
275
|
const conds: SQL[] = [eq(table.id, c.req.param('id')), isNull(table.deletedAt) as SQL];
|
|
259
276
|
if (spec.orgScope) {
|