composable-functions
Advanced tools
Comparing version 0.0.0-experimental-20240412-0 to 0.0.0-experimental-20240412-1
@@ -154,2 +154,25 @@ import { composable, failure, success } from './constructors.js'; | ||
} | ||
export { pipe, all, collect, sequence, map, catchError, mapError, mergeObjects }; | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
function trace(traceFn) { | ||
return (fn) => (async (...args) => { | ||
const originalResult = await fn(...args); | ||
const traceResult = await composable(traceFn)(originalResult, ...args); | ||
if (traceResult.success) | ||
return originalResult; | ||
return failure(traceResult.errors); | ||
}); | ||
} | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, trace, }; |
@@ -39,17 +39,4 @@ import { mapError } from './combinators.js'; | ||
} | ||
/** | ||
* It can be used to call a domain function from another domain function. It will return the output of the given domain function if it was successfull, otherwise it will throw a `ErrorList` that will bubble up to the parent function. | ||
* Also good to use it in successfull test cases. | ||
* @example | ||
* import { mdf, fromSuccess } from 'domain-functions' | ||
* | ||
* const add1 = mdf(z.number())((n) => n + 1) | ||
* const result = await add1(1) | ||
* // ^? Result<number> | ||
* const data = await fromSuccess(add1)(n) | ||
* // ^? number | ||
* expect(data).toBe(n + 1) | ||
*/ | ||
function fromSuccess(fn, onError = (e) => e) { | ||
return (async (...args) => { | ||
return async (...args) => { | ||
const result = await mapError(fn, onError)(...args); | ||
@@ -59,4 +46,4 @@ if (result.success) | ||
throw new ErrorList(result.errors); | ||
}); | ||
}; | ||
} | ||
export { composable, failure, fromSuccess, success }; |
import * as A from '../combinators.js'; | ||
import { composable, failure, fromSuccess } from '../constructors.js'; | ||
import { composable, fromSuccess } from '../constructors.js'; | ||
import { ErrorList } from '../errors.js'; | ||
@@ -33,4 +33,4 @@ import { applyEnvironment } from './constructors.js'; | ||
function collect(fns) { | ||
const dfsWithKey = Object.entries(fns).map(([key, df]) => map(df, (result) => ({ [key]: result }))); | ||
return map(all(...dfsWithKey), A.mergeObjects); | ||
const dfsWithKey = Object.entries(fns).map(([key, df]) => A.map(df, (result) => ({ [key]: result }))); | ||
return A.map(all(...dfsWithKey), A.mergeObjects); | ||
} | ||
@@ -70,3 +70,3 @@ /** | ||
function merge(...fns) { | ||
return map(all(...fns), A.mergeObjects); | ||
return A.map(all(...fns), A.mergeObjects); | ||
} | ||
@@ -105,3 +105,3 @@ /** | ||
const keys = Object.keys(fns); | ||
return map(map(sequence(...Object.values(fns)), (outputs) => outputs.map((o, i) => ({ | ||
return A.map(A.map(sequence(...Object.values(fns)), (outputs) => outputs.map((o, i) => ({ | ||
[keys[i]]: o, | ||
@@ -124,14 +124,2 @@ }))), A.mergeObjects); | ||
/** | ||
* It takes a domain function and a predicate to apply a transformation over the result.data of that function. It only runs if the function was successfull. When the given domain function fails, its error is returned wihout changes. | ||
* @example | ||
* import { mdf, map } from 'domain-functions' | ||
* | ||
* const a = mdf(z.object({ n: z.number() }))(({ n }) => n + 1) | ||
* const df = map(a, (n) => String(n)) | ||
* // ^? DomainFunction<string> | ||
*/ | ||
function map(dfn, mapper) { | ||
return A.map(dfn, mapper); | ||
} | ||
/** | ||
* Use it to add conditional logic to your domain functions' compositions. | ||
@@ -173,48 +161,2 @@ * It receives a domain function and a predicate function that should return the next domain function to be executed based on the previous domain function's output, like `pipe`. If the predicate returns `null` the result of the previous domain function will be returned and it won't be piped. | ||
} | ||
/** | ||
* Creates a single domain function that will apply a transformation over the ErrorResult of a failed DomainFunction. When the given domain function succeeds, its result is returned without changes. | ||
* @example | ||
* import { mdf, mapError } from 'domain-functions' | ||
* | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const summarizeErrors = (result: ErrorData) => | ||
* ({ | ||
* errors: [{ message: 'Errors count: ' + result.errors.length }], | ||
* inputErrors: [{ message: 'Input errors count: ' + result.inputErrors.length }], | ||
* environmentErrors: [{ message: 'Environment errors count: ' + result.environmentErrors.length }], | ||
* } as ErrorData) | ||
* | ||
* const incrementWithErrorSummary = mapError(increment, summarizeErrors) | ||
*/ | ||
function mapError(dfn, mapper) { | ||
return A.mapError(dfn, mapper); | ||
} | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
function trace(traceFn) { | ||
return (fn) => async (input, environment) => { | ||
const originalResult = await fn(input, environment); | ||
const traceResult = await composable(traceFn)({ | ||
input, | ||
environment, | ||
// TODO: Remove this casting when we unify the Unpack types | ||
result: originalResult, | ||
}); | ||
if (traceResult.success) | ||
return originalResult; | ||
return failure(traceResult.errors); | ||
}; | ||
} | ||
export { all, branch, collect, collectSequence, first, map, mapError, merge, pipe, sequence, trace, }; | ||
export { all, branch, collect, collectSequence, first, merge, pipe, sequence, }; |
export { applyEnvironment, make, fromComposable } from './constructors.js'; | ||
export { all, branch, collect, collectSequence, first, map, mapError, merge, pipe, sequence, trace, } from './combinators.js'; | ||
export { all, branch, collect, collectSequence, first, merge, pipe, sequence, } from './combinators.js'; |
export { composable, failure, fromSuccess, success } from './constructors.js'; | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, } from './combinators.js'; | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, trace } from './combinators.js'; | ||
export { inputFromForm, inputFromFormData, inputFromSearch, inputFromUrl, } from './input-resolvers.js'; | ||
@@ -4,0 +4,0 @@ export { toErrorPayload, serialize } from './serializer.js'; |
{ | ||
"name": "composable-functions", | ||
"version": "0.0.0-experimental-20240412-0", | ||
"version": "0.0.0-experimental-20240412-1", | ||
"description": "Decouple your business logic from your controllers. With first-class type inference from end to end.", | ||
@@ -40,2 +40,2 @@ "author": "Seasoned", | ||
"_generatedBy": "dnt@0.40.0" | ||
} | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergeObjects = exports.mapError = exports.catchError = exports.map = exports.sequence = exports.collect = exports.all = exports.pipe = void 0; | ||
exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.mapError = exports.map = exports.collect = exports.catchError = exports.all = void 0; | ||
const constructors_js_1 = require("./constructors.js"); | ||
@@ -165,1 +165,25 @@ /** | ||
exports.mapError = mapError; | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
function trace(traceFn) { | ||
return (fn) => (async (...args) => { | ||
const originalResult = await fn(...args); | ||
const traceResult = await (0, constructors_js_1.composable)(traceFn)(originalResult, ...args); | ||
if (traceResult.success) | ||
return originalResult; | ||
return (0, constructors_js_1.failure)(traceResult.errors); | ||
}); | ||
} | ||
exports.trace = trace; |
@@ -45,17 +45,4 @@ "use strict"; | ||
exports.composable = composable; | ||
/** | ||
* It can be used to call a domain function from another domain function. It will return the output of the given domain function if it was successfull, otherwise it will throw a `ErrorList` that will bubble up to the parent function. | ||
* Also good to use it in successfull test cases. | ||
* @example | ||
* import { mdf, fromSuccess } from 'domain-functions' | ||
* | ||
* const add1 = mdf(z.number())((n) => n + 1) | ||
* const result = await add1(1) | ||
* // ^? Result<number> | ||
* const data = await fromSuccess(add1)(n) | ||
* // ^? number | ||
* expect(data).toBe(n + 1) | ||
*/ | ||
function fromSuccess(fn, onError = (e) => e) { | ||
return (async (...args) => { | ||
return async (...args) => { | ||
const result = await (0, combinators_js_1.mapError)(fn, onError)(...args); | ||
@@ -65,4 +52,4 @@ if (result.success) | ||
throw new errors_js_1.ErrorList(result.errors); | ||
}); | ||
}; | ||
} | ||
exports.fromSuccess = fromSuccess; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trace = exports.sequence = exports.pipe = exports.merge = exports.mapError = exports.map = exports.first = exports.collectSequence = exports.collect = exports.branch = exports.all = void 0; | ||
exports.sequence = exports.pipe = exports.merge = exports.first = exports.collectSequence = exports.collect = exports.branch = exports.all = void 0; | ||
const A = __importStar(require("../combinators.js")); | ||
@@ -61,4 +61,4 @@ const constructors_js_1 = require("../constructors.js"); | ||
function collect(fns) { | ||
const dfsWithKey = Object.entries(fns).map(([key, df]) => map(df, (result) => ({ [key]: result }))); | ||
return map(all(...dfsWithKey), A.mergeObjects); | ||
const dfsWithKey = Object.entries(fns).map(([key, df]) => A.map(df, (result) => ({ [key]: result }))); | ||
return A.map(all(...dfsWithKey), A.mergeObjects); | ||
} | ||
@@ -100,3 +100,3 @@ exports.collect = collect; | ||
function merge(...fns) { | ||
return map(all(...fns), A.mergeObjects); | ||
return A.map(all(...fns), A.mergeObjects); | ||
} | ||
@@ -137,3 +137,3 @@ exports.merge = merge; | ||
const keys = Object.keys(fns); | ||
return map(map(sequence(...Object.values(fns)), (outputs) => outputs.map((o, i) => ({ | ||
return A.map(A.map(sequence(...Object.values(fns)), (outputs) => outputs.map((o, i) => ({ | ||
[keys[i]]: o, | ||
@@ -158,15 +158,2 @@ }))), A.mergeObjects); | ||
/** | ||
* It takes a domain function and a predicate to apply a transformation over the result.data of that function. It only runs if the function was successfull. When the given domain function fails, its error is returned wihout changes. | ||
* @example | ||
* import { mdf, map } from 'domain-functions' | ||
* | ||
* const a = mdf(z.object({ n: z.number() }))(({ n }) => n + 1) | ||
* const df = map(a, (n) => String(n)) | ||
* // ^? DomainFunction<string> | ||
*/ | ||
function map(dfn, mapper) { | ||
return A.map(dfn, mapper); | ||
} | ||
exports.map = map; | ||
/** | ||
* Use it to add conditional logic to your domain functions' compositions. | ||
@@ -209,49 +196,1 @@ * It receives a domain function and a predicate function that should return the next domain function to be executed based on the previous domain function's output, like `pipe`. If the predicate returns `null` the result of the previous domain function will be returned and it won't be piped. | ||
exports.branch = branch; | ||
/** | ||
* Creates a single domain function that will apply a transformation over the ErrorResult of a failed DomainFunction. When the given domain function succeeds, its result is returned without changes. | ||
* @example | ||
* import { mdf, mapError } from 'domain-functions' | ||
* | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const summarizeErrors = (result: ErrorData) => | ||
* ({ | ||
* errors: [{ message: 'Errors count: ' + result.errors.length }], | ||
* inputErrors: [{ message: 'Input errors count: ' + result.inputErrors.length }], | ||
* environmentErrors: [{ message: 'Environment errors count: ' + result.environmentErrors.length }], | ||
* } as ErrorData) | ||
* | ||
* const incrementWithErrorSummary = mapError(increment, summarizeErrors) | ||
*/ | ||
function mapError(dfn, mapper) { | ||
return A.mapError(dfn, mapper); | ||
} | ||
exports.mapError = mapError; | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
function trace(traceFn) { | ||
return (fn) => async (input, environment) => { | ||
const originalResult = await fn(input, environment); | ||
const traceResult = await (0, constructors_js_1.composable)(traceFn)({ | ||
input, | ||
environment, | ||
// TODO: Remove this casting when we unify the Unpack types | ||
result: originalResult, | ||
}); | ||
if (traceResult.success) | ||
return originalResult; | ||
return (0, constructors_js_1.failure)(traceResult.errors); | ||
}; | ||
} | ||
exports.trace = trace; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trace = exports.sequence = exports.pipe = exports.merge = exports.mapError = exports.map = exports.first = exports.collectSequence = exports.collect = exports.branch = exports.all = exports.fromComposable = exports.make = exports.applyEnvironment = void 0; | ||
exports.sequence = exports.pipe = exports.merge = exports.first = exports.collectSequence = exports.collect = exports.branch = exports.all = exports.fromComposable = exports.make = exports.applyEnvironment = void 0; | ||
var constructors_js_1 = require("./constructors.js"); | ||
@@ -14,7 +14,4 @@ Object.defineProperty(exports, "applyEnvironment", { enumerable: true, get: function () { return constructors_js_1.applyEnvironment; } }); | ||
Object.defineProperty(exports, "first", { enumerable: true, get: function () { return combinators_js_1.first; } }); | ||
Object.defineProperty(exports, "map", { enumerable: true, get: function () { return combinators_js_1.map; } }); | ||
Object.defineProperty(exports, "mapError", { enumerable: true, get: function () { return combinators_js_1.mapError; } }); | ||
Object.defineProperty(exports, "merge", { enumerable: true, get: function () { return combinators_js_1.merge; } }); | ||
Object.defineProperty(exports, "pipe", { enumerable: true, get: function () { return combinators_js_1.pipe; } }); | ||
Object.defineProperty(exports, "sequence", { enumerable: true, get: function () { return combinators_js_1.sequence; } }); | ||
Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return combinators_js_1.trace; } }); |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compat = exports.df = exports.InputError = exports.ErrorList = exports.EnvironmentError = exports.serialize = exports.toErrorPayload = exports.inputFromUrl = exports.inputFromSearch = exports.inputFromFormData = exports.inputFromForm = exports.sequence = exports.pipe = exports.mergeObjects = exports.mapError = exports.map = exports.collect = exports.catchError = exports.all = exports.success = exports.fromSuccess = exports.failure = exports.composable = void 0; | ||
exports.compat = exports.df = exports.InputError = exports.ErrorList = exports.EnvironmentError = exports.serialize = exports.toErrorPayload = exports.inputFromUrl = exports.inputFromSearch = exports.inputFromFormData = exports.inputFromForm = exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.mapError = exports.map = exports.collect = exports.catchError = exports.all = exports.success = exports.fromSuccess = exports.failure = exports.composable = void 0; | ||
var constructors_js_1 = require("./constructors.js"); | ||
@@ -42,2 +42,3 @@ Object.defineProperty(exports, "composable", { enumerable: true, get: function () { return constructors_js_1.composable; } }); | ||
Object.defineProperty(exports, "sequence", { enumerable: true, get: function () { return combinators_js_1.sequence; } }); | ||
Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return combinators_js_1.trace; } }); | ||
var input_resolvers_js_1 = require("./input-resolvers.js"); | ||
@@ -44,0 +45,0 @@ Object.defineProperty(exports, "inputFromForm", { enumerable: true, get: function () { return input_resolvers_js_1.inputFromForm; } }); |
@@ -1,2 +0,2 @@ | ||
import type { AllArguments, CollectArguments, Composable, Failure, First, Fn, MergeObjs, PipeArguments, PipeReturn, RecordToTuple, UnpackAll, UnpackData } from './types.js'; | ||
import type { AllArguments, CollectArguments, Composable, First, MergeObjs, PipeArguments, PipeReturn, RecordToTuple, Result, UnpackAll, UnpackData } from './types.js'; | ||
/** | ||
@@ -84,3 +84,3 @@ * Merges a list of objects into a single object. | ||
*/ | ||
declare function catchError<T extends Fn, R>(fn: Composable<T>, catcher: (err: Failure['errors'], ...originalInput: Parameters<T>) => R): Composable<(...args: Parameters<T>) => ReturnType<T> extends any[] ? R extends never[] ? ReturnType<T> : ReturnType<T> | R : ReturnType<T> | R>; | ||
declare function catchError<F extends Composable, C extends (err: Error[], ...originalInput: Parameters<F>) => any>(fn: F, catcher: C): Composable<(...args: Parameters<F>) => Awaited<ReturnType<C>> extends never[] ? UnpackData<ReturnType<F>> extends any[] ? UnpackData<ReturnType<F>> : Awaited<ReturnType<C>> | UnpackData<ReturnType<F>> : Awaited<ReturnType<C>> | UnpackData<ReturnType<F>>>; | ||
/** | ||
@@ -97,2 +97,17 @@ * Creates a new function that will apply a transformation over a resulting Failure from the given function. When the given function succeeds, its result is returned without changes. | ||
declare function mapError<T extends Composable, R>(fn: T, mapper: (err: Error[]) => Error[] | Promise<Error[]>): T; | ||
export { pipe, all, collect, sequence, map, catchError, mapError, mergeObjects }; | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
declare function trace(traceFn: (result: Result<unknown>, ...originalInput: unknown[]) => Promise<void> | void): <C extends Composable>(fn: C) => C; | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, trace, }; |
@@ -0,1 +1,2 @@ | ||
import { DomainFunction } from './index.js'; | ||
import type { Composable, Failure, Fn, Success } from './types.js'; | ||
@@ -23,3 +24,5 @@ declare function success<const T>(data: T): Success<T>; | ||
*/ | ||
declare function fromSuccess<T extends Composable>(fn: T, onError?: (errors: Error[]) => Error[] | Promise<Error[]>): T extends Composable<(...a: infer A) => infer O> ? (...a: A) => Promise<Awaited<O>> : never; | ||
type OnError = (errors: Error[]) => Error[] | Promise<Error[]>; | ||
declare function fromSuccess<O, T extends Composable<(...a: any[]) => O>>(fn: T, onError?: OnError): T extends Composable<(...a: infer P) => infer O> ? (...args: P) => Promise<O> : never; | ||
declare function fromSuccess<O, T extends DomainFunction<O>>(fn: T, onError?: OnError): (...args: Parameters<DomainFunction>) => Promise<O>; | ||
export { composable, failure, fromSuccess, success }; |
import type { Last, MergeObjs, TupleToUnion, UnpackAll } from '../types.js'; | ||
import type { DomainFunction, UnpackDFObject, UnpackData, UnpackResult } from './types.js'; | ||
import type { DomainFunction, UnpackDFObject, UnpackData } from './types.js'; | ||
/** | ||
@@ -89,12 +89,2 @@ * Creates a single domain function out of multiple domain functions. It will pass the same input and environment to each provided function. The functions will run in parallel. If all constituent functions are successful, The data field will be a tuple containing each function's output. | ||
/** | ||
* It takes a domain function and a predicate to apply a transformation over the result.data of that function. It only runs if the function was successfull. When the given domain function fails, its error is returned wihout changes. | ||
* @example | ||
* import { mdf, map } from 'domain-functions' | ||
* | ||
* const a = mdf(z.object({ n: z.number() }))(({ n }) => n + 1) | ||
* const df = map(a, (n) => String(n)) | ||
* // ^? DomainFunction<string> | ||
*/ | ||
declare function map<O, R>(dfn: DomainFunction<O>, mapper: (element: O) => R | Promise<R>): DomainFunction<R>; | ||
/** | ||
* Use it to add conditional logic to your domain functions' compositions. | ||
@@ -124,38 +114,2 @@ * It receives a domain function and a predicate function that should return the next domain function to be executed based on the previous domain function's output, like `pipe`. If the predicate returns `null` the result of the previous domain function will be returned and it won't be piped. | ||
declare function branch<T, R extends DomainFunction | null>(dfn: DomainFunction<T>, resolver: (o: T) => Promise<R> | R): DomainFunction<R extends DomainFunction<infer U> ? U : T | UnpackData<NonNullable<R>>>; | ||
/** | ||
* Creates a single domain function that will apply a transformation over the ErrorResult of a failed DomainFunction. When the given domain function succeeds, its result is returned without changes. | ||
* @example | ||
* import { mdf, mapError } from 'domain-functions' | ||
* | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const summarizeErrors = (result: ErrorData) => | ||
* ({ | ||
* errors: [{ message: 'Errors count: ' + result.errors.length }], | ||
* inputErrors: [{ message: 'Input errors count: ' + result.inputErrors.length }], | ||
* environmentErrors: [{ message: 'Environment errors count: ' + result.environmentErrors.length }], | ||
* } as ErrorData) | ||
* | ||
* const incrementWithErrorSummary = mapError(increment, summarizeErrors) | ||
*/ | ||
declare function mapError<O>(dfn: DomainFunction<O>, mapper: (errors: Error[]) => Error[] | Promise<Error[]>): DomainFunction<O>; | ||
type TraceData<T> = { | ||
input: unknown; | ||
environment: unknown; | ||
result: T; | ||
}; | ||
/** | ||
* Whenever you need to intercept inputs and a domain function result without changing them you can use this function. | ||
* The most common use case is to log failures to the console or to an external service. | ||
* @param traceFn A function that receives the input, environment and result of a domain function. | ||
* @example | ||
* import { mdf, trace } from 'domain-functions' | ||
* | ||
* const trackErrors = trace(({ input, output, result }) => { | ||
* if(!result.success) sendToExternalService({ input, output, result }) | ||
* }) | ||
* const increment = mdf(z.object({ id: z.number() }))(({ id }) => id + 1) | ||
* const incrementAndTrackErrors = trackErrors(increment) | ||
* // ^? DomainFunction<number> | ||
*/ | ||
declare function trace<D extends DomainFunction = DomainFunction<unknown>>(traceFn: ({ input, environment, result, }: TraceData<UnpackResult<D>>) => Promise<void> | void): <T>(fn: DomainFunction<T>) => DomainFunction<T>; | ||
export { all, branch, collect, collectSequence, first, map, mapError, merge, pipe, sequence, trace, }; | ||
export { all, branch, collect, collectSequence, first, merge, pipe, sequence, }; |
export { applyEnvironment, make, fromComposable } from './constructors.js'; | ||
export type { DomainFunction, ParserIssue, ParserResult, ParserSchema, UnpackData, UnpackResult, UnpackDFObject, UnpackSuccess, } from './types.js'; | ||
export { all, branch, collect, collectSequence, first, map, mapError, merge, pipe, sequence, trace, } from './combinators.js'; | ||
export { all, branch, collect, collectSequence, first, merge, pipe, sequence, } from './combinators.js'; |
@@ -1,2 +0,2 @@ | ||
import { Result } from '../types.js'; | ||
import { Composable } from '../types.js'; | ||
/** | ||
@@ -6,5 +6,3 @@ * A domain function. | ||
*/ | ||
type DomainFunction<Output = unknown> = { | ||
(input?: unknown, environment?: unknown): Promise<Result<Output>>; | ||
}; | ||
type DomainFunction<Output = unknown> = Composable<(input?: unknown, environment?: unknown) => Output>; | ||
/** | ||
@@ -11,0 +9,0 @@ * Unpacks the result of a domain function. |
export { composable, failure, fromSuccess, success } from './constructors.js'; | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, } from './combinators.js'; | ||
export { all, catchError, collect, map, mapError, mergeObjects, pipe, sequence, trace } from './combinators.js'; | ||
export { inputFromForm, inputFromFormData, inputFromSearch, inputFromUrl, } from './input-resolvers.js'; | ||
@@ -4,0 +4,0 @@ export { toErrorPayload, serialize } from './serializer.js'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
0
123239
47
2114