Socket
Book a DemoInstallSign in
Socket

@mado/invariant

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@mado/invariant

A tiny invariant function

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

@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)) {
    // ...
  }
}

Keywords

invariant

FAQs

Package last updated on 11 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts