1.0.21 • Published 1 year ago

easy-error-ts v1.0.21

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

EasyError 🚨

EasyError is a lightweight TypeScript library for handling Either types and custom errors design for express. manage your errors without importing or drilling with your res :)

Project Prerequisites ⚙️

  • Node.js
  • npm
  • Visual Studio Code

Installation 📦

npm install easy-error-ts

Usage 🚀

Importing 🔗

import { EasyCustomError, EasyLeft, EasyRight, EasyEither, left, right } from 'easy-error-ts';

Creating Either instances 🔧

Creating a Left instance

const leftValue: EasyLeft<number> = left(42);

Creating a Right instance

const rightValue: EasyRight<EasyCustomError> = right(500, 'Internal Server Error');

Working with Either instances 🛠️

easy to use contains only an EasyEither type for your return and a left and right functions the left is for the good result and the right is for the error. EasyEither is only a type for your return or others.

const fetchMyData = async () : Promise<EasyEither<unknown>> => {
 try {
    const req = await fetch(
        `https://toto.com/api`
    );
    if (!req.ok) {
      return right(500 , `Error message`)
    }
    return left(await req.json())
  } catch (error) {
    return right(500 , `Error message`)
  }
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<unknown | CustomError>
) {

  const dataOrError = await fetchMyData();

  const [data, error] = dataOrError;
  return data !== undefined
    ? res.status(200).json({ data })
    : res.status(error?.code ?? 500).json(error ?? { message: 'Internal Server Error' });
}

Contributing 🤝

We welcome contributions! Feel free to open issues or pull requests.

License 📝

This project is licensed under the MIT License - see the LICENSE.md file for details.

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago