New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-error-as-value

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-error-as-value - npm Package Compare versions

Comparing version 0.4.206 to 0.4.207

lib/partition-results.d.ts

60

lib/globals.d.ts

@@ -1,1 +0,59 @@

export {};
type Success<T> = import(".").Success<T>;
type Failure<T, E extends Error> = import(".").Failure<T, any extends infer u ? u : never, E>;
type Result<T = void, E extends Error = Error> = import(".").Result<T, E>;
declare function ok<T = void>(data?: T): Success<T>;
declare function err<E extends Error>(error: E): Failure<E>;
// For backwards compatibility
declare class ResultIs {
static success: <T = void>(data?: T) => Success<T>;
static failure: <E extends Error>(failure: E) => Failure<E>;
}
/**
* @desc Function which wraps another function and returns a new function that has the same argument types as the wrapped function.
* This new function will return a Fail result if the wrapped function throws an error, and returns an Ok result if the wrapped function does not.
* @param fn The wrapped function
* @param fnContext (optional) the context to execute the wrapped function in (for e.g. if it's a class method and needs the instance's properties to function)
*
* @example
const { data: bufferFromUTF, error } = withResult(Buffer.from)([], "utf-8");
if (error) {
return ResultIs.failure(error);
}
console.log(bufferFromUTF);
*/
declare function withResult<
E extends Error,
F extends ((...args: any[]) => any)
>(
fn: F,
fnContext?: ThisParameterType<F>
): (
...args: Parameters<F>
) => ReturnType<F> extends Promise<infer u> ? Promise<Result<u, E>> : Result<ReturnType<F>, E>;
interface PartitionedResults<T, E extends Error = Error> {
data: T[],
errors: AggregateError | E | null
}
declare function partitionResults<T, E extends Error>(
results: Result<T, E>[]
): PartitionedResults<T, E>;
declare function partitionResults<T, E extends Error>(
results: Promise<Result<T, E>[]>
): Promise<PartitionedResults<T, E>>;
declare function partitionResults<T, E extends Error>(
results: Promise<Result<T, E>>[]
): Promise<PartitionedResults<T, E>>;
declare function partitionResults<T, E extends Error>(
results: Result<T, E>[]
): PartitionedResults<T, E>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _1 = require(".");
var with_result_1 = require("./with-result");
const _1 = require(".");
const with_result_1 = require("./with-result");
const partition_results_1 = require("./partition-results");
const ResultIs = {
success: (...args) => {
console.warn("ResultIs.success is deprecated and will be removed in a later update. Use ok instead.");
return _1.ok(...args);
},
failure: (...args) => {
console.warn("ResultIs.failure is deprecated and will be removed in a later update. Use err instead.");
return _1.err(...args);
}
};
if (typeof window !== "undefined") {
window.ResultIs = _1.ResultIs;
window.err = _1.err;
window.ok = _1.ok;
window.ResultIs = ResultIs; // For backwards compatibility
window.withResult = with_result_1.withResult;
window.partitionResults = partition_results_1.partitionResults;
}
else {
globalThis.ResultIs = _1.ResultIs;
globalThis.err = _1.err;
globalThis.ok = _1.ok;
globalThis.ResultIs = ResultIs; // For backwards compatibility
globalThis.withResult = with_result_1.withResult;
globalThis.partitionResults = partition_results_1.partitionResults;
}

11

lib/index.d.ts

@@ -0,1 +1,2 @@

export { withResult } from "./with-result";
export declare type Failure<T = void, E extends Error = Error> = {

@@ -18,6 +19,8 @@ data: null;

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<T = void, E extends Error = Error>(failure: E): Failure<T, E>;
export declare function err<T = void, E extends Error = Error>(error: E): Failure<T, E>;
export declare function ok(): Success;
export declare function ok<T>(data?: T): Success<T>;
export declare const ResultIs: {
success: typeof ok;
failure: typeof err;
};
export declare const ResultIs: ResultIs;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResultIs = void 0;
function failure(error) {
exports.ResultIs = exports.ok = exports.err = exports.withResult = void 0;
var with_result_1 = require("./with-result");
Object.defineProperty(exports, "withResult", { enumerable: true, get: function () { return with_result_1.withResult; } });
function err(error) {
return {
data: null,
error: error,
successOrThrow: function () {
successOrThrow() {
throw error;
},
successOrDefault: function (defaultValue) {
successOrDefault(defaultValue) {
return defaultValue;
},
transformOnFailure: function (fn) {
return failure(fn(error));
transformOnFailure(fn) {
return err(fn(error));
},
transformOnSuccess: function () {
transformOnSuccess() {
return this;

@@ -22,24 +24,25 @@ }

}
function success(data) {
if (data === void 0) { data = undefined; }
exports.err = err;
function ok(data = undefined) {
return {
data: data,
error: null,
successOrThrow: function () {
successOrThrow() {
return data;
},
successOrDefault: function () {
successOrDefault() {
return data;
},
transformOnFailure: function () {
transformOnFailure() {
return this;
},
transformOnSuccess: function (fn) {
return success(fn(data));
transformOnSuccess(fn) {
return ok(fn(data));
}
};
}
exports.ok = ok;
exports.ResultIs = {
success: success,
failure: failure
success: ok,
failure: err
};
import { Result } from "./index";
export declare const isPromise: <T>(value: T | Promise<T>) => value is Promise<T>;
/** Function which wraps another function and returns a Fail result if the wrapped function throws an error,
* and returns an Ok result if the wrapped function does not. */
export declare const withResult: <T, E extends Error, R>(fn: (...args: T[]) => R) => (...args: T[]) => R extends Promise<infer u> ? Promise<Result<u, E>> : Result<R, E>;
/**
* @desc Function which wraps another function and returns a new function that has the same argument types as the wrapped function.
* This new function will return a Fail result if the wrapped function throws an error, and returns an Ok result if the wrapped function does not.
* @param fn The wrapped function
* @param fnContext (optional) the context to execute the wrapped function in (for e.g. if it's a class method and needs the instance's properties to function)
*
* @example
const { data: bufferFromUTF, error } = withResult(Buffer.from)([], "utf-8");
if (error) {
return ResultIs.failure(error);
}
console.log(bufferFromUTF);
*/
export declare const withResult: <T, E extends Error, R>(fn: (...args: T[]) => R, fnContext?: any) => (...args: T[]) => R extends Promise<infer u> ? Promise<Result<u, E>> : Result<R, E>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withResult = exports.isPromise = void 0;
var index_1 = require("./index");
var isPromise = function (value) {
return value.then != null;
};
exports.isPromise = isPromise;
/** Function which wraps another function and returns a Fail result if the wrapped function throws an error,
* and returns an Ok result if the wrapped function does not. */
var withResult = function (fn) { return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
exports.withResult = void 0;
const index_1 = require("./index");
const utils_1 = require("./utils");
/**
* @desc Function which wraps another function and returns a new function that has the same argument types as the wrapped function.
* This new function will return a Fail result if the wrapped function throws an error, and returns an Ok result if the wrapped function does not.
* @param fn The wrapped function
* @param fnContext (optional) the context to execute the wrapped function in (for e.g. if it's a class method and needs the instance's properties to function)
*
* @example
const { data: bufferFromUTF, error } = withResult(Buffer.from)([], "utf-8");
if (error) {
return ResultIs.failure(error);
}
console.log(bufferFromUTF);
*/
const withResult = (fn, fnContext) => (...args) => {
try {
var data = fn.bind(fn).apply(void 0, args);
if ((0, exports.isPromise)(data)) {
const data = fnContext ? fn.apply(fnContext, args) : fn(...args);
if ((0, utils_1.isPromise)(data)) {
return data
.then(function (value) { return index_1.ResultIs.success(value); })
.catch(function (e) { return index_1.ResultIs.failure(e); });
.then(value => (0, index_1.ok)(value))
.catch(e => (0, index_1.err)(e));
}
return index_1.ResultIs.success(data);
return (0, index_1.ok)(data);
}
catch (error) {
var e = error instanceof Error ? error : new Error("Unknown error");
return index_1.ResultIs.failure(e);
const e = error instanceof Error ? error : new Error("Unknown error");
return (0, index_1.err)(e);
}
}; };
};
exports.withResult = withResult;
{
"name": "ts-error-as-value",
"version": "0.4.206",
"version": "0.4.207",
"description": "Errors as values in typescript",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

type Success<T> = import(".").Success<T>;
type Failure<E extends Error> = import(".").Failure<any extends infer u ? u : never, E>;
type Failure<T, E extends Error> = import(".").Failure<T, any extends infer u ? u : never, E>;
type Result<T = void, E extends Error = Error> = import(".").Result<T, E>;

@@ -5,0 +5,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc