@clipboard-health/util-ts 3.5.0 → 3.7.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/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/util-ts",
|
|
3
3
|
"description": "TypeScript utilities.",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.7.0",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"tslib": "2.8.1"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"type-fest": "4.41.0",
|
|
11
|
-
"zod": "3.25.
|
|
11
|
+
"zod": "3.25.65"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"typescript",
|
|
@@ -12,30 +12,7 @@ export declare const ERROR_CODES: {
|
|
|
12
12
|
readonly tooManyRequests: "tooManyRequests";
|
|
13
13
|
readonly internal: "internal";
|
|
14
14
|
};
|
|
15
|
-
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
16
|
-
export interface ServiceIssue {
|
|
17
|
-
/** Standardized {@link ERROR_CODES} */
|
|
18
|
-
code?: ErrorCode;
|
|
19
|
-
/** Details about what caused the issue */
|
|
20
|
-
message?: string | undefined;
|
|
21
|
-
/** Path to issue location */
|
|
22
|
-
path?: Array<string | number>;
|
|
23
|
-
}
|
|
24
|
-
export type ErrorSource = "header" | "parameter" | "pointer";
|
|
25
|
-
export type ServiceErrorParams = string | {
|
|
26
|
-
/** Error cause */
|
|
27
|
-
cause?: unknown;
|
|
28
|
-
/** Unique identifier for the issue */
|
|
29
|
-
id?: string;
|
|
30
|
-
/** Array of issues contributing to the error */
|
|
31
|
-
issues: readonly ServiceIssue[];
|
|
32
|
-
/**
|
|
33
|
-
* Source of the error
|
|
34
|
-
*
|
|
35
|
-
* @see {@link https://jsonapi.org/format/#error-objects}
|
|
36
|
-
*/
|
|
37
|
-
source?: ErrorSource | undefined;
|
|
38
|
-
};
|
|
15
|
+
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES] | (string & {});
|
|
39
16
|
declare const ERROR_METADATA: {
|
|
40
17
|
readonly badRequest: {
|
|
41
18
|
readonly status: 400;
|
|
@@ -71,9 +48,43 @@ declare const ERROR_METADATA: {
|
|
|
71
48
|
};
|
|
72
49
|
};
|
|
73
50
|
type Status = (typeof ERROR_METADATA)[keyof typeof ERROR_METADATA]["status"];
|
|
51
|
+
export interface ServiceIssue {
|
|
52
|
+
/**
|
|
53
|
+
* - Use {@link ERROR_CODES}
|
|
54
|
+
* – Or pass any custom string (e.g. "invalidPromoCode") for new cases
|
|
55
|
+
*/
|
|
56
|
+
code?: ErrorCode;
|
|
57
|
+
/** Details about what caused the issue */
|
|
58
|
+
message?: string | undefined;
|
|
59
|
+
/** Path to issue location */
|
|
60
|
+
path?: Array<string | number>;
|
|
61
|
+
/**
|
|
62
|
+
* Short, reusable summary of the problem (`errors.title`).
|
|
63
|
+
* Keep it concise and localizable (e.g. "Invalid Input").
|
|
64
|
+
*/
|
|
65
|
+
title?: string;
|
|
66
|
+
/**
|
|
67
|
+
* HTTP status code (`errors.status` in JSON:API).
|
|
68
|
+
*/
|
|
69
|
+
status?: Status;
|
|
70
|
+
}
|
|
71
|
+
export type ErrorSource = "header" | "parameter" | "pointer";
|
|
72
|
+
export type ServiceErrorParams = string | {
|
|
73
|
+
/** Error cause */
|
|
74
|
+
cause?: unknown;
|
|
75
|
+
/** Unique identifier for the issue */
|
|
76
|
+
id?: string;
|
|
77
|
+
/** Array of issues contributing to the error */
|
|
78
|
+
issues: readonly ServiceIssue[];
|
|
79
|
+
/**
|
|
80
|
+
* Source of the error
|
|
81
|
+
*
|
|
82
|
+
* @see {@link https://jsonapi.org/format/#error-objects}
|
|
83
|
+
*/
|
|
84
|
+
source?: ErrorSource | undefined;
|
|
85
|
+
};
|
|
74
86
|
export interface Issue extends ServiceIssue {
|
|
75
87
|
code: Required<ServiceIssue>["code"];
|
|
76
|
-
title: string;
|
|
77
88
|
}
|
|
78
89
|
/**
|
|
79
90
|
* Error class for service-level errors, convertible to JSON:API errors.
|
|
@@ -105,7 +116,7 @@ export declare class ServiceError extends Error {
|
|
|
105
116
|
static fromJsonApi(jsonApiError: {
|
|
106
117
|
errors: Array<{
|
|
107
118
|
id?: string;
|
|
108
|
-
status?:
|
|
119
|
+
status?: `${Status}`;
|
|
109
120
|
code?: ErrorCode;
|
|
110
121
|
title?: string;
|
|
111
122
|
detail?: string;
|
|
@@ -139,7 +150,7 @@ export declare class ServiceError extends Error {
|
|
|
139
150
|
id: string;
|
|
140
151
|
status: string;
|
|
141
152
|
code: ErrorCode;
|
|
142
|
-
title: string;
|
|
153
|
+
title: string | undefined;
|
|
143
154
|
}[];
|
|
144
155
|
};
|
|
145
156
|
}
|
|
@@ -100,6 +100,8 @@ class ServiceError extends Error {
|
|
|
100
100
|
return toIssue({
|
|
101
101
|
code: error.code ?? exports.ERROR_CODES.internal,
|
|
102
102
|
message: error.detail,
|
|
103
|
+
...(error.title ? { title: error.title } : undefined),
|
|
104
|
+
...(error.status ? { status: Number(error.status) } : undefined),
|
|
103
105
|
...(path && path.length > 0 && { path }),
|
|
104
106
|
});
|
|
105
107
|
});
|
|
@@ -132,7 +134,7 @@ class ServiceError extends Error {
|
|
|
132
134
|
this.issues = (0, deepFreeze_1.deepFreeze)(issues);
|
|
133
135
|
this.name = this.constructor.name;
|
|
134
136
|
this.source = source ?? "pointer";
|
|
135
|
-
this.status = Math.max(...issues.map((issue) =>
|
|
137
|
+
this.status = Math.max(...issues.map((issue) => getStatusFromIssue(issue)));
|
|
136
138
|
/**
|
|
137
139
|
* Maintain proper prototype chain in transpiled code
|
|
138
140
|
* @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget}
|
|
@@ -154,7 +156,7 @@ class ServiceError extends Error {
|
|
|
154
156
|
return {
|
|
155
157
|
errors: this.issues.map((issue) => ({
|
|
156
158
|
id: this.id,
|
|
157
|
-
status: String(
|
|
159
|
+
status: String(getStatusFromIssue(issue)),
|
|
158
160
|
code: issue.code,
|
|
159
161
|
title: issue.title,
|
|
160
162
|
...(issue.message && { detail: issue.message }),
|
|
@@ -193,6 +195,16 @@ function createServiceErrorMessage(issues) {
|
|
|
193
195
|
}
|
|
194
196
|
function toIssue(issue) {
|
|
195
197
|
const code = issue.code ?? exports.ERROR_CODES.internal;
|
|
196
|
-
|
|
198
|
+
const title = issue.title ?? (isKeyOf(code, ERROR_METADATA) ? ERROR_METADATA[code].title : undefined);
|
|
199
|
+
return { ...issue, code, ...(title ? { title } : undefined) };
|
|
200
|
+
}
|
|
201
|
+
function isKeyOf(field, object) {
|
|
202
|
+
return field in object;
|
|
203
|
+
}
|
|
204
|
+
function getStatusFromIssue(issue) {
|
|
205
|
+
return (issue.status ??
|
|
206
|
+
(issue.code && isKeyOf(issue.code, ERROR_METADATA)
|
|
207
|
+
? ERROR_METADATA[issue.code].status
|
|
208
|
+
: ERROR_METADATA.internal.status));
|
|
197
209
|
}
|
|
198
210
|
//# sourceMappingURL=serviceError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;AAyPA,gCAMC;AA/PD,6CAAyC;AAIzC,8CAA2C;AAC3C,uCAAoC;AAEpC;;GAEG;AACU,QAAA,WAAW,GAAG;IACzB,UAAU,EAAE,YAAY;IACxB,YAAY,EAAE,cAAc;IAC5B,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,mBAAmB,EAAE,qBAA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