import { type AnyFunction, type Kind, type IsEqual, type MaybePromise, type MaybeAsyncGenerator, type GetKindValue, type ComputedTypeError, type AnyTuple, type Unwrap } from "../common"; import * as DEither from "../either"; export declare const requirementsChainedFunctionKind: import("../common").KindHandler>; export interface RequirementsChainedFunction = AnyTuple> extends Kind { } export type FunctionOfChain = [string, AnyFunction, RequirementsChainedFunction?]; export type FunctionChain = [ FunctionOfChain, FunctionOfChain, ...FunctionOfChain[] ]; export declare const chainEndKind: import("../common").KindHandler>; export interface ChainEnd extends Kind { } export interface CreateChainEnd { (): ChainEnd; (value: GenericValue): ChainEnd; } export type Link = >(theFunction: (theFunction: { [Prop in GenericFunction[0]]: GenericFunction[1]; }) => GenericOutput, ...args: GenericFunction[2] extends RequirementsChainedFunction ? [ requirements: GetKindValue ] : []) => ((Extract> extends infer InferredPromise ? IsEqual extends true ? never : Awaited extends infer InferredValue extends unknown ? AsyncGenerator, [ Exclude, GenericNext ]> : never : never) | (Exclude> extends infer InferredValue ? IsEqual extends true ? never : Generator, [ Exclude, GenericNext ]> : never)); export type Chain = GenericFunctionChain extends readonly [] ? CreateChainEnd : GenericFunctionChain extends [ infer InferredFirst extends FunctionOfChain, ...infer InferredRest extends readonly FunctionOfChain[] ] ? Chain extends infer InferredRestResult extends (Link | CreateChainEnd) ? Link : never : never; type OutputMustContainChainEnd = IsEqual ? InferredReturnValue extends ChainEnd ? InferredReturnValue : never : never, never> extends true ? ComputedTypeError<"Output must contain a chainEnd"> : unknown; type ComputeResult = GenericGenerator extends Generator ? (InferredIterateValue | InferredReturnValue) extends infer InferredResult ? InferredResult extends ChainEnd ? GetKindValue : InferredResult : never : GenericGenerator extends AsyncGenerator ? Promise extends infer InferredResult ? InferredResult extends ChainEnd ? GetKindValue : InferredResult : never> : never; declare function breakIfLeft(value: GenericValue): Generator, Exclude>; declare function unwrapResult | ChainEnd]>(resultLink: GenericLinkResult): [Unwrap, GenericLinkResult[1]]; export interface ChainedFunctionParams { breakIfLeft: typeof breakIfLeft; unwrapResult: typeof unwrapResult; } export interface ChainedFunction { , MaybePromise>>(callback: (firstLink: Chain, params: ChainedFunctionParams) => (GenericGenerator & OutputMustContainChainEnd)): ComputeResult; /** * @deprecated use this only for the tests. */ functions: { [Entry in GenericValue[number] as Entry[0]]: Entry[1]; }; } /** * Declares a typed aggregate of pure linked business actions that must run in order. * * **Supported call styles:** * - Classic: `chainedFunction(firstFunction, secondFunction, ...functions)` -> returns an implementation function driven by generator links * * Use it inside a Clean Architecture use case when several pure domain operations that update different entities must belong to the same business consistency boundary. Each link exposes exactly one named action, yields `Left` values to short-circuit the implementation, and provides the next link until the last step returns `chainEnd(value)`. Repository calls stay in the use case through the library repository system; functions passed to `chainedFunction` remain pure domain functions. * * You can also declare typed `requirements` on a link (`C.chainedFunction.requirements<[...args]>()`). These values are mandatory when calling that link, not to feed runtime computation directly, but to prove that prerequisite lifecycle information was obtained earlier in the flow. * * ```ts * const CommentPublicationAggregate = C.chainedFunction( * [ * "createComment", * (draft: CommentDraft): Comment | E.Fail => draft.content.trim() * ? { * id: 1, * articleId: draft.articleId, * content: draft.content.trim(), * } * : E.fail(), * ], * [ * "incrementArticleCommentCount", * (article: Article): Article => ({ * ...article, * commentCount: article.commentCount + 1, * }), * ], * ); * * const PublishCommentUseCase = C.createUseCase( * { * CommentRepository, * ArticleRepository, * }, * ({ * commentRepository, * articleRepository, * }) => (draft: CommentDraft) => CommentPublicationAggregate(function *(link1, { breakIfLeft }) { * const [comment, link2] = yield *link1(({ createComment }) => createComment(draft)); * * const savedComment = yield *breakIfLeft(commentRepository.save(comment)); * * const article = yield *breakIfLeft(articleRepository.findById(savedComment.articleId)); * * const [updatedArticle, chainEnd] = yield *link2( * ({ incrementArticleCommentCount }) => incrementArticleCommentCount(article), * ); * * const savedArticle = yield *breakIfLeft(articleRepository.save(updatedArticle)); * * return chainEnd({ * comment: savedComment, * article: savedArticle, * }); * }), * ); * * const publishComment = PublishCommentUseCase.getUseCase({ * commentRepository: CommentRepository.createImplementation({ * save: (comment) => comment, * }), * articleRepository: ArticleRepository.createImplementation({ * findById: (articleId) => ({ * id: articleId, * commentCount: 11, * }), * save: (article) => article, * }), * }); * * const publishedComment = publishComment({ * articleId: 12, * content: " New comment ", * }); * * type CheckPublishedComment = ExpectType< * typeof publishedComment, * { * comment: Comment; * article: Article; * } | E.Fail, * "strict" * >; * * const emptyContentResult = publishComment({ * articleId: 12, * content: " ", * }); * * type CheckEmptyContentResult = ExpectType< * typeof emptyContentResult, * { * comment: Comment; * article: Article; * } | E.Fail, * "strict" * >; * * const failingPublishComment = PublishCommentUseCase.getUseCase({ * commentRepository: CommentRepository.createImplementation({ * save: () => E.fail(), * }), * articleRepository: ArticleRepository.createImplementation({ * findById: (articleId) => ({ * id: articleId, * commentCount: 11, * }), * save: (article) => article, * }), * }); * * const repositoryFailureResult = failingPublishComment({ * articleId: 12, * content: "New comment", * }); * * type CheckRepositoryFailureResult = ExpectType< * typeof repositoryFailureResult, * { * comment: Comment; * article: Article; * } | E.Fail, * "strict" * >; * ``` * * @remarks `chainedFunction` expects at least two functions in the chain. It does not catch thrown exceptions or rejected promises; model handled business errors with `Either.Left`. * The callback receives `(firstLink, { breakIfLeft, unwrapResult })`. `breakIfLeft` is synchronous and narrows `value | Left` to `value`, yielding the `Left` branch to short-circuit when needed. * `unwrapResult` unwraps the value returned by a link result tuple (`[value, nextLinkOrChainEnd]`) while preserving the next link in the same tuple shape. * `requirements` act as compile-time guards for flow invariants: they can enforce that a step cannot run unless specific typed markers are provided, even when those markers are not useful as runtime arguments for the next function. * * @see https://utils.duplojs.dev/en/v1/api/clean/chainedFunction * @see [`C.createUseCase`](https://utils.duplojs.dev/en/v1/api/clean/useCase) * @see [`E.Left`](https://utils.duplojs.dev/en/v1/api/either/left) * * @namespace C * */ export declare function chainedFunction(function1: GenericFunction1, function2: GenericFunction2, ...functions: GenericFunctions): ChainedFunction<[ GenericFunction1, GenericFunction2, ...GenericFunctions ]>; export declare namespace chainedFunction { var requirements: >() => RequirementsChainedFunction; } export {};