@nice-code/common-errors 0.2.10 → 0.2.11
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.js +99 -3
- package/build/index.js +60 -2
- package/package.json +2 -2
package/build/hono/index.js
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/validation/err_validation.ts
|
|
2
|
+
import { err, err_nice } from "@nice-code/error";
|
|
3
|
+
import { StatusCodes } from "http-status-codes";
|
|
4
|
+
|
|
5
|
+
// src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
6
|
+
function extractPathFromIssue(issue) {
|
|
7
|
+
let pathString = "";
|
|
8
|
+
for (const segment of issue) {
|
|
9
|
+
if (typeof segment === "object") {
|
|
10
|
+
if (segment.key != null) {
|
|
11
|
+
if (typeof segment.key === "number") {
|
|
12
|
+
pathString += `[${String(segment.key)}]`;
|
|
13
|
+
} else if (typeof segment.key === "symbol") {
|
|
14
|
+
pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
15
|
+
} else {
|
|
16
|
+
pathString += `.${String(segment.key)}`;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
pathString += `.${String(segment)}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return pathString.slice(1);
|
|
24
|
+
}
|
|
25
|
+
var extractMessageFromStandardSchema = (failureResult) => {
|
|
26
|
+
let message = `Data validation failed:
|
|
27
|
+
`;
|
|
28
|
+
let issueCount = 0;
|
|
29
|
+
for (const issue of failureResult.issues) {
|
|
30
|
+
issueCount++;
|
|
31
|
+
if (issue.path == null || issue.path.length === 0) {
|
|
32
|
+
message += ` (issue ${issueCount}) ${issue.message}
|
|
33
|
+
`;
|
|
34
|
+
} else {
|
|
35
|
+
message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return message;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/validation/err_validation.ts
|
|
43
|
+
var EValidator;
|
|
44
|
+
((EValidator2) => {
|
|
45
|
+
EValidator2["standard_schema"] = "standard_schema";
|
|
46
|
+
})(EValidator ||= {});
|
|
47
|
+
var err_validation = err_nice.createChildDomain({
|
|
48
|
+
domain: "err_validation",
|
|
49
|
+
defaultHttpStatusCode: StatusCodes.BAD_REQUEST,
|
|
50
|
+
schema: {
|
|
51
|
+
["standard_schema" /* standard_schema */]: err({
|
|
52
|
+
message: ({ issues }) => extractMessageFromStandardSchema({ issues })
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
// src/hono/niceCatchSValidation.ts
|
|
57
|
+
var niceCatchSValidation = () => async (ctx, next) => {
|
|
58
|
+
await next();
|
|
59
|
+
if (!ctx.res.ok) {
|
|
60
|
+
const clonedResponse = ctx.res.clone();
|
|
61
|
+
const contentType = clonedResponse.headers.get("content-type");
|
|
62
|
+
if (contentType?.includes("application/json")) {
|
|
63
|
+
try {
|
|
64
|
+
const responseJson = await clonedResponse.json();
|
|
65
|
+
if (responseJson["success"] != null && responseJson["error"] != null) {
|
|
66
|
+
console.log("Intercepted JSON:", responseJson);
|
|
67
|
+
const result = responseJson;
|
|
68
|
+
const newError = err_validation.fromId("standard_schema" /* standard_schema */, {
|
|
69
|
+
issues: result.error
|
|
70
|
+
});
|
|
71
|
+
ctx.res = undefined;
|
|
72
|
+
ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {
|
|
73
|
+
status: newError.httpStatusCode,
|
|
74
|
+
headers: {
|
|
75
|
+
"Content-Type": "application/json"
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error("Failed to parse response JSON:", error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
// src/hono/niceSValidator.ts
|
|
86
|
+
import { sValidator } from "@hono/standard-validator";
|
|
87
|
+
function niceSValidator(target, schema) {
|
|
88
|
+
return sValidator(target, schema, (result) => {
|
|
89
|
+
if (!result.success) {
|
|
90
|
+
throw err_validation.fromId("standard_schema" /* standard_schema */, {
|
|
91
|
+
issues: result.error
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
niceSValidator,
|
|
98
|
+
niceCatchSValidation
|
|
99
|
+
};
|
package/build/index.js
CHANGED
|
@@ -1,2 +1,60 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
1
|
+
// src/validation/err_validation.ts
|
|
2
|
+
import { err, err_nice } from "@nice-code/error";
|
|
3
|
+
import { StatusCodes } from "http-status-codes";
|
|
4
|
+
|
|
5
|
+
// src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
6
|
+
function extractPathFromIssue(issue) {
|
|
7
|
+
let pathString = "";
|
|
8
|
+
for (const segment of issue) {
|
|
9
|
+
if (typeof segment === "object") {
|
|
10
|
+
if (segment.key != null) {
|
|
11
|
+
if (typeof segment.key === "number") {
|
|
12
|
+
pathString += `[${String(segment.key)}]`;
|
|
13
|
+
} else if (typeof segment.key === "symbol") {
|
|
14
|
+
pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
15
|
+
} else {
|
|
16
|
+
pathString += `.${String(segment.key)}`;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
pathString += `.${String(segment)}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return pathString.slice(1);
|
|
24
|
+
}
|
|
25
|
+
var extractMessageFromStandardSchema = (failureResult) => {
|
|
26
|
+
let message = `Data validation failed:
|
|
27
|
+
`;
|
|
28
|
+
let issueCount = 0;
|
|
29
|
+
for (const issue of failureResult.issues) {
|
|
30
|
+
issueCount++;
|
|
31
|
+
if (issue.path == null || issue.path.length === 0) {
|
|
32
|
+
message += ` (issue ${issueCount}) ${issue.message}
|
|
33
|
+
`;
|
|
34
|
+
} else {
|
|
35
|
+
message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return message;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/validation/err_validation.ts
|
|
43
|
+
var EValidator;
|
|
44
|
+
((EValidator2) => {
|
|
45
|
+
EValidator2["standard_schema"] = "standard_schema";
|
|
46
|
+
})(EValidator ||= {});
|
|
47
|
+
var err_validation = err_nice.createChildDomain({
|
|
48
|
+
domain: "err_validation",
|
|
49
|
+
defaultHttpStatusCode: StatusCodes.BAD_REQUEST,
|
|
50
|
+
schema: {
|
|
51
|
+
["standard_schema" /* standard_schema */]: err({
|
|
52
|
+
message: ({ issues }) => extractMessageFromStandardSchema({ issues })
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
export {
|
|
57
|
+
extractMessageFromStandardSchema,
|
|
58
|
+
err_validation,
|
|
59
|
+
EValidator
|
|
60
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice-code/common-errors",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"build-types": "tsc --project tsconfig.build.json"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@nice-code/error": "0.2.
|
|
32
|
+
"@nice-code/error": "0.2.11",
|
|
33
33
|
"@standard-schema/spec": "^1.1.0",
|
|
34
34
|
"@hono/standard-validator": "^0.2.2",
|
|
35
35
|
"http-status-codes": "^2.3.0"
|