/** * Represents the {@link IResult | result} of some operation or sequence of operations. * @remarks * {@link Success | Success} and {@link Failure | Failure} share the common * contract {@link IResult}, enabling commingled discriminated usage. * @public */ export type Result = Success | Failure; /** * Represents a deferred result that will be evaluated if needed. * @public */ export type DeferredResult = () => Result; /** * Checks if a result is a deferred result. * @param result - The result to check. * @returns `true` if the result is a deferred result, `false` otherwise. * @public */ export declare function isDeferredResult(result: Result | DeferredResult): result is DeferredResult; /** * Continuation callback to be called in the event that an * {@link Result} is successful. * @public */ export type SuccessContinuation = (value: T) => Result; /** * Continuation callback to be called in the event that an * {@link Result} fails. * @public */ export type FailureContinuation = (message: string) => Result; /** * Type inference to determine the result type of an {@link Result}. * @beta */ export type ResultValueType = T extends Result ? TV : never; /** * Formats an error message. * @param message - The error message to be formatted. * @param detail - An optional detail to be included in the formatted message. * @public */ export type ErrorFormatter = (message: string, detail?: TD) => string; /** * Simple logger interface used by {@link IResult.(orThrow:1) | orThrow(logger)} and {@link IResult.(orThrow:2) | orThrow(formatter)}. * @public */ export interface IResultLogger { /** * Log an error message. * @param message - The message to be logged. */ error(message: string, detail?: TD): void; } /** * The severity level at which a message should be logged. * @public */ export type MessageLogLevel = 'quiet' | 'detail' | 'info' | 'warning' | 'error'; /** * Details for reporting a message. * @public */ export interface IMessageReportDetail { level?: MessageLogLevel; message?: ErrorFormatter; detail?: TD; } /** * Options for reporting a result. * @public */ export interface IResultReportOptions { /** * The level of reporting to be used for failure results. Default is 'error'. */ failure?: MessageLogLevel | IMessageReportDetail; /** * The level of reporting to be used for success results. Default is 'quiet'. */ success?: MessageLogLevel | IMessageReportDetail; } /** * Interface for reporting a result. * @public */ export interface IResultReporter { reportSuccess(level: MessageLogLevel, value: T, detail?: TD, message?: ErrorFormatter): void; reportFailure(level: MessageLogLevel, message: string, detail?: TD): void; } /** * Simple error aggregator to simplify collecting all errors in * a flow. * @public */ export interface IMessageAggregator { /** * Indicates whether any messages have been aggregated. */ readonly hasMessages: boolean; /** * The number of messages aggregated. */ readonly numMessages: number; /** * The aggregated messages. */ readonly messages: ReadonlyArray; /** * Adds a message to the aggregator, if defined. * @param message - The message to add - pass `undefined` * or the empty string to continue without adding a message. */ addMessage(message: string | undefined): this; /** * Adds multiple messages to the aggregator. * @param messages - the messages to add. */ addMessages(messages: string[] | undefined): this; /** * Returns all messages as a single string joined * using the optionally-supplied `separator`, or * newline if no separator is specified. * @param separator - The optional separator used * to join strings. */ toString(separator?: string): string; } /** * Represents the result of some operation of sequence of operations. * @remarks * This common contract enables commingled discriminated usage of {@link Success | Success} * and {@link Failure | Failure}. * @public */ export interface IResult { /** * Indicates whether the operation was successful. */ readonly success: boolean; /** * Value returned by a successful operation, undefined * for a failed operation. */ readonly value: T | undefined; /** * Error message returned by a failed operation, undefined * for a successful operation. */ readonly message: string | undefined; /** * Indicates whether this operation was successful. Functions * as a type guard for {@link Success | Success}. */ isSuccess(): this is Success; /** * Indicates whether this operation failed. Functions * as a type guard for {@link Failure | Failure}. */ isFailure(): this is Failure; /** * Gets the value associated with a successful {@link IResult | result}, * or throws the error message if the corresponding operation failed. * * Note that `getValueOrThrow` is being superseded by `orThrow` and * will eventually be deprecated. Please use orDefault instead. * * @param logger - An optional {@link IResultLogger | logger} to which the * error will also be reported. * @returns The return value, if the operation was successful. * @throws The error message if the operation failed. * @deprecated Use {@link IResult.(orThrow:1) | orThrow(logger)} or {@link IResult.(orThrow:2) | orThrow(formatter)} instead. */ getValueOrThrow(logger?: IResultLogger): T; /** * Gets the value associated with a successful {@link IResult | result}, * or a default value if the corresponding operation failed. * @param dflt - The value to be returned if the operation failed (default is * `undefined`). * * Note that `getValueOrDefault` is being superseded by `orDefault` and * will eventually be deprecated. Please use orDefault instead. * * @returns The return value, if the operation was successful. Returns * the supplied default value or `undefined` if no default is supplied. * @deprecated Use {@link IResult.(orDefault:1) | orDefault(T)} or {@link IResult.(orDefault:2) | orDefault()} instead. */ getValueOrDefault(dflt?: T): T | undefined; /** * Gets the value associated with a successful {@link IResult | result}, * or throws the error message if the corresponding operation failed. * @param logger - An optional {@link IResultLogger | logger} to which the * error will also be reported. * @returns The return value, if the operation was successful. * @throws The error message if the operation failed. * {@label logger} */ orThrow(logger?: IResultLogger): T; /** * Gets the value associated with a successful {@link IResult | result}, * or throws the error message if the corresponding operation failed. * @param cb - The {@link ErrorFormatter | error formatter} to be called in the event of failure. * @returns The return value, if the operation was successful. * @throws The error message if the operation failed. * {@label formatter} */ orThrow(cb: ErrorFormatter): T; /** * Gets the value associated with a successful {@link IResult | result}, * or a default value if the corresponding operation failed. * @param dflt - The value to be returned if the operation failed. * @returns The return value, if the operation was successful. Returns * the supplied default if an error occurred. * {@label SUPPLIED} */ orDefault(dflt: T): T; /** * Gets the value associated with a successful {@link IResult | result}, * or a default value if the corresponding operation failed. * @returns The return value, if the operation was successful, or * `undefined` if an error occurs. * {@label MISSING} */ orDefault(): T | undefined; /** * Calls a supplied {@link SuccessContinuation | success continuation} if * the operation was a success. * @remarks * The {@link SuccessContinuation | success continuation} might return a * different result type than {@link IResult} on which it is invoked. This * enables chaining of operations with heterogenous return types. * * @param cb - The {@link SuccessContinuation | success continuation} to * be called in the event of success. * @returns If this operation was successful, returns the value returned * by the {@link SuccessContinuation | success continuation}. If this result * failed, propagates the error message from this failure. */ onSuccess(cb: SuccessContinuation): Result; /** * Calls a supplied {@link FailureContinuation | failed continuation} if * the operation failed. * @param cb - The {@link FailureContinuation | failure continuation} to * be called in the event of failure. * @returns If this operation failed, returns the value returned by the * {@link FailureContinuation | failure continuation}. If this result * was successful, propagates the result value from the successful event. */ onFailure(cb: FailureContinuation): Result; /** * Calls a supplied {@link ErrorFormatter | error formatter} if * the operation failed. * @param cb - The {@link ErrorFormatter | error formatter} to * be called in the event of failure. * @returns If this operation failed, returns the returns {@link Failure | Failure} * with the message returned by the formatter. If this result * was successful, propagates the result value from the successful event. */ withErrorFormat(cb: ErrorFormatter): Result; /** * Converts a {@link IResult | IResult} to a {@link DetailedResult | DetailedResult}, * adding a supplied detail if the operation failed. * @param detail - The detail to be added if this operation failed. * @returns A new {@link DetailedResult | DetailedResult} with either * the success result or the error message from this {@link IResult}, with * the supplied detail (if this event failed) or detail `undefined` (if * this result succeeded). */ withFailureDetail(detail: TD): DetailedResult; /** * Converts a {@link IResult | IResult} to a {@link DetailedResult | DetailedResult}, * adding supplied details. * @param detail - The default detail to be added to the new {@link DetailedResult}. * @param successDetail - An optional detail to be added if this result was successful. * @returns A new {@link DetailedResult | DetailedResult} with either * the success result or the error message from this {@link IResult} and the * appropriate added detail. */ withDetail(detail: TD, successDetail?: TD): DetailedResult; /** * Propagates interior result, appending any error message to the * supplied errors array. * @param errors - {@link IMessageAggregator | Error aggregator} in which * errors will be aggregated. * @param formatter - An optional {@link ErrorFormatter | error formatter} to be used to format the error message. */ aggregateError(errors: IMessageAggregator, formatter?: ErrorFormatter): this; /** * Reports the result to the supplied reporter * @param reporter - The {@link IResultReporter | reporter} to which the result will be reported. * @param options - The {@link IResultReportOptions | options} for reporting the result. */ report(reporter?: IResultReporter, options?: IResultReportOptions): Result; } /** * Reports a successful {@link IResult | result} from some operation and the * corresponding value. * @public */ export declare class Success implements IResult { /** * {@inheritdoc IResult.success} */ readonly success: true; /** * For a successful operation, the error message is always `undefined`. */ readonly message: undefined; /** * @internal */ protected readonly _value: T; /** * Constructs a {@link Success} with the supplied value. * @param value - The value to be returned. */ constructor(value: T); /** * The result value returned by the successful operation. */ get value(): T; /** * {@inheritdoc IResult.isSuccess} */ isSuccess(): this is Success; /** * {@inheritdoc IResult.isFailure} */ isFailure(): this is Failure; /** * {@inheritdoc IResult.(orThrow:1)} */ orThrow(logger?: IResultLogger): T; /** * {@inheritdoc IResult.(orThrow:2)} */ orThrow(cb: ErrorFormatter): T; /** * {@inheritdoc IResult.(orDefault:1)} */ orDefault(dflt: T): T; /** * {@inheritdoc IResult.(orDefault:2)} */ orDefault(): T | undefined; /** * {@inheritdoc IResult.getValueOrThrow} * @deprecated Use {@link Success.(orThrow:1) | orThrow(logger)} or {@link Success.(orThrow:2) | orThrow(formatter)} instead. */ getValueOrThrow(__logger?: IResultLogger): T; /** * {@inheritdoc IResult.getValueOrDefault} * @deprecated Use {@link Success.(orDefault:1) | orDefault(T)} or {@link Success.(orDefault:2) | orDefault()} instead. */ getValueOrDefault(dflt?: T): T | undefined; /** * {@inheritdoc IResult.onSuccess} */ onSuccess(cb: SuccessContinuation): Result; /** * {@inheritdoc IResult.onFailure} */ onFailure(__: FailureContinuation): Result; /** * {@inheritdoc IResult.withErrorFormat} */ withErrorFormat(__cb: ErrorFormatter): Result; /** * {@inheritdoc IResult.withFailureDetail} */ withFailureDetail(__detail: TD): DetailedResult; /** * {@inheritdoc IResult.withDetail} */ withDetail(detail: TD, successDetail?: TD): DetailedResult; /** * {@inheritdoc IResult.aggregateError} */ aggregateError(__errors: IMessageAggregator, __formatter?: ErrorFormatter): this; /** * {@inheritdoc IResult.report} */ report(reporter?: IResultReporter, options?: IResultReportOptions): Success; /** * Creates a {@link Success | Success} with the supplied value. * @param value - The value to be returned. * @returns The resulting {@link Success | Success} with the supplied value. * @public */ static with(value: T): Success; } /** * Reports a failed {@link IResult | result} from some operation, with an error message. * @public */ export declare class Failure implements IResult { /** * {@inheritdoc IResult.success} */ readonly success: false; /** * Failed operation always returns undefined for value. */ readonly value: undefined; /** * @internal */ protected readonly _message: string; /** * Constructs a {@link Failure} with the supplied message. * @param message - Error message to be reported. */ constructor(message: string); /** * Gets the error message associated with this error. */ get message(): string; /** * {@inheritdoc IResult.isSuccess} */ isSuccess(): this is Success; /** * {@inheritdoc IResult.isFailure} */ isFailure(): this is Failure; /** * {@inheritdoc IResult.(orThrow:1)} */ orThrow(logger?: IResultLogger): never; /** * {@inheritdoc IResult.(orThrow:2)} */ orThrow(cb: ErrorFormatter): never; /** * {@inheritdoc IResult.(orDefault:1)} */ orDefault(dflt: T): T; /** * {@inheritdoc IResult.(orDefault:2)} */ orDefault(): T | undefined; /** * {@inheritdoc IResult.getValueOrThrow} * @deprecated Use {@link Failure.(orThrow:1) | orThrow(logger)} or {@link Failure.(orThrow:2) | orThrow(formatter)} instead. */ getValueOrThrow(logger?: IResultLogger): never; /** * {@inheritdoc IResult.getValueOrDefault} * @deprecated Use {@link Failure.(orDefault:1) | orDefault(T)} or {@link Failure.(orDefault:2) | orDefault()} instead. */ getValueOrDefault(dflt?: T): T | undefined; /** * {@inheritdoc IResult.onSuccess} */ onSuccess(__: SuccessContinuation): Result; /** * {@inheritdoc IResult.onFailure} */ onFailure(cb: FailureContinuation): Result; /** * {@inheritdoc IResult.withErrorFormat} */ withErrorFormat(cb: ErrorFormatter): Result; /** * {@inheritdoc IResult.withFailureDetail} */ withFailureDetail(detail: TD): DetailedResult; /** * {@inheritdoc IResult.withDetail} */ withDetail(detail: TD, __successDetail?: TD): DetailedResult; /** * {@inheritdoc IResult.aggregateError} */ aggregateError(errors: IMessageAggregator, formatter?: ErrorFormatter): this; /** * {@inheritdoc IResult.report} */ report(reporter?: IResultReporter, options?: IResultReportOptions): Failure; /** * Get a 'friendly' string representation of this object. * @remarks * The string representation of a {@link Failure} value is the error message. * @returns A string representing this object. */ toString(): string; /** * Creates a {@link Failure | Failure} with the supplied error message. * @param message - The error message to be returned. * @returns The resulting {@link Failure | Failure} with the supplied error message. */ static with(message: string): Failure; } /** * Returns {@link Success | Success} with the supplied result value. * @param value - The successful result value to be returned * @remarks * A `succeeds` alias was added in release 5.0 for * naming consistency with {@link fails | fails}, which was added * to avoid conflicts with test frameworks and libraries. * @public */ export declare function succeed(value: T): Success; /** * {@inheritdoc succeed} * @public */ export declare function succeeds(value: T): Success; /** * Returns {@link Failure | Failure} with the supplied error message. * @param message - Error message to be returned. * @remarks * A `fails` alias was added in release 5.0 due to * issues with the name `fail` being used test frameworks and libraries. * @public */ export declare function fail(message: string): Failure; /** * {@inheritdoc fail} * @public */ export declare function fails(message: string): Failure; /** * Uses a value or calls a supplied initializer if the supplied value is undefined. * @param value - the value * @param initializer - a function that initializes the value if it is undefined * @returns `Success` with the value if it is defined, or the result of calling the initializer function. * @public */ export declare function useOrInitialize(value: T | undefined, initializer: () => Result): Result; /** * Callback to be called when a {@link DetailedResult | DetailedResult} encounters success. * @remarks * A success callback can return a different result type than it receives, allowing * success results to chain through intermediate result types. * @public */ export type DetailedSuccessContinuation = (value: T, detail?: TD) => DetailedResult; /** * Callback to be called when a {@link DetailedResult | DetailedResult} encounters a failure. * @remarks * A failure callback can change {@link DetailedFailure | DetailedFailure} to * {@link DetailedSuccess | DetailedSuccess} (e.g. by returning a default value) * or it can change or embellish the error message, but it cannot change the success return type. * @public */ export type DetailedFailureContinuation = (message: string, detail?: TD) => DetailedResult; /** * A {@link DetailedSuccess | DetailedSuccess} extends {@link Success | Success} to report optional success * details in addition to the error message. * @public */ export declare class DetailedSuccess extends Success { /** * @internal */ protected _detail?: TD; /** * Constructs a new {@link DetailedSuccess | DetailedSuccess} with the supplied * value and detail. * @param value - The value to be returned. * @param detail - An optional successful detail to be returned. If omitted, detail * will be `undefined`. */ constructor(value: T, detail?: TD); /** * The success detail associated with this {@link DetailedSuccess}, or `undefined` if * no detail was supplied. */ get detail(): TD | undefined; /** * Reports that this {@link DetailedSuccess} is a success. * @remarks * Always true for {@link DetailedSuccess} but can be used as type guard * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in * a {@link DetailedResult}. * @returns `true` */ isSuccess(): this is DetailedSuccess; /** * Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates * its returned {@link DetailedResult | DetailedResult}. * @remarks * The success callback mutates the return type from `` to ``. * @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked. * @returns The {@link DetailedResult | DetailedResult} returned by the success callback. */ onSuccess(cb: DetailedSuccessContinuation): DetailedResult; /** * Propagates this {@link DetailedSuccess}. * @remarks * Failure does not mutate return type so we can return this event directly. * @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called * on a {@link DetailedResult} in case of failure (ignored). * @returns `this` */ onFailure(__cb: DetailedFailureContinuation): DetailedResult; /** * {@inheritdoc Success.withErrorFormat} */ withErrorFormat(cb: ErrorFormatter): DetailedResult; /** * {@inheritdoc IResult.report} */ report(reporter?: IResultReporter, options?: IResultReportOptions): DetailedSuccess; /** * Creates a {@link DetailedSuccess | DetailedSuccess} with the supplied value and * optional detail. */ static with(value: T, detail?: TD): DetailedSuccess; /** * Returns this {@link DetailedSuccess} as a {@link Result}. */ get asResult(): Result; } /** * A {@link DetailedFailure | DetailedFailure} extends {@link Failure | Failure} to report optional * failure details in addition to the error message. * @public */ export declare class DetailedFailure extends Failure { /** * @internal */ protected _detail?: TD; /** * Constructs a new {@link DetailedFailure | DetailedFailure} with the supplied * message and detail. * @param message - The message to be returned. * @param detail - The error detail to be returned. */ constructor(message: string, detail?: TD); /** * The error detail associated with this {@link DetailedFailure}. */ get detail(): TD | undefined; /** * Reports that this {@link DetailedFailure} is a failure. * @remarks * Always true for {@link DetailedFailure} but can be used as type guard * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in * a {@link DetailedResult}. * @returns `true` */ isFailure(): this is DetailedFailure; /** * Propagates the error message and detail from this result. * @remarks * Mutates the success type as the success callback would have, but does not * call the success callback. * @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called * on a {@link DetailedResult} in case of success (ignored). * @returns A new {@link DetailedFailure | DetailedFailure} which contains * the error message and detail from this one. */ onSuccess(__cb: DetailedSuccessContinuation): DetailedResult; /** * Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates * its returned {@link DetailedResult | DetailedResult}. * @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked. * @returns The {@link DetailedResult | DetailedResult} returned by the failure callback. */ onFailure(cb: DetailedFailureContinuation): DetailedResult; /** * {@inheritdoc IResult.withErrorFormat} */ withErrorFormat(cb: ErrorFormatter): DetailedResult; /** * {@inheritdoc IResult.aggregateError} */ aggregateError(errors: IMessageAggregator, formatter?: ErrorFormatter): this; /** * {@inheritdoc IResult.report} */ report(reporter?: IResultReporter, options?: IResultReportOptions): DetailedFailure; orThrow(logOrFormat?: IResultLogger | ErrorFormatter): never; orThrow(cb: ErrorFormatter): never; /** * Returns this {@link DetailedFailure} as a {@link Result}. */ get asResult(): Result; /** * Creates a {@link DetailedFailure | DetailedFailure} with the supplied error message * and optional detail. * @param message - The error message to be returned. * @param detail - The error detail to be returned. * @returns The resulting {@link DetailedFailure | DetailedFailure} with the supplied * error message and detail. * @public */ static with(message: string, detail?: TD): DetailedFailure; } /** * Type inference to determine the result type `T` of a {@link DetailedResult | DetailedResult}. * @beta */ export type DetailedResult = DetailedSuccess | DetailedFailure; /** * Type inference to determine the detail type `TD` of a {@link DetailedResult | DetailedResult}. * @beta */ export type ResultDetailType = T extends DetailedResult ? TD : never; /** * Returns {@link DetailedSuccess | DetailedSuccess} with a supplied value and optional * detail. * @param value - The value of type `` to be returned. * @param detail - An optional detail of type `` to be returned. * @returns A {@link DetailedSuccess | DetailedSuccess} with the supplied value * and detail, if supplied. * @remarks * The `succeedsWithDetail` alias was added in release 5.0 for * naming consistency with {@link fails | fails}, which was added to avoid conflicts * with test frameworks and libraries. * @public */ export declare function succeedWithDetail(value: T, detail?: TD): DetailedSuccess; /** * {@inheritdoc succeedWithDetail} * @public */ export declare function succeedsWithDetail(value: T, detail?: TD): DetailedSuccess; /** * Returns {@link DetailedFailure | DetailedFailure} with a supplied error message and detail. * @param message - The error message to be returned. * @param detail - The event detail to be returned. * @returns An {@link DetailedFailure | DetailedFailure} with the supplied error * message and detail. * @remarks * The `failsWithDetail` alias was added in release 5.0 for naming consistency * with {@link fails | fails}, which was added to avoid conflicts with test frameworks and libraries. * @public */ export declare function failWithDetail(message: string, detail?: TD): DetailedFailure; /** * {@inheritdoc failWithDetail} * @public */ export declare function failsWithDetail(message: string, detail?: TD): DetailedFailure; /** * Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied * event details as appropriate. * @param result - The {@link Result} to be propagated. * @param detail - The event detail (type ``) to be added to the {@link Result | result}. * @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail` * is omitted or `undefined`, then `detail` will be applied to {@link Success} results. * @returns A new {@link DetailedResult | DetailedResult} with the success value or error * message from the original `result` but with the specified detail added. * @public */ export declare function propagateWithDetail(result: Result, detail: TD, successDetail?: TD): DetailedResult; /** * Wraps a function which might throw to convert exception results * to {@link Failure}. * @param func - The function to be captured. * @returns Returns {@link Success} with a value of type `` on * success , or {@link Failure} with the thrown error message if * `func` throws an `Error`. * @public */ export declare function captureResult(func: () => T): Result; //# sourceMappingURL=result.d.ts.map