@metaobjectsdev/runtime-ts 1.0.8 → 1.0.9-rc.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/dist/drizzle-fastify/filter-allowlist.d.ts +23 -0
- package/dist/drizzle-fastify/filter-allowlist.d.ts.map +1 -1
- package/dist/drizzle-fastify/filter-allowlist.js +13 -1
- package/dist/drizzle-fastify/filter-allowlist.js.map +1 -1
- package/dist/drizzle-fastify/filter-parser.d.ts.map +1 -1
- package/dist/drizzle-fastify/filter-parser.js +73 -10
- package/dist/drizzle-fastify/filter-parser.js.map +1 -1
- package/dist/drizzle-fastify/filter-value-format.d.ts +21 -0
- package/dist/drizzle-fastify/filter-value-format.d.ts.map +1 -0
- package/dist/drizzle-fastify/filter-value-format.js +54 -0
- package/dist/drizzle-fastify/filter-value-format.js.map +1 -0
- package/dist/drizzle-fastify/index.d.ts.map +1 -1
- package/dist/drizzle-fastify/index.js +5 -1
- package/dist/drizzle-fastify/index.js.map +1 -1
- package/dist/drizzle-fastify/mount-m2m.d.ts.map +1 -1
- package/dist/drizzle-fastify/mount-m2m.js +3 -1
- package/dist/drizzle-fastify/mount-m2m.js.map +1 -1
- package/dist/drizzle-fastify/mount-read-only.d.ts.map +1 -1
- package/dist/drizzle-fastify/mount-read-only.js +4 -1
- package/dist/drizzle-fastify/mount-read-only.js.map +1 -1
- package/dist/drizzle-fastify/route-error-handler.d.ts +27 -0
- package/dist/drizzle-fastify/route-error-handler.d.ts.map +1 -0
- package/dist/drizzle-fastify/route-error-handler.js +41 -0
- package/dist/drizzle-fastify/route-error-handler.js.map +1 -0
- package/dist/fastify/index.d.ts.map +1 -1
- package/dist/fastify/index.js +8 -5
- package/dist/fastify/index.js.map +1 -1
- package/dist/hono/index.d.ts.map +1 -1
- package/dist/hono/index.js +23 -15
- package/dist/hono/index.js.map +1 -1
- package/dist/hono/mount-read-only.d.ts.map +1 -1
- package/dist/hono/mount-read-only.js +6 -4
- package/dist/hono/mount-read-only.js.map +1 -1
- package/dist/hono/route-guard.d.ts +34 -0
- package/dist/hono/route-guard.d.ts.map +1 -0
- package/dist/hono/route-guard.js +46 -0
- package/dist/hono/route-guard.js.map +1 -0
- package/dist/route-errors.d.ts +50 -0
- package/dist/route-errors.d.ts.map +1 -0
- package/dist/route-errors.js +67 -0
- package/dist/route-errors.js.map +1 -0
- package/package.json +3 -3
- package/src/drizzle-fastify/filter-allowlist.ts +33 -0
- package/src/drizzle-fastify/filter-parser.ts +89 -10
- package/src/drizzle-fastify/filter-value-format.ts +68 -0
- package/src/drizzle-fastify/index.ts +5 -1
- package/src/drizzle-fastify/mount-m2m.ts +3 -1
- package/src/drizzle-fastify/mount-read-only.ts +4 -1
- package/src/drizzle-fastify/route-error-handler.ts +54 -0
- package/src/fastify/index.ts +8 -5
- package/src/hono/index.ts +22 -16
- package/src/hono/mount-read-only.ts +6 -4
- package/src/hono/route-guard.ts +58 -0
- package/src/route-errors.ts +77 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error envelopes the HTTP mount helpers answer with when a request fails for a
|
|
3
|
+
* reason that is NOT a filter, validation or constraint problem — shared by the
|
|
4
|
+
* drizzle-fastify, fastify (ObjectManager) and hono adapters so they cannot drift.
|
|
5
|
+
*
|
|
6
|
+
* Two defects, both found by a cold review of a generated app:
|
|
7
|
+
*
|
|
8
|
+
* 1. **An unexpected error leaked the query.** With the Drizzle schema out of step with
|
|
9
|
+
* the database (a renamed column), a list answered Fastify's default 500 —
|
|
10
|
+
* `{"statusCode":500,…,"message":"Failed query: select \"id\", \"display_name\" …
|
|
11
|
+
* \nparams: …"}`: the SQL, the table and column names and the bound VALUES, to an
|
|
12
|
+
* unauthenticated caller. The write paths had already been redacted
|
|
13
|
+
* (`constraint-errors.ts`); the reads never were. Now every mounted route answers
|
|
14
|
+
* `500 { "error": "internal" }` — the code the cross-port reference servers already
|
|
15
|
+
* use — and the detail goes to the server log.
|
|
16
|
+
* 2. **A malformed JSON body bypassed the envelope.** Fastify's content-type parser
|
|
17
|
+
* answered `{"statusCode":400,"code":"FST_ERR_CTP_INVALID_JSON_BODY",…}`, and the Hono
|
|
18
|
+
* mount folded it into a Zod `validation` error. Both now answer
|
|
19
|
+
* `400 { "error": "invalid_json" }`.
|
|
20
|
+
*
|
|
21
|
+
* The api-contract corpus pins neither response (5xx is implementation-defined there, and
|
|
22
|
+
* no scenario sends a malformed body), so both are TS-helper extensions — documented in
|
|
23
|
+
* docs/features/api-contract.md, "TS-only error responses".
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { RedactedDatabaseError, logAndRedact } from "./constraint-errors.js";
|
|
27
|
+
|
|
28
|
+
/** Wire code for an unexpected server-side failure. Carries no detail, by design. */
|
|
29
|
+
export const ERROR_CODE_INTERNAL = "internal";
|
|
30
|
+
/** Wire code for a request body that is not parseable JSON. */
|
|
31
|
+
export const ERROR_CODE_INVALID_JSON = "invalid_json";
|
|
32
|
+
|
|
33
|
+
export const HTTP_STATUS_BAD_REQUEST = 400;
|
|
34
|
+
export const HTTP_STATUS_INTERNAL = 500;
|
|
35
|
+
|
|
36
|
+
export interface ErrorEnvelope {
|
|
37
|
+
readonly error: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const INTERNAL_ERROR_BODY: ErrorEnvelope = Object.freeze({ error: ERROR_CODE_INTERNAL });
|
|
41
|
+
export const INVALID_JSON_BODY: ErrorEnvelope = Object.freeze({ error: ERROR_CODE_INVALID_JSON });
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fastify's error codes for a body its JSON parser refused: syntactically invalid, and
|
|
45
|
+
* empty-while-declared-JSON. Both are the caller sending something that is not JSON.
|
|
46
|
+
*/
|
|
47
|
+
const FASTIFY_MALFORMED_JSON_CODES: ReadonlySet<string> = new Set([
|
|
48
|
+
"FST_ERR_CTP_INVALID_JSON_BODY",
|
|
49
|
+
"FST_ERR_CTP_EMPTY_JSON_BODY",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/** True for Fastify's "the body is not valid JSON" errors. */
|
|
53
|
+
export function isMalformedJsonBodyError(err: unknown): boolean {
|
|
54
|
+
const code = (err as { code?: unknown } | null)?.code;
|
|
55
|
+
return typeof code === "string" && FASTIFY_MALFORMED_JSON_CODES.has(code);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* True when the error deliberately carries a CLIENT status (4xx) — an auth hook's 401, a
|
|
60
|
+
* schema-validation 400, a 413 for an oversized body. Those were raised on purpose by the
|
|
61
|
+
* adopter or the framework and are theirs to answer; the mount helpers only take over the
|
|
62
|
+
* errors nobody meant to send.
|
|
63
|
+
*/
|
|
64
|
+
export function isDeliberateClientError(err: unknown): boolean {
|
|
65
|
+
const o = err as { statusCode?: unknown; status?: unknown } | null;
|
|
66
|
+
const status = typeof o?.statusCode === "number" ? o.statusCode : o?.status;
|
|
67
|
+
return typeof status === "number" && status >= 400 && status < 500;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Log an unexpected error server-side, exactly once. A `RedactedDatabaseError` was
|
|
72
|
+
* already logged by the write path that raised it (it carries no detail of its own).
|
|
73
|
+
*/
|
|
74
|
+
export function logUnexpectedError(err: unknown): void {
|
|
75
|
+
if (err instanceof RedactedDatabaseError) return;
|
|
76
|
+
logAndRedact(err);
|
|
77
|
+
}
|