@clipboard-health/util-ts 2.8.7 → 2.9.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.
Files changed (112) hide show
  1. package/README.md +219 -2
  2. package/package.json +13 -5
  3. package/src/index.d.ts +13 -15
  4. package/src/index.js +14 -14
  5. package/src/index.js.map +1 -1
  6. package/src/lib/{head.d.ts → arrays/head.d.ts} +6 -2
  7. package/src/lib/{head.js → arrays/head.js} +4 -1
  8. package/src/lib/arrays/head.js.map +1 -0
  9. package/src/lib/{non-empty-array.d.ts → arrays/nonEmptyArray.d.ts} +0 -1
  10. package/src/lib/{non-empty-array.js → arrays/nonEmptyArray.js} +1 -1
  11. package/src/lib/arrays/nonEmptyArray.js.map +1 -0
  12. package/src/lib/deepFreeze.d.ts +9 -0
  13. package/src/lib/deepFreeze.js +27 -0
  14. package/src/lib/deepFreeze.js.map +1 -0
  15. package/src/lib/errors/cbhError.d.ts +33 -0
  16. package/src/lib/{cbh-error.js → errors/cbhError.js} +10 -3
  17. package/src/lib/errors/cbhError.js.map +1 -0
  18. package/src/lib/errors/isError.d.ts +7 -0
  19. package/src/lib/errors/isError.js +13 -0
  20. package/src/lib/errors/isError.js.map +1 -0
  21. package/src/lib/errors/serviceError.d.ts +86 -0
  22. package/src/lib/errors/serviceError.js +180 -0
  23. package/src/lib/errors/serviceError.js.map +1 -0
  24. package/src/lib/errors/toError.d.ts +8 -0
  25. package/src/lib/errors/toError.js +35 -0
  26. package/src/lib/errors/toError.js.map +1 -0
  27. package/src/lib/errors/toErrorMessage.d.ts +7 -0
  28. package/src/lib/errors/toErrorMessage.js +14 -0
  29. package/src/lib/errors/toErrorMessage.js.map +1 -0
  30. package/src/lib/{force-cast.d.ts → forceCast.d.ts} +2 -1
  31. package/src/lib/{force-cast.js → forceCast.js} +3 -1
  32. package/src/lib/forceCast.js.map +1 -0
  33. package/src/lib/functional/cbhResponse.d.ts +23 -0
  34. package/src/lib/{cbh-response.js → functional/cbhResponse.js} +9 -3
  35. package/src/lib/functional/cbhResponse.js.map +1 -0
  36. package/src/lib/functional/either.d.ts +86 -0
  37. package/src/lib/functional/either.js +97 -0
  38. package/src/lib/functional/either.js.map +1 -0
  39. package/src/lib/functional/option.d.ts +71 -0
  40. package/src/lib/functional/option.js +82 -0
  41. package/src/lib/functional/option.js.map +1 -0
  42. package/src/lib/functional/pipe.d.ts +21 -0
  43. package/src/lib/functional/pipe.js +7 -0
  44. package/src/lib/functional/pipe.js.map +1 -0
  45. package/src/lib/functional/serviceResult.d.ts +23 -0
  46. package/src/lib/functional/serviceResult.js +27 -0
  47. package/src/lib/functional/serviceResult.js.map +1 -0
  48. package/src/lib/nullish/isDefined.d.ts +9 -0
  49. package/src/lib/nullish/isDefined.js +15 -0
  50. package/src/lib/nullish/isDefined.js.map +1 -0
  51. package/src/lib/nullish/isNil.d.ts +12 -0
  52. package/src/lib/nullish/isNil.js +20 -0
  53. package/src/lib/nullish/isNil.js.map +1 -0
  54. package/src/lib/{null-to-undefined.d.ts → nullish/nullToUndefined.d.ts} +0 -1
  55. package/src/lib/{null-to-undefined.js → nullish/nullToUndefined.js} +1 -1
  56. package/src/lib/nullish/nullToUndefined.js.map +1 -0
  57. package/src/lib/strings/isString.d.ts +7 -0
  58. package/src/lib/strings/isString.js +13 -0
  59. package/src/lib/strings/isString.js.map +1 -0
  60. package/src/lib/strings/stringify.d.ts +8 -0
  61. package/src/lib/strings/stringify.js +14 -0
  62. package/src/lib/strings/stringify.js.map +1 -0
  63. package/src/lib/types.d.ts +1 -0
  64. package/src/lib/types.js +3 -0
  65. package/src/lib/types.js.map +1 -0
  66. package/src/index.d.ts.map +0 -1
  67. package/src/lib/cbh-error.d.ts +0 -22
  68. package/src/lib/cbh-error.d.ts.map +0 -1
  69. package/src/lib/cbh-error.js.map +0 -1
  70. package/src/lib/cbh-response.d.ts +0 -18
  71. package/src/lib/cbh-response.d.ts.map +0 -1
  72. package/src/lib/cbh-response.js.map +0 -1
  73. package/src/lib/defined-utils.d.ts +0 -11
  74. package/src/lib/defined-utils.d.ts.map +0 -1
  75. package/src/lib/defined-utils.js +0 -17
  76. package/src/lib/defined-utils.js.map +0 -1
  77. package/src/lib/delay.d.ts +0 -5
  78. package/src/lib/delay.d.ts.map +0 -1
  79. package/src/lib/delay.js +0 -12
  80. package/src/lib/delay.js.map +0 -1
  81. package/src/lib/either.d.ts +0 -32
  82. package/src/lib/either.d.ts.map +0 -1
  83. package/src/lib/either.js +0 -31
  84. package/src/lib/either.js.map +0 -1
  85. package/src/lib/force-cast.d.ts.map +0 -1
  86. package/src/lib/force-cast.js.map +0 -1
  87. package/src/lib/head.d.ts.map +0 -1
  88. package/src/lib/head.js.map +0 -1
  89. package/src/lib/identity.d.ts +0 -7
  90. package/src/lib/identity.d.ts.map +0 -1
  91. package/src/lib/identity.js +0 -12
  92. package/src/lib/identity.js.map +0 -1
  93. package/src/lib/is-string.d.ts +0 -2
  94. package/src/lib/is-string.d.ts.map +0 -1
  95. package/src/lib/is-string.js +0 -7
  96. package/src/lib/is-string.js.map +0 -1
  97. package/src/lib/non-empty-array.d.ts.map +0 -1
  98. package/src/lib/non-empty-array.js.map +0 -1
  99. package/src/lib/null-to-undefined.d.ts.map +0 -1
  100. package/src/lib/null-to-undefined.js.map +0 -1
  101. package/src/lib/option.d.ts +0 -28
  102. package/src/lib/option.d.ts.map +0 -1
  103. package/src/lib/option.js +0 -29
  104. package/src/lib/option.js.map +0 -1
  105. package/src/lib/stringify.d.ts +0 -5
  106. package/src/lib/stringify.d.ts.map +0 -1
  107. package/src/lib/stringify.js +0 -10
  108. package/src/lib/stringify.js.map +0 -1
  109. package/src/lib/to-error.d.ts +0 -13
  110. package/src/lib/to-error.d.ts.map +0 -1
  111. package/src/lib/to-error.js +0 -34
  112. package/src/lib/to-error.js.map +0 -1
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServiceError = exports.ERROR_CODES = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const deepFreeze_1 = require("../deepFreeze");
6
+ const isDefined_1 = require("../nullish/isDefined");
7
+ const toError_1 = require("./toError");
8
+ /**
9
+ * Standard error codes used across microservices.
10
+ */
11
+ exports.ERROR_CODES = {
12
+ badRequest: "badRequest",
13
+ unauthorized: "unauthorized",
14
+ forbidden: "forbidden",
15
+ notFound: "notFound",
16
+ conflict: "conflict",
17
+ unprocessableEntity: "unprocessableEntity",
18
+ tooManyRequests: "tooManyRequests",
19
+ internal: "internal",
20
+ };
21
+ const ERROR_METADATA = {
22
+ badRequest: {
23
+ status: 400,
24
+ title: "Invalid or malformed request",
25
+ },
26
+ unauthorized: {
27
+ status: 401,
28
+ title: "Invalid or missing credentials",
29
+ },
30
+ forbidden: {
31
+ status: 403,
32
+ title: "Access to resource denied",
33
+ },
34
+ notFound: {
35
+ status: 404,
36
+ title: "Resource not found",
37
+ },
38
+ conflict: {
39
+ status: 409,
40
+ title: "Conflict with server state",
41
+ },
42
+ unprocessableEntity: {
43
+ status: 422,
44
+ title: "Request failed validation",
45
+ },
46
+ tooManyRequests: {
47
+ status: 429,
48
+ title: "Request limit exceeded",
49
+ },
50
+ internal: {
51
+ status: 500,
52
+ title: "Internal server error",
53
+ },
54
+ };
55
+ function isZodLike(value) {
56
+ const zodLike = value;
57
+ return ((0, isDefined_1.isDefined)(zodLike) &&
58
+ "issues" in zodLike &&
59
+ Array.isArray(zodLike.issues) &&
60
+ zodLike.issues.every((issue) => typeof issue === "object" &&
61
+ (0, isDefined_1.isDefined)(issue) &&
62
+ "path" in issue &&
63
+ Array.isArray(issue.path)));
64
+ }
65
+ /**
66
+ * Error class for service-level errors, convertible to JSON:API errors.
67
+ */
68
+ class ServiceError extends Error {
69
+ /**
70
+ * Converts an error to a `ServiceError`.
71
+ *
72
+ * @param value - The value to convert, which can be any type
73
+ * @returns If the value is already a `ServiceError`, returns it unchanged. Otherwise, convert it
74
+ * to a `ServiceError`.
75
+ */
76
+ static fromError(value) {
77
+ if (value instanceof ServiceError) {
78
+ return value;
79
+ }
80
+ const error = (0, toError_1.toError)(value);
81
+ return new ServiceError({
82
+ issues: [{ code: exports.ERROR_CODES.internal, message: error.message }],
83
+ cause: error,
84
+ });
85
+ }
86
+ /**
87
+ * Converts a Zod-like error to a `ServiceError`.
88
+ *
89
+ * @param value - An object with a Zod-like error structure containing issues
90
+ * @returns The converted `ServiceError`
91
+ * @throws {ServiceError} If the provided value does not match the expected Zod-like structure
92
+ */
93
+ static fromZodLike(value) {
94
+ if (!isZodLike(value)) {
95
+ throw new ServiceError({
96
+ issues: [{ code: exports.ERROR_CODES.internal, message: "Invalid ZodLike" }],
97
+ cause: value,
98
+ });
99
+ }
100
+ return new ServiceError({
101
+ issues: value.issues.map((issue) => ({
102
+ code: exports.ERROR_CODES.unprocessableEntity,
103
+ message: issue.message,
104
+ path: issue.path,
105
+ })),
106
+ cause: value,
107
+ });
108
+ }
109
+ id;
110
+ issues;
111
+ /**
112
+ * Creates a new `ServiceError`
113
+ * @param parameters - Issues contributing to the error or a message string
114
+ */
115
+ constructor(parameters) {
116
+ const params = typeof parameters === "string"
117
+ ? { issues: [toIssue({ message: parameters })] }
118
+ : { ...parameters, issues: parameters.issues.map(toIssue) };
119
+ super(createServiceErrorMessage(params.issues));
120
+ this.id = (0, node_crypto_1.randomUUID)();
121
+ this.issues = (0, deepFreeze_1.deepFreeze)(params.issues);
122
+ this.cause = "cause" in params ? params.cause : undefined;
123
+ this.name = this.constructor.name;
124
+ /**
125
+ * Maintain proper prototype chain in transpiled code
126
+ * @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget}
127
+ */
128
+ Object.setPrototypeOf(this, new.target.prototype);
129
+ }
130
+ /**
131
+ * Return string representation of the error for logging.
132
+ */
133
+ toString() {
134
+ const cause = this.cause ? `; [cause]: ${(0, toError_1.toError)(this.cause).toString()}` : "";
135
+ return `${this.name}[${this.id}]: ${this.message}${cause}`;
136
+ }
137
+ /**
138
+ * Converts the error to JSON:API format
139
+ * @see {@link https://jsonapi.org/format/#error-objects}
140
+ * @returns Object conforming to JSON:API error format
141
+ */
142
+ toJsonApi() {
143
+ return {
144
+ errors: this.issues.map((issue) => ({
145
+ id: this.id,
146
+ status: String(ERROR_METADATA[issue.code].status),
147
+ code: issue.code,
148
+ title: issue.title,
149
+ ...(issue.message && { detail: issue.message }),
150
+ ...(issue.path && {
151
+ source: {
152
+ pointer: `/${issue.path.join("/")}`,
153
+ },
154
+ }),
155
+ })),
156
+ };
157
+ }
158
+ }
159
+ exports.ServiceError = ServiceError;
160
+ /**
161
+ * Creates a human-readable error message from an array of service issues
162
+ * @param issues - Array of issues to include in the message
163
+ * @returns Message string in format "[code1]: detail1; [code2]: detail2"
164
+ */
165
+ function createServiceErrorMessage(issues) {
166
+ if (issues.length === 0) {
167
+ return "[unknown]";
168
+ }
169
+ return issues
170
+ .map((issue) => {
171
+ const message = issue.message ? `: ${issue.message}` : "";
172
+ return `[${issue.code}]${message}`;
173
+ })
174
+ .join("; ");
175
+ }
176
+ function toIssue(issue) {
177
+ const code = issue.code ?? exports.ERROR_CODES.internal;
178
+ return { ...issue, code, title: ERROR_METADATA[code].title };
179
+ }
180
+ //# sourceMappingURL=serviceError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;AAAA,6CAAyC;AAEzC,8CAA2C;AAC3C,oDAAiD;AACjD,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;AA4BX,MAAM,cAAc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E,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;qBACpC;iBACF,CAAC;aACH,CAAC,CAAC;SACJ,CAAC;IACJ,CAAC;CACF;AApGD,oCAoGC;AAED;;;;GAIG;AACH,SAAS,yBAAyB,CAAC,MAAwB;IACzD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,OAAO,MAAM;SACV,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACb,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1D,OAAO,IAAI,KAAK,CAAC,IAAI,IAAI,OAAO,EAAE,CAAC;IACrC,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAS,OAAO,CAAC,KAAmB;IAClC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,mBAAW,CAAC,QAAQ,CAAC;IAChD,OAAO,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Converts an error-like value to an Error instance. If the input is already an Error, returns it
3
+ * directly. Otherwise, creates a new Error with an appropriate string representation of the input.
4
+ *
5
+ * @param value - The value to convert.
6
+ * @returns An Error instance containing a string representation of the input
7
+ */
8
+ export declare function toError(value: unknown): Error;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toError = toError;
4
+ const isString_1 = require("../strings/isString");
5
+ const stringify_1 = require("../strings/stringify");
6
+ const isError_1 = require("./isError");
7
+ /**
8
+ * Converts an error-like value to an Error instance. If the input is already an Error, returns it
9
+ * directly. Otherwise, creates a new Error with an appropriate string representation of the input.
10
+ *
11
+ * @param value - The value to convert.
12
+ * @returns An Error instance containing a string representation of the input
13
+ */
14
+ function toError(value) {
15
+ if ((0, isError_1.isError)(value)) {
16
+ return value;
17
+ }
18
+ if (value && typeof value === "object" && "message" in value) {
19
+ const error = new Error(String(value.message));
20
+ if ("stack" in value) {
21
+ error.stack = String(value.stack);
22
+ }
23
+ return error;
24
+ }
25
+ if (typeof value === "symbol") {
26
+ return new Error(String(value));
27
+ }
28
+ try {
29
+ return new Error((0, isString_1.isString)(value) ? value : (0, stringify_1.stringify)(value));
30
+ }
31
+ catch {
32
+ return new Error(String(value));
33
+ }
34
+ }
35
+ //# sourceMappingURL=toError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/toError.ts"],"names":[],"mappings":";;AAWA,0BAuBC;AAlCD,kDAA+C;AAC/C,oDAAiD;AACjD,uCAAoC;AAEpC;;;;;;GAMG;AACH,SAAgB,OAAO,CAAC,KAAc;IACpC,IAAI,IAAA,iBAAO,EAAC,KAAK,CAAC,EAAE,CAAC;QACnB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;QAC7D,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;QAC/C,IAAI,OAAO,IAAI,KAAK,EAAE,CAAC;YACrB,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACpC,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,CAAC;QACH,OAAO,IAAI,KAAK,CAAC,IAAA,mBAAQ,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAA,qBAAS,EAAC,KAAK,CAAC,CAAC,CAAC;IAC/D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAClC,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Converts any value to an Error instance and returns the message.
3
+ *
4
+ * @param value - The value to convert.
5
+ * @returns The Error instance's message field.
6
+ */
7
+ export declare function toErrorMessage(value: unknown): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toErrorMessage = toErrorMessage;
4
+ const toError_1 = require("./toError");
5
+ /**
6
+ * Converts any value to an Error instance and returns the message.
7
+ *
8
+ * @param value - The value to convert.
9
+ * @returns The Error instance's message field.
10
+ */
11
+ function toErrorMessage(value) {
12
+ return (0, toError_1.toError)(value).message;
13
+ }
14
+ //# sourceMappingURL=toErrorMessage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toErrorMessage.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/toErrorMessage.ts"],"names":[],"mappings":";;AAQA,wCAEC;AAVD,uCAAoC;AAEpC;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,IAAA,iBAAO,EAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AAChC,CAAC"}
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Force cast to the provided type.
3
+ *
4
+ * @deprecated Use type guards instead.
3
5
  */
4
6
  export declare function forceCast<T>(value: unknown): T;
5
- //# sourceMappingURL=force-cast.d.ts.map
@@ -3,8 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.forceCast = forceCast;
4
4
  /**
5
5
  * Force cast to the provided type.
6
+ *
7
+ * @deprecated Use type guards instead.
6
8
  */
7
9
  function forceCast(value) {
8
10
  return value;
9
11
  }
10
- //# sourceMappingURL=force-cast.js.map
12
+ //# sourceMappingURL=forceCast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"forceCast.js","sourceRoot":"","sources":["../../../../../packages/util-ts/src/lib/forceCast.ts"],"names":[],"mappings":";;AAKA,8BAEC;AAPD;;;;GAIG;AACH,SAAgB,SAAS,CAAI,KAAc;IACzC,OAAO,KAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { type Arrayable } from "type-fest";
2
+ import { CbhError, type CbhIssue } from "../errors/cbhError";
3
+ export type CbhResponse<T> = {
4
+ success: true;
5
+ data: T;
6
+ } | {
7
+ success: false;
8
+ error: CbhError;
9
+ };
10
+ /**
11
+ * @deprecated Use {@link failure} instead.
12
+ */
13
+ export declare function toErrorCbhResponse(issues: Arrayable<CbhIssue>): {
14
+ success: false;
15
+ error: CbhError;
16
+ };
17
+ /**
18
+ * @deprecated Use {@link success} instead.
19
+ */
20
+ export declare function toSuccessCbhResponse<T>(data: T): {
21
+ success: true;
22
+ data: T;
23
+ };
@@ -2,11 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toErrorCbhResponse = toErrorCbhResponse;
4
4
  exports.toSuccessCbhResponse = toSuccessCbhResponse;
5
- const cbh_error_1 = require("./cbh-error");
5
+ const cbhError_1 = require("../errors/cbhError");
6
+ /**
7
+ * @deprecated Use {@link failure} instead.
8
+ */
6
9
  function toErrorCbhResponse(issues) {
7
- return { success: false, error: new cbh_error_1.CbhError(issues) };
10
+ return { success: false, error: new cbhError_1.CbhError(issues) };
8
11
  }
12
+ /**
13
+ * @deprecated Use {@link success} instead.
14
+ */
9
15
  function toSuccessCbhResponse(data) {
10
16
  return { success: true, data };
11
17
  }
12
- //# sourceMappingURL=cbh-response.js.map
18
+ //# sourceMappingURL=cbhResponse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cbhResponse.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/cbhResponse.ts"],"names":[],"mappings":";;AASA,gDAKC;AAKD,oDAEC;AAnBD,iDAA6D;AAI7D;;GAEG;AACH,SAAgB,kBAAkB,CAAC,MAA2B;IAI5D,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,mBAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;AACzD,CAAC;AAED;;GAEG;AACH,SAAgB,oBAAoB,CAAI,IAAO;IAC7C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACjC,CAAC"}
@@ -0,0 +1,86 @@
1
+ export type Left<E> = Readonly<{
2
+ isRight: false;
3
+ left: E;
4
+ }>;
5
+ export type Right<A> = Readonly<{
6
+ isRight: true;
7
+ right: A;
8
+ }>;
9
+ /**
10
+ * A value of `Either` type `Left<E>` or type `Right<A>`; a disjoint union.
11
+ *
12
+ * A common use case is as an alternative to {@link Option} where `Left<E>` contains useful
13
+ * information. Convention dictates that `Left<E>` is used for failure and `Right<A>` for success.
14
+ * To help remember, the success case is "right"; it's the result you want.
15
+ *
16
+ * @includeExample ./packages/util-ts/examples/either.ts
17
+ * @see [Usage example](../../util-ts/examples/either.ts)
18
+ */
19
+ export type Either<E, A> = Left<E> | Right<A>;
20
+ /**
21
+ * Constructs an `Either` holding a `Left<E>` value, usually representing a failure.
22
+ *
23
+ * @param left - The value to wrap in a `Left`
24
+ * @returns A `Left` containing the value
25
+ */
26
+ export declare function left<E, A = never>(left: E): Either<E, A>;
27
+ /**
28
+ * Constructs an `Either` holding a `Right<A>`, representing a success.
29
+ *
30
+ * @param value - The value to wrap in a `Right`
31
+ * @returns A `Right` containing the value
32
+ */
33
+ export declare function right<A, E = never>(right: A): Either<E, A>;
34
+ /**
35
+ * Type guard that checks if an either is `Left<E>`.
36
+ *
37
+ * @param either - The `Either` to check
38
+ * @returns `true` if the `Either` is `Left<E>`, `false` if it is `Right<A>`
39
+ */
40
+ export declare function isLeft<E, A>(either: Either<E, A>): either is Left<E>;
41
+ /**
42
+ * Type guard that checks if an either is `Right<A>`.
43
+ *
44
+ * @param either - The `Either` to check
45
+ * @returns `true` if the `Either` is `Right<A>`, `false` if it is `Left<E>`
46
+ */
47
+ export declare function isRight<E, A>(either: Either<E, A>): either is Right<A>;
48
+ /**
49
+ * Transforms the value inside an `Either` using the provided function. If the `Either` is
50
+ * `Right(value)`, returns `Right(f(value))`. If the `Either` is `Left(value)`, returns
51
+ * `Left(value)`.
52
+ *
53
+ * @param f - The function to apply to the `Right` value
54
+ * @returns A function that transforms `Either<E, A>` to `Either<E, B>`
55
+ */
56
+ export declare function map<A, B>(f: (right: A) => B): <E>(either: Either<E, A>) => Either<E, B>;
57
+ /**
58
+ * Transforms the value inside an `Either` using the provided function. If the `Either` is
59
+ * `Left(value)`, returns `Left(f(value))`. If the `Either` is `Right(value)`, returns
60
+ * `Right(value)`.
61
+ */
62
+ export declare function mapLeft<E, G>(f: (left: E) => G): <A>(either: Either<E, A>) => Either<G, A>;
63
+ /**
64
+ * Chains `Either` operations that return `Either`s. Unlike `map` which wraps the result in a new
65
+ * `Either`, `flatMap` prevents nested `Either`s like `Right(Right(value))`.
66
+ *
67
+ * @param f - A function that returns an `Either`
68
+ * @returns The `Either` returned by the function if the input is `Right`, `Left` otherwise
69
+ */
70
+ export declare function flatMap<E, A, B>(f: (right: A) => Either<E, B>): (either: Either<E, A>) => Either<E, B>;
71
+ /**
72
+ * Safely extracts the value from an `Either` with a fallback. Use this function when you need to
73
+ * convert an `Either<E, A>` to an `A`, providing a default value for the `Left` case.
74
+ *
75
+ * @param defaultValue - The value to return if the `Either` is `Left`
76
+ * @returns The contained value if `Right`, `defaultValue` if `Left`
77
+ */
78
+ export declare function getOrElse<E, A>(onLeft: (left: E) => A): (either: Either<E, A>) => A;
79
+ /**
80
+ * Pattern matches on an `Either`, handling both `Right` and `Left` cases.
81
+ *
82
+ * @param onLeft - Function to handle the `Left` case
83
+ * @param onRight - Function to handle the `Right` case
84
+ * @returns The result of `Either` `onLeft` or `onRight` based on the `Either` state
85
+ */
86
+ export declare function match<E, A, B>(onLeft: (left: E) => B, onRight: (right: A) => B): (either: Either<E, A>) => B;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.left = left;
4
+ exports.right = right;
5
+ exports.isLeft = isLeft;
6
+ exports.isRight = isRight;
7
+ exports.map = map;
8
+ exports.mapLeft = mapLeft;
9
+ exports.flatMap = flatMap;
10
+ exports.getOrElse = getOrElse;
11
+ exports.match = match;
12
+ /**
13
+ * Constructs an `Either` holding a `Left<E>` value, usually representing a failure.
14
+ *
15
+ * @param left - The value to wrap in a `Left`
16
+ * @returns A `Left` containing the value
17
+ */
18
+ function left(left) {
19
+ return { isRight: false, left };
20
+ }
21
+ /**
22
+ * Constructs an `Either` holding a `Right<A>`, representing a success.
23
+ *
24
+ * @param value - The value to wrap in a `Right`
25
+ * @returns A `Right` containing the value
26
+ */
27
+ function right(right) {
28
+ return { isRight: true, right };
29
+ }
30
+ /**
31
+ * Type guard that checks if an either is `Left<E>`.
32
+ *
33
+ * @param either - The `Either` to check
34
+ * @returns `true` if the `Either` is `Left<E>`, `false` if it is `Right<A>`
35
+ */
36
+ function isLeft(either) {
37
+ return !either.isRight;
38
+ }
39
+ /**
40
+ * Type guard that checks if an either is `Right<A>`.
41
+ *
42
+ * @param either - The `Either` to check
43
+ * @returns `true` if the `Either` is `Right<A>`, `false` if it is `Left<E>`
44
+ */
45
+ function isRight(either) {
46
+ return either.isRight;
47
+ }
48
+ /**
49
+ * Transforms the value inside an `Either` using the provided function. If the `Either` is
50
+ * `Right(value)`, returns `Right(f(value))`. If the `Either` is `Left(value)`, returns
51
+ * `Left(value)`.
52
+ *
53
+ * @param f - The function to apply to the `Right` value
54
+ * @returns A function that transforms `Either<E, A>` to `Either<E, B>`
55
+ */
56
+ function map(f) {
57
+ return (either) => (isRight(either) ? right(f(either.right)) : either);
58
+ }
59
+ /**
60
+ * Transforms the value inside an `Either` using the provided function. If the `Either` is
61
+ * `Left(value)`, returns `Left(f(value))`. If the `Either` is `Right(value)`, returns
62
+ * `Right(value)`.
63
+ */
64
+ function mapLeft(f) {
65
+ return (either) => (isLeft(either) ? left(f(either.left)) : either);
66
+ }
67
+ /**
68
+ * Chains `Either` operations that return `Either`s. Unlike `map` which wraps the result in a new
69
+ * `Either`, `flatMap` prevents nested `Either`s like `Right(Right(value))`.
70
+ *
71
+ * @param f - A function that returns an `Either`
72
+ * @returns The `Either` returned by the function if the input is `Right`, `Left` otherwise
73
+ */
74
+ function flatMap(f) {
75
+ return (either) => (isRight(either) ? f(either.right) : either);
76
+ }
77
+ /**
78
+ * Safely extracts the value from an `Either` with a fallback. Use this function when you need to
79
+ * convert an `Either<E, A>` to an `A`, providing a default value for the `Left` case.
80
+ *
81
+ * @param defaultValue - The value to return if the `Either` is `Left`
82
+ * @returns The contained value if `Right`, `defaultValue` if `Left`
83
+ */
84
+ function getOrElse(onLeft) {
85
+ return (either) => (isRight(either) ? either.right : onLeft(either.left));
86
+ }
87
+ /**
88
+ * Pattern matches on an `Either`, handling both `Right` and `Left` cases.
89
+ *
90
+ * @param onLeft - Function to handle the `Left` case
91
+ * @param onRight - Function to handle the `Right` case
92
+ * @returns The result of `Either` `onLeft` or `onRight` based on the `Either` state
93
+ */
94
+ function match(onLeft, onRight) {
95
+ return (either) => (isRight(either) ? onRight(either.right) : onLeft(either.left));
96
+ }
97
+ //# sourceMappingURL=either.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"either.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/either.ts"],"names":[],"mappings":";;AA4BA,oBAEC;AAQD,sBAEC;AAQD,wBAEC;AAQD,0BAEC;AAUD,kBAEC;AAOD,0BAEC;AASD,0BAIC;AASD,8BAEC;AASD,sBAKC;AAjGD;;;;;GAKG;AACH,SAAgB,IAAI,CAAe,IAA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AAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AAC5E,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CACnB,MAAsB,EACtB,OAAwB;IAExB,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACrF,CAAC"}
@@ -0,0 +1,71 @@
1
+ export type None = Readonly<{
2
+ isSome: false;
3
+ }>;
4
+ export type Some<A> = Readonly<{
5
+ isSome: true;
6
+ value: A;
7
+ }>;
8
+ /**
9
+ * An optional value. If the value exists, it's of type `Some<A>`, otherwise it's of type `None`.
10
+ *
11
+ * @includeExample ./packages/util-ts/examples/option.ts
12
+ * @see [Usage example](../../util-ts/examples/option.ts)
13
+ */
14
+ export type Option<A> = None | Some<A>;
15
+ /**
16
+ * Constructs an `Option` of `None`, representing a missing value.
17
+ */
18
+ export declare const none: None;
19
+ /**
20
+ * Constructs an `Option` holding a `Some<A>`, representing an optional value that exists.
21
+ *
22
+ * @param value - The value to wrap in a `Some`
23
+ * @returns A `Some` containing the value
24
+ */
25
+ export declare function some<A>(value: A): Some<A>;
26
+ /**
27
+ * Type guard that checks if an `Option` is `None`.
28
+ *
29
+ * @param option - The `Option` to check
30
+ * @returns `true` if the `Option` is `None`, `false` if it is `Some<A>`
31
+ */
32
+ export declare function isNone<A>(option: Option<A>): option is None;
33
+ /**
34
+ * Type guard that checks if an `Option` is `Some<A>`.
35
+ *
36
+ * @param option - The `Option` to check
37
+ * @returns `true` if the `Option` is `Some<A>`, `false` if it is `None`
38
+ */
39
+ export declare function isSome<A>(option: Option<A>): option is Some<A>;
40
+ /**
41
+ * Transforms the value inside an `Option` using the provided function. If the `Option` is
42
+ * `Some(value)`, returns `Some(f(value))`. If the `Option` is `None`, returns `None`.
43
+ *
44
+ * @param f - The function to apply to the value if it exists
45
+ * @returns A new `Option` containing the transformed value
46
+ */
47
+ export declare function map<A, B>(f: (a: A) => B): (option: Option<A>) => Option<B>;
48
+ /**
49
+ * Chains `Option` operations that return `Option`s. Unlike `map` which wraps the result in a new
50
+ * `Option`, `flatMap` prevents nested `Option`s like `Some(Some(value))`.
51
+ *
52
+ * @param f - A function that returns an `Option`
53
+ * @returns The `Option` returned by the function if the input is `Some`, `None` otherwise
54
+ */
55
+ export declare function flatMap<A, B>(f: (a: A) => Option<B>): (option: Option<A>) => Option<B>;
56
+ /**
57
+ * Safely extracts the value from an `Option` with a fallback. Use this function when you need to
58
+ * convert an `Option<A>` to an `A`, providing a default value for the `None` case.
59
+ *
60
+ * @param defaultValue - The value to return if the `Option` is `None`
61
+ * @returns The contained value if `Some`, `defaultValue` if `None`
62
+ */
63
+ export declare function getOrElse<A>(defaultValue: A): (option: Option<A>) => A;
64
+ /**
65
+ * Pattern matches on an `Option`, handling both `Some` and `None` cases.
66
+ *
67
+ * @param onNone - Function to handle the `None` case
68
+ * @param onSome - Function to handle the `Some` case
69
+ * @returns The result of either `onNone` or `onSome` based on the `Option` state
70
+ */
71
+ export declare function match<A, B>(onNone: () => B, onSome: (value: A) => B): (option: Option<A>) => B;