@nice-code/common-errors 0.9.0 → 0.11.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/build/hono/index.cjs +37 -0
- package/build/hono/index.cjs.map +1 -0
- package/build/hono/{index.d.ts → index.d.cts} +1 -1
- package/build/hono/index.d.mts +14 -0
- package/build/hono/{index.js → index.mjs} +2 -2
- package/build/hono/index.mjs.map +1 -0
- package/build/index.cjs +5 -0
- package/build/index.d.cts +24 -0
- package/build/{index.d.ts → index.d.mts} +1 -1
- package/build/{index.js → index.mjs} +1 -1
- package/build/{validation-788jnLm7.js → validation-788jnLm7.mjs} +1 -1
- package/build/validation-788jnLm7.mjs.map +1 -0
- package/build/{validation-DeF-MFLS.js → validation-DeF-MFLS.cjs} +1 -1
- package/build/validation-DeF-MFLS.cjs.map +1 -0
- package/package.json +22 -9
- package/build/hono/index.js.map +0 -1
- package/build/validation-788jnLm7.js.map +0 -1
- package/build/validation-DeF-MFLS.js.map +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_validation = require("../validation-DeF-MFLS.cjs");
|
|
3
|
+
let _hono_standard_validator = require("@hono/standard-validator");
|
|
4
|
+
//#region src/hono/niceCatchSValidation.ts
|
|
5
|
+
const niceCatchSValidation = () => async (ctx, next) => {
|
|
6
|
+
await next();
|
|
7
|
+
if (!ctx.res.ok) {
|
|
8
|
+
const clonedResponse = ctx.res.clone();
|
|
9
|
+
if (clonedResponse.headers.get("content-type")?.includes("application/json")) try {
|
|
10
|
+
const responseJson = await clonedResponse.json();
|
|
11
|
+
if (responseJson["success"] != null && responseJson["error"] != null) {
|
|
12
|
+
console.log("Intercepted JSON:", responseJson);
|
|
13
|
+
const result = responseJson;
|
|
14
|
+
const newError = require_validation.err_validation.fromId("standard_schema", { issues: result.error });
|
|
15
|
+
ctx.res = void 0;
|
|
16
|
+
ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {
|
|
17
|
+
status: newError.httpStatusCode,
|
|
18
|
+
headers: { "Content-Type": "application/json" }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error("Failed to parse response JSON:", error);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/hono/niceSValidator.ts
|
|
28
|
+
function niceSValidator(target, schema) {
|
|
29
|
+
return (0, _hono_standard_validator.sValidator)(target, schema, (result) => {
|
|
30
|
+
if (!result.success) throw require_validation.err_validation.fromId("standard_schema", { issues: result.error });
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.niceCatchSValidation = niceCatchSValidation;
|
|
35
|
+
exports.niceSValidator = niceSValidator;
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["err_validation","err_validation"],"sources":["../../src/hono/niceCatchSValidation.ts","../../src/hono/niceSValidator.ts"],"sourcesContent":["import type { MiddlewareHandler } from \"hono\";\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\";\nimport { EValidator, err_validation } from \"../validation\";\nimport type { IErrContext_HonoStandardSchema } from \"../validation/err_validation.types\";\n\nexport const niceCatchSValidation = (): MiddlewareHandler => async (ctx, next) => {\n // 1. Execute downstream routes and wait for them to finish\n await next();\n\n if (!ctx.res.ok) {\n // 2. Clone the response so we don't consume the original body stream\n const clonedResponse = ctx.res.clone();\n\n // 3. Safely check if the response is actually JSON\n const contentType = clonedResponse.headers.get(\"content-type\");\n\n if (contentType?.includes(\"application/json\")) {\n try {\n // 4. Parse the JSON from the cloned response\n const responseJson = (await clonedResponse.json()) as any;\n\n if (responseJson[\"success\"] != null && responseJson[\"error\"] != null) {\n console.log(\"Intercepted JSON:\", responseJson);\n const result = responseJson as {\n success: false;\n error: IErrContext_HonoStandardSchema[\"issues\"];\n data: unknown;\n };\n const newError = err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n ctx.res = undefined;\n ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {\n status: newError.httpStatusCode as ContentfulStatusCode,\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n }\n } catch (error) {\n console.error(\"Failed to parse response JSON:\", error);\n }\n }\n }\n};\n","import { sValidator } from \"@hono/standard-validator\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\nimport { EValidator, err_validation } from \"../validation/err_validation\";\n\nexport function niceSValidator<\n Schema extends StandardSchemaV1,\n Target extends keyof ValidationTargets,\n>(target: Target, schema: Schema) {\n return sValidator(target, schema, (result) => {\n if (!result.success) {\n throw err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n }\n });\n}\n"],"mappings":";;;;AAKA,MAAa,6BAAgD,OAAO,KAAK,SAAS;CAEhF,MAAM,KAAK;CAEX,IAAI,CAAC,IAAI,IAAI,IAAI;EAEf,MAAM,iBAAiB,IAAI,IAAI,MAAM;EAKrC,IAFoB,eAAe,QAAQ,IAAI,cAEjC,CAAC,EAAE,SAAS,kBAAkB,GAC1C,IAAI;GAEF,MAAM,eAAgB,MAAM,eAAe,KAAK;GAEhD,IAAI,aAAa,cAAc,QAAQ,aAAa,YAAY,MAAM;IACpE,QAAQ,IAAI,qBAAqB,YAAY;IAC7C,MAAM,SAAS;IAKf,MAAM,WAAWA,mBAAAA,eAAe,OAAA,mBAAmC,EACjE,QAAQ,OAAO,MACjB,CAAC;IACD,IAAI,MAAM,KAAA;IACV,IAAI,MAAM,IAAI,SAAS,KAAK,UAAU,SAAS,aAAa,CAAC,GAAG;KAC9D,QAAQ,SAAS;KACjB,SAAS,EACP,gBAAgB,mBAClB;IACF,CAAC;GACH;EACF,SAAS,OAAO;GACd,QAAQ,MAAM,kCAAkC,KAAK;EACvD;CAEJ;AACF;;;ACvCA,SAAgB,eAGd,QAAgB,QAAgB;CAChC,QAAA,GAAA,yBAAA,WAAA,CAAkB,QAAQ,SAAS,WAAW;EAC5C,IAAI,CAAC,OAAO,SACV,MAAMC,mBAAAA,eAAe,OAAA,mBAAmC,EACtD,QAAQ,OAAO,MACjB,CAAC;CAEL,CAAC;AACH"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MiddlewareHandler, ValidationTargets } from "hono";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/hono/niceCatchSValidation.d.ts
|
|
5
|
+
declare const niceCatchSValidation: () => MiddlewareHandler;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/hono/niceSValidator.d.ts
|
|
8
|
+
declare function niceSValidator<Schema extends StandardSchemaV1, Target extends keyof ValidationTargets>(target: Target, schema: Schema): import("hono").Handler<import("hono").Env, string, {
|
|
9
|
+
in: (undefined extends StandardSchemaV1.InferInput<Schema> ? true : false) extends true ? { [K in Target]?: (StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K] ? T : { [K2 in keyof T]?: ValidationTargets[K][K2] | undefined } : never : never) | undefined } : { [K_1 in Target]: StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K_1] ? T : { [K2 in keyof T]: ValidationTargets[K_1][K2] } : never : never };
|
|
10
|
+
out: { [K_2 in Target]: StandardSchemaV1.InferOutput<Schema> };
|
|
11
|
+
}, never>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { niceCatchSValidation, niceSValidator };
|
|
14
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as err_validation } from "../validation-788jnLm7.
|
|
1
|
+
import { n as err_validation } from "../validation-788jnLm7.mjs";
|
|
2
2
|
import { sValidator } from "@hono/standard-validator";
|
|
3
3
|
//#region src/hono/niceCatchSValidation.ts
|
|
4
4
|
const niceCatchSValidation = () => async (ctx, next) => {
|
|
@@ -32,4 +32,4 @@ function niceSValidator(target, schema) {
|
|
|
32
32
|
//#endregion
|
|
33
33
|
export { niceCatchSValidation, niceSValidator };
|
|
34
34
|
|
|
35
|
-
//# sourceMappingURL=index.
|
|
35
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/hono/niceCatchSValidation.ts","../../src/hono/niceSValidator.ts"],"sourcesContent":["import type { MiddlewareHandler } from \"hono\";\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\";\nimport { EValidator, err_validation } from \"../validation\";\nimport type { IErrContext_HonoStandardSchema } from \"../validation/err_validation.types\";\n\nexport const niceCatchSValidation = (): MiddlewareHandler => async (ctx, next) => {\n // 1. Execute downstream routes and wait for them to finish\n await next();\n\n if (!ctx.res.ok) {\n // 2. Clone the response so we don't consume the original body stream\n const clonedResponse = ctx.res.clone();\n\n // 3. Safely check if the response is actually JSON\n const contentType = clonedResponse.headers.get(\"content-type\");\n\n if (contentType?.includes(\"application/json\")) {\n try {\n // 4. Parse the JSON from the cloned response\n const responseJson = (await clonedResponse.json()) as any;\n\n if (responseJson[\"success\"] != null && responseJson[\"error\"] != null) {\n console.log(\"Intercepted JSON:\", responseJson);\n const result = responseJson as {\n success: false;\n error: IErrContext_HonoStandardSchema[\"issues\"];\n data: unknown;\n };\n const newError = err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n ctx.res = undefined;\n ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {\n status: newError.httpStatusCode as ContentfulStatusCode,\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n }\n } catch (error) {\n console.error(\"Failed to parse response JSON:\", error);\n }\n }\n }\n};\n","import { sValidator } from \"@hono/standard-validator\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\nimport { EValidator, err_validation } from \"../validation/err_validation\";\n\nexport function niceSValidator<\n Schema extends StandardSchemaV1,\n Target extends keyof ValidationTargets,\n>(target: Target, schema: Schema) {\n return sValidator(target, schema, (result) => {\n if (!result.success) {\n throw err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n }\n });\n}\n"],"mappings":";;;AAKA,MAAa,6BAAgD,OAAO,KAAK,SAAS;CAEhF,MAAM,KAAK;CAEX,IAAI,CAAC,IAAI,IAAI,IAAI;EAEf,MAAM,iBAAiB,IAAI,IAAI,MAAM;EAKrC,IAFoB,eAAe,QAAQ,IAAI,cAEjC,CAAC,EAAE,SAAS,kBAAkB,GAC1C,IAAI;GAEF,MAAM,eAAgB,MAAM,eAAe,KAAK;GAEhD,IAAI,aAAa,cAAc,QAAQ,aAAa,YAAY,MAAM;IACpE,QAAQ,IAAI,qBAAqB,YAAY;IAC7C,MAAM,SAAS;IAKf,MAAM,WAAW,eAAe,OAAA,mBAAmC,EACjE,QAAQ,OAAO,MACjB,CAAC;IACD,IAAI,MAAM,KAAA;IACV,IAAI,MAAM,IAAI,SAAS,KAAK,UAAU,SAAS,aAAa,CAAC,GAAG;KAC9D,QAAQ,SAAS;KACjB,SAAS,EACP,gBAAgB,mBAClB;IACF,CAAC;GACH;EACF,SAAS,OAAO;GACd,QAAQ,MAAM,kCAAkC,KAAK;EACvD;CAEJ;AACF;;;ACvCA,SAAgB,eAGd,QAAgB,QAAgB;CAChC,OAAO,WAAW,QAAQ,SAAS,WAAW;EAC5C,IAAI,CAAC,OAAO,SACV,MAAM,eAAe,OAAA,mBAAmC,EACtD,QAAQ,OAAO,MACjB,CAAC;CAEL,CAAC;AACH"}
|
package/build/index.cjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_validation = require("./validation-DeF-MFLS.cjs");
|
|
3
|
+
exports.EValidator = require_validation.EValidator;
|
|
4
|
+
exports.err_validation = require_validation.err_validation;
|
|
5
|
+
exports.extractMessageFromStandardSchema = require_validation.extractMessageFromStandardSchema;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/validation/err_validation.types.d.ts
|
|
4
|
+
interface IErrContext_HonoStandardSchema {
|
|
5
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/validation/err_validation.d.ts
|
|
9
|
+
declare enum EValidator {
|
|
10
|
+
standard_schema = "standard_schema"
|
|
11
|
+
}
|
|
12
|
+
declare const err_validation: import("@nice-code/error").NiceErrorDefined<{
|
|
13
|
+
domain: string;
|
|
14
|
+
allDomains: [string, "err_nice"];
|
|
15
|
+
schema: {
|
|
16
|
+
standard_schema: import("@nice-code/error").INiceErrorIdMetadata<IErrContext_HonoStandardSchema, import("@nice-code/error").JSONSerializableValue>;
|
|
17
|
+
};
|
|
18
|
+
}>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/validation/standard_schema/extractMessageFromStandardSchema.d.ts
|
|
21
|
+
declare const extractMessageFromStandardSchema: (failureResult: StandardSchemaV1.FailureResult) => string;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { EValidator, err_validation, extractMessageFromStandardSchema };
|
|
24
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -21,4 +21,4 @@ declare const err_validation: import("@nice-code/error").NiceErrorDomain<{
|
|
|
21
21
|
declare const extractMessageFromStandardSchema: (failureResult: StandardSchemaV1.FailureResult) => string;
|
|
22
22
|
//#endregion
|
|
23
23
|
export { EValidator, err_validation, extractMessageFromStandardSchema };
|
|
24
|
-
//# sourceMappingURL=index.d.
|
|
24
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as err_validation, r as extractMessageFromStandardSchema, t as EValidator } from "./validation-788jnLm7.
|
|
1
|
+
import { n as err_validation, r as extractMessageFromStandardSchema, t as EValidator } from "./validation-788jnLm7.mjs";
|
|
2
2
|
export { EValidator, err_validation, extractMessageFromStandardSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-788jnLm7.mjs","names":[],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiB,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuB,YAAY;CACnC,QAAQ,GAAA,oBACwB,IAAoC,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-DeF-MFLS.cjs","names":["err_nice","StatusCodes"],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiBA,iBAAAA,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuBC,kBAAAA,YAAY;CACnC,QAAQ,GAAA,qBAAA,GAAA,iBAAA,IAAA,CAC4D,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice-code/common-errors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
+
"main": "./build/index.cjs",
|
|
7
|
+
"module": "./build/index.mjs",
|
|
8
|
+
"types": "./build/index.d.cts",
|
|
6
9
|
"exports": {
|
|
7
10
|
".": {
|
|
8
11
|
"source": "./src/index.ts",
|
|
9
|
-
"
|
|
10
|
-
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./build/index.d.mts",
|
|
14
|
+
"default": "./build/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./build/index.d.cts",
|
|
18
|
+
"default": "./build/index.cjs"
|
|
19
|
+
}
|
|
11
20
|
},
|
|
12
21
|
"./hono": {
|
|
13
22
|
"source": "./src/hono/index.ts",
|
|
14
|
-
"
|
|
15
|
-
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./build/hono/index.d.mts",
|
|
25
|
+
"default": "./build/hono/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./build/hono/index.d.cts",
|
|
29
|
+
"default": "./build/hono/index.cjs"
|
|
30
|
+
}
|
|
16
31
|
}
|
|
17
32
|
},
|
|
18
33
|
"files": [
|
|
@@ -27,12 +42,10 @@
|
|
|
27
42
|
"type-check": "bunx tsc --noEmit",
|
|
28
43
|
"type-check-watch": "bunx tsc --noEmit --watch",
|
|
29
44
|
"clean-build": "bunx rimraf build",
|
|
30
|
-
"build
|
|
31
|
-
"build": "bun run clean-build && tsdown",
|
|
32
|
-
"build-types": "tsc --project tsconfig.build.json"
|
|
45
|
+
"build": "bun run clean-build && tsdown"
|
|
33
46
|
},
|
|
34
47
|
"dependencies": {
|
|
35
|
-
"@nice-code/error": "0.
|
|
48
|
+
"@nice-code/error": "0.11.0",
|
|
36
49
|
"@standard-schema/spec": "^1.1.0",
|
|
37
50
|
"@hono/standard-validator": "^0.2.2",
|
|
38
51
|
"http-status-codes": "^2.3.0"
|
package/build/hono/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/hono/niceCatchSValidation.ts","../../src/hono/niceSValidator.ts"],"sourcesContent":["import type { MiddlewareHandler } from \"hono\";\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\";\nimport { EValidator, err_validation } from \"../validation\";\nimport type { IErrContext_HonoStandardSchema } from \"../validation/err_validation.types\";\n\nexport const niceCatchSValidation = (): MiddlewareHandler => async (ctx, next) => {\n // 1. Execute downstream routes and wait for them to finish\n await next();\n\n if (!ctx.res.ok) {\n // 2. Clone the response so we don't consume the original body stream\n const clonedResponse = ctx.res.clone();\n\n // 3. Safely check if the response is actually JSON\n const contentType = clonedResponse.headers.get(\"content-type\");\n\n if (contentType?.includes(\"application/json\")) {\n try {\n // 4. Parse the JSON from the cloned response\n const responseJson = (await clonedResponse.json()) as any;\n\n if (responseJson[\"success\"] != null && responseJson[\"error\"] != null) {\n console.log(\"Intercepted JSON:\", responseJson);\n const result = responseJson as {\n success: false;\n error: IErrContext_HonoStandardSchema[\"issues\"];\n data: unknown;\n };\n const newError = err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n ctx.res = undefined;\n ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {\n status: newError.httpStatusCode as ContentfulStatusCode,\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n }\n } catch (error) {\n console.error(\"Failed to parse response JSON:\", error);\n }\n }\n }\n};\n","import { sValidator } from \"@hono/standard-validator\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\nimport { EValidator, err_validation } from \"../validation/err_validation\";\n\nexport function niceSValidator<\n Schema extends StandardSchemaV1,\n Target extends keyof ValidationTargets,\n>(target: Target, schema: Schema) {\n return sValidator(target, schema, (result) => {\n if (!result.success) {\n throw err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n }\n });\n}\n"],"mappings":";;;AAKA,MAAa,6BAAgD,OAAO,KAAK,SAAS;CAEhF,MAAM,KAAK;CAEX,IAAI,CAAC,IAAI,IAAI,IAAI;EAEf,MAAM,iBAAiB,IAAI,IAAI,MAAM;EAKrC,IAFoB,eAAe,QAAQ,IAAI,cAEjC,CAAC,EAAE,SAAS,kBAAkB,GAC1C,IAAI;GAEF,MAAM,eAAgB,MAAM,eAAe,KAAK;GAEhD,IAAI,aAAa,cAAc,QAAQ,aAAa,YAAY,MAAM;IACpE,QAAQ,IAAI,qBAAqB,YAAY;IAC7C,MAAM,SAAS;IAKf,MAAM,WAAW,eAAe,OAAA,mBAAmC,EACjE,QAAQ,OAAO,MACjB,CAAC;IACD,IAAI,MAAM,KAAA;IACV,IAAI,MAAM,IAAI,SAAS,KAAK,UAAU,SAAS,aAAa,CAAC,GAAG;KAC9D,QAAQ,SAAS;KACjB,SAAS,EACP,gBAAgB,mBAClB;IACF,CAAC;GACH;EACF,SAAS,OAAO;GACd,QAAQ,MAAM,kCAAkC,KAAK;EACvD;CAEJ;AACF;;;ACvCA,SAAgB,eAGd,QAAgB,QAAgB;CAChC,OAAO,WAAW,QAAQ,SAAS,WAAW;EAC5C,IAAI,CAAC,OAAO,SACV,MAAM,eAAe,OAAA,mBAAmC,EACtD,QAAQ,OAAO,MACjB,CAAC;CAEL,CAAC;AACH"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation-788jnLm7.js","names":[],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiB,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuB,YAAY;CACnC,QAAQ,GAAA,oBACwB,IAAoC,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation-DeF-MFLS.js","names":["err_nice","StatusCodes"],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiBA,iBAAAA,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuBC,kBAAAA,YAAY;CACnC,QAAQ,GAAA,qBAAA,GAAA,iBAAA,IAAA,CAC4D,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|