@dossierhq/core
Version:
The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.
63 lines (62 loc) • 3.75 kB
TypeScript
import type { LoggerContext } from './Logger.js';
export declare const ErrorType: {
/** Corresponds to 400 Bad Request */
readonly BadRequest: "BadRequest";
/** Corresponds to 409 Conflict */
readonly Conflict: "Conflict";
/** Corresponds to 401 Unauthenticated */
readonly NotAuthenticated: "NotAuthenticated";
/** Corresponds to 403 Forbidden */
readonly NotAuthorized: "NotAuthorized";
/** Corresponds to 404 Not Found */
readonly NotFound: "NotFound";
readonly Generic: "Generic";
};
export type ErrorType = (typeof ErrorType)[keyof typeof ErrorType];
export type Result<TOk, TError extends ErrorType> = OkResult<TOk, TError> | ErrorResult<unknown, TError>;
export type PromiseResult<TOk, TError extends ErrorType> = Promise<Result<TOk, TError>>;
export type OkFromResult<T> = Awaited<T> extends Result<infer TOk, infer _TError> ? TOk : never;
export type ErrorFromResult<T> = Awaited<T> extends Result<infer _TOk, infer TError> ? TError : never;
export declare class OkResult<TOk, TError extends ErrorType> {
readonly value: TOk;
constructor(value: TOk);
isOk(): this is OkResult<TOk, TError>;
isError(): this is ErrorResult<TOk, TError>;
toError(): Error;
throwIfError(): void;
valueOrThrow(): TOk;
map<TNewOk, TNewError extends ErrorType>(mapper: (value: TOk) => TNewOk): Result<TNewOk, TNewError>;
}
export declare class ErrorResult<TOk, TError extends ErrorType> {
readonly error: TError;
readonly message: string;
constructor(error: TError, message: string);
isOk(): this is OkResult<TOk, TError>;
isError(): this is ErrorResult<TOk, TError>;
toError(): Error;
throwIfError(): never;
valueOrThrow(): never;
isErrorType<TSubError extends TError>(errorType: TSubError): this is ErrorResult<TOk, TSubError>;
get httpStatus(): 400 | 401 | 403 | 404 | 409 | 500;
}
export declare class ErrorResultError extends Error {
errorType: ErrorType;
errorMessage: string;
constructor(result: ErrorResult<unknown, ErrorType>);
}
export declare function createErrorResult<TError extends ErrorType>(error: TError, message: string): ErrorResult<unknown, TError>;
export declare function createErrorResultFromError<TError extends ErrorType | typeof ErrorType.Generic>(context: LoggerContext, error: ErrorResultError | unknown, expectedErrorTypes?: TError[] | null): ErrorResult<unknown, TError | typeof ErrorType.Generic>;
export declare function ok<TOk, TError extends ErrorType>(value: TOk): OkResult<TOk, TError>;
export declare const notOk: {
fromHttpStatus: (status: number, message: string) => ErrorResult<unknown, ErrorType>;
BadRequest: (message: string) => ErrorResult<unknown, typeof ErrorType.BadRequest>;
Conflict: (message: string) => ErrorResult<unknown, typeof ErrorType.Conflict>;
Generic: (message: string) => ErrorResult<unknown, typeof ErrorType.Generic>;
GenericUnexpectedError: (result: ErrorResult<unknown, ErrorType>) => ErrorResult<unknown, typeof ErrorType.Generic>;
GenericUnexpectedException: (context: LoggerContext, error: unknown) => ErrorResult<unknown, typeof ErrorType.Generic>;
NotAuthenticated: (message: string) => ErrorResult<unknown, typeof ErrorType.NotAuthenticated>;
NotAuthorized: (message: string) => ErrorResult<unknown, typeof ErrorType.NotAuthorized>;
NotFound: (message: string) => ErrorResult<unknown, typeof ErrorType.NotFound>;
};
export declare function assertOkResult<TOk, TError extends ErrorType>(actual: Result<unknown, ErrorType>): asserts actual is OkResult<TOk, TError>;
export declare function assertErrorResultType(actual: Result<unknown, ErrorType>, expectedErrorType: ErrorType): asserts actual is ErrorResult<unknown, ErrorType>;