@salesforce/capg-client 0.1.0-beta.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.
Files changed (68) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +122 -0
  3. package/dist/client.d.ts +86 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +167 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/connect-api-client.d.ts +82 -0
  8. package/dist/connect-api-client.d.ts.map +1 -0
  9. package/dist/connect-api-client.js +106 -0
  10. package/dist/connect-api-client.js.map +1 -0
  11. package/dist/constants.d.ts +64 -0
  12. package/dist/constants.d.ts.map +1 -0
  13. package/dist/constants.js +68 -0
  14. package/dist/constants.js.map +1 -0
  15. package/dist/errors/api.d.ts +79 -0
  16. package/dist/errors/api.d.ts.map +1 -0
  17. package/dist/errors/api.js +128 -0
  18. package/dist/errors/api.js.map +1 -0
  19. package/dist/errors/auth.d.ts +35 -0
  20. package/dist/errors/auth.d.ts.map +1 -0
  21. package/dist/errors/auth.js +50 -0
  22. package/dist/errors/auth.js.map +1 -0
  23. package/dist/errors/base.d.ts +103 -0
  24. package/dist/errors/base.d.ts.map +1 -0
  25. package/dist/errors/base.js +162 -0
  26. package/dist/errors/base.js.map +1 -0
  27. package/dist/errors/hub-org.d.ts +108 -0
  28. package/dist/errors/hub-org.d.ts.map +1 -0
  29. package/dist/errors/hub-org.js +144 -0
  30. package/dist/errors/hub-org.js.map +1 -0
  31. package/dist/errors/index.d.ts +33 -0
  32. package/dist/errors/index.d.ts.map +1 -0
  33. package/dist/errors/index.js +42 -0
  34. package/dist/errors/index.js.map +1 -0
  35. package/dist/errors/network.d.ts +53 -0
  36. package/dist/errors/network.d.ts.map +1 -0
  37. package/dist/errors/network.js +77 -0
  38. package/dist/errors/network.js.map +1 -0
  39. package/dist/errors/validation.d.ts +69 -0
  40. package/dist/errors/validation.d.ts.map +1 -0
  41. package/dist/errors/validation.js +96 -0
  42. package/dist/errors/validation.js.map +1 -0
  43. package/dist/index.d.ts +16 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +28 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/org-pref-checker.d.ts +129 -0
  48. package/dist/org-pref-checker.d.ts.map +1 -0
  49. package/dist/org-pref-checker.js +193 -0
  50. package/dist/org-pref-checker.js.map +1 -0
  51. package/dist/policy-fetcher.d.ts +144 -0
  52. package/dist/policy-fetcher.d.ts.map +1 -0
  53. package/dist/policy-fetcher.js +241 -0
  54. package/dist/policy-fetcher.js.map +1 -0
  55. package/dist/tsconfig.tsbuildinfo +1 -0
  56. package/dist/types/index.d.ts +532 -0
  57. package/dist/types/index.d.ts.map +1 -0
  58. package/dist/types/index.js +6 -0
  59. package/dist/types/index.js.map +1 -0
  60. package/dist/types/result.d.ts +282 -0
  61. package/dist/types/result.d.ts.map +1 -0
  62. package/dist/types/result.js +351 -0
  63. package/dist/types/result.js.map +1 -0
  64. package/dist/utils/error-classifier.d.ts +55 -0
  65. package/dist/utils/error-classifier.d.ts.map +1 -0
  66. package/dist/utils/error-classifier.js +90 -0
  67. package/dist/utils/error-classifier.js.map +1 -0
  68. package/package.json +143 -0
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Result type - discriminated union representing success or failure
3
+ *
4
+ * A Result<T, E> is either:
5
+ * - { ok: true, value: T } - success case with value of type T
6
+ * - { ok: false, error: E } - failure case with error of type E
7
+ *
8
+ * The `ok` field is the discriminant that enables type narrowing.
9
+ *
10
+ * @typeParam T - Success value type
11
+ * @typeParam E - Error type (defaults to Error)
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const success: Result<number, string> = { ok: true, value: 42 };
16
+ * const failure: Result<number, string> = { ok: false, error: 'failed' };
17
+ * ```
18
+ */
19
+ export type Result<T, E = Error> = {
20
+ ok: true;
21
+ value: T;
22
+ } | {
23
+ ok: false;
24
+ error: E;
25
+ };
26
+ /**
27
+ * Creates a successful result
28
+ *
29
+ * @typeParam T - Success value type
30
+ * @param value - The success value
31
+ * @returns Result in success state
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const result = ok(42);
36
+ * // result: Result<number, never>
37
+ * // { ok: true, value: 42 }
38
+ * ```
39
+ *
40
+ * @example With complex types
41
+ * ```typescript
42
+ * const result = ok({ id: 1, name: 'test' });
43
+ * // result: Result<{ id: number, name: string }, never>
44
+ * ```
45
+ */
46
+ export declare function ok<T>(value: T): Result<T, never>;
47
+ /**
48
+ * Creates a failed result
49
+ *
50
+ * @typeParam E - Error type
51
+ * @param error - The error value
52
+ * @returns Result in error state
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const result = err('Something went wrong');
57
+ * // result: Result<never, string>
58
+ * // { ok: false, error: 'Something went wrong' }
59
+ * ```
60
+ *
61
+ * @example With Error objects
62
+ * ```typescript
63
+ * const result = err(new Error('Network timeout'));
64
+ * // result: Result<never, Error>
65
+ * ```
66
+ */
67
+ export declare function err<E>(error: E): Result<never, E>;
68
+ /**
69
+ * Type guard to check if result is successful
70
+ *
71
+ * Narrows the Result type to the success case, allowing safe access to `.value`.
72
+ *
73
+ * @typeParam T - Success value type
74
+ * @typeParam E - Error type
75
+ * @param result - Result to check
76
+ * @returns True if result is successful
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const result: Result<number, string> = ok(42);
81
+ *
82
+ * if (isOk(result)) {
83
+ * console.log(result.value); // TypeScript knows result.value exists
84
+ * }
85
+ * ```
86
+ */
87
+ export declare function isOk<T, E>(result: Result<T, E>): result is {
88
+ ok: true;
89
+ value: T;
90
+ };
91
+ /**
92
+ * Type guard to check if result is failed
93
+ *
94
+ * Narrows the Result type to the error case, allowing safe access to `.error`.
95
+ *
96
+ * @typeParam T - Success value type
97
+ * @typeParam E - Error type
98
+ * @param result - Result to check
99
+ * @returns True if result is failed
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const result: Result<number, string> = err('failed');
104
+ *
105
+ * if (isErr(result)) {
106
+ * console.error(result.error); // TypeScript knows result.error exists
107
+ * }
108
+ * ```
109
+ */
110
+ export declare function isErr<T, E>(result: Result<T, E>): result is {
111
+ ok: false;
112
+ error: E;
113
+ };
114
+ /**
115
+ * Extracts the value from a successful result or throws an error
116
+ *
117
+ * WARNING: This function throws on error results. Use `unwrapOr()` or `match()`
118
+ * for safer alternatives.
119
+ *
120
+ * @typeParam T - Success value type
121
+ * @typeParam E - Error type
122
+ * @param result - Result to unwrap
123
+ * @returns The success value
124
+ * @throws CAPGError if result is failed
125
+ *
126
+ * @example Success case
127
+ * ```typescript
128
+ * const result = ok(42);
129
+ * const value = unwrap(result); // 42
130
+ * ```
131
+ *
132
+ * @example Error case
133
+ * ```typescript
134
+ * const result = err('failed');
135
+ * const value = unwrap(result); // throws CAPGError
136
+ * ```
137
+ */
138
+ export declare function unwrap<T, E>(result: Result<T, E>): T;
139
+ /**
140
+ * Extracts the value from a result or returns a default value
141
+ *
142
+ * This is a safer alternative to `unwrap()` that never throws.
143
+ *
144
+ * @typeParam T - Success value type
145
+ * @typeParam E - Error type
146
+ * @param result - Result to unwrap
147
+ * @param defaultValue - Value to return if result is failed
148
+ * @returns The success value or default value
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * const result1 = ok(42);
153
+ * unwrapOr(result1, 0); // 42
154
+ *
155
+ * const result2 = err('failed');
156
+ * unwrapOr(result2, 0); // 0
157
+ * ```
158
+ */
159
+ export declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
160
+ /**
161
+ * Transforms the success value of a result
162
+ *
163
+ * If the result is successful, applies the transform function to the value.
164
+ * If the result is failed, returns the error unchanged.
165
+ *
166
+ * @typeParam T - Input success value type
167
+ * @typeParam U - Output success value type
168
+ * @typeParam E - Error type
169
+ * @param result - Result to transform
170
+ * @param fn - Transform function
171
+ * @returns Result with transformed value or original error
172
+ *
173
+ * @example Basic transformation
174
+ * ```typescript
175
+ * const result = ok(42);
176
+ * const doubled = map(result, (x) => x * 2);
177
+ * // doubled: Result<number, never> = ok(84)
178
+ * ```
179
+ *
180
+ * @example Type transformation
181
+ * ```typescript
182
+ * const result = ok(42);
183
+ * const formatted = map(result, (x) => `Value: ${x}`);
184
+ * // formatted: Result<string, never> = ok('Value: 42')
185
+ * ```
186
+ *
187
+ * @example Error case (unchanged)
188
+ * ```typescript
189
+ * const result: Result<number, string> = err('failed');
190
+ * const doubled = map(result, (x) => x * 2);
191
+ * // doubled: Result<number, string> = err('failed')
192
+ * ```
193
+ */
194
+ export declare function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
195
+ /**
196
+ * Transforms the error value of a result
197
+ *
198
+ * If the result is failed, applies the transform function to the error.
199
+ * If the result is successful, returns the value unchanged.
200
+ *
201
+ * @typeParam T - Success value type
202
+ * @typeParam E - Input error type
203
+ * @typeParam F - Output error type
204
+ * @param result - Result to transform
205
+ * @param fn - Transform function for errors
206
+ * @returns Result with original value or transformed error
207
+ *
208
+ * @example Basic error transformation
209
+ * ```typescript
210
+ * const result: Result<number, string> = err('error');
211
+ * const withError = mapErr(result, (e) => new Error(e));
212
+ * // withError: Result<number, Error> = err(Error('error'))
213
+ * ```
214
+ *
215
+ * @example Success case (unchanged)
216
+ * ```typescript
217
+ * const result: Result<number, string> = ok(42);
218
+ * const mapped = mapErr(result, (e) => new Error(e));
219
+ * // mapped: Result<number, Error> = ok(42)
220
+ * ```
221
+ *
222
+ * @example Enriching errors
223
+ * ```typescript
224
+ * const result: Result<number, number> = err(404);
225
+ * const enriched = mapErr(result, (code) => ({
226
+ * status: code,
227
+ * message: 'Not Found'
228
+ * }));
229
+ * // enriched: Result<number, { status: number, message: string }>
230
+ * ```
231
+ */
232
+ export declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
233
+ /**
234
+ * Pattern matching for exhaustive handling of Result cases
235
+ *
236
+ * Forces explicit handling of both success and error cases. Both handlers
237
+ * must return the same type.
238
+ *
239
+ * @typeParam T - Success value type
240
+ * @typeParam E - Error type
241
+ * @typeParam U - Return type (must be same for both handlers)
242
+ * @param result - Result to match against
243
+ * @param handlers - Object with ok and err handler functions
244
+ * @returns Result of the executed handler
245
+ *
246
+ * @example Basic matching
247
+ * ```typescript
248
+ * const result: Result<number, string> = ok(42);
249
+ * const message = match(result, {
250
+ * ok: (value) => `Success: ${value}`,
251
+ * err: (error) => `Error: ${error}`
252
+ * });
253
+ * // message: 'Success: 42'
254
+ * ```
255
+ *
256
+ * @example With side effects
257
+ * ```typescript
258
+ * match(result, {
259
+ * ok: (value) => {
260
+ * console.log(`Got value: ${value}`);
261
+ * return value;
262
+ * },
263
+ * err: (error) => {
264
+ * console.error(`Got error: ${error}`);
265
+ * return 0;
266
+ * }
267
+ * });
268
+ * ```
269
+ *
270
+ * @example Complex return types
271
+ * ```typescript
272
+ * const output = match(result, {
273
+ * ok: (point) => ({ x: point.x * 2, y: point.y * 2 }),
274
+ * err: () => ({ x: 0, y: 0 })
275
+ * });
276
+ * ```
277
+ */
278
+ export declare function match<T, E, U>(result: Result<T, E>, handlers: {
279
+ ok: (value: T) => U;
280
+ err: (error: E) => U;
281
+ }): U;
282
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/types/result.ts"],"names":[],"mappings":"AAyDA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAEpF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAEhD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB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E,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAMvF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAC3B,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EACpB,QAAQ,EAAE;IACR,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;IACpB,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;CACtB,GACA,CAAC,CAMH"}
@@ -0,0 +1,351 @@
1
+ /*
2
+ * Copyright 2026, Salesforce, Inc. All rights reserved.
3
+ * See LICENSE.txt for license terms.
4
+ */
5
+ /**
6
+ * Result Type Pattern
7
+ *
8
+ * A type-safe, functional approach to error handling that makes success and
9
+ * failure cases explicit in the type system. This eliminates the need for
10
+ * try-catch blocks and forces consumers to handle both success and error cases.
11
+ *
12
+ * Benefits:
13
+ * - Type-safe error handling (errors are values, not exceptions)
14
+ * - Forces explicit handling of both success and error cases
15
+ * - Composable with map/mapErr for transformations
16
+ * - Pattern matching with match() for exhaustive case handling
17
+ * - No hidden control flow (no try-catch)
18
+ *
19
+ * @example Basic usage
20
+ * ```typescript
21
+ * function divide(a: number, b: number): Result<number, string> {
22
+ * if (b === 0) {
23
+ * return err('Division by zero');
24
+ * }
25
+ * return ok(a / b);
26
+ * }
27
+ *
28
+ * const result = divide(10, 2);
29
+ * if (isOk(result)) {
30
+ * console.log(result.value); // 5
31
+ * } else {
32
+ * console.error(result.error); // 'Division by zero'
33
+ * }
34
+ * ```
35
+ *
36
+ * @example Transformations
37
+ * ```typescript
38
+ * const result = ok(42);
39
+ * const doubled = map(result, (x) => x * 2); // ok(84)
40
+ * const formatted = map(doubled, (x) => `Value: ${x}`); // ok('Value: 84')
41
+ * ```
42
+ *
43
+ * @example Pattern matching
44
+ * ```typescript
45
+ * const message = match(result, {
46
+ * ok: (value) => `Success: ${value}`,
47
+ * err: (error) => `Error: ${error}`
48
+ * });
49
+ * ```
50
+ *
51
+ * @packageDocumentation
52
+ * @since v1.0.0
53
+ */
54
+ import { CAPGError } from '../errors/base.js';
55
+ /**
56
+ * Creates a successful result
57
+ *
58
+ * @typeParam T - Success value type
59
+ * @param value - The success value
60
+ * @returns Result in success state
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const result = ok(42);
65
+ * // result: Result<number, never>
66
+ * // { ok: true, value: 42 }
67
+ * ```
68
+ *
69
+ * @example With complex types
70
+ * ```typescript
71
+ * const result = ok({ id: 1, name: 'test' });
72
+ * // result: Result<{ id: number, name: string }, never>
73
+ * ```
74
+ */
75
+ export function ok(value) {
76
+ return { ok: true, value };
77
+ }
78
+ /**
79
+ * Creates a failed result
80
+ *
81
+ * @typeParam E - Error type
82
+ * @param error - The error value
83
+ * @returns Result in error state
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const result = err('Something went wrong');
88
+ * // result: Result<never, string>
89
+ * // { ok: false, error: 'Something went wrong' }
90
+ * ```
91
+ *
92
+ * @example With Error objects
93
+ * ```typescript
94
+ * const result = err(new Error('Network timeout'));
95
+ * // result: Result<never, Error>
96
+ * ```
97
+ */
98
+ export function err(error) {
99
+ return { ok: false, error };
100
+ }
101
+ /**
102
+ * Type guard to check if result is successful
103
+ *
104
+ * Narrows the Result type to the success case, allowing safe access to `.value`.
105
+ *
106
+ * @typeParam T - Success value type
107
+ * @typeParam E - Error type
108
+ * @param result - Result to check
109
+ * @returns True if result is successful
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const result: Result<number, string> = ok(42);
114
+ *
115
+ * if (isOk(result)) {
116
+ * console.log(result.value); // TypeScript knows result.value exists
117
+ * }
118
+ * ```
119
+ */
120
+ export function isOk(result) {
121
+ return result.ok === true;
122
+ }
123
+ /**
124
+ * Type guard to check if result is failed
125
+ *
126
+ * Narrows the Result type to the error case, allowing safe access to `.error`.
127
+ *
128
+ * @typeParam T - Success value type
129
+ * @typeParam E - Error type
130
+ * @param result - Result to check
131
+ * @returns True if result is failed
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const result: Result<number, string> = err('failed');
136
+ *
137
+ * if (isErr(result)) {
138
+ * console.error(result.error); // TypeScript knows result.error exists
139
+ * }
140
+ * ```
141
+ */
142
+ export function isErr(result) {
143
+ return result.ok === false;
144
+ }
145
+ /**
146
+ * Extracts the value from a successful result or throws an error
147
+ *
148
+ * WARNING: This function throws on error results. Use `unwrapOr()` or `match()`
149
+ * for safer alternatives.
150
+ *
151
+ * @typeParam T - Success value type
152
+ * @typeParam E - Error type
153
+ * @param result - Result to unwrap
154
+ * @returns The success value
155
+ * @throws CAPGError if result is failed
156
+ *
157
+ * @example Success case
158
+ * ```typescript
159
+ * const result = ok(42);
160
+ * const value = unwrap(result); // 42
161
+ * ```
162
+ *
163
+ * @example Error case
164
+ * ```typescript
165
+ * const result = err('failed');
166
+ * const value = unwrap(result); // throws CAPGError
167
+ * ```
168
+ */
169
+ export function unwrap(result) {
170
+ if (result.ok) {
171
+ return result.value;
172
+ }
173
+ // Type assertion: we know result.ok is false, so error exists
174
+ const error = result.error;
175
+ if (error instanceof CAPGError) {
176
+ throw error;
177
+ }
178
+ if (error instanceof Error) {
179
+ throw new CAPGError('RESULT_UNWRAP_ERROR', error.message,
180
+ // Suggestion: Guide developers to use safe patterns (isOk check or unwrapOr)
181
+ 'Check the result before unwrapping or use unwrapOr()', error);
182
+ }
183
+ // Error is not an Error object (string, number, etc.)
184
+ throw new CAPGError('RESULT_UNWRAP_ERROR', `Unwrap failed: ${String(error)}`,
185
+ // Suggestion: Guide developers to use safe patterns (isOk check or unwrapOr)
186
+ 'Check the result before unwrapping or use unwrapOr()');
187
+ }
188
+ /**
189
+ * Extracts the value from a result or returns a default value
190
+ *
191
+ * This is a safer alternative to `unwrap()` that never throws.
192
+ *
193
+ * @typeParam T - Success value type
194
+ * @typeParam E - Error type
195
+ * @param result - Result to unwrap
196
+ * @param defaultValue - Value to return if result is failed
197
+ * @returns The success value or default value
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * const result1 = ok(42);
202
+ * unwrapOr(result1, 0); // 42
203
+ *
204
+ * const result2 = err('failed');
205
+ * unwrapOr(result2, 0); // 0
206
+ * ```
207
+ */
208
+ export function unwrapOr(result, defaultValue) {
209
+ if (result.ok) {
210
+ return result.value;
211
+ }
212
+ return defaultValue;
213
+ }
214
+ /**
215
+ * Transforms the success value of a result
216
+ *
217
+ * If the result is successful, applies the transform function to the value.
218
+ * If the result is failed, returns the error unchanged.
219
+ *
220
+ * @typeParam T - Input success value type
221
+ * @typeParam U - Output success value type
222
+ * @typeParam E - Error type
223
+ * @param result - Result to transform
224
+ * @param fn - Transform function
225
+ * @returns Result with transformed value or original error
226
+ *
227
+ * @example Basic transformation
228
+ * ```typescript
229
+ * const result = ok(42);
230
+ * const doubled = map(result, (x) => x * 2);
231
+ * // doubled: Result<number, never> = ok(84)
232
+ * ```
233
+ *
234
+ * @example Type transformation
235
+ * ```typescript
236
+ * const result = ok(42);
237
+ * const formatted = map(result, (x) => `Value: ${x}`);
238
+ * // formatted: Result<string, never> = ok('Value: 42')
239
+ * ```
240
+ *
241
+ * @example Error case (unchanged)
242
+ * ```typescript
243
+ * const result: Result<number, string> = err('failed');
244
+ * const doubled = map(result, (x) => x * 2);
245
+ * // doubled: Result<number, string> = err('failed')
246
+ * ```
247
+ */
248
+ export function map(result, fn) {
249
+ if (result.ok) {
250
+ return ok(fn(result.value));
251
+ }
252
+ // Type assertion: result.ok is false, so error exists
253
+ return err(result.error);
254
+ }
255
+ /**
256
+ * Transforms the error value of a result
257
+ *
258
+ * If the result is failed, applies the transform function to the error.
259
+ * If the result is successful, returns the value unchanged.
260
+ *
261
+ * @typeParam T - Success value type
262
+ * @typeParam E - Input error type
263
+ * @typeParam F - Output error type
264
+ * @param result - Result to transform
265
+ * @param fn - Transform function for errors
266
+ * @returns Result with original value or transformed error
267
+ *
268
+ * @example Basic error transformation
269
+ * ```typescript
270
+ * const result: Result<number, string> = err('error');
271
+ * const withError = mapErr(result, (e) => new Error(e));
272
+ * // withError: Result<number, Error> = err(Error('error'))
273
+ * ```
274
+ *
275
+ * @example Success case (unchanged)
276
+ * ```typescript
277
+ * const result: Result<number, string> = ok(42);
278
+ * const mapped = mapErr(result, (e) => new Error(e));
279
+ * // mapped: Result<number, Error> = ok(42)
280
+ * ```
281
+ *
282
+ * @example Enriching errors
283
+ * ```typescript
284
+ * const result: Result<number, number> = err(404);
285
+ * const enriched = mapErr(result, (code) => ({
286
+ * status: code,
287
+ * message: 'Not Found'
288
+ * }));
289
+ * // enriched: Result<number, { status: number, message: string }>
290
+ * ```
291
+ */
292
+ export function mapErr(result, fn) {
293
+ if (result.ok) {
294
+ return ok(result.value);
295
+ }
296
+ // Type assertion: result.ok is false, so error exists
297
+ return err(fn(result.error));
298
+ }
299
+ /**
300
+ * Pattern matching for exhaustive handling of Result cases
301
+ *
302
+ * Forces explicit handling of both success and error cases. Both handlers
303
+ * must return the same type.
304
+ *
305
+ * @typeParam T - Success value type
306
+ * @typeParam E - Error type
307
+ * @typeParam U - Return type (must be same for both handlers)
308
+ * @param result - Result to match against
309
+ * @param handlers - Object with ok and err handler functions
310
+ * @returns Result of the executed handler
311
+ *
312
+ * @example Basic matching
313
+ * ```typescript
314
+ * const result: Result<number, string> = ok(42);
315
+ * const message = match(result, {
316
+ * ok: (value) => `Success: ${value}`,
317
+ * err: (error) => `Error: ${error}`
318
+ * });
319
+ * // message: 'Success: 42'
320
+ * ```
321
+ *
322
+ * @example With side effects
323
+ * ```typescript
324
+ * match(result, {
325
+ * ok: (value) => {
326
+ * console.log(`Got value: ${value}`);
327
+ * return value;
328
+ * },
329
+ * err: (error) => {
330
+ * console.error(`Got error: ${error}`);
331
+ * return 0;
332
+ * }
333
+ * });
334
+ * ```
335
+ *
336
+ * @example Complex return types
337
+ * ```typescript
338
+ * const output = match(result, {
339
+ * ok: (point) => ({ x: point.x * 2, y: point.y * 2 }),
340
+ * err: () => ({ x: 0, y: 0 })
341
+ * });
342
+ * ```
343
+ */
344
+ export function match(result, handlers) {
345
+ if (result.ok) {
346
+ return handlers.ok(result.value);
347
+ }
348
+ // Type assertion: result.ok is false, so error exists
349
+ return handlers.err(result.error);
350
+ }
351
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/types/result.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAsB9C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC5B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,GAAG,CAAI,KAAQ;IAC7B,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,IAAI,CAAO,MAAoB;IAC7C,OAAO,MAAM,CAAC,EAAE,KAAK,IAAI,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,KAAK,CAAO,MAAoB;IAC9C,OAAO,MAAM,CAAC,EAAE,KAAK,KAAK,CAAC;AAC7B,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,MAAM,CAAO,MAAoB;IAC/C,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;QACd,OA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DAAsD;IACtD,OAAO,GAAG,CAAC,EAAE,CAAE,MAAkC,CAAC,KAAK,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,UAAU,KAAK,CACnB,MAAoB,EACpB,QAGC;IAED,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;QACd,OAAO,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IACD,sDAAsD;IACtD,OAAO,QAAQ,CAAC,GAAG,CAAE,MAAkC,CAAC,KAAK,CAAC,CAAC;AACjE,CAAC"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Error Classification Utility
3
+ *
4
+ * Classifies unknown errors into appropriate CAPG error types based on
5
+ * status codes, error codes, and other error properties.
6
+ *
7
+ * @packageDocumentation
8
+ * @since v1.0.0
9
+ * @internal
10
+ */
11
+ import { CAPGError } from '../errors/index.js';
12
+ /**
13
+ * Redact credentials from a string to prevent leakage in logs/errors
14
+ *
15
+ * Removes:
16
+ * - Bearer tokens: 'Bearer abc123...' → 'Bearer ***'
17
+ * - Authorization headers: 'Authorization: Bearer xyz' → 'Authorization: ***'
18
+ * - Access tokens: 'access_token=abc123' → 'access_token=***'
19
+ *
20
+ * @param message - String that may contain credentials
21
+ * @returns Redacted string with credentials replaced by ***
22
+ *
23
+ * @internal
24
+ */
25
+ export declare function redactCredentials(message: string): string;
26
+ /**
27
+ * Classify unknown error into appropriate CAPG error type
28
+ *
29
+ * Classifies errors by type:
30
+ * - 401 → CAPGAuthError (not retryable)
31
+ * - ETIMEDOUT, ECONNREFUSED, ENOTFOUND → CAPGNetworkError (retryable)
32
+ * - 4xx/5xx → CAPGAPIError with status code
33
+ * - Unknown → CAPGError
34
+ *
35
+ * @param error - Error from external operation (query, network, etc.)
36
+ * @param context - Context information (username, operation name, etc.)
37
+ * @returns CAPGError or subclass
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * try {
42
+ * await connection.query(soql);
43
+ * } catch (error: unknown) {
44
+ * const capgError = classifyError(error, { username: 'user@example.com' });
45
+ * return err(capgError);
46
+ * }
47
+ * ```
48
+ *
49
+ * @internal
50
+ */
51
+ export declare function classifyError(error: unknown, context?: {
52
+ username?: string;
53
+ operation?: string;
54
+ }): CAPGError;
55
+ //# sourceMappingURL=error-classifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-classifier.d.ts","sourceRoot":"","sources":["../../src/utils/error-classifier.ts"],"names":[],"mappings":"AAKA;;;;;;;;;GASG;AAEH,OAAO,EAAE,SAAS,EAAiD,MAAM,oBAAoB,CAAC;AAE9F;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAKzD;AA4BD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAqD5G"}