All files Result.ts

72.97% Statements 27/37
25% Branches 2/8
66.66% Functions 18/27
72.97% Lines 27/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136  1x 1x 1x     1x 1x     1x     3x     1x     2x     1x                                     1x               1x     2x 2x     5x     4x     2x     6x     2x           2x               1x               1x     4x           5x           1x     2x             1x                                        
import {z} from "zod"
import type {TSuccessResultSchema, TFailureResultSchema, ISuccessResult, IFailureResult, IResult} from "./Result.spec"
 
export class SuccessResult<R = unknown> implements ISuccessResult<R> {
	private constructor(private readonly _value: R) {
		Object.freeze(this)
	}
 
	get value(): R {
		return this._value
	}
	get isOk(): true {
		return true
	}
	get isSuccess(): true {
		return true
	}
	get isError(): false {
		return false
	}
	get isFailure(): false {
		return false
	}
 
	private static _serializedRawShape<R, RS extends z.ZodType<R> = z.ZodType<R>>(resultTypeSchema: RS): z.ZodRawShape {
		return {
			v: resultTypeSchema,
			error: z.never(),
			isOk: z.literal(true).optional(),
			isSuccess: z.literal(true).optional(),
			isError: z.literal(false).optional(),
			isFailure: z.literal(false).optional(),
		}
	}

	static serializedSchema<R, RS extends z.ZodType<R> = z.ZodType<R>>(
		resultTypeSchema: RS,
	): z.ZodObject<ReturnType<typeof SuccessResult._serializedRawShape<RS>>> {
		return z.object(SuccessResult._serializedRawShape<R>(resultTypeSchema))
	}
 
	static validateSerialized<R, RS extends z.ZodType<R> = z.ZodType<R>>(
		j: any,
		resultTypeSchema: RS,
	): j is TSuccessResultSchema<RS> {
		return SuccessResult.serializedSchema(resultTypeSchema).safeParse(j).success
	}
 
	static of<R = unknown>(value: R): ISuccessResult<R> {
		return new SuccessResult<R>(value)
	}
 
	static fromSerialized<R = unknown, RS extends z.ZodType<R> = z.ZodType<R>>(
		j: any,
		resultTypeSchema: RS,
	): IResult<ISuccessResult<R>> {
		return SuccessResult.validateSerialized<R, RS>(j, resultTypeSchema)
			? SuccessResult.of(SuccessResult.of<R>(j.v))
			: FailureResult.of(`Invalid serialized result: ${JSON.stringify(j)}`)
	}
}
 
export class FailureResult implements IFailureResult {
	private constructor(private readonly _error: Error) {
		Object.freeze(this)
	}
 
	get error(): Error {
		return this._error
	}
	get isOk(): false {
		return false
	}
	get isSuccess(): false {
		return false
	}
	get isError(): true {
		return true
	}
	get isFailure(): true {
		return true
	}
 
	static readonly serializedSchema = z.object({
		v: z.never(),
		error: z.string(),
		isOk: z.literal(false).optional(),
		isSuccess: z.literal(false).optional(),
		isError: z.literal(true).optional(),
		isFailure: z.literal(true).optional(),
	})
 
	static validateSerialized(j: any): j is TFailureResultSchema {
		return FailureResult.serializedSchema.safeParse(j).success
	}

	static of(err: string | Error): IFailureResult {
		return new FailureResult(typeof err === "string" ? new Error(err) : err)
	}
 
	static fromSerialized(j: any): IResult<IFailureResult> {
		return FailureResult.validateSerialized(j)
			? SuccessResult.of(FailureResult.of(j.error))
			: FailureResult.of(`Invalid serialized result: ${JSON.stringify(j)}`)
	}
}
 
export class Result {
	static isOk<R>(r: IResult<R>): r is ISuccessResult<R> {
		return r.isOk
	}
	static isSuccess<R>(r: IResult<R>): r is ISuccessResult<R> {
		return r.isOk
	}
 
	static isError<R>(r: IResult<R>): r is IFailureResult {
		return r.isError
	}
	static isFailure<R>(r: IResult<R>): r is IFailureResult {
		return r.isError
	}
 
	static success<R>(value: R): IResult<R> {
		return SuccessResult.of<R>(value)
	}
 
	static fail<R = unknown>(err: string | Error): IResult<R> {
		return FailureResult.of(err)
	}
 
	static failIn<R = unknown>(where: string, err: string | Error): IResult<R> {
		const msg = typeof err === "string" ? err : err.message
		return FailureResult.of(`${where}: ${msg}`)
	}
}