ts-error-as-value
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,14 +0,1 @@ | ||
type Success<T> = import(".").Success<T>; | ||
type Failure<E extends Error> = import(".").Failure<E>; | ||
type Result<T, E extends Error> = import(".").Result<T, E>; | ||
declare class ResultIs { | ||
static success: <T>(data: T) => Success<T>; | ||
static failure: <E extends Error>(failure: E) => Failure<E>; | ||
} | ||
declare function withResult<T, E extends Error, R>( | ||
fn: (...args: T[]) => R | ||
): ( | ||
...args: T[] | ||
) => R extends Promise<infer u> ? Promise<Result<u, E>> : Result<R, E>; | ||
export {}; |
@@ -1,22 +0,22 @@ | ||
export declare type Failure<E extends Error = Error> = { | ||
export declare type Failure<T = void, E extends Error = Error> = { | ||
data: null; | ||
error: E; | ||
unwrap(): void; | ||
unwrapOr<D>(defaultValue: D): D; | ||
transformOnFailure<E2 extends Error>(fn: (fail: E) => E2): Failure<E2>; | ||
transformOnSuccess<N>(fn: (data: never) => N): Failure<E>; | ||
successOrThrow(): T; | ||
successOrDefault<D>(defaultValue: D): D; | ||
transformOnFailure<E2 extends Error>(fn: (fail: E) => E2): Failure<T, E2>; | ||
transformOnSuccess<N>(fn: (data: never) => N): Failure<T, E>; | ||
}; | ||
export declare type Success<T> = { | ||
export declare type Success<T = void> = { | ||
data: T; | ||
error: null; | ||
unwrap(): T; | ||
unwrapOr<D>(defaultValue: D): T; | ||
successOrThrow(): T; | ||
successOrDefault<D>(defaultValue: D): T; | ||
transformOnFailure<E2 extends Error>(fn: (fail: never) => E2): Success<T>; | ||
transformOnSuccess<N>(fn: (data: T) => N): Success<N>; | ||
}; | ||
export declare type Result<T, E extends Error = Error> = Failure<E> | Success<T>; | ||
export declare type Result<T = void, E extends Error = Error> = Failure<T, E> | Success<T>; | ||
export declare type ResultIs = { | ||
success<T>(data: T): Success<T>; | ||
failure<E extends Error>(failure: E): Failure<E>; | ||
success<T>(data?: T): Success<T>; | ||
failure<T = void, E extends Error = Error>(failure: E): Failure<T, E>; | ||
}; | ||
export declare const ResultIs: ResultIs; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ResultIs = void 0; | ||
var failure = function (error) { return ({ | ||
data: null, | ||
error: error, | ||
unwrap: function () { | ||
throw error; | ||
}, | ||
unwrapOr: function (defaultValue) { | ||
return defaultValue; | ||
}, | ||
transformOnFailure: function (fn) { | ||
return failure(fn(error)); | ||
}, | ||
transformOnSuccess: function () { | ||
return this; | ||
} | ||
}); }; | ||
var success = function (data) { return ({ | ||
data: data, | ||
error: null, | ||
unwrap: function () { | ||
return data; | ||
}, | ||
unwrapOr: function () { | ||
return data; | ||
}, | ||
transformOnFailure: function () { | ||
return this; | ||
}, | ||
transformOnSuccess: function (fn) { | ||
return success(fn(data)); | ||
} | ||
}); }; | ||
function failure(error) { | ||
return { | ||
data: null, | ||
error: error, | ||
successOrThrow: function () { | ||
throw error; | ||
}, | ||
successOrDefault: function (defaultValue) { | ||
return defaultValue; | ||
}, | ||
transformOnFailure: function (fn) { | ||
return failure(fn(error)); | ||
}, | ||
transformOnSuccess: function () { | ||
return this; | ||
} | ||
}; | ||
} | ||
function success(data) { | ||
if (data === void 0) { data = undefined; } | ||
return { | ||
data: data, | ||
error: null, | ||
successOrThrow: function () { | ||
return data; | ||
}, | ||
successOrDefault: function () { | ||
return data; | ||
}, | ||
transformOnFailure: function () { | ||
return this; | ||
}, | ||
transformOnSuccess: function (fn) { | ||
return success(fn(data)); | ||
} | ||
}; | ||
} | ||
exports.ResultIs = { | ||
@@ -37,0 +42,0 @@ success: success, |
@@ -10,8 +10,2 @@ "use strict"; | ||
}; | ||
var _a = fn(), data = _a.data, error = _a.error; | ||
if (error) { | ||
data; | ||
} | ||
else { | ||
data; | ||
} | ||
var thing = fn().successOrThrow(); |
{ | ||
"name": "ts-error-as-value", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Errors as values in typescript", | ||
@@ -10,3 +10,3 @@ "main": "lib/index.js", | ||
"preinstall": "npx only-allow yarn && npx rimraf lib", | ||
"start": "node --require ts-node/register --require @babel/register src/types-scratch-board.ts", | ||
"start": "node --require ts-node/register --require @babel/register src/index.ts", | ||
"test": "jest", | ||
@@ -13,0 +13,0 @@ "postinstall": "npx tsc && node cp.js" |
export type Failure<E extends Error = Error> = { | ||
export type Failure< | ||
T = void, | ||
E extends Error = Error | ||
> = { | ||
data: null, | ||
error: E, | ||
successOrThrow(): void, // Returns the value, but throws an error if the result is an Error | ||
successOrThrow(): T, // Returns the value, but throws an error if the result is an Error | ||
successOrDefault<D>(defaultValue: D): D, // Returns the value or gives you a default value if it's an error | ||
transformOnFailure<E2 extends Error>(fn: (fail: E) => E2): Failure<E2>, // If the result is an error, map the error to another error | ||
transformOnSuccess<N>(fn: (data: never) => N): Failure<E> // If the result is not an error, map the data in it | ||
transformOnFailure<E2 extends Error>(fn: (fail: E) => E2): Failure<T, E2>, // If the result is an error, map the error to another error | ||
transformOnSuccess<N>(fn: (data: never) => N): Failure<T, E> // If the result is not an error, map the data in it | ||
}; | ||
@@ -23,11 +26,14 @@ | ||
T = void, E extends Error = Error | ||
> = Failure<E> | Success<T>; | ||
> = Failure<T, E> | Success<T>; | ||
function failure<E extends Error>( | ||
function failure< | ||
T = void, | ||
E extends Error = Error | ||
>( | ||
error: E | ||
): Failure<E> { | ||
): Failure<T, E> { | ||
return { | ||
data: null, | ||
error: error, | ||
successOrThrow() { | ||
successOrThrow(): never { | ||
throw error; | ||
@@ -38,6 +44,6 @@ }, | ||
}, | ||
transformOnFailure<E2 extends Error>(fn: (err: E) => E2): Failure<E2> { | ||
return failure<E2>(fn(error)); | ||
transformOnFailure<E2 extends Error>(fn: (err: E) => E2): Failure<T, E2> { | ||
return failure<T, E2>(fn(error)); | ||
}, | ||
transformOnSuccess(): Failure<E> { | ||
transformOnSuccess(): Failure<T, E> { | ||
return this; | ||
@@ -73,3 +79,3 @@ } | ||
success<T>(data?: T): Success<T>, | ||
failure<E extends Error>(failure: E): Failure<E> | ||
failure<T = void, E extends Error = Error>(failure: E): Failure<T, E> | ||
}; | ||
@@ -76,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
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
28217
598