outvariant
Advanced tools
Comparing version 1.2.1 to 1.3.0
export declare class InvariantError extends Error { | ||
readonly message: string; | ||
name: string; | ||
constructor(message: string, ...positionals: any[]); | ||
} | ||
export declare function invariant<T>(predicate: T, message: string, ...positionals: any[]): asserts predicate; | ||
export interface InvariantFunction { | ||
(predicate: unknown, message: string, ...positionals: unknown[]): asserts predicate; | ||
} | ||
export interface CustomErrorConstructor { | ||
new (message: string): Error; | ||
} | ||
export interface CustomErrorFactory { | ||
(message: string): Error; | ||
} | ||
export declare type CustomError = CustomErrorConstructor | CustomErrorFactory; | ||
export declare function createInvariantWith(ErrorConstructor: CustomError): InvariantFunction; | ||
declare function polymorphicInvariant(ErrorClass: CustomError, ...args: Parameters<InvariantFunction>): ReturnType<InvariantFunction>; | ||
export declare const invariant: InvariantFunction & { | ||
as: typeof polymorphicInvariant; | ||
}; | ||
export {}; |
@@ -23,5 +23,18 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.invariant = exports.InvariantError = void 0; | ||
exports.invariant = exports.createInvariantWith = exports.InvariantError = void 0; | ||
var format_1 = require("./format"); | ||
var STACK_FRAMES_TO_IGNORE = 2; | ||
/** | ||
* Remove the "outvariant" package trace from the given error. | ||
* This scopes down the error stack to the relevant parts | ||
* when used in other applications. | ||
*/ | ||
function cleanErrorStack(error) { | ||
if (!error.stack) { | ||
return; | ||
} | ||
var nextStack = error.stack.split('\n'); | ||
nextStack.splice(1, STACK_FRAMES_TO_IGNORE); | ||
error.stack = nextStack.join('\n'); | ||
} | ||
var InvariantError = /** @class */ (function (_super) { | ||
@@ -35,9 +48,6 @@ __extends(InvariantError, _super); | ||
var _this = _super.call(this, message) || this; | ||
_this.message = message; | ||
_this.name = 'Invariant Violation'; | ||
_this.message = format_1.format.apply(void 0, __spreadArray([message], positionals)); | ||
if (_this.stack) { | ||
var nextStack = _this.stack.split('\n'); | ||
nextStack.splice(1, STACK_FRAMES_TO_IGNORE); | ||
_this.stack = nextStack.join('\n'); | ||
} | ||
cleanErrorStack(_this); | ||
return _this; | ||
@@ -48,11 +58,31 @@ } | ||
exports.InvariantError = InvariantError; | ||
function invariant(predicate, message) { | ||
var positionals = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
positionals[_i - 2] = arguments[_i]; | ||
function createInvariantWith(ErrorConstructor) { | ||
var invariant = function (predicate, message) { | ||
var positionals = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
positionals[_i - 2] = arguments[_i]; | ||
} | ||
if (!predicate) { | ||
var resolvedMessage = format_1.format.apply(void 0, __spreadArray([message], positionals)); | ||
var isConstructor = !!ErrorConstructor.prototype.name; | ||
var error = isConstructor | ||
? // @ts-expect-error Construct/call signature too dynamic. | ||
new ErrorConstructor(resolvedMessage) | ||
: // @ts-expect-error Construct/call signature too dynamic. | ||
ErrorConstructor(resolvedMessage); | ||
cleanErrorStack(error); | ||
throw error; | ||
} | ||
}; | ||
return invariant; | ||
} | ||
exports.createInvariantWith = createInvariantWith; | ||
function polymorphicInvariant(ErrorClass) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
if (!predicate) { | ||
throw new (InvariantError.bind.apply(InvariantError, __spreadArray([void 0, message], positionals)))(); | ||
} | ||
return createInvariantWith(ErrorClass).apply(void 0, args); | ||
} | ||
exports.invariant = invariant; | ||
exports.invariant = createInvariantWith(InvariantError); | ||
exports.invariant.as = polymorphicInvariant; |
{ | ||
"name": "outvariant", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Type-safe implementation of invariant with positionals.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -96,4 +96,52 @@ # `outvariant` | ||
## Polymorphic errors | ||
It is possible to throw a custom `Error` instance using `invariant.as`: | ||
```js | ||
import { invariant } from 'outvariant' | ||
class NetworkError extends Error { | ||
constructor(message) { | ||
super(message) | ||
} | ||
} | ||
invariant.as(NetworkError, res.fulfilled, 'Failed to handle response') | ||
``` | ||
Note that providing a custom error constructor as the argument to `invariant.as` requires the custom constructor's signature to be compatible with the `Error` class constructor. | ||
If your error constructor has a different signature, you can pass a function as the first argument to `invariant.as` that creates a new custom error instance. | ||
```js | ||
import { invariant } from 'outvariant' | ||
class NetworkError extends Error { | ||
constructor(statusCode, message) { | ||
super(message) | ||
this.statusCode = statusCode | ||
} | ||
} | ||
invariant.as( | ||
(message) => new NetworkError(500, message), | ||
res.fulfilled, | ||
'Failed to handle response' | ||
) | ||
``` | ||
Abstract the error into helper functions for flexibility: | ||
```js | ||
function toNetworkError(statusCode) { | ||
return (message) => new NetworkError(statusCode, message) | ||
} | ||
invariant.as(toNetworkError(404), res?.user?.id, 'User Not Found') | ||
invariant.as(toNetworkError(500), res.fulfilled, 'Internal Server Error') | ||
``` | ||
## Contributing | ||
Please [open an issue](https://github.com/open-draft/outvariant/issues) or [submit a pull request](https://github.com/open-draft/outvariant/pulls) if you wish to contribute. Thank you. |
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
11719
180
147