@shware/http 2.11.3 → 2.12.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/_virtual/_rolldown/runtime.cjs +13 -0
- package/dist/_virtual/_rolldown/runtime.mjs +13 -0
- package/dist/cache/index.cjs +5 -30
- package/dist/cache/index.cjs.map +1 -1
- package/dist/cache/index.d.cts +7 -5
- package/dist/cache/index.d.cts.map +1 -0
- package/dist/cache/index.d.mts +11 -0
- package/dist/cache/index.d.mts.map +1 -0
- package/dist/cache/index.mjs +5 -6
- package/dist/cache/index.mjs.map +1 -1
- package/dist/error/detail.cjs +100 -90
- package/dist/error/detail.cjs.map +1 -1
- package/dist/error/detail.d.cts +77 -78
- package/dist/error/detail.d.cts.map +1 -0
- package/dist/error/detail.d.mts +108 -0
- package/dist/error/detail.d.mts.map +1 -0
- package/dist/error/detail.mjs +99 -65
- package/dist/error/detail.mjs.map +1 -1
- package/dist/error/i18n/en-us.cjs +5 -29
- package/dist/error/i18n/en-us.cjs.map +1 -1
- package/dist/error/i18n/en-us.d.cts +3 -1
- package/dist/error/i18n/en-us.d.cts.map +1 -0
- package/dist/error/i18n/en-us.d.mts +5 -0
- package/dist/error/i18n/en-us.d.mts.map +1 -0
- package/dist/error/i18n/en-us.mjs +5 -5
- package/dist/error/i18n/en-us.mjs.map +1 -1
- package/dist/error/index.cjs +29 -56
- package/dist/error/index.cjs.map +1 -1
- package/dist/error/index.d.cts +12 -10
- package/dist/error/index.d.cts.map +1 -0
- package/dist/error/index.d.mts +21 -0
- package/dist/error/index.d.mts.map +1 -0
- package/dist/error/index.mjs +25 -28
- package/dist/error/index.mjs.map +1 -1
- package/dist/error/parse.cjs +63 -61
- package/dist/error/parse.cjs.map +1 -1
- package/dist/error/parse.d.cts +8 -7
- package/dist/error/parse.d.cts.map +1 -0
- package/dist/error/{parse.d.ts → parse.d.mts} +8 -7
- package/dist/error/parse.d.mts.map +1 -0
- package/dist/error/parse.mjs +59 -33
- package/dist/error/parse.mjs.map +1 -1
- package/dist/error/reason.cjs +0 -19
- package/dist/error/reason.d.cts +5 -4
- package/dist/error/reason.d.cts.map +1 -0
- package/dist/error/reason.d.mts +6 -0
- package/dist/error/reason.d.mts.map +1 -0
- package/dist/error/reason.mjs +1 -1
- package/dist/error/status.cjs +103 -256
- package/dist/error/status.cjs.map +1 -1
- package/dist/error/status.d.cts +58 -58
- package/dist/error/status.d.cts.map +1 -0
- package/dist/error/status.d.mts +69 -0
- package/dist/error/status.d.mts.map +1 -0
- package/dist/error/status.mjs +98 -227
- package/dist/error/status.mjs.map +1 -1
- package/dist/google-one-tap/index.cjs +135 -138
- package/dist/google-one-tap/index.cjs.map +1 -1
- package/dist/google-one-tap/index.d.cts +49 -48
- package/dist/google-one-tap/index.d.cts.map +1 -0
- package/dist/google-one-tap/index.d.mts +82 -0
- package/dist/google-one-tap/index.d.mts.map +1 -0
- package/dist/google-one-tap/index.mjs +133 -112
- package/dist/google-one-tap/index.mjs.map +1 -1
- package/dist/google-one-tap/types.cjs +0 -19
- package/dist/google-one-tap/types.d.cts +124 -122
- package/dist/google-one-tap/types.d.cts.map +1 -0
- package/dist/google-one-tap/types.d.mts +157 -0
- package/dist/google-one-tap/types.d.mts.map +1 -0
- package/dist/google-one-tap/types.mjs +1 -1
- package/dist/hono/authorizer.cjs +28 -65
- package/dist/hono/authorizer.cjs.map +1 -1
- package/dist/hono/authorizer.d.cts +12 -11
- package/dist/hono/authorizer.d.cts.map +1 -0
- package/dist/hono/authorizer.d.mts +19 -0
- package/dist/hono/authorizer.d.mts.map +1 -0
- package/dist/hono/authorizer.mjs +23 -36
- package/dist/hono/authorizer.mjs.map +1 -1
- package/dist/hono/csrf.cjs +81 -92
- package/dist/hono/csrf.cjs.map +1 -1
- package/dist/hono/csrf.d.cts +46 -45
- package/dist/hono/csrf.d.cts.map +1 -0
- package/dist/hono/csrf.d.mts +76 -0
- package/dist/hono/csrf.d.mts.map +1 -0
- package/dist/hono/csrf.mjs +76 -63
- package/dist/hono/csrf.mjs.map +1 -1
- package/dist/hono/geolocation.cjs +10 -34
- package/dist/hono/geolocation.cjs.map +1 -1
- package/dist/hono/geolocation.d.cts +5 -4
- package/dist/hono/geolocation.d.cts.map +1 -0
- package/dist/hono/geolocation.d.mts +7 -0
- package/dist/hono/geolocation.d.mts.map +1 -0
- package/dist/hono/geolocation.mjs +8 -12
- package/dist/hono/geolocation.mjs.map +1 -1
- package/dist/hono/handler.cjs +37 -61
- package/dist/hono/handler.cjs.map +1 -1
- package/dist/hono/handler.d.cts +26 -25
- package/dist/hono/handler.d.cts.map +1 -0
- package/dist/hono/handler.d.mts +32 -0
- package/dist/hono/handler.d.mts.map +1 -0
- package/dist/hono/handler.mjs +34 -34
- package/dist/hono/handler.mjs.map +1 -1
- package/dist/hono/index.cjs +14 -48
- package/dist/hono/index.d.cts +7 -15
- package/dist/hono/index.d.mts +7 -0
- package/dist/hono/index.mjs +4 -14
- package/dist/hono/rate-limit.cjs +65 -110
- package/dist/hono/rate-limit.cjs.map +1 -1
- package/dist/hono/rate-limit.d.cts +27 -26
- package/dist/hono/rate-limit.d.cts.map +1 -0
- package/dist/hono/rate-limit.d.mts +36 -0
- package/dist/hono/rate-limit.d.mts.map +1 -0
- package/dist/hono/rate-limit.mjs +60 -81
- package/dist/hono/rate-limit.mjs.map +1 -1
- package/dist/hono/validator.cjs +47 -66
- package/dist/hono/validator.cjs.map +1 -1
- package/dist/hono/validator.d.cts +25 -30
- package/dist/hono/validator.d.cts.map +1 -0
- package/dist/hono/validator.d.mts +26 -0
- package/dist/hono/validator.d.mts.map +1 -0
- package/dist/hono/validator.mjs +43 -43
- package/dist/hono/validator.mjs.map +1 -1
- package/dist/index.cjs +39 -95
- package/dist/index.d.cts +12 -14
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +5 -48
- package/dist/iso/iso_3601_1.cjs +1512 -290
- package/dist/iso/iso_3601_1.cjs.map +1 -1
- package/dist/iso/iso_3601_1.d.cts +8 -6
- package/dist/iso/iso_3601_1.d.cts.map +1 -0
- package/dist/iso/iso_3601_1.d.mts +11 -0
- package/dist/iso/iso_3601_1.d.mts.map +1 -0
- package/dist/iso/iso_3601_1.mjs +1511 -265
- package/dist/iso/iso_3601_1.mjs.map +1 -1
- package/dist/max-length/index.cjs +158 -91
- package/dist/max-length/index.cjs.map +1 -1
- package/dist/max-length/index.d.cts +110 -1
- package/dist/max-length/index.d.cts.map +1 -0
- package/dist/{index-BhgdyigB.d.ts → max-length/index.d.mts} +9 -28
- package/dist/max-length/index.d.mts.map +1 -0
- package/dist/max-length/index.mjs +133 -47
- package/dist/max-length/index.mjs.map +1 -1
- package/dist/message.cjs +5 -30
- package/dist/message.cjs.map +1 -1
- package/dist/message.d.cts +3 -1
- package/dist/message.d.cts.map +1 -0
- package/dist/{message.d.ts → message.d.mts} +3 -1
- package/dist/message.d.mts.map +1 -0
- package/dist/message.mjs +5 -6
- package/dist/message.mjs.map +1 -1
- package/dist/polyfills/index.cjs +4 -4
- package/dist/polyfills/index.cjs.map +1 -1
- package/dist/polyfills/index.d.cts +1 -2
- package/dist/polyfills/index.d.mts +1 -0
- package/dist/polyfills/index.mjs +5 -2
- package/dist/polyfills/index.mjs.map +1 -1
- package/dist/response.cjs +191 -208
- package/dist/response.cjs.map +1 -1
- package/dist/response.d.cts +53 -54
- package/dist/response.d.cts.map +1 -0
- package/dist/response.d.mts +82 -0
- package/dist/response.d.mts.map +1 -0
- package/dist/response.mjs +182 -175
- package/dist/response.mjs.map +1 -1
- package/dist/snowflake.cjs +48 -70
- package/dist/snowflake.cjs.map +1 -1
- package/dist/snowflake.d.cts +16 -14
- package/dist/snowflake.d.cts.map +1 -0
- package/dist/snowflake.d.mts +31 -0
- package/dist/snowflake.d.mts.map +1 -0
- package/dist/snowflake.mjs +46 -44
- package/dist/snowflake.mjs.map +1 -1
- package/dist/utils/geolocation.cjs +51 -75
- package/dist/utils/geolocation.cjs.map +1 -1
- package/dist/utils/geolocation.d.cts +14 -12
- package/dist/utils/geolocation.d.cts.map +1 -0
- package/dist/utils/{geolocation.d.ts → geolocation.d.mts} +14 -12
- package/dist/utils/geolocation.d.mts.map +1 -0
- package/dist/utils/geolocation.mjs +47 -47
- package/dist/utils/geolocation.mjs.map +1 -1
- package/dist/utils/google-tag-gateway.cjs +35 -56
- package/dist/utils/google-tag-gateway.cjs.map +1 -1
- package/dist/utils/google-tag-gateway.d.cts +3 -1
- package/dist/utils/google-tag-gateway.d.cts.map +1 -0
- package/dist/utils/{google-tag-gateway.d.ts → google-tag-gateway.d.mts} +3 -1
- package/dist/utils/google-tag-gateway.d.mts.map +1 -0
- package/dist/utils/google-tag-gateway.mjs +33 -30
- package/dist/utils/google-tag-gateway.mjs.map +1 -1
- package/dist/utils/timing.cjs +50 -71
- package/dist/utils/timing.cjs.map +1 -1
- package/dist/utils/timing.d.cts +11 -9
- package/dist/utils/timing.d.cts.map +1 -0
- package/dist/utils/timing.d.mts +15 -0
- package/dist/utils/timing.d.mts.map +1 -0
- package/dist/utils/timing.mjs +49 -46
- package/dist/utils/timing.mjs.map +1 -1
- package/dist/webhook/index.cjs +43 -72
- package/dist/webhook/index.cjs.map +1 -1
- package/dist/webhook/index.d.cts +3 -1
- package/dist/webhook/index.d.cts.map +1 -0
- package/dist/webhook/{index.d.ts → index.d.mts} +3 -1
- package/dist/webhook/index.d.mts.map +1 -0
- package/dist/webhook/index.mjs +41 -46
- package/dist/webhook/index.mjs.map +1 -1
- package/package.json +50 -25
- package/dist/cache/index.d.ts +0 -9
- package/dist/error/detail.d.ts +0 -109
- package/dist/error/i18n/en-us.d.ts +0 -3
- package/dist/error/index.d.ts +0 -19
- package/dist/error/reason.cjs.map +0 -1
- package/dist/error/reason.d.ts +0 -5
- package/dist/error/reason.mjs.map +0 -1
- package/dist/error/status.d.ts +0 -69
- package/dist/google-one-tap/index.d.ts +0 -81
- package/dist/google-one-tap/types.cjs.map +0 -1
- package/dist/google-one-tap/types.d.ts +0 -155
- package/dist/google-one-tap/types.mjs.map +0 -1
- package/dist/hono/authorizer.d.ts +0 -18
- package/dist/hono/csrf.d.ts +0 -75
- package/dist/hono/geolocation.d.ts +0 -6
- package/dist/hono/handler.d.ts +0 -31
- package/dist/hono/index.cjs.map +0 -1
- package/dist/hono/index.d.ts +0 -15
- package/dist/hono/index.mjs.map +0 -1
- package/dist/hono/rate-limit.d.ts +0 -35
- package/dist/hono/validator.d.ts +0 -31
- package/dist/index-BhgdyigB.d.cts +0 -129
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.mjs.map +0 -1
- package/dist/iso/iso_3601_1.d.ts +0 -9
- package/dist/max-length/index.d.ts +0 -1
- package/dist/polyfills/index.d.ts +0 -2
- package/dist/response.d.ts +0 -83
- package/dist/snowflake.d.ts +0 -29
- package/dist/utils/timing.d.ts +0 -13
package/dist/error/parse.cjs
CHANGED
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/error/parse.ts
|
|
21
|
-
var parse_exports = {};
|
|
22
|
-
__export(parse_exports, {
|
|
23
|
-
getErrorInfo: () => getErrorInfo,
|
|
24
|
-
getErrorMessage: () => getErrorMessage,
|
|
25
|
-
getFieldViolations: () => getFieldViolations
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(parse_exports);
|
|
28
|
-
var import_utils = require("@shware/utils");
|
|
29
|
-
var import_detail = require("./detail.cjs");
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./detail.cjs");
|
|
3
|
+
let _shware_utils = require("@shware/utils");
|
|
4
|
+
//#region src/error/parse.ts
|
|
30
5
|
function getErrorInfo(data) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return errorInfo ?? null;
|
|
6
|
+
if (typeof data !== "object" || data === null || !("error" in data)) return null;
|
|
7
|
+
const { error } = data;
|
|
8
|
+
return error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.ErrorInfo") ?? null;
|
|
35
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* @example For axios:
|
|
12
|
+
*
|
|
13
|
+
* const { t } = useTranslation('error');
|
|
14
|
+
*
|
|
15
|
+
* const { message } = getErrorMessage(error.response.data, t);
|
|
16
|
+
* toast.error(message);
|
|
17
|
+
*/
|
|
36
18
|
function getErrorMessage(data, t) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
19
|
+
if (typeof data !== "object" || data === null || !("error" in data)) {
|
|
20
|
+
console.error("Unknown API error:", data);
|
|
21
|
+
return {
|
|
22
|
+
reason: null,
|
|
23
|
+
message: t("UNKNOWN")
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const { error } = data;
|
|
27
|
+
const localizedMessage = error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.LocalizedMessage");
|
|
28
|
+
if (localizedMessage) return {
|
|
29
|
+
reason: null,
|
|
30
|
+
message: localizedMessage.message
|
|
31
|
+
};
|
|
32
|
+
const errorInfo = error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.ErrorInfo");
|
|
33
|
+
if (errorInfo) return {
|
|
34
|
+
reason: errorInfo.reason,
|
|
35
|
+
message: t(errorInfo.reason, errorInfo.metadata)
|
|
36
|
+
};
|
|
37
|
+
if ((0, _shware_utils.hasText)(error.message)) return {
|
|
38
|
+
reason: null,
|
|
39
|
+
message: error.message
|
|
40
|
+
};
|
|
41
|
+
if (error.status) return {
|
|
42
|
+
reason: null,
|
|
43
|
+
message: t(error.status)
|
|
44
|
+
};
|
|
45
|
+
console.error("Unknown API error:", data);
|
|
46
|
+
return {
|
|
47
|
+
reason: null,
|
|
48
|
+
message: t("UNKNOWN")
|
|
49
|
+
};
|
|
55
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* @example For react-hook-form:
|
|
53
|
+
*
|
|
54
|
+
* const { setError } = useForm();
|
|
55
|
+
* const fieldViolations = getFieldViolations(error.response.data);
|
|
56
|
+
* fieldViolations.forEach((violation) => {
|
|
57
|
+
* setError(violation.field, { message: violation.description });
|
|
58
|
+
* });
|
|
59
|
+
*/
|
|
56
60
|
function getFieldViolations(data) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return badRequest?.fieldViolations ?? [];
|
|
61
|
+
if (typeof data !== "object" || data === null || !("error" in data)) return [];
|
|
62
|
+
const { error } = data;
|
|
63
|
+
return (error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.BadRequest"))?.fieldViolations ?? [];
|
|
61
64
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
});
|
|
65
|
+
//#endregion
|
|
66
|
+
exports.getErrorInfo = getErrorInfo;
|
|
67
|
+
exports.getErrorMessage = getErrorMessage;
|
|
68
|
+
exports.getFieldViolations = getFieldViolations;
|
|
69
|
+
|
|
68
70
|
//# sourceMappingURL=parse.cjs.map
|
package/dist/error/parse.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.cjs","names":[],"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";;;;AAMA,SAAgB,aAAa,MAAiC;CAC5D,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO,OAAO;CAC5E,MAAM,EAAE,UAAU;CAElB,OADkB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,0CAAkC,KAC9D;AACtB;;;;;;;;;AAUA,SAAgB,gBACd,MACA,GACyD;CAEzD,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;EACnE,QAAQ,MAAM,sBAAsB,IAAI;EACxC,OAAO;GAAE,QAAQ;GAAM,SAAS,EAAE,SAAS;EAAE;CAC/C;CAEA,MAAM,EAAE,UAAU;CAGlB,MAAM,mBAAmB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,iDAAyC;CAChG,IAAI,kBAAkB,OAAO;EAAE,QAAQ;EAAM,SAAS,iBAAiB;CAAQ;CAG/E,MAAM,YAAY,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,0CAAkC;CAClF,IAAI,WACF,OAAO;EACL,QAAQ,UAAU;EAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;CACjD;CAIF,KAAA,GAAA,cAAA,QAAA,CAAY,MAAM,OAAO,GAAG,OAAO;EAAE,QAAQ;EAAM,SAAS,MAAM;CAAQ;CAG1E,IAAI,MAAM,QAAQ,OAAO;EAAE,QAAQ;EAAM,SAAS,EAAE,MAAM,MAAM;CAAE;CAGlE,QAAQ,MAAM,sBAAsB,IAAI;CACxC,OAAO;EAAE,QAAQ;EAAM,SAAS,EAAE,SAAS;CAAE;AAC/C;;;;;;;;;;AAWA,SAAgB,mBAAmB,MAA8C;CAC/E,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO,OAAO,CAAC;CAC7E,MAAM,EAAE,UAAU;CAElB,QADmB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,2CAAmC,EAAA,EACjE,mBAAmB,CAAC;AACzC"}
|
package/dist/error/parse.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { ResolvedErrorReason } from "./reason.cjs";
|
|
2
|
+
import { BadRequest, ErrorInfo } from "./detail.cjs";
|
|
3
|
+
import { DefaultNamespace, Namespace, TFunction } from "i18next";
|
|
4
|
+
//#region src/error/parse.d.ts
|
|
5
5
|
declare function getErrorInfo(data: unknown): ErrorInfo | null;
|
|
6
6
|
/**
|
|
7
7
|
* @example For axios:
|
|
@@ -12,8 +12,8 @@ declare function getErrorInfo(data: unknown): ErrorInfo | null;
|
|
|
12
12
|
* toast.error(message);
|
|
13
13
|
*/
|
|
14
14
|
declare function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(data: unknown, t: TFunction<Ns, KPrefix>): {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
reason: ResolvedErrorReason | null;
|
|
16
|
+
message: string;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* @example For react-hook-form:
|
|
@@ -25,5 +25,6 @@ declare function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefi
|
|
|
25
25
|
* });
|
|
26
26
|
*/
|
|
27
27
|
declare function getFieldViolations(data: unknown): BadRequest['fieldViolations'];
|
|
28
|
-
|
|
28
|
+
//#endregion
|
|
29
29
|
export { getErrorInfo, getErrorMessage, getFieldViolations };
|
|
30
|
+
//# sourceMappingURL=parse.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.cts","names":[],"sources":["../../src/error/parse.ts"],"mappings":";;;;iBAMgB,aAAa,gBAAgB;;;;;;;;;iBAe7B,gBAAgB,WAAW,YAAY,kBAAkB,qBACvE,eACA,GAAG,UAAU,IAAI;EACd,QAAQ;EAA4B;;;;;;;;;;;iBA0CzB,mBAAmB,gBAAgB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { ResolvedErrorReason } from "./reason.mjs";
|
|
2
|
+
import { BadRequest, ErrorInfo } from "./detail.mjs";
|
|
3
|
+
import { DefaultNamespace, Namespace, TFunction } from "i18next";
|
|
4
|
+
//#region src/error/parse.d.ts
|
|
5
5
|
declare function getErrorInfo(data: unknown): ErrorInfo | null;
|
|
6
6
|
/**
|
|
7
7
|
* @example For axios:
|
|
@@ -12,8 +12,8 @@ declare function getErrorInfo(data: unknown): ErrorInfo | null;
|
|
|
12
12
|
* toast.error(message);
|
|
13
13
|
*/
|
|
14
14
|
declare function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(data: unknown, t: TFunction<Ns, KPrefix>): {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
reason: ResolvedErrorReason | null;
|
|
16
|
+
message: string;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* @example For react-hook-form:
|
|
@@ -25,5 +25,6 @@ declare function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefi
|
|
|
25
25
|
* });
|
|
26
26
|
*/
|
|
27
27
|
declare function getFieldViolations(data: unknown): BadRequest['fieldViolations'];
|
|
28
|
-
|
|
28
|
+
//#endregion
|
|
29
29
|
export { getErrorInfo, getErrorMessage, getFieldViolations };
|
|
30
|
+
//# sourceMappingURL=parse.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.mts","names":[],"sources":["../../src/error/parse.ts"],"mappings":";;;;iBAMgB,aAAa,gBAAgB;;;;;;;;;iBAe7B,gBAAgB,WAAW,YAAY,kBAAkB,qBACvE,eACA,GAAG,UAAU,IAAI;EACd,QAAQ;EAA4B;;;;;;;;;;;iBA0CzB,mBAAmB,gBAAgB"}
|
package/dist/error/parse.mjs
CHANGED
|
@@ -1,41 +1,67 @@
|
|
|
1
|
-
|
|
1
|
+
import "./detail.mjs";
|
|
2
2
|
import { hasText } from "@shware/utils";
|
|
3
|
-
|
|
3
|
+
//#region src/error/parse.ts
|
|
4
4
|
function getErrorInfo(data) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return errorInfo ?? null;
|
|
5
|
+
if (typeof data !== "object" || data === null || !("error" in data)) return null;
|
|
6
|
+
const { error } = data;
|
|
7
|
+
return error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.ErrorInfo") ?? null;
|
|
9
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* @example For axios:
|
|
11
|
+
*
|
|
12
|
+
* const { t } = useTranslation('error');
|
|
13
|
+
*
|
|
14
|
+
* const { message } = getErrorMessage(error.response.data, t);
|
|
15
|
+
* toast.error(message);
|
|
16
|
+
*/
|
|
10
17
|
function getErrorMessage(data, t) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
if (typeof data !== "object" || data === null || !("error" in data)) {
|
|
19
|
+
console.error("Unknown API error:", data);
|
|
20
|
+
return {
|
|
21
|
+
reason: null,
|
|
22
|
+
message: t("UNKNOWN")
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const { error } = data;
|
|
26
|
+
const localizedMessage = error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.LocalizedMessage");
|
|
27
|
+
if (localizedMessage) return {
|
|
28
|
+
reason: null,
|
|
29
|
+
message: localizedMessage.message
|
|
30
|
+
};
|
|
31
|
+
const errorInfo = error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.ErrorInfo");
|
|
32
|
+
if (errorInfo) return {
|
|
33
|
+
reason: errorInfo.reason,
|
|
34
|
+
message: t(errorInfo.reason, errorInfo.metadata)
|
|
35
|
+
};
|
|
36
|
+
if (hasText(error.message)) return {
|
|
37
|
+
reason: null,
|
|
38
|
+
message: error.message
|
|
39
|
+
};
|
|
40
|
+
if (error.status) return {
|
|
41
|
+
reason: null,
|
|
42
|
+
message: t(error.status)
|
|
43
|
+
};
|
|
44
|
+
console.error("Unknown API error:", data);
|
|
45
|
+
return {
|
|
46
|
+
reason: null,
|
|
47
|
+
message: t("UNKNOWN")
|
|
48
|
+
};
|
|
29
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* @example For react-hook-form:
|
|
52
|
+
*
|
|
53
|
+
* const { setError } = useForm();
|
|
54
|
+
* const fieldViolations = getFieldViolations(error.response.data);
|
|
55
|
+
* fieldViolations.forEach((violation) => {
|
|
56
|
+
* setError(violation.field, { message: violation.description });
|
|
57
|
+
* });
|
|
58
|
+
*/
|
|
30
59
|
function getFieldViolations(data) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return badRequest?.fieldViolations ?? [];
|
|
60
|
+
if (typeof data !== "object" || data === null || !("error" in data)) return [];
|
|
61
|
+
const { error } = data;
|
|
62
|
+
return (error?.details?.find((d) => d["@type"] === "type.googleapis.com/google.rpc.BadRequest"))?.fieldViolations ?? [];
|
|
35
63
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
getFieldViolations
|
|
40
|
-
};
|
|
64
|
+
//#endregion
|
|
65
|
+
export { getErrorInfo, getErrorMessage, getFieldViolations };
|
|
66
|
+
|
|
41
67
|
//# sourceMappingURL=parse.mjs.map
|
package/dist/error/parse.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.mjs","names":[],"sources":["../../src/error/parse.ts"],"sourcesContent":["import { hasText } from '@shware/utils';\nimport type { DefaultNamespace, Namespace, TFunction } from 'i18next';\nimport { type BadRequest, DetailType, type ErrorInfo } from './detail';\nimport type { ResolvedErrorReason } from './reason';\nimport type { ErrorBody } from './status';\n\nexport function getErrorInfo(data: unknown): ErrorInfo | null {\n if (typeof data !== 'object' || data === null || !('error' in data)) return null;\n const { error } = data as ErrorBody;\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n return errorInfo ?? null;\n}\n\n/**\n * @example For axios:\n *\n * const { t } = useTranslation('error');\n *\n * const { message } = getErrorMessage(error.response.data, t);\n * toast.error(message);\n */\nexport function getErrorMessage<Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(\n data: unknown,\n t: TFunction<Ns, KPrefix>\n): { reason: ResolvedErrorReason | null; message: string } {\n // 0. unknown error\n if (typeof data !== 'object' || data === null || !('error' in data)) {\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n }\n\n const { error } = data as ErrorBody;\n\n // 1. from server via Accept-Language header or lng param\n const localizedMessage = error?.details?.find((d) => d['@type'] === DetailType.LOCALIZED_MESSAGE);\n if (localizedMessage) return { reason: null, message: localizedMessage.message };\n\n // 2. from business logic error\n const errorInfo = error?.details?.find((d) => d['@type'] === DetailType.ERROR_INFO);\n if (errorInfo) {\n return {\n reason: errorInfo.reason,\n message: t(errorInfo.reason, errorInfo.metadata),\n };\n }\n\n // 3. error message in english\n if (hasText(error.message)) return { reason: null, message: error.message };\n\n // 4. from server via status code\n if (error.status) return { reason: null, message: t(error.status) };\n\n // 5. unknown error\n console.error('Unknown API error:', data);\n return { reason: null, message: t('UNKNOWN') };\n}\n\n/**\n * @example For react-hook-form:\n *\n * const { setError } = useForm();\n * const fieldViolations = getFieldViolations(error.response.data);\n * fieldViolations.forEach((violation) => {\n * setError(violation.field, { message: violation.description });\n * });\n */\nexport function getFieldViolations(data: unknown): BadRequest['fieldViolations'] {\n if (typeof data !== 'object' || data === null || !('error' in data)) return [];\n const { error } = data as ErrorBody;\n const badRequest = error?.details?.find((d) => d['@type'] === DetailType.BAD_REQUEST);\n return badRequest?.fieldViolations ?? [];\n}\n"],"mappings":";;;AAMA,SAAgB,aAAa,MAAiC;CAC5D,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO,OAAO;CAC5E,MAAM,EAAE,UAAU;CAElB,OADkB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,0CAAkC,KAC9D;AACtB;;;;;;;;;AAUA,SAAgB,gBACd,MACA,GACyD;CAEzD,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO;EACnE,QAAQ,MAAM,sBAAsB,IAAI;EACxC,OAAO;GAAE,QAAQ;GAAM,SAAS,EAAE,SAAS;EAAE;CAC/C;CAEA,MAAM,EAAE,UAAU;CAGlB,MAAM,mBAAmB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,iDAAyC;CAChG,IAAI,kBAAkB,OAAO;EAAE,QAAQ;EAAM,SAAS,iBAAiB;CAAQ;CAG/E,MAAM,YAAY,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,0CAAkC;CAClF,IAAI,WACF,OAAO;EACL,QAAQ,UAAU;EAClB,SAAS,EAAE,UAAU,QAAQ,UAAU,QAAQ;CACjD;CAIF,IAAI,QAAQ,MAAM,OAAO,GAAG,OAAO;EAAE,QAAQ;EAAM,SAAS,MAAM;CAAQ;CAG1E,IAAI,MAAM,QAAQ,OAAO;EAAE,QAAQ;EAAM,SAAS,EAAE,MAAM,MAAM;CAAE;CAGlE,QAAQ,MAAM,sBAAsB,IAAI;CACxC,OAAO;EAAE,QAAQ;EAAM,SAAS,EAAE,SAAS;CAAE;AAC/C;;;;;;;;;;AAWA,SAAgB,mBAAmB,MAA8C;CAC/E,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,EAAE,WAAW,OAAO,OAAO,CAAC;CAC7E,MAAM,EAAE,UAAU;CAElB,QADmB,OAAO,SAAS,MAAM,MAAM,EAAE,aAAA,2CAAmC,EAAA,EACjE,mBAAmB,CAAC;AACzC"}
|
package/dist/error/reason.cjs
CHANGED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
|
|
16
|
-
// src/error/reason.ts
|
|
17
|
-
var reason_exports = {};
|
|
18
|
-
module.exports = __toCommonJS(reason_exports);
|
|
19
|
-
//# sourceMappingURL=reason.cjs.map
|
package/dist/error/reason.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
}
|
|
1
|
+
//#region src/error/reason.d.ts
|
|
2
|
+
interface ErrorReason {}
|
|
3
3
|
type ResolvedErrorReason = keyof ErrorReason extends never ? string : keyof ErrorReason;
|
|
4
|
-
|
|
5
|
-
export
|
|
4
|
+
//#endregion
|
|
5
|
+
export { ErrorReason, ResolvedErrorReason };
|
|
6
|
+
//# sourceMappingURL=reason.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reason.d.cts","names":[],"sources":["../../src/error/reason.ts"],"mappings":";UACiB;KAEL,4BAA4B,2CAA2C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reason.d.mts","names":[],"sources":["../../src/error/reason.ts"],"mappings":";UACiB;KAEL,4BAA4B,2CAA2C"}
|
package/dist/error/reason.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|