@clipboard-health/util-ts 2.10.0 → 2.11.1
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 +1 -1
- package/src/lib/errors/serviceError.d.ts +15 -10
- package/src/lib/errors/serviceError.js +23 -11
- package/src/lib/errors/serviceError.js.map +1 -1
- package/src/lib/functional/serviceResult.d.ts +2 -0
- package/src/lib/functional/serviceResult.js +6 -0
- package/src/lib/functional/serviceResult.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ZodError, type ZodIssue } from "zod";
|
|
1
2
|
/**
|
|
2
3
|
* Standard error codes used across microservices.
|
|
3
4
|
*/
|
|
@@ -20,6 +21,7 @@ export interface ServiceIssue {
|
|
|
20
21
|
/** Path to issue location */
|
|
21
22
|
path?: Array<string | number>;
|
|
22
23
|
}
|
|
24
|
+
type Source = "header" | "parameter" | "pointer";
|
|
23
25
|
export type ServiceErrorParams = string | {
|
|
24
26
|
/** Error cause */
|
|
25
27
|
cause?: Readonly<unknown>;
|
|
@@ -27,14 +29,13 @@ export type ServiceErrorParams = string | {
|
|
|
27
29
|
id?: string;
|
|
28
30
|
/** Array of issues contributing to the error */
|
|
29
31
|
issues: readonly ServiceIssue[];
|
|
32
|
+
/**
|
|
33
|
+
* Source of the error
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://jsonapi.org/format/#error-objects}
|
|
36
|
+
*/
|
|
37
|
+
source?: Source | undefined;
|
|
30
38
|
};
|
|
31
|
-
export interface ZodLike {
|
|
32
|
-
name: string;
|
|
33
|
-
issues: ReadonlyArray<{
|
|
34
|
-
message?: string | undefined;
|
|
35
|
-
path: Array<string | number>;
|
|
36
|
-
}>;
|
|
37
|
-
}
|
|
38
39
|
declare const ERROR_METADATA: {
|
|
39
40
|
readonly badRequest: {
|
|
40
41
|
readonly status: 400;
|
|
@@ -89,13 +90,16 @@ export declare class ServiceError extends Error {
|
|
|
89
90
|
/**
|
|
90
91
|
* Converts a ZodError to a `ServiceError`.
|
|
91
92
|
*
|
|
92
|
-
* @param
|
|
93
|
+
* @param error - A ZodError
|
|
93
94
|
* @returns The converted `ServiceError`
|
|
94
95
|
*/
|
|
95
|
-
static fromZodError(
|
|
96
|
+
static fromZodError(error: ZodError, options?: {
|
|
97
|
+
source?: Source;
|
|
98
|
+
}): ServiceError;
|
|
96
99
|
readonly id: string;
|
|
97
100
|
readonly issues: readonly Issue[];
|
|
98
101
|
readonly status: Status;
|
|
102
|
+
readonly source: Source;
|
|
99
103
|
/**
|
|
100
104
|
* Creates a new `ServiceError`
|
|
101
105
|
* @param parameters - Issues contributing to the error or a message string
|
|
@@ -113,7 +117,7 @@ export declare class ServiceError extends Error {
|
|
|
113
117
|
toJsonApi(): {
|
|
114
118
|
errors: {
|
|
115
119
|
source?: {
|
|
116
|
-
|
|
120
|
+
[x: string]: string;
|
|
117
121
|
};
|
|
118
122
|
detail?: string;
|
|
119
123
|
id: string;
|
|
@@ -123,4 +127,5 @@ export declare class ServiceError extends Error {
|
|
|
123
127
|
}[];
|
|
124
128
|
};
|
|
125
129
|
}
|
|
130
|
+
export declare function toZodIssue(issue: ZodIssue): ServiceIssue;
|
|
126
131
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ServiceError = exports.ERROR_CODES = void 0;
|
|
4
|
+
exports.toZodIssue = toZodIssue;
|
|
4
5
|
const node_crypto_1 = require("node:crypto");
|
|
5
6
|
const deepFreeze_1 = require("../deepFreeze");
|
|
6
7
|
const toError_1 = require("./toError");
|
|
@@ -75,36 +76,40 @@ class ServiceError extends Error {
|
|
|
75
76
|
/**
|
|
76
77
|
* Converts a ZodError to a `ServiceError`.
|
|
77
78
|
*
|
|
78
|
-
* @param
|
|
79
|
+
* @param error - A ZodError
|
|
79
80
|
* @returns The converted `ServiceError`
|
|
80
81
|
*/
|
|
81
|
-
static fromZodError(
|
|
82
|
+
static fromZodError(error, options) {
|
|
82
83
|
return new ServiceError({
|
|
83
|
-
cause:
|
|
84
|
-
issues:
|
|
85
|
-
|
|
86
|
-
message: issue.message,
|
|
87
|
-
path: issue.path,
|
|
88
|
-
})),
|
|
84
|
+
cause: error,
|
|
85
|
+
issues: error.issues.map(toZodIssue),
|
|
86
|
+
source: options?.source,
|
|
89
87
|
});
|
|
90
88
|
}
|
|
91
89
|
id;
|
|
92
90
|
issues;
|
|
93
91
|
status;
|
|
92
|
+
source;
|
|
94
93
|
/**
|
|
95
94
|
* Creates a new `ServiceError`
|
|
96
95
|
* @param parameters - Issues contributing to the error or a message string
|
|
97
96
|
*/
|
|
98
97
|
constructor(parameters) {
|
|
99
98
|
const params = typeof parameters === "string"
|
|
100
|
-
? {
|
|
99
|
+
? {
|
|
100
|
+
cause: undefined,
|
|
101
|
+
id: undefined,
|
|
102
|
+
issues: [toIssue({ message: parameters })],
|
|
103
|
+
source: undefined,
|
|
104
|
+
}
|
|
101
105
|
: { ...parameters, issues: parameters.issues.map(toIssue) };
|
|
102
106
|
super(createServiceErrorMessage(params.issues));
|
|
103
|
-
const { cause, id, issues } = params;
|
|
107
|
+
const { cause, id, issues, source } = params;
|
|
104
108
|
this.cause = cause;
|
|
105
109
|
this.id = id ?? (0, node_crypto_1.randomUUID)();
|
|
106
110
|
this.issues = (0, deepFreeze_1.deepFreeze)(issues);
|
|
107
111
|
this.name = this.constructor.name;
|
|
112
|
+
this.source = source ?? "pointer";
|
|
108
113
|
this.status = Math.max(...issues.map((issue) => ERROR_METADATA[issue.code].status));
|
|
109
114
|
/**
|
|
110
115
|
* Maintain proper prototype chain in transpiled code
|
|
@@ -133,7 +138,7 @@ class ServiceError extends Error {
|
|
|
133
138
|
...(issue.message && { detail: issue.message }),
|
|
134
139
|
...(issue.path && {
|
|
135
140
|
source: {
|
|
136
|
-
|
|
141
|
+
[this.source]: `/${issue.path.join("/")}`,
|
|
137
142
|
},
|
|
138
143
|
}),
|
|
139
144
|
})),
|
|
@@ -141,6 +146,13 @@ class ServiceError extends Error {
|
|
|
141
146
|
}
|
|
142
147
|
}
|
|
143
148
|
exports.ServiceError = ServiceError;
|
|
149
|
+
function toZodIssue(issue) {
|
|
150
|
+
return {
|
|
151
|
+
code: exports.ERROR_CODES.unprocessableEntity,
|
|
152
|
+
message: issue.message,
|
|
153
|
+
path: issue.path,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
144
156
|
/**
|
|
145
157
|
* Creates a human-readable error message from an array of service issues
|
|
146
158
|
* @param issues - Array of issues to include in the message
|
|
@@ -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":";;;AAsMA,gCAMC;AA5MD,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,qBAAqB;IAC1C,eAAe,EAAE,iBAAiB;IAClC,QAAQ,EAAE,UAAU;CACZ,CAAC;AAmCX,MAAM,cAAc,GAAG;IACrB,UAAU,EAAE;QACV,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,8BAA8B;KACtC;IACD,YAAY,EAAE;QACZ,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,gCAAgC;KACxC;IACD,SAAS,EAAE;QACT,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,2BAA2B;KACnC;IACD,QAAQ,EAAE;QACR,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,oBAAoB;KAC5B;IACD,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AE,GAAG,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;AAC/D,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { either as E } from "@clipboard-health/util-ts";
|
|
2
|
+
import { type SafeParseReturnType } from "zod";
|
|
2
3
|
import { ServiceError, type ServiceErrorParams } from "../errors/serviceError";
|
|
3
4
|
/**
|
|
4
5
|
* Represents the result of a service operation that may fail.
|
|
@@ -21,3 +22,4 @@ export declare function success<A>(value: A): ServiceResult<A>;
|
|
|
21
22
|
* @returns A `ServiceResult` containing the error
|
|
22
23
|
*/
|
|
23
24
|
export declare function failure<A = never>(params: ServiceErrorParams | ServiceError): ServiceResult<A>;
|
|
25
|
+
export declare function fromSafeParseReturnType<A>(value: SafeParseReturnType<unknown, A>): ServiceResult<A>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.success = success;
|
|
4
4
|
exports.failure = failure;
|
|
5
|
+
exports.fromSafeParseReturnType = fromSafeParseReturnType;
|
|
5
6
|
const util_ts_1 = require("@clipboard-health/util-ts");
|
|
6
7
|
const serviceError_1 = require("../errors/serviceError");
|
|
7
8
|
/**
|
|
@@ -24,4 +25,9 @@ function success(value) {
|
|
|
24
25
|
function failure(params) {
|
|
25
26
|
return util_ts_1.either.left(params instanceof serviceError_1.ServiceError ? params : new serviceError_1.ServiceError(params));
|
|
26
27
|
}
|
|
28
|
+
function fromSafeParseReturnType(value) {
|
|
29
|
+
return value.success
|
|
30
|
+
? success(value.data)
|
|
31
|
+
: failure({ issues: value.error.issues.map(serviceError_1.toZodIssue) });
|
|
32
|
+
}
|
|
27
33
|
//# sourceMappingURL=serviceResult.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceResult.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/serviceResult.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"serviceResult.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/serviceResult.ts"],"names":[],"mappings":";;AAkBA,0BAEC;AASD,0BAEC;AAED,0DAMC;AAvCD,uDAAwD;AAGxD,yDAA2F;AAQ3F;;;;;;GAMG;AACH,SAAgB,OAAO,CAAI,KAAQ;IACjC,OAAO,gBAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,OAAO,CAAY,MAAyC;IAC1E,OAAO,gBAAC,CAAC,IAAI,CAAC,MAAM,YAAY,2BAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,2BAAY,CAAC,MAAM,CAAC,CAAC,CAAC;AACpF,CAAC;AAED,SAAgB,uBAAuB,CACrC,KAAsC;IAEtC,OAAO,KAAK,CAAC,OAAO;QAClB,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;QACrB,CAAC,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,yBAAU,CAAC,EAAE,CAAC,CAAC;AAC9D,CAAC"}
|