@mado/invariant
v0.1.0
Published
A tiny invariant function
Downloads
1
Readme
@mado/invariant
Installation
npm install @mado/invariant
# or
yarn add @mado/invariant
# or
pnpm add @mado/invariant
Usage
invariant
Basic Usage
import { invariant } from '@mado/invariant';
const creature = { name: 'Dragon', type: 'Fire' };
invariant(creature.name === 'Dragon', 'Creature must be a Dragon');
Using Callback for Error Message
import { invariant } from '@mado/invariant';
const creature = { name: 'Elf', type: 'Forest' };
invariant(creature.type === 'Water', () => 'Creature must be of type Water');
invariantResponse
Basic Usage
import { invariantResponse } from '@mado/invariant';
const creature = { name: 'Phoenix', type: 'Fire' };
invariantResponse(creature.type === 'Fire', 'Creature must be of type Fire');
Using Callback for Response Message
import { invariantResponse } from '@mado/invariant';
const creature = { name: 'Mermaid', type: 'Water' };
invariantResponse(
creature.type === 'Land',
() => `Expected a Land creature, but got a ${creature.type} creature`,
);
Throwing a Response with Additional Options
import { invariantResponse } from '@mado/invariant';
const creature = { name: 'Cerberus', type: 'Underworld' };
invariantResponse(
creature.type === 'Sky',
JSON.stringify({ error: 'Creature must be of type Sky' }),
{ status: 500, headers: { 'Content-Type': 'text/json' } },
);
isInvariantError
import { invariant, isInvariantError } from '@mado/invariant';
try {
const creature = { name: 'Mermaid', type: 'Water' };
invariant(creature.name === 'Dragon', 'Creature must be a Dragon');
} catch (error) {
if (isInvariantError(error)) {
// ...
}
}