@backendkit-labs/result 0.1.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/README.md +1121 -0
- package/dist/index.cjs +340 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +281 -0
- package/dist/index.d.ts +281 -0
- package/dist/index.js +298 -0
- package/dist/index.js.map +1 -0
- package/dist/nestjs/index.cjs +117 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +61 -0
- package/dist/nestjs/index.d.ts +61 -0
- package/dist/nestjs/index.js +114 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/types-b-fNbJBy.d.cts +33 -0
- package/dist/types-b-fNbJBy.d.ts +33 -0
- package/package.json +70 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// src/result/constructors.ts
|
|
2
|
+
var ok = (value) => ({ ok: true, value });
|
|
3
|
+
var fail = (error) => ({ ok: false, error });
|
|
4
|
+
function fromThrowable(fn, errorTransform) {
|
|
5
|
+
try {
|
|
6
|
+
return ok(fn());
|
|
7
|
+
} catch (caught) {
|
|
8
|
+
return fail(errorTransform ? errorTransform(caught) : caught);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
async function fromPromise(promise, errorTransform) {
|
|
12
|
+
try {
|
|
13
|
+
return ok(await promise);
|
|
14
|
+
} catch (caught) {
|
|
15
|
+
return fail(errorTransform ? errorTransform(caught) : caught);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function fromNullable(value, error) {
|
|
19
|
+
return value != null ? ok(value) : fail(error);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/result/guards.ts
|
|
23
|
+
function isOk(result) {
|
|
24
|
+
return result.ok === true;
|
|
25
|
+
}
|
|
26
|
+
function isFail(result) {
|
|
27
|
+
return result.ok === false;
|
|
28
|
+
}
|
|
29
|
+
function isRich(result) {
|
|
30
|
+
return "durationMs" in result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/result/operations.ts
|
|
34
|
+
function map(result, fn) {
|
|
35
|
+
return result.ok ? ok(fn(result.value)) : result;
|
|
36
|
+
}
|
|
37
|
+
function mapError(result, fn) {
|
|
38
|
+
return result.ok ? result : fail(fn(result.error));
|
|
39
|
+
}
|
|
40
|
+
function flatMap(result, fn) {
|
|
41
|
+
return result.ok ? fn(result.value) : result;
|
|
42
|
+
}
|
|
43
|
+
async function flatMapAsync(result, fn) {
|
|
44
|
+
return result.ok ? fn(result.value) : Promise.resolve(result);
|
|
45
|
+
}
|
|
46
|
+
async function mapAsync(result, fn) {
|
|
47
|
+
if (!result.ok) return Promise.resolve(result);
|
|
48
|
+
return ok(await fn(result.value));
|
|
49
|
+
}
|
|
50
|
+
function match(result, handlers) {
|
|
51
|
+
return result.ok ? handlers.ok(result.value) : handlers.fail(result.error);
|
|
52
|
+
}
|
|
53
|
+
var fold = match;
|
|
54
|
+
function tap(result, fn) {
|
|
55
|
+
if (result.ok) fn(result.value);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
function tapError(result, fn) {
|
|
59
|
+
if (!result.ok) fn(result.error);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function unwrap(result) {
|
|
63
|
+
if (result.ok) return result.value;
|
|
64
|
+
throw result.error instanceof Error ? result.error : new Error(String(result.error));
|
|
65
|
+
}
|
|
66
|
+
function unwrapError(result) {
|
|
67
|
+
if (!result.ok) return result.error;
|
|
68
|
+
throw new Error("Called unwrapError on an Ok result");
|
|
69
|
+
}
|
|
70
|
+
function unwrapOr(result, defaultValue) {
|
|
71
|
+
return result.ok ? result.value : defaultValue;
|
|
72
|
+
}
|
|
73
|
+
function unwrapOrElse(result, fn) {
|
|
74
|
+
return result.ok ? result.value : fn(result.error);
|
|
75
|
+
}
|
|
76
|
+
function expect(result, message) {
|
|
77
|
+
if (result.ok) return result.value;
|
|
78
|
+
throw new Error(message);
|
|
79
|
+
}
|
|
80
|
+
function toPromise(result) {
|
|
81
|
+
return result.ok ? Promise.resolve(result.value) : Promise.reject(result.error);
|
|
82
|
+
}
|
|
83
|
+
function toNullable(result) {
|
|
84
|
+
return result.ok ? result.value : null;
|
|
85
|
+
}
|
|
86
|
+
function toUndefined(result) {
|
|
87
|
+
return result.ok ? result.value : void 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/result/run.ts
|
|
91
|
+
async function run(fn, errorTransform) {
|
|
92
|
+
try {
|
|
93
|
+
return ok(await fn());
|
|
94
|
+
} catch (caught) {
|
|
95
|
+
return fail(errorTransform ? errorTransform(caught) : caught);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function track(fn, options) {
|
|
99
|
+
const start = performance.now();
|
|
100
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
101
|
+
const meta = {
|
|
102
|
+
durationMs: 0,
|
|
103
|
+
timestamp,
|
|
104
|
+
operation: options?.operation,
|
|
105
|
+
correlationId: options?.correlationId,
|
|
106
|
+
tags: options?.tags
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
const value = await fn();
|
|
110
|
+
return { ok: true, value, ...meta, durationMs: Math.round(performance.now() - start) };
|
|
111
|
+
} catch (caught) {
|
|
112
|
+
const error = options?.errorTransform ? options.errorTransform(caught) : caught;
|
|
113
|
+
return { ok: false, error, ...meta, durationMs: Math.round(performance.now() - start) };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function enrich(result, options) {
|
|
117
|
+
return {
|
|
118
|
+
...result,
|
|
119
|
+
durationMs: 0,
|
|
120
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
121
|
+
operation: options?.operation,
|
|
122
|
+
correlationId: options?.correlationId,
|
|
123
|
+
tags: options?.tags
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function simplify(rich) {
|
|
127
|
+
return rich.ok ? { ok: true, value: rich.value } : { ok: false, error: rich.error };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/result/resilience.ts
|
|
131
|
+
async function retry(fn, options) {
|
|
132
|
+
let last;
|
|
133
|
+
for (let attempt = 1; attempt <= options.attempts; attempt++) {
|
|
134
|
+
last = await fn();
|
|
135
|
+
if (last.ok) return last;
|
|
136
|
+
if (attempt === options.attempts) break;
|
|
137
|
+
if (options.shouldRetry && !options.shouldRetry(last.error, attempt)) break;
|
|
138
|
+
options.onRetry?.(last.error, attempt);
|
|
139
|
+
if (options.delayMs) await sleep(options.delayMs);
|
|
140
|
+
}
|
|
141
|
+
return last;
|
|
142
|
+
}
|
|
143
|
+
async function retryWithBackoff(fn, options) {
|
|
144
|
+
let last;
|
|
145
|
+
for (let attempt = 1; attempt <= options.attempts; attempt++) {
|
|
146
|
+
last = await fn();
|
|
147
|
+
if (last.ok) return last;
|
|
148
|
+
if (attempt === options.attempts) break;
|
|
149
|
+
if (options.shouldRetry && !options.shouldRetry(last.error, attempt)) break;
|
|
150
|
+
options.onRetry?.(last.error, attempt);
|
|
151
|
+
const delay = Math.min(
|
|
152
|
+
(options.delayMs ?? 100) * Math.pow(2, attempt - 1),
|
|
153
|
+
options.maxDelayMs ?? 3e4
|
|
154
|
+
);
|
|
155
|
+
await sleep(delay);
|
|
156
|
+
}
|
|
157
|
+
return last;
|
|
158
|
+
}
|
|
159
|
+
async function withTimeout(fn, ms, timeoutError) {
|
|
160
|
+
const timer = new Promise(
|
|
161
|
+
(resolve) => setTimeout(() => resolve(fail(timeoutError)), ms)
|
|
162
|
+
);
|
|
163
|
+
return Promise.race([fn(), timer]);
|
|
164
|
+
}
|
|
165
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
166
|
+
|
|
167
|
+
// src/result/combinators.ts
|
|
168
|
+
function all(results) {
|
|
169
|
+
const values = [];
|
|
170
|
+
for (const r of results) {
|
|
171
|
+
if (!r.ok) return r;
|
|
172
|
+
values.push(r.value);
|
|
173
|
+
}
|
|
174
|
+
return ok(values);
|
|
175
|
+
}
|
|
176
|
+
async function any(operations) {
|
|
177
|
+
let last;
|
|
178
|
+
for (const op of operations) {
|
|
179
|
+
last = await op();
|
|
180
|
+
if (last.ok) return last;
|
|
181
|
+
}
|
|
182
|
+
return last;
|
|
183
|
+
}
|
|
184
|
+
async function parallel(operations, options) {
|
|
185
|
+
const concurrency = options?.concurrency ?? operations.length;
|
|
186
|
+
const results = [];
|
|
187
|
+
for (let i = 0; i < operations.length; i += concurrency) {
|
|
188
|
+
const batch = await Promise.all(operations.slice(i, i + concurrency).map((op) => op()));
|
|
189
|
+
results.push(...batch);
|
|
190
|
+
}
|
|
191
|
+
return all(results);
|
|
192
|
+
}
|
|
193
|
+
function partition(results) {
|
|
194
|
+
const values = [];
|
|
195
|
+
const errors = [];
|
|
196
|
+
for (const r of results) {
|
|
197
|
+
if (r.ok) values.push(r.value);
|
|
198
|
+
else errors.push(r.error);
|
|
199
|
+
}
|
|
200
|
+
return [values, errors];
|
|
201
|
+
}
|
|
202
|
+
function collect(results) {
|
|
203
|
+
return results.filter((r) => r.ok).map((r) => r.value);
|
|
204
|
+
}
|
|
205
|
+
function traverse(items, fn) {
|
|
206
|
+
return all(items.map(fn));
|
|
207
|
+
}
|
|
208
|
+
function combine2(r1, r2) {
|
|
209
|
+
if (!r1.ok) return r1;
|
|
210
|
+
if (!r2.ok) return r2;
|
|
211
|
+
return ok([r1.value, r2.value]);
|
|
212
|
+
}
|
|
213
|
+
function combine3(r1, r2, r3) {
|
|
214
|
+
if (!r1.ok) return r1;
|
|
215
|
+
if (!r2.ok) return r2;
|
|
216
|
+
if (!r3.ok) return r3;
|
|
217
|
+
return ok([r1.value, r2.value, r3.value]);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/result/flow.ts
|
|
221
|
+
var Flow = class _Flow {
|
|
222
|
+
constructor(_result) {
|
|
223
|
+
this._result = _result;
|
|
224
|
+
}
|
|
225
|
+
_result;
|
|
226
|
+
/** Start a pipeline from an existing Result. */
|
|
227
|
+
static from(result) {
|
|
228
|
+
return new _Flow(result);
|
|
229
|
+
}
|
|
230
|
+
/** Start an empty pipeline (value is `undefined`). */
|
|
231
|
+
static start() {
|
|
232
|
+
return new _Flow(ok(void 0));
|
|
233
|
+
}
|
|
234
|
+
/** Maps the success value. Skipped on failure. */
|
|
235
|
+
map(fn) {
|
|
236
|
+
if (!this._result.ok) return new _Flow(this._result);
|
|
237
|
+
return new _Flow(ok(fn(this._result.value)));
|
|
238
|
+
}
|
|
239
|
+
/** Maps the error value. Skipped on success. */
|
|
240
|
+
mapError(fn) {
|
|
241
|
+
if (this._result.ok) return new _Flow(this._result);
|
|
242
|
+
return new _Flow(fail(fn(this._result.error)));
|
|
243
|
+
}
|
|
244
|
+
/** Chains a Result-returning function. Skipped on failure. */
|
|
245
|
+
flatMap(fn) {
|
|
246
|
+
if (!this._result.ok) return new _Flow(this._result);
|
|
247
|
+
return new _Flow(fn(this._result.value));
|
|
248
|
+
}
|
|
249
|
+
/** Filters the success value. Becomes `fail(error)` if predicate is false. */
|
|
250
|
+
filter(pred, error) {
|
|
251
|
+
if (!this._result.ok) return this;
|
|
252
|
+
return pred(this._result.value) ? this : new _Flow(fail(error));
|
|
253
|
+
}
|
|
254
|
+
/** Runs a side effect on success; returns `this` unchanged. */
|
|
255
|
+
tap(fn) {
|
|
256
|
+
if (this._result.ok) fn(this._result.value);
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
/** Runs a side effect on failure; returns `this` unchanged. */
|
|
260
|
+
tapError(fn) {
|
|
261
|
+
if (!this._result.ok) fn(this._result.error);
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Recovers from a failure by computing a new success value.
|
|
266
|
+
* Skipped if already Ok.
|
|
267
|
+
*/
|
|
268
|
+
recover(fn) {
|
|
269
|
+
if (this._result.ok) return new _Flow(this._result);
|
|
270
|
+
return new _Flow(ok(fn(this._result.error)));
|
|
271
|
+
}
|
|
272
|
+
/** Exhaustive pattern match — always produces a value. */
|
|
273
|
+
match(handlers) {
|
|
274
|
+
return this._result.ok ? handlers.ok(this._result.value) : handlers.fail(this._result.error);
|
|
275
|
+
}
|
|
276
|
+
/** Returns the underlying `Result<T, E>`. */
|
|
277
|
+
getResult() {
|
|
278
|
+
return this._result;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns the underlying result as `RichResult<T, E>` if it was created by `track()`.
|
|
282
|
+
* Throws if the result has no observability metadata.
|
|
283
|
+
*/
|
|
284
|
+
getRichResult() {
|
|
285
|
+
if (!isRich(this._result)) throw new Error("Result is not a RichResult");
|
|
286
|
+
return this._result;
|
|
287
|
+
}
|
|
288
|
+
isOk() {
|
|
289
|
+
return this._result.ok;
|
|
290
|
+
}
|
|
291
|
+
isFail() {
|
|
292
|
+
return !this._result.ok;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
export { Flow, all, any, collect, combine2, combine3, enrich, expect, fail, flatMap, flatMapAsync, fold, fromNullable, fromPromise, fromThrowable, isFail, isOk, isRich, map, mapAsync, mapError, match, ok, parallel, partition, retry, retryWithBackoff, run, simplify, tap, tapError, toNullable, toPromise, toUndefined, track, traverse, unwrap, unwrapError, unwrapOr, unwrapOrElse, withTimeout };
|
|
297
|
+
//# sourceMappingURL=index.js.map
|
|
298
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/result/constructors.ts","../src/result/guards.ts","../src/result/operations.ts","../src/result/run.ts","../src/result/resilience.ts","../src/result/combinators.ts","../src/result/flow.ts"],"names":[],"mappings":";AAGO,IAAM,KAAK,CAAe,KAAA,MAC9B,EAAE,EAAA,EAAI,MAAM,KAAA,EAAM;AAGd,IAAM,OAAO,CAAuB,KAAA,MACxC,EAAE,EAAA,EAAI,OAAO,KAAA,EAAM;AASf,SAAS,aAAA,CACd,IACA,cAAA,EACc;AACd,EAAA,IAAI;AACF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AQ,KAAK,CAAA,GAC9B,QAAA,CAAS,IAAA,CAAK,IAAA,CAAK,QAAQ,KAAK,CAAA;AAAA,EACtC;AAAA;AAAA,EAGA,SAAA,GAA0B;AACxB,IAAA,OAAO,IAAA,CAAK,OAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAA,GAAkC;AAChC,IAAA,IAAI,CAAC,OAAO,IAAA,CAAK,OAAO,GAAG,MAAM,IAAI,MAAM,4BAA4B,CAAA;AACvE,IAAA,OAAO,IAAA,CAAK,OAAA;AAAA,EACd;AAAA,EAEA,IAAA,GAAkB;AAAE,IAAA,OAAO,KAAK,OAAA,CAAQ,EAAA;AAAA,EAAI;AAAA,EAC5C,MAAA,GAAkB;AAAE,IAAA,OAAO,CAAC,KAAK,OAAA,CAAQ,EAAA;AAAA,EAAI;AAC/C","file":"index.js","sourcesContent":["import type { Result } from './types.js';\n\n/** Creates a successful Result. */\nexport const ok = <T, E = never>(value: T): Result<T, E> =>\n ({ ok: true, value });\n\n/** Creates a failed Result. */\nexport const fail = <T = never, E = Error>(error: E): Result<T, E> =>\n ({ ok: false, error });\n\n/**\n * Wraps a synchronous throwable function. Catches any thrown value and\n * passes it through `errorTransform` (defaults to identity cast).\n *\n * @example\n * const result = fromThrowable(() => JSON.parse(raw), (e) => new ParseError(e));\n */\nexport function fromThrowable<T, E = Error>(\n fn: () => T,\n errorTransform?: (caught: unknown) => E,\n): Result<T, E> {\n try {\n return ok(fn());\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Converts a Promise to a `Promise<Result<T, E>>`, catching rejections.\n *\n * @example\n * const result = await fromPromise(fetch(url), (e) => new NetworkError(e));\n */\nexport async function fromPromise<T, E = Error>(\n promise: Promise<T>,\n errorTransform?: (caught: unknown) => E,\n): Promise<Result<T, E>> {\n try {\n return ok(await promise);\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Converts a nullable value to a Result.\n * Returns `ok(value)` if non-null/undefined, `fail(error)` otherwise.\n *\n * @example\n * const result = fromNullable(user, new NotFoundError('user'));\n */\nexport function fromNullable<T, E>(\n value: T | null | undefined,\n error: E,\n): Result<T, E> {\n return value != null ? ok(value) : fail(error);\n}\n","import type { Result, RichResult } from './types.js';\n\ntype OkResult<T, E> = Extract<Result<T, E>, { ok: true }>;\ntype FailResult<T, E> = Extract<Result<T, E>, { ok: false }>;\n\n/** Narrows a `Result<T, E>` to its success branch. */\nexport function isOk<T, E>(result: Result<T, E>): result is OkResult<T, E> {\n return result.ok === true;\n}\n\n/** Narrows a `Result<T, E>` to its failure branch. */\nexport function isFail<T, E>(result: Result<T, E>): result is FailResult<T, E> {\n return result.ok === false;\n}\n\n/** Returns `true` if the result carries observability metadata (`track()` output). */\nexport function isRich<T, E>(result: Result<T, E>): result is RichResult<T, E> {\n return 'durationMs' in result;\n}\n","import type { Result } from './types.js';\nimport { ok, fail } from './constructors.js';\n\n// ── Transformations ────────────────────────────────────────────────────────\n\n/** Maps the success value. Passes failures through unchanged. */\nexport function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {\n return result.ok ? ok(fn(result.value)) : result;\n}\n\n/** Maps the error value. Passes successes through unchanged. */\nexport function mapError<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {\n return result.ok ? result : fail(fn(result.error));\n}\n\n/** Monadic bind — chains a Result-returning function on success. */\nexport function flatMap<T, U, E>(\n result: Result<T, E>,\n fn: (value: T) => Result<U, E>,\n): Result<U, E> {\n return result.ok ? fn(result.value) : result;\n}\n\n/** Async variant of `flatMap`. */\nexport async function flatMapAsync<T, U, E>(\n result: Result<T, E>,\n fn: (value: T) => Promise<Result<U, E>>,\n): Promise<Result<U, E>> {\n return result.ok ? fn(result.value) : Promise.resolve(result);\n}\n\n/** Async variant of `map`. */\nexport async function mapAsync<T, U, E>(\n result: Result<T, E>,\n fn: (value: T) => Promise<U>,\n): Promise<Result<U, E>> {\n if (!result.ok) return Promise.resolve(result);\n return ok(await fn(result.value));\n}\n\n// ── Pattern matching ───────────────────────────────────────────────────────\n\n/**\n * Exhaustive pattern match over both branches.\n *\n * @example\n * const msg = match(result, {\n * ok: (user) => `Welcome, ${user.name}`,\n * fail: (error) => `Error: ${error.message}`,\n * });\n */\nexport function match<T, E, R>(\n result: Result<T, E>,\n handlers: { ok: (value: T) => R; fail: (error: E) => R },\n): R {\n return result.ok ? handlers.ok(result.value) : handlers.fail(result.error);\n}\n\n/** Alias for `match` — familiar to fp-ts / Scala users. */\nexport const fold = match;\n\n// ── Side effects ───────────────────────────────────────────────────────────\n\n/** Runs a side effect on the success value; returns the result unchanged. */\nexport function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E> {\n if (result.ok) fn(result.value);\n return result;\n}\n\n/** Runs a side effect on the error value; returns the result unchanged. */\nexport function tapError<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E> {\n if (!result.ok) fn(result.error);\n return result;\n}\n\n// ── Unwrapping ─────────────────────────────────────────────────────────────\n\n/** Extracts the value or throws the error (re-thrown as-is if it's an Error, wrapped otherwise). */\nexport function unwrap<T, E>(result: Result<T, E>): T {\n if (result.ok) return result.value;\n throw result.error instanceof Error ? result.error : new Error(String(result.error));\n}\n\n/** Extracts the error or throws if the result is Ok. */\nexport function unwrapError<T, E>(result: Result<T, E>): E {\n if (!result.ok) return result.error;\n throw new Error('Called unwrapError on an Ok result');\n}\n\n/** Extracts the value or returns `defaultValue` on failure. */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n return result.ok ? result.value : defaultValue;\n}\n\n/** Extracts the value or computes a fallback from the error. */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T {\n return result.ok ? result.value : fn(result.error);\n}\n\n/** Extracts the value or throws with a custom `message`. */\nexport function expect<T, E>(result: Result<T, E>, message: string): T {\n if (result.ok) return result.value;\n throw new Error(message);\n}\n\n// ── Conversion ─────────────────────────────────────────────────────────────\n\n/** Converts to a Promise — resolves on Ok, rejects on Fail. */\nexport function toPromise<T, E>(result: Result<T, E>): Promise<T> {\n return result.ok ? Promise.resolve(result.value) : Promise.reject(result.error);\n}\n\n/** Returns the value or `null` on failure. */\nexport function toNullable<T, E>(result: Result<T, E>): T | null {\n return result.ok ? result.value : null;\n}\n\n/** Returns the value or `undefined` on failure. */\nexport function toUndefined<T, E>(result: Result<T, E>): T | undefined {\n return result.ok ? result.value : undefined;\n}\n","import type { Result, RichResult, TrackOptions } from './types.js';\nimport { ok, fail } from './constructors.js';\n\n/**\n * Executes an async (or sync) function and captures any thrown exception,\n * returning a `Result<T, E>` instead of propagating the error.\n *\n * @example\n * const result = await run(() => fetchUser(id));\n * const result = await run(() => fetchUser(id), (e) => new UserError(e));\n */\nexport async function run<T, E = Error>(\n fn: () => T | Promise<T>,\n errorTransform?: (caught: unknown) => E,\n): Promise<Result<T, E>> {\n try {\n return ok(await fn());\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Like `run()` but also captures timing and metadata, returning a `RichResult<T, E>`.\n *\n * @example\n * const result = await track(() => fetchUser(id), { operation: 'user.fetch', tags: ['db'] });\n */\nexport async function track<T, E = Error>(\n fn: () => T | Promise<T>,\n options?: TrackOptions & { errorTransform?: (caught: unknown) => E },\n): Promise<RichResult<T, E>> {\n const start = performance.now();\n const timestamp = new Date().toISOString();\n const meta = {\n durationMs: 0,\n timestamp,\n operation: options?.operation,\n correlationId: options?.correlationId,\n tags: options?.tags,\n };\n\n try {\n const value = await fn();\n return { ok: true, value, ...meta, durationMs: Math.round(performance.now() - start) };\n } catch (caught) {\n const error = options?.errorTransform ? options.errorTransform(caught) : (caught as E);\n return { ok: false, error, ...meta, durationMs: Math.round(performance.now() - start) };\n }\n}\n\n/**\n * Promotes a plain `Result<T, E>` to a `RichResult<T, E>` with a zero-duration snapshot.\n * Useful when you already have a Result and want to attach metadata.\n */\nexport function enrich<T, E>(result: Result<T, E>, options?: TrackOptions): RichResult<T, E> {\n return {\n ...result,\n durationMs: 0,\n timestamp: new Date().toISOString(),\n operation: options?.operation,\n correlationId: options?.correlationId,\n tags: options?.tags,\n };\n}\n\n/**\n * Strips observability metadata from a `RichResult`, returning a plain `Result<T, E>`.\n */\nexport function simplify<T, E>(rich: RichResult<T, E>): Result<T, E> {\n return rich.ok\n ? { ok: true, value: rich.value }\n : { ok: false, error: rich.error };\n}\n","import type { Result } from './types.js';\nimport { fail } from './constructors.js';\n\nexport interface RetryOptions<E> {\n /** Maximum number of attempts (including the first). */\n attempts: number;\n /** Fixed delay in ms between attempts. Default: 0 */\n delayMs?: number;\n /** Return false to stop retrying early based on the error. */\n shouldRetry?: (error: E, attempt: number) => boolean;\n /** Called before each retry with the previous error and attempt number. */\n onRetry?: (error: E, attempt: number) => void;\n}\n\nexport interface BackoffOptions<E> extends RetryOptions<E> {\n /** Cap for the computed backoff delay. Default: 30_000 */\n maxDelayMs?: number;\n}\n\n/**\n * Retries a Result-returning function up to `options.attempts` times with\n * an optional fixed delay between attempts.\n *\n * @example\n * const result = await retry(() => run(() => callApi()), {\n * attempts: 3,\n * delayMs: 500,\n * shouldRetry: (err) => err.code === 'ECONNRESET',\n * });\n */\nexport async function retry<T, E = Error>(\n fn: () => Promise<Result<T, E>>,\n options: RetryOptions<E>,\n): Promise<Result<T, E>> {\n let last!: Result<T, E>;\n\n for (let attempt = 1; attempt <= options.attempts; attempt++) {\n last = await fn();\n if (last.ok) return last;\n if (attempt === options.attempts) break;\n if (options.shouldRetry && !options.shouldRetry(last.error, attempt)) break;\n options.onRetry?.(last.error, attempt);\n if (options.delayMs) await sleep(options.delayMs);\n }\n\n return last;\n}\n\n/**\n * Like `retry()` but uses exponential backoff: delay doubles on each attempt,\n * capped at `maxDelayMs`.\n *\n * @example\n * const result = await retryWithBackoff(() => run(() => callApi()), {\n * attempts: 4,\n * delayMs: 100, // 100ms → 200ms → 400ms\n * maxDelayMs: 1_000,\n * });\n */\nexport async function retryWithBackoff<T, E = Error>(\n fn: () => Promise<Result<T, E>>,\n options: BackoffOptions<E>,\n): Promise<Result<T, E>> {\n let last!: Result<T, E>;\n\n for (let attempt = 1; attempt <= options.attempts; attempt++) {\n last = await fn();\n if (last.ok) return last;\n if (attempt === options.attempts) break;\n if (options.shouldRetry && !options.shouldRetry(last.error, attempt)) break;\n options.onRetry?.(last.error, attempt);\n const delay = Math.min(\n (options.delayMs ?? 100) * Math.pow(2, attempt - 1),\n options.maxDelayMs ?? 30_000,\n );\n await sleep(delay);\n }\n\n return last;\n}\n\n/**\n * Races a Result-returning function against a timeout.\n * Returns `fail(timeoutError)` if `ms` elapses before the function resolves.\n *\n * @example\n * const result = await withTimeout(\n * () => run(() => fetchData()),\n * 5_000,\n * new TimeoutError('fetchData timed out'),\n * );\n */\nexport async function withTimeout<T, E>(\n fn: () => Promise<Result<T, E>>,\n ms: number,\n timeoutError: E,\n): Promise<Result<T, E>> {\n const timer = new Promise<Result<T, E>>(resolve =>\n setTimeout(() => resolve(fail<T, E>(timeoutError)), ms),\n );\n return Promise.race([fn(), timer]);\n}\n\nconst sleep = (ms: number): Promise<void> => new Promise(r => setTimeout(r, ms));\n","import type { Result } from './types.js';\nimport { ok } from './constructors.js';\n\n/**\n * Returns `ok([...values])` if every result succeeds, or the first `fail` encountered.\n *\n * @example\n * const result = all([findUser(id), findAccount(id)]);\n * // Result<[User, Account], Error>\n */\nexport function all<T, E>(results: Result<T, E>[]): Result<T[], E> {\n const values: T[] = [];\n for (const r of results) {\n if (!r.ok) return r;\n values.push(r.value);\n }\n return ok(values);\n}\n\n/**\n * Returns the first successful result from a list of async operations,\n * tried sequentially. Returns the last failure if all fail.\n *\n * @example\n * const result = await any([\n * () => run(() => primaryCache.get(key)),\n * () => run(() => database.find(key)),\n * ]);\n */\nexport async function any<T, E>(\n operations: (() => Promise<Result<T, E>>)[],\n): Promise<Result<T, E>> {\n let last!: Result<T, E>;\n for (const op of operations) {\n last = await op();\n if (last.ok) return last;\n }\n return last;\n}\n\nexport interface ParallelOptions {\n /** Max number of operations running concurrently. Default: all at once. */\n concurrency?: number;\n}\n\n/**\n * Runs operations concurrently (optionally throttled by `concurrency`).\n * Returns `ok([...values])` if all succeed, or the first failure.\n *\n * @example\n * const result = await parallel(\n * userIds.map(id => () => run(() => fetchUser(id))),\n * { concurrency: 5 },\n * );\n */\nexport async function parallel<T, E>(\n operations: (() => Promise<Result<T, E>>)[],\n options?: ParallelOptions,\n): Promise<Result<T[], E>> {\n const concurrency = options?.concurrency ?? operations.length;\n const results: Result<T, E>[] = [];\n\n for (let i = 0; i < operations.length; i += concurrency) {\n const batch = await Promise.all(operations.slice(i, i + concurrency).map(op => op()));\n results.push(...batch);\n }\n\n return all(results);\n}\n\n/**\n * Splits an array of Results into `[successValues, errorValues]`.\n *\n * @example\n * const [users, errors] = partition(results);\n */\nexport function partition<T, E>(results: Result<T, E>[]): [T[], E[]] {\n const values: T[] = [];\n const errors: E[] = [];\n for (const r of results) {\n if (r.ok) values.push(r.value);\n else errors.push(r.error);\n }\n return [values, errors];\n}\n\n/**\n * Extracts only the success values, silently discarding failures.\n *\n * @example\n * const users = collect(results); // User[]\n */\nexport function collect<T, E>(results: Result<T, E>[]): T[] {\n return results\n .filter((r): r is Extract<Result<T, E>, { ok: true }> => r.ok)\n .map(r => r.value);\n}\n\n/**\n * Maps each item through a Result-returning function, collecting all successes\n * into an array or short-circuiting on the first failure.\n *\n * @example\n * const result = traverse(ids, id => fromNullable(cache.get(id), 'not-found'));\n */\nexport function traverse<T, U, E>(\n items: T[],\n fn: (item: T) => Result<U, E>,\n): Result<U[], E> {\n return all(items.map(fn));\n}\n\n// ── Typed tuple combinators ────────────────────────────────────────────────\n\n/** Combines two Results into a tuple. Short-circuits on first failure. */\nexport function combine2<A, B, E>(\n r1: Result<A, E>,\n r2: Result<B, E>,\n): Result<[A, B], E> {\n if (!r1.ok) return r1;\n if (!r2.ok) return r2;\n return ok([r1.value, r2.value]);\n}\n\n/** Combines three Results into a typed tuple. Short-circuits on first failure. */\nexport function combine3<A, B, C, E>(\n r1: Result<A, E>,\n r2: Result<B, E>,\n r3: Result<C, E>,\n): Result<[A, B, C], E> {\n if (!r1.ok) return r1;\n if (!r2.ok) return r2;\n if (!r3.ok) return r3;\n return ok([r1.value, r2.value, r3.value]);\n}\n","import type { Result, RichResult } from './types.js';\nimport { ok, fail } from './constructors.js';\nimport { isRich } from './guards.js';\n\n/**\n * Fluent pipeline wrapper around `Result<T, E>`.\n * Operations short-circuit on failure — subsequent transforms are skipped.\n *\n * @example\n * const price = Flow.from(findProduct(id))\n * .map(p => p.price)\n * .filter(price => price > 0, new Error('Price must be positive'))\n * .map(price => price * taxRate)\n * .getResult();\n */\nexport class Flow<T, E = Error> {\n private constructor(private readonly _result: Result<T, E>) {}\n\n /** Start a pipeline from an existing Result. */\n static from<T, E>(result: Result<T, E>): Flow<T, E> {\n return new Flow(result);\n }\n\n /** Start an empty pipeline (value is `undefined`). */\n static start(): Flow<void, never> {\n return new Flow(ok(undefined));\n }\n\n /** Maps the success value. Skipped on failure. */\n map<U>(fn: (value: T) => U): Flow<U, E> {\n if (!this._result.ok) return new Flow<U, E>(this._result as unknown as Result<U, E>);\n return new Flow(ok(fn(this._result.value)));\n }\n\n /** Maps the error value. Skipped on success. */\n mapError<F>(fn: (error: E) => F): Flow<T, F> {\n if (this._result.ok) return new Flow<T, F>(this._result as unknown as Result<T, F>);\n return new Flow(fail(fn(this._result.error)));\n }\n\n /** Chains a Result-returning function. Skipped on failure. */\n flatMap<U>(fn: (value: T) => Result<U, E>): Flow<U, E> {\n if (!this._result.ok) return new Flow<U, E>(this._result as unknown as Result<U, E>);\n return new Flow(fn(this._result.value));\n }\n\n /** Filters the success value. Becomes `fail(error)` if predicate is false. */\n filter(pred: (value: T) => boolean, error: E): Flow<T, E> {\n if (!this._result.ok) return this;\n return pred(this._result.value) ? this : new Flow(fail(error));\n }\n\n /** Runs a side effect on success; returns `this` unchanged. */\n tap(fn: (value: T) => void): this {\n if (this._result.ok) fn(this._result.value);\n return this;\n }\n\n /** Runs a side effect on failure; returns `this` unchanged. */\n tapError(fn: (error: E) => void): this {\n if (!this._result.ok) fn(this._result.error);\n return this;\n }\n\n /**\n * Recovers from a failure by computing a new success value.\n * Skipped if already Ok.\n */\n recover(fn: (error: E) => T): Flow<T, never> {\n if (this._result.ok) return new Flow<T, never>(this._result as unknown as Result<T, never>);\n return new Flow(ok(fn(this._result.error)));\n }\n\n /** Exhaustive pattern match — always produces a value. */\n match<R>(handlers: { ok: (value: T) => R; fail: (error: E) => R }): R {\n return this._result.ok\n ? handlers.ok(this._result.value)\n : handlers.fail(this._result.error);\n }\n\n /** Returns the underlying `Result<T, E>`. */\n getResult(): Result<T, E> {\n return this._result;\n }\n\n /**\n * Returns the underlying result as `RichResult<T, E>` if it was created by `track()`.\n * Throws if the result has no observability metadata.\n */\n getRichResult(): RichResult<T, E> {\n if (!isRich(this._result)) throw new Error('Result is not a RichResult');\n return this._result;\n }\n\n isOk(): boolean { return this._result.ok; }\n isFail(): boolean { return !this._result.ok; }\n}\n"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var common = require('@nestjs/common');
|
|
4
|
+
var operators = require('rxjs/operators');
|
|
5
|
+
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
8
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
9
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
10
|
+
if (decorator = decorators[i])
|
|
11
|
+
result = (decorator(result)) || result;
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/result/constructors.ts
|
|
16
|
+
var ok = (value) => ({ ok: true, value });
|
|
17
|
+
var fail = (error) => ({ ok: false, error });
|
|
18
|
+
|
|
19
|
+
// src/result/run.ts
|
|
20
|
+
async function run(fn, errorTransform) {
|
|
21
|
+
try {
|
|
22
|
+
return ok(await fn());
|
|
23
|
+
} catch (caught) {
|
|
24
|
+
return fail(caught);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function track(fn, options) {
|
|
28
|
+
const start = performance.now();
|
|
29
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
30
|
+
const meta = {
|
|
31
|
+
durationMs: 0,
|
|
32
|
+
timestamp,
|
|
33
|
+
operation: options?.operation,
|
|
34
|
+
correlationId: options?.correlationId,
|
|
35
|
+
tags: options?.tags
|
|
36
|
+
};
|
|
37
|
+
try {
|
|
38
|
+
const value = await fn();
|
|
39
|
+
return { ok: true, value, ...meta, durationMs: Math.round(performance.now() - start) };
|
|
40
|
+
} catch (caught) {
|
|
41
|
+
const error = options?.errorTransform ? options.errorTransform(caught) : caught;
|
|
42
|
+
return { ok: false, error, ...meta, durationMs: Math.round(performance.now() - start) };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/nestjs/decorators.ts
|
|
47
|
+
function AsResult(operation) {
|
|
48
|
+
return function(_target, _key, descriptor) {
|
|
49
|
+
const original = descriptor.value;
|
|
50
|
+
descriptor.value = async function(...args) {
|
|
51
|
+
return run(
|
|
52
|
+
() => original.apply(this, args));
|
|
53
|
+
};
|
|
54
|
+
if (operation) {
|
|
55
|
+
descriptor.value.operationName = operation;
|
|
56
|
+
}
|
|
57
|
+
return descriptor;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function WithMetrics(options) {
|
|
61
|
+
const opts = typeof options === "string" ? { operation: options } : options ?? {};
|
|
62
|
+
return function(_target, _key, descriptor) {
|
|
63
|
+
const original = descriptor.value;
|
|
64
|
+
descriptor.value = async function(...args) {
|
|
65
|
+
return track(() => original.apply(this, args), opts);
|
|
66
|
+
};
|
|
67
|
+
return descriptor;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/result/guards.ts
|
|
72
|
+
function isRich(result) {
|
|
73
|
+
return "durationMs" in result;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/nestjs/interceptor.ts
|
|
77
|
+
function isResult(value) {
|
|
78
|
+
return typeof value === "object" && value !== null && "ok" in value && typeof value["ok"] === "boolean";
|
|
79
|
+
}
|
|
80
|
+
exports.ResultInterceptor = class ResultInterceptor {
|
|
81
|
+
intercept(_ctx, next) {
|
|
82
|
+
return next.handle().pipe(
|
|
83
|
+
operators.map((value) => {
|
|
84
|
+
if (!isResult(value)) return value;
|
|
85
|
+
const base = value.ok ? { ok: true, data: value.value } : { ok: false, error: String(value.error) };
|
|
86
|
+
if (!isRich(value)) return base;
|
|
87
|
+
const rich = value;
|
|
88
|
+
return {
|
|
89
|
+
...base,
|
|
90
|
+
meta: {
|
|
91
|
+
...rich.operation ? { operation: rich.operation } : {},
|
|
92
|
+
...rich.correlationId ? { correlationId: rich.correlationId } : {},
|
|
93
|
+
...rich.tags?.length ? { tags: rich.tags } : {},
|
|
94
|
+
durationMs: rich.durationMs,
|
|
95
|
+
timestamp: rich.timestamp
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
exports.ResultInterceptor = __decorateClass([
|
|
103
|
+
common.Injectable()
|
|
104
|
+
], exports.ResultInterceptor);
|
|
105
|
+
exports.ResultModule = class ResultModule {
|
|
106
|
+
};
|
|
107
|
+
exports.ResultModule = __decorateClass([
|
|
108
|
+
common.Module({
|
|
109
|
+
providers: [exports.ResultInterceptor],
|
|
110
|
+
exports: [exports.ResultInterceptor]
|
|
111
|
+
})
|
|
112
|
+
], exports.ResultModule);
|
|
113
|
+
|
|
114
|
+
exports.AsResult = AsResult;
|
|
115
|
+
exports.WithMetrics = WithMetrics;
|
|
116
|
+
//# sourceMappingURL=index.cjs.map
|
|
117
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/result/constructors.ts","../../src/result/run.ts","../../src/nestjs/decorators.ts","../../src/result/guards.ts","../../src/nestjs/interceptor.ts","../../src/nestjs/module.ts"],"names":["ResultInterceptor","map","Injectable","ResultModule","Module"],"mappings":";;;;;;;;;;;;;;;AAGO,IAAM,KAAK,CAAe,KAAA,MAC9B,EAAE,EAAA,EAAI,MAAM,KAAA,EAAM,CAAA;AAGd,IAAM,OAAO,CAAuB,KAAA,MACxC,EAAE,EAAA,EAAI,OAAO,KAAA,EAAM,CAAA;;;ACGtB,eAAsB,GAAA,CACpB,IACA,cAAA,EACuB;AACvB,EAAA,IAAI;AACF,IAAA,OAAO,EAAA,CAAG,MAAM,EAAA,EAAI,CAAA;AAAA,EACtB,SAAS,MAAA,EAAQ;AACf,IAAA,OAAO,IAAA,CAAgD,MAAY,CAAA;AAAA,EACrE;AACF;AAQA,eAAsB,KAAA,CACpB,IACA,OAAA,EAC2B;AAC3B,EAAA,MAAM,KAAA,GAAY,YAAY,GAAA,EAAI;AAClC,EAAA,MAAM,SAAA,GAAA,iBAAY,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AACzC,EAAA,MAAM,IAAA,GAAO;AAAA,IACX,UAAA,EAAe,CAAA;AAAA,IACf,SAAA;AAAA,IACA,WAAe,OAAA,EAAS,SAAA;AAAA,IACxB,eAAe,OAAA,EAAS,aAAA;AAAA,IACxB,MAAe,OAAA,EAAS;AAAA,GAC1B;AAEA,EAAA,IAAI;AACF,IAAA,MAAM,KAAA,GAAQ,MAAM,EAAA,EAAG;AACvB,IAAA,OAAO,EAAE,EAAA,EAAI,IAAA,EAAM,KAAA,EAAO,GAAG,IAAA,EAAM,UAAA,EAAY,IAAA,CAAK,KAAA,CAAM,WAAA,CAAY,GAAA,EAAI,GAAI,KAAK,CAAA,EAAE;AAAA,EACvF,SAAS,MAAA,EAAQ;AACf,IAAA,MAAM,QAAQ,OA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A;ACxCAG,uBAAN,kBAAA,CAAmB;AAAC;AAAdA,oBAAA,GAAN,eAAA,CAAA;AAAA,EAJNC,aAAA,CAAO;AAAA,IACN,SAAA,EAAW,CAACJ,yBAAiB,CAAA;AAAA,IAC7B,OAAA,EAAW,CAACA,yBAAiB;AAAA,GAC9B;AAAA,CAAA,EACYG,oBAAA,CAAA","file":"index.cjs","sourcesContent":["import type { Result } from './types.js';\n\n/** Creates a successful Result. */\nexport const ok = <T, E = never>(value: T): Result<T, E> =>\n ({ ok: true, value });\n\n/** Creates a failed Result. */\nexport const fail = <T = never, E = Error>(error: E): Result<T, E> =>\n ({ ok: false, error });\n\n/**\n * Wraps a synchronous throwable function. Catches any thrown value and\n * passes it through `errorTransform` (defaults to identity cast).\n *\n * @example\n * const result = fromThrowable(() => JSON.parse(raw), (e) => new ParseError(e));\n */\nexport function fromThrowable<T, E = Error>(\n fn: () => T,\n errorTransform?: (caught: unknown) => E,\n): Result<T, E> {\n try {\n return ok(fn());\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Converts a Promise to a `Promise<Result<T, E>>`, catching rejections.\n *\n * @example\n * const result = await fromPromise(fetch(url), (e) => new NetworkError(e));\n */\nexport async function fromPromise<T, E = Error>(\n promise: Promise<T>,\n errorTransform?: (caught: unknown) => E,\n): Promise<Result<T, E>> {\n try {\n return ok(await promise);\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Converts a nullable value to a Result.\n * Returns `ok(value)` if non-null/undefined, `fail(error)` otherwise.\n *\n * @example\n * const result = fromNullable(user, new NotFoundError('user'));\n */\nexport function fromNullable<T, E>(\n value: T | null | undefined,\n error: E,\n): Result<T, E> {\n return value != null ? ok(value) : fail(error);\n}\n","import type { Result, RichResult, TrackOptions } from './types.js';\nimport { ok, fail } from './constructors.js';\n\n/**\n * Executes an async (or sync) function and captures any thrown exception,\n * returning a `Result<T, E>` instead of propagating the error.\n *\n * @example\n * const result = await run(() => fetchUser(id));\n * const result = await run(() => fetchUser(id), (e) => new UserError(e));\n */\nexport async function run<T, E = Error>(\n fn: () => T | Promise<T>,\n errorTransform?: (caught: unknown) => E,\n): Promise<Result<T, E>> {\n try {\n return ok(await fn());\n } catch (caught) {\n return fail(errorTransform ? errorTransform(caught) : (caught as E));\n }\n}\n\n/**\n * Like `run()` but also captures timing and metadata, returning a `RichResult<T, E>`.\n *\n * @example\n * const result = await track(() => fetchUser(id), { operation: 'user.fetch', tags: ['db'] });\n */\nexport async function track<T, E = Error>(\n fn: () => T | Promise<T>,\n options?: TrackOptions & { errorTransform?: (caught: unknown) => E },\n): Promise<RichResult<T, E>> {\n const start = performance.now();\n const timestamp = new Date().toISOString();\n const meta = {\n durationMs: 0,\n timestamp,\n operation: options?.operation,\n correlationId: options?.correlationId,\n tags: options?.tags,\n };\n\n try {\n const value = await fn();\n return { ok: true, value, ...meta, durationMs: Math.round(performance.now() - start) };\n } catch (caught) {\n const error = options?.errorTransform ? options.errorTransform(caught) : (caught as E);\n return { ok: false, error, ...meta, durationMs: Math.round(performance.now() - start) };\n }\n}\n\n/**\n * Promotes a plain `Result<T, E>` to a `RichResult<T, E>` with a zero-duration snapshot.\n * Useful when you already have a Result and want to attach metadata.\n */\nexport function enrich<T, E>(result: Result<T, E>, options?: TrackOptions): RichResult<T, E> {\n return {\n ...result,\n durationMs: 0,\n timestamp: new Date().toISOString(),\n operation: options?.operation,\n correlationId: options?.correlationId,\n tags: options?.tags,\n };\n}\n\n/**\n * Strips observability metadata from a `RichResult`, returning a plain `Result<T, E>`.\n */\nexport function simplify<T, E>(rich: RichResult<T, E>): Result<T, E> {\n return rich.ok\n ? { ok: true, value: rich.value }\n : { ok: false, error: rich.error };\n}\n","import { run, track } from '../result/run.js';\nimport type { TrackOptions } from '../result/types.js';\n\n/**\n * Method decorator that wraps the return value in `run()`, converting any\n * thrown exception into a `Result<T, E>`.\n *\n * @example\n * @AsResult('user.find')\n * async findUser(id: string) {\n * return this.db.users.findOrThrow(id);\n * }\n */\nexport function AsResult(operation?: string) {\n return function (\n _target: unknown,\n _key: string,\n descriptor: PropertyDescriptor,\n ): PropertyDescriptor {\n const original = descriptor.value as (...args: unknown[]) => unknown;\n descriptor.value = async function (...args: unknown[]) {\n return run(\n () => original.apply(this, args) as Promise<unknown>,\n undefined,\n );\n };\n if (operation) {\n (descriptor.value as { operationName?: string }).operationName = operation;\n }\n return descriptor;\n };\n}\n\n/**\n * Method decorator that wraps the return value in `track()`, capturing\n * execution duration, timestamp, and optional metadata.\n *\n * @example\n * @WithMetrics({ operation: 'payment.charge', tags: ['stripe'] })\n * async charge(dto: ChargeDto) {\n * return this.stripe.charge(dto);\n * }\n */\nexport function WithMetrics(options?: string | TrackOptions) {\n const opts: TrackOptions =\n typeof options === 'string' ? { operation: options } : (options ?? {});\n\n return function (\n _target: unknown,\n _key: string,\n descriptor: PropertyDescriptor,\n ): PropertyDescriptor {\n const original = descriptor.value as (...args: unknown[]) => unknown;\n descriptor.value = async function (...args: unknown[]) {\n return track(() => original.apply(this, args) as Promise<unknown>, opts);\n };\n return descriptor;\n };\n}\n","import type { Result, RichResult } from './types.js';\n\ntype OkResult<T, E> = Extract<Result<T, E>, { ok: true }>;\ntype FailResult<T, E> = Extract<Result<T, E>, { ok: false }>;\n\n/** Narrows a `Result<T, E>` to its success branch. */\nexport function isOk<T, E>(result: Result<T, E>): result is OkResult<T, E> {\n return result.ok === true;\n}\n\n/** Narrows a `Result<T, E>` to its failure branch. */\nexport function isFail<T, E>(result: Result<T, E>): result is FailResult<T, E> {\n return result.ok === false;\n}\n\n/** Returns `true` if the result carries observability metadata (`track()` output). */\nexport function isRich<T, E>(result: Result<T, E>): result is RichResult<T, E> {\n return 'durationMs' in result;\n}\n","import {\n Injectable,\n NestInterceptor,\n ExecutionContext,\n CallHandler,\n} from '@nestjs/common';\nimport { Observable } from 'rxjs';\nimport { map } from 'rxjs/operators';\nimport type { Result, RichResult } from '../result/types.js';\nimport { isRich } from '../result/guards.js';\n\nfunction isResult(value: unknown): value is Result<unknown, unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'ok' in value &&\n typeof (value as Record<string, unknown>)['ok'] === 'boolean'\n );\n}\n\n/**\n * Transforms `Result<T, E>` and `RichResult<T, E>` return values into a\n * consistent JSON response shape, so controllers don't need to unwrap manually.\n *\n * Plain results:\n * ```json\n * { \"ok\": true, \"data\": <value> }\n * { \"ok\": false, \"error\": \"<string>\" }\n * ```\n *\n * Rich results also include a `meta` block:\n * ```json\n * { \"ok\": true, \"data\": <value>, \"meta\": { \"operation\": \"...\", \"durationMs\": 12, ... } }\n * ```\n *\n * Non-Result return values are passed through unchanged.\n *\n * @example\n * // Global\n * app.useGlobalInterceptors(app.get(ResultInterceptor));\n *\n * // Per-controller\n * @UseInterceptors(ResultInterceptor)\n * @Controller('users')\n * export class UsersController { ... }\n */\n@Injectable()\nexport class ResultInterceptor implements NestInterceptor {\n intercept(_ctx: ExecutionContext, next: CallHandler): Observable<unknown> {\n return next.handle().pipe(\n map(value => {\n if (!isResult(value)) return value;\n\n const base = value.ok\n ? { ok: true, data: (value as { value: unknown }).value }\n : { ok: false, error: String((value as { error: unknown }).error) };\n\n if (!isRich(value)) return base;\n\n const rich = value as RichResult<unknown, unknown>;\n return {\n ...base,\n meta: {\n ...(rich.operation ? { operation: rich.operation } : {}),\n ...(rich.correlationId ? { correlationId: rich.correlationId } : {}),\n ...(rich.tags?.length ? { tags: rich.tags } : {}),\n durationMs: rich.durationMs,\n timestamp: rich.timestamp,\n },\n };\n }),\n );\n }\n}\n","import { Module } from '@nestjs/common';\nimport { ResultInterceptor } from './interceptor.js';\n\n@Module({\n providers: [ResultInterceptor],\n exports: [ResultInterceptor],\n})\nexport class ResultModule {}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { T as TrackOptions } from '../types-b-fNbJBy.cjs';
|
|
2
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Method decorator that wraps the return value in `run()`, converting any
|
|
7
|
+
* thrown exception into a `Result<T, E>`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* @AsResult('user.find')
|
|
11
|
+
* async findUser(id: string) {
|
|
12
|
+
* return this.db.users.findOrThrow(id);
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
declare function AsResult(operation?: string): (_target: unknown, _key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
16
|
+
/**
|
|
17
|
+
* Method decorator that wraps the return value in `track()`, capturing
|
|
18
|
+
* execution duration, timestamp, and optional metadata.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* @WithMetrics({ operation: 'payment.charge', tags: ['stripe'] })
|
|
22
|
+
* async charge(dto: ChargeDto) {
|
|
23
|
+
* return this.stripe.charge(dto);
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
declare function WithMetrics(options?: string | TrackOptions): (_target: unknown, _key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Transforms `Result<T, E>` and `RichResult<T, E>` return values into a
|
|
30
|
+
* consistent JSON response shape, so controllers don't need to unwrap manually.
|
|
31
|
+
*
|
|
32
|
+
* Plain results:
|
|
33
|
+
* ```json
|
|
34
|
+
* { "ok": true, "data": <value> }
|
|
35
|
+
* { "ok": false, "error": "<string>" }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Rich results also include a `meta` block:
|
|
39
|
+
* ```json
|
|
40
|
+
* { "ok": true, "data": <value>, "meta": { "operation": "...", "durationMs": 12, ... } }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Non-Result return values are passed through unchanged.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Global
|
|
47
|
+
* app.useGlobalInterceptors(app.get(ResultInterceptor));
|
|
48
|
+
*
|
|
49
|
+
* // Per-controller
|
|
50
|
+
* @UseInterceptors(ResultInterceptor)
|
|
51
|
+
* @Controller('users')
|
|
52
|
+
* export class UsersController { ... }
|
|
53
|
+
*/
|
|
54
|
+
declare class ResultInterceptor implements NestInterceptor {
|
|
55
|
+
intercept(_ctx: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class ResultModule {
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { AsResult, ResultInterceptor, ResultModule, WithMetrics };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { T as TrackOptions } from '../types-b-fNbJBy.js';
|
|
2
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Method decorator that wraps the return value in `run()`, converting any
|
|
7
|
+
* thrown exception into a `Result<T, E>`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* @AsResult('user.find')
|
|
11
|
+
* async findUser(id: string) {
|
|
12
|
+
* return this.db.users.findOrThrow(id);
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
declare function AsResult(operation?: string): (_target: unknown, _key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
16
|
+
/**
|
|
17
|
+
* Method decorator that wraps the return value in `track()`, capturing
|
|
18
|
+
* execution duration, timestamp, and optional metadata.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* @WithMetrics({ operation: 'payment.charge', tags: ['stripe'] })
|
|
22
|
+
* async charge(dto: ChargeDto) {
|
|
23
|
+
* return this.stripe.charge(dto);
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
declare function WithMetrics(options?: string | TrackOptions): (_target: unknown, _key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Transforms `Result<T, E>` and `RichResult<T, E>` return values into a
|
|
30
|
+
* consistent JSON response shape, so controllers don't need to unwrap manually.
|
|
31
|
+
*
|
|
32
|
+
* Plain results:
|
|
33
|
+
* ```json
|
|
34
|
+
* { "ok": true, "data": <value> }
|
|
35
|
+
* { "ok": false, "error": "<string>" }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Rich results also include a `meta` block:
|
|
39
|
+
* ```json
|
|
40
|
+
* { "ok": true, "data": <value>, "meta": { "operation": "...", "durationMs": 12, ... } }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Non-Result return values are passed through unchanged.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Global
|
|
47
|
+
* app.useGlobalInterceptors(app.get(ResultInterceptor));
|
|
48
|
+
*
|
|
49
|
+
* // Per-controller
|
|
50
|
+
* @UseInterceptors(ResultInterceptor)
|
|
51
|
+
* @Controller('users')
|
|
52
|
+
* export class UsersController { ... }
|
|
53
|
+
*/
|
|
54
|
+
declare class ResultInterceptor implements NestInterceptor {
|
|
55
|
+
intercept(_ctx: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class ResultModule {
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { AsResult, ResultInterceptor, ResultModule, WithMetrics };
|