@shware/http 3.4.1 → 3.5.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/dist/hono/index.cjs +1 -0
- package/dist/hono/index.d.cts +2 -2
- package/dist/hono/index.d.mts +2 -2
- package/dist/hono/index.mjs +2 -2
- package/dist/hono/validator.cjs +40 -14
- package/dist/hono/validator.cjs.map +1 -1
- package/dist/hono/validator.d.cts +33 -15
- package/dist/hono/validator.d.cts.map +1 -1
- package/dist/hono/validator.d.mts +33 -15
- package/dist/hono/validator.d.mts.map +1 -1
- package/dist/hono/validator.mjs +40 -15
- package/dist/hono/validator.mjs.map +1 -1
- package/package.json +1 -1
package/dist/hono/index.cjs
CHANGED
|
@@ -11,4 +11,5 @@ exports.csrf = require_hono_csrf.csrf;
|
|
|
11
11
|
exports.errorHandler = require_hono_handler.errorHandler;
|
|
12
12
|
exports.geolocation = require_hono_geolocation.geolocation;
|
|
13
13
|
exports.rateLimit = require_hono_rate_limit.rateLimit;
|
|
14
|
+
exports.zBeaconJson = require_hono_validator.zBeaconJson;
|
|
14
15
|
exports.zValidator = require_hono_validator.zValidator;
|
package/dist/hono/index.d.cts
CHANGED
|
@@ -2,6 +2,6 @@ import { AuthorizerConfig, authorizer } from "./authorizer.cjs";
|
|
|
2
2
|
import { CSRFConfig, CSRFIgnoreRule, csrf } from "./csrf.cjs";
|
|
3
3
|
import { geolocation } from "./geolocation.cjs";
|
|
4
4
|
import { errorHandler } from "./handler.cjs";
|
|
5
|
-
import { bigintId, zValidator } from "./validator.cjs";
|
|
5
|
+
import { bigintId, zBeaconJson, zValidator } from "./validator.cjs";
|
|
6
6
|
import { RateLimitOptions, rateLimit } from "./rate-limit.cjs";
|
|
7
|
-
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
|
7
|
+
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zBeaconJson, zValidator };
|
package/dist/hono/index.d.mts
CHANGED
|
@@ -2,6 +2,6 @@ import { AuthorizerConfig, authorizer } from "./authorizer.mjs";
|
|
|
2
2
|
import { CSRFConfig, CSRFIgnoreRule, csrf } from "./csrf.mjs";
|
|
3
3
|
import { geolocation } from "./geolocation.mjs";
|
|
4
4
|
import { errorHandler } from "./handler.mjs";
|
|
5
|
-
import { bigintId, zValidator } from "./validator.mjs";
|
|
5
|
+
import { bigintId, zBeaconJson, zValidator } from "./validator.mjs";
|
|
6
6
|
import { RateLimitOptions, rateLimit } from "./rate-limit.mjs";
|
|
7
|
-
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
|
7
|
+
export { type AuthorizerConfig, type CSRFConfig, type CSRFIgnoreRule, type RateLimitOptions, authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zBeaconJson, zValidator };
|
package/dist/hono/index.mjs
CHANGED
|
@@ -2,6 +2,6 @@ import { authorizer } from "./authorizer.mjs";
|
|
|
2
2
|
import { csrf } from "./csrf.mjs";
|
|
3
3
|
import { geolocation } from "./geolocation.mjs";
|
|
4
4
|
import { errorHandler } from "./handler.mjs";
|
|
5
|
-
import { bigintId, zValidator } from "./validator.mjs";
|
|
5
|
+
import { bigintId, zBeaconJson, zValidator } from "./validator.mjs";
|
|
6
6
|
import { rateLimit } from "./rate-limit.mjs";
|
|
7
|
-
export { authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zValidator };
|
|
7
|
+
export { authorizer, bigintId, csrf, errorHandler, geolocation, rateLimit, zBeaconJson, zValidator };
|
package/dist/hono/validator.cjs
CHANGED
|
@@ -4,21 +4,46 @@ const require_error_status = require("../error/status.cjs");
|
|
|
4
4
|
let zod_mini = require("zod/mini");
|
|
5
5
|
let hono_validator = require("hono/validator");
|
|
6
6
|
//#region src/hono/validator.ts
|
|
7
|
+
/** Validates with the schema; a violation is an `INVALID_ARGUMENT` listing every field. */
|
|
8
|
+
async function check(schema, value) {
|
|
9
|
+
const result = await schema.safeParseAsync(value);
|
|
10
|
+
if (result.success) return result.data;
|
|
11
|
+
const fieldViolations = result.error.issues.map(({ code, path, message }) => ({
|
|
12
|
+
field: path.join("."),
|
|
13
|
+
description: message,
|
|
14
|
+
reason: code.toUpperCase(),
|
|
15
|
+
localizedMessage: {
|
|
16
|
+
locale: "en-US",
|
|
17
|
+
message
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
const details = require_error_detail.Details.new().badRequest({ fieldViolations });
|
|
21
|
+
throw require_error_status.Status.invalidArgument().error(details);
|
|
22
|
+
}
|
|
7
23
|
function zValidator(target, schema) {
|
|
8
|
-
return (0, hono_validator.validator)(target,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
return (0, hono_validator.validator)(target, (value) => check(schema, value));
|
|
25
|
+
}
|
|
26
|
+
const TEXT_PLAIN = /^text\/plain\b/i;
|
|
27
|
+
/**
|
|
28
|
+
* A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with
|
|
29
|
+
* `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin
|
|
30
|
+
* beacon needs a preflight, which a page being closed often cannot complete, and the beacon is
|
|
31
|
+
* then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any
|
|
32
|
+
* content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.
|
|
33
|
+
*
|
|
34
|
+
* Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can
|
|
35
|
+
* post without a preflight, which is harmless for an endpoint that takes no credentials.
|
|
36
|
+
*/
|
|
37
|
+
function zBeaconJson(schema) {
|
|
38
|
+
return (0, hono_validator.validator)("json", async (value, c) => {
|
|
39
|
+
if (!TEXT_PLAIN.test(c.req.header("Content-Type") ?? "")) return check(schema, value);
|
|
40
|
+
let body;
|
|
41
|
+
try {
|
|
42
|
+
body = JSON.parse(await c.req.text());
|
|
43
|
+
} catch {
|
|
44
|
+
throw require_error_status.Status.invalidArgument("Malformed JSON in request body").error();
|
|
45
|
+
}
|
|
46
|
+
return check(schema, body);
|
|
22
47
|
});
|
|
23
48
|
}
|
|
24
49
|
const bigintId = (0, zod_mini.pipe)((0, zod_mini.string)(), (0, zod_mini.transform)((input, ctx) => {
|
|
@@ -45,6 +70,7 @@ const bigintId = (0, zod_mini.pipe)((0, zod_mini.string)(), (0, zod_mini.transfo
|
|
|
45
70
|
}));
|
|
46
71
|
//#endregion
|
|
47
72
|
exports.bigintId = bigintId;
|
|
73
|
+
exports.zBeaconJson = zBeaconJson;
|
|
48
74
|
exports.zValidator = zValidator;
|
|
49
75
|
|
|
50
76
|
//# sourceMappingURL=validator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.cjs","names":["
|
|
1
|
+
{"version":3,"file":"validator.cjs","names":["Details","Status","validator","pipe","string","transform","NEVER"],"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\n/** Validates with the schema; a violation is an `INVALID_ARGUMENT` listing every field. */\nasync function check<S extends ZodType | ZodMiniType>(schema: S, value: unknown) {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code.toUpperCase(),\n localizedMessage: { locale: 'en-US', message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n}\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, (value) => check(schema, value));\n}\n\nconst TEXT_PLAIN = /^text\\/plain\\b/i;\n\n/**\n * A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with\n * `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin\n * beacon needs a preflight, which a page being closed often cannot complete, and the beacon is\n * then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any\n * content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.\n *\n * Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can\n * post without a preflight, which is harmless for an endpoint that takes no credentials.\n */\nexport function zBeaconJson<S extends ZodType | ZodMiniType>(schema: S) {\n return validator('json', async (value, c) => {\n if (!TEXT_PLAIN.test(c.req.header('Content-Type') ?? '')) return check(schema, value);\n let body: unknown;\n try {\n body = JSON.parse(await c.req.text());\n } catch {\n throw Status.invalidArgument('Malformed JSON in request body').error();\n }\n return check(schema, body);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";;;;;;;AAeA,eAAe,MAAuC,QAAW,OAAgB;CAC/E,MAAM,SAAS,MAAM,OAAO,eAAe,KAAK;CAChD,IAAI,OAAO,SAAS,OAAO,OAAO;CAElC,MAAM,kBAAiD,OAAO,MAAM,OAAO,KACxE,EAAE,MAAM,MAAM,eAAe;EAC5B,OAAO,KAAK,KAAK,GAAG;EACpB,aAAa;EACb,QAAQ,KAAK,YAAY;EACzB,kBAAkB;GAAE,QAAQ;GAAS;EAAQ;CAC/C,EACF;CACA,MAAM,UAAUA,qBAAAA,QAAQ,IAAI,CAAC,CAAC,WAAW,EAAE,gBAAgB,CAAC;CAC5D,MAAMC,qBAAAA,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO;AAC9C;AAEA,SAAgB,WACd,QACA,QACA;CACA,QAAA,GAAOC,eAAAA,UAAAA,CAAU,SAAS,UAAU,MAAM,QAAQ,KAAK,CAAC;AAC1D;AAEA,MAAM,aAAa;;;;;;;;;;;AAYnB,SAAgB,YAA6C,QAAW;CACtE,QAAA,GAAOA,eAAAA,UAAAA,CAAU,QAAQ,OAAO,OAAO,MAAM;EAC3C,IAAI,CAAC,WAAW,KAAK,EAAE,IAAI,OAAO,cAAc,KAAK,EAAE,GAAG,OAAO,MAAM,QAAQ,KAAK;EACpF,IAAI;EACJ,IAAI;GACF,OAAO,KAAK,MAAM,MAAM,EAAE,IAAI,KAAK,CAAC;EACtC,QAAQ;GACN,MAAMD,qBAAAA,OAAO,gBAAgB,gCAAgC,CAAC,CAAC,MAAM;EACvE;EACA,OAAO,MAAM,QAAQ,IAAI;CAC3B,CAAC;AACH;AAEA,MAAa,YAAA,GAAWE,SAAAA,KAAAA,EAAAA,GACtBC,SAAAA,OAAAA,CAAO,IAAA,GACPC,SAAAA,UAAAA,EAAW,OAAO,QAAQ;CACxB,IAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;EACtC,MAAM,UAAU,sBAAsB;EACtC,IAAI,OAAO,KAAK;GAAE,MAAM;GAAU;GAAO;EAAQ,CAAC;EAClD,OAAOC,SAAAA;CACT;CACA,IAAI;EACF,OAAO,OAAO,KAAK;CACrB,QAAQ;EACN,MAAM,UAAU,oBAAoB,MAAM;EAC1C,IAAI,OAAO,KAAK;GAAE,MAAM;GAAU;GAAO;EAAQ,CAAC;EAClD,OAAOA,SAAAA;CACT;AACF,CAAC,CACH"}
|
|
@@ -1,25 +1,43 @@
|
|
|
1
1
|
import { ValidationTargets } from "hono";
|
|
2
|
-
import { ZodType
|
|
3
|
-
import { ZodMiniType
|
|
2
|
+
import { ZodType } from "zod";
|
|
3
|
+
import { ZodMiniType } from "zod/mini";
|
|
4
4
|
//#region src/hono/validator.d.ts
|
|
5
5
|
export declare function zValidator<S extends ZodType | ZodMiniType>(target: keyof ValidationTargets, schema: S): import("hono").MiddlewareHandler<any, string, {
|
|
6
6
|
in: {
|
|
7
|
-
cookie: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
8
|
-
form: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
9
|
-
header: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
10
|
-
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
11
|
-
param: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
12
|
-
query: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
7
|
+
cookie: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "cookie", import("hono/types").FormValue>;
|
|
8
|
+
form: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "form", import("hono/types").FormValue>;
|
|
9
|
+
header: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "header", import("hono/types").FormValue>;
|
|
10
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_1) ? T_1 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_1 extends Response | import("hono").TypedResponse<any, any, any> ? never : T_1 : never : never;
|
|
11
|
+
param: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "param", import("hono/types").FormValue>;
|
|
12
|
+
query: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "query", import("hono/types").FormValue>;
|
|
13
13
|
};
|
|
14
14
|
out: {
|
|
15
|
-
cookie: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
16
|
-
form: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
17
|
-
header: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
18
|
-
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
19
|
-
param: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
20
|
-
query: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
15
|
+
cookie: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
16
|
+
form: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
17
|
+
header: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
18
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
19
|
+
param: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
20
|
+
query: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
21
21
|
};
|
|
22
|
-
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
22
|
+
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_2) ? T_2 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_2 extends import("hono").TypedResponse<infer T_3, infer S_1 extends import("hono/utils/http-status").StatusCode, infer F extends string> ? import("hono").TypedResponse<T_3, S_1, F> : T_2 extends Response ? T_2 : T_2 extends undefined ? never : never : never : never>;
|
|
23
|
+
/**
|
|
24
|
+
* A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with
|
|
25
|
+
* `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin
|
|
26
|
+
* beacon needs a preflight, which a page being closed often cannot complete, and the beacon is
|
|
27
|
+
* then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any
|
|
28
|
+
* content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.
|
|
29
|
+
*
|
|
30
|
+
* Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can
|
|
31
|
+
* post without a preflight, which is harmless for an endpoint that takes no credentials.
|
|
32
|
+
*/
|
|
33
|
+
export declare function zBeaconJson<S extends ZodType | ZodMiniType>(schema: S): import("hono").MiddlewareHandler<any, string, {
|
|
34
|
+
in: {
|
|
35
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
36
|
+
};
|
|
37
|
+
out: {
|
|
38
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_1) ? T_1 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_1 extends Response | import("hono").TypedResponse<any, any, any> ? never : T_1 : never : never;
|
|
39
|
+
};
|
|
40
|
+
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_2) ? T_2 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_2 extends import("hono").TypedResponse<infer T_3, infer S_1 extends import("hono/utils/http-status").StatusCode, infer F extends string> ? import("hono").TypedResponse<T_3, S_1, F> : T_2 extends Response ? T_2 : T_2 extends undefined ? never : never : never : never>;
|
|
23
41
|
export declare const bigintId: import("zod/mini").ZodMiniPipe<import("zod/mini").ZodMiniString<string>, import("zod/mini").ZodMiniTransform<bigint, string>>;
|
|
24
42
|
//#endregion
|
|
25
43
|
//# sourceMappingURL=validator.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.cts","names":[
|
|
1
|
+
{"version":3,"file":"validator.d.cts","names":[],"sources":["../../src/hono/validator.ts"],"mappings":";;;;wBA+BgB,WAAW,UAAU,UAAU,aAC7C,cAAc,mBACd,QAAQ,mBAAC;;;;;;;;;;;;;;;;;IAGV,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,mBAAA,OAAA,aAAA,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,MAAA,2BAAA,oBAAA,WAAA,6CAAA,kBAAA,mCAAA,cAAA,KAAA,KAAA,KAAA,YAAA,WAAA,MAAA;;;;;;;;;;;wBAce,YAAY,UAAU,UAAU,aAAa,QAAQ,mBAAC;;;;;;;IAWrE,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,mBAAA,OAAA,aAAA,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,MAAA,2BAAA,oBAAA,WAAA,6CAAA,kBAAA,mCAAA,cAAA,KAAA,KAAA,KAAA,YAAA,WAAA,MAAA;qBAEY,6BAAQ,+BAAA,0CAAA"}
|
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
import { ZodMiniType
|
|
1
|
+
import { ZodMiniType } from "zod/mini";
|
|
2
2
|
import { ValidationTargets } from "hono";
|
|
3
|
-
import { ZodType
|
|
3
|
+
import { ZodType } from "zod";
|
|
4
4
|
//#region src/hono/validator.d.ts
|
|
5
5
|
export declare function zValidator<S extends ZodType | ZodMiniType>(target: keyof ValidationTargets, schema: S): import("hono").MiddlewareHandler<any, string, {
|
|
6
6
|
in: {
|
|
7
|
-
cookie: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
8
|
-
form: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
9
|
-
header: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
10
|
-
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
11
|
-
param: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
12
|
-
query: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
7
|
+
cookie: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "cookie", import("hono/types").FormValue>;
|
|
8
|
+
form: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "form", import("hono/types").FormValue>;
|
|
9
|
+
header: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "header", import("hono/types").FormValue>;
|
|
10
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_1) ? T_1 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_1 extends Response | import("hono").TypedResponse<any, any, any> ? never : T_1 : never : never;
|
|
11
|
+
param: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "param", import("hono/types").FormValue>;
|
|
12
|
+
query: import("hono/validator").InferInput<(S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never, "query", import("hono/types").FormValue>;
|
|
13
13
|
};
|
|
14
14
|
out: {
|
|
15
|
-
cookie: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
16
|
-
form: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
17
|
-
header: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
18
|
-
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
19
|
-
param: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
20
|
-
query: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
15
|
+
cookie: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
16
|
+
form: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
17
|
+
header: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
18
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
19
|
+
param: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
20
|
+
query: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
21
21
|
};
|
|
22
|
-
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ?
|
|
22
|
+
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_2) ? T_2 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_2 extends import("hono").TypedResponse<infer T_3, infer S_1 extends import("hono/utils/http-status").StatusCode, infer F extends string> ? import("hono").TypedResponse<T_3, S_1, F> : T_2 extends Response ? T_2 : T_2 extends undefined ? never : never : never : never>;
|
|
23
|
+
/**
|
|
24
|
+
* A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with
|
|
25
|
+
* `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin
|
|
26
|
+
* beacon needs a preflight, which a page being closed often cannot complete, and the beacon is
|
|
27
|
+
* then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any
|
|
28
|
+
* content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.
|
|
29
|
+
*
|
|
30
|
+
* Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can
|
|
31
|
+
* post without a preflight, which is harmless for an endpoint that takes no credentials.
|
|
32
|
+
*/
|
|
33
|
+
export declare function zBeaconJson<S extends ZodType | ZodMiniType>(schema: S): import("hono").MiddlewareHandler<any, string, {
|
|
34
|
+
in: {
|
|
35
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T) ? T extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T extends Response | import("hono").TypedResponse<any, any, any> ? never : T : never : never;
|
|
36
|
+
};
|
|
37
|
+
out: {
|
|
38
|
+
json: (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_1) ? T_1 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_1 extends Response | import("hono").TypedResponse<any, any, any> ? never : T_1 : never : never;
|
|
39
|
+
};
|
|
40
|
+
}, (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) extends (infer T_2) ? T_2 extends (S extends ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> ? import("zod").infer<S> : import("zod").infer<S>) ? T_2 extends import("hono").TypedResponse<infer T_3, infer S_1 extends import("hono/utils/http-status").StatusCode, infer F extends string> ? import("hono").TypedResponse<T_3, S_1, F> : T_2 extends Response ? T_2 : T_2 extends undefined ? never : never : never : never>;
|
|
23
41
|
export declare const bigintId: import("zod/mini").ZodMiniPipe<import("zod/mini").ZodMiniString<string>, import("zod/mini").ZodMiniTransform<bigint, string>>;
|
|
24
42
|
//#endregion
|
|
25
43
|
//# sourceMappingURL=validator.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.mts","names":[
|
|
1
|
+
{"version":3,"file":"validator.d.mts","names":[],"sources":["../../src/hono/validator.ts"],"mappings":";;;;wBA+BgB,WAAW,UAAU,UAAU,aAC7C,cAAc,mBACd,QAAQ,mBAAC;;;;;;;;;;;;;;;;;IAGV,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,mBAAA,OAAA,aAAA,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,MAAA,2BAAA,oBAAA,WAAA,6CAAA,kBAAA,mCAAA,cAAA,KAAA,KAAA,KAAA,YAAA,WAAA,MAAA;;;;;;;;;;;wBAce,YAAY,UAAU,UAAU,aAAa,QAAQ,mBAAC;;;;;;;IAWrE,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,mBAAA,OAAA,aAAA,UAAA,gDAAA,qDAAA,MAAA,mBAAA,MAAA,MAAA,2BAAA,oBAAA,WAAA,6CAAA,kBAAA,mCAAA,cAAA,KAAA,KAAA,KAAA,YAAA,WAAA,MAAA;qBAEY,6BAAQ,+BAAA,0CAAA"}
|
package/dist/hono/validator.mjs
CHANGED
|
@@ -3,21 +3,46 @@ import { Status } from "../error/status.mjs";
|
|
|
3
3
|
import { NEVER, pipe, string, transform } from "zod/mini";
|
|
4
4
|
import { validator } from "hono/validator";
|
|
5
5
|
//#region src/hono/validator.ts
|
|
6
|
+
/** Validates with the schema; a violation is an `INVALID_ARGUMENT` listing every field. */
|
|
7
|
+
async function check(schema, value) {
|
|
8
|
+
const result = await schema.safeParseAsync(value);
|
|
9
|
+
if (result.success) return result.data;
|
|
10
|
+
const fieldViolations = result.error.issues.map(({ code, path, message }) => ({
|
|
11
|
+
field: path.join("."),
|
|
12
|
+
description: message,
|
|
13
|
+
reason: code.toUpperCase(),
|
|
14
|
+
localizedMessage: {
|
|
15
|
+
locale: "en-US",
|
|
16
|
+
message
|
|
17
|
+
}
|
|
18
|
+
}));
|
|
19
|
+
const details = Details.new().badRequest({ fieldViolations });
|
|
20
|
+
throw Status.invalidArgument().error(details);
|
|
21
|
+
}
|
|
6
22
|
function zValidator(target, schema) {
|
|
7
|
-
return validator(target,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
return validator(target, (value) => check(schema, value));
|
|
24
|
+
}
|
|
25
|
+
const TEXT_PLAIN = /^text\/plain\b/i;
|
|
26
|
+
/**
|
|
27
|
+
* A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with
|
|
28
|
+
* `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin
|
|
29
|
+
* beacon needs a preflight, which a page being closed often cannot complete, and the beacon is
|
|
30
|
+
* then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any
|
|
31
|
+
* content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.
|
|
32
|
+
*
|
|
33
|
+
* Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can
|
|
34
|
+
* post without a preflight, which is harmless for an endpoint that takes no credentials.
|
|
35
|
+
*/
|
|
36
|
+
function zBeaconJson(schema) {
|
|
37
|
+
return validator("json", async (value, c) => {
|
|
38
|
+
if (!TEXT_PLAIN.test(c.req.header("Content-Type") ?? "")) return check(schema, value);
|
|
39
|
+
let body;
|
|
40
|
+
try {
|
|
41
|
+
body = JSON.parse(await c.req.text());
|
|
42
|
+
} catch {
|
|
43
|
+
throw Status.invalidArgument("Malformed JSON in request body").error();
|
|
44
|
+
}
|
|
45
|
+
return check(schema, body);
|
|
21
46
|
});
|
|
22
47
|
}
|
|
23
48
|
const bigintId = pipe(string(), transform((input, ctx) => {
|
|
@@ -43,6 +68,6 @@ const bigintId = pipe(string(), transform((input, ctx) => {
|
|
|
43
68
|
}
|
|
44
69
|
}));
|
|
45
70
|
//#endregion
|
|
46
|
-
export { bigintId, zValidator };
|
|
71
|
+
export { bigintId, zBeaconJson, zValidator };
|
|
47
72
|
|
|
48
73
|
//# sourceMappingURL=validator.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.mjs","names":[],"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\
|
|
1
|
+
{"version":3,"file":"validator.mjs","names":[],"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n NEVER,\n type ZodMiniType,\n type output as outputMini,\n pipe,\n string,\n transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\n/** Validates with the schema; a violation is an `INVALID_ARGUMENT` listing every field. */\nasync function check<S extends ZodType | ZodMiniType>(schema: S, value: unknown) {\n const result = await schema.safeParseAsync(value);\n if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n ({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code.toUpperCase(),\n localizedMessage: { locale: 'en-US', message },\n })\n );\n const details = Details.new().badRequest({ fieldViolations });\n throw Status.invalidArgument().error(details);\n}\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n target: keyof ValidationTargets,\n schema: S\n) {\n return validator(target, (value) => check(schema, value));\n}\n\nconst TEXT_PLAIN = /^text\\/plain\\b/i;\n\n/**\n * A JSON body that may also arrive as `text/plain`, validated as `json`: what a browser sends with\n * `navigator.sendBeacon` to stay a CORS simple request. As `application/json` a cross-origin\n * beacon needs a preflight, which a page being closed often cannot complete, and the beacon is\n * then dropped — exactly the one sent as the visitor leaves. Hono's `json` target reads any\n * content type but JSON as `{}`, so the text is parsed here. Anything else is read as before.\n *\n * Use it on the routes a beacon is sent to, and only there: it widens what a cross-site page can\n * post without a preflight, which is harmless for an endpoint that takes no credentials.\n */\nexport function zBeaconJson<S extends ZodType | ZodMiniType>(schema: S) {\n return validator('json', async (value, c) => {\n if (!TEXT_PLAIN.test(c.req.header('Content-Type') ?? '')) return check(schema, value);\n let body: unknown;\n try {\n body = JSON.parse(await c.req.text());\n } catch {\n throw Status.invalidArgument('Malformed JSON in request body').error();\n }\n return check(schema, body);\n });\n}\n\nexport const bigintId = pipe(\n string(),\n transform((input, ctx) => {\n if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n const message = `Invalid bigint id: ${input}`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n try {\n return BigInt(input);\n } catch {\n const message = `Parse bigint id: ${input} failed`;\n ctx.issues.push({ code: 'custom', input, message });\n return NEVER;\n }\n })\n);\n"],"mappings":";;;;;;AAeA,eAAe,MAAuC,QAAW,OAAgB;CAC/E,MAAM,SAAS,MAAM,OAAO,eAAe,KAAK;CAChD,IAAI,OAAO,SAAS,OAAO,OAAO;CAElC,MAAM,kBAAiD,OAAO,MAAM,OAAO,KACxE,EAAE,MAAM,MAAM,eAAe;EAC5B,OAAO,KAAK,KAAK,GAAG;EACpB,aAAa;EACb,QAAQ,KAAK,YAAY;EACzB,kBAAkB;GAAE,QAAQ;GAAS;EAAQ;CAC/C,EACF;CACA,MAAM,UAAU,QAAQ,IAAI,CAAC,CAAC,WAAW,EAAE,gBAAgB,CAAC;CAC5D,MAAM,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO;AAC9C;AAEA,SAAgB,WACd,QACA,QACA;CACA,OAAO,UAAU,SAAS,UAAU,MAAM,QAAQ,KAAK,CAAC;AAC1D;AAEA,MAAM,aAAa;;;;;;;;;;;AAYnB,SAAgB,YAA6C,QAAW;CACtE,OAAO,UAAU,QAAQ,OAAO,OAAO,MAAM;EAC3C,IAAI,CAAC,WAAW,KAAK,EAAE,IAAI,OAAO,cAAc,KAAK,EAAE,GAAG,OAAO,MAAM,QAAQ,KAAK;EACpF,IAAI;EACJ,IAAI;GACF,OAAO,KAAK,MAAM,MAAM,EAAE,IAAI,KAAK,CAAC;EACtC,QAAQ;GACN,MAAM,OAAO,gBAAgB,gCAAgC,CAAC,CAAC,MAAM;EACvE;EACA,OAAO,MAAM,QAAQ,IAAI;CAC3B,CAAC;AACH;AAEA,MAAa,WAAW,KACtB,OAAO,GACP,WAAW,OAAO,QAAQ;CACxB,IAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;EACtC,MAAM,UAAU,sBAAsB;EACtC,IAAI,OAAO,KAAK;GAAE,MAAM;GAAU;GAAO;EAAQ,CAAC;EAClD,OAAO;CACT;CACA,IAAI;EACF,OAAO,OAAO,KAAK;CACrB,QAAQ;EACN,MAAM,UAAU,oBAAoB,MAAM;EAC1C,IAAI,OAAO,KAAK;GAAE,MAAM;GAAU;GAAO;EAAQ,CAAC;EAClD,OAAO;CACT;AACF,CAAC,CACH"}
|