Skip to content
/ ajel Public

Ajel is a Typescript Result<T,E> made from a set of thin wrappers around try-catch and an eslint plugin.

Notifications You must be signed in to change notification settings

Handfish/ajel

Repository files navigation

ajel is a set of thin wrappers for try-catch, coupled with eslint rules to make a Typescript Result Type.

NPM Version Package License NPM Downloads

Installation

yarn add ajel eslint-plugin-ajel

pnpm add ajel eslint-plugin-ajel

Basic Example

// Handling async functions that throw
import { ajel } from 'ajel';

async function main() {
  const result = await ajel(Promise.resolve('hello world'));

  // Accessing result before narrowing union type throws eslint error
  // console.log(result);

  if (result instanceof Error) {
    // This return narrows the type of result to its Result
    return;
  }

  return result;
}
// Handling syncronous functions that throw
import { sjel } from 'ajel';

async function main() {
  const result = sjel(JSON.parse, "{}");

  // Accessing result before narrowing union type throws eslint error
  // console.log(result);

  if (result instanceof Error) {
    // This return narrows the type of result to its Result
    return;
  }

  return result;
}

ajel and sjel are thin wrappers for try-catch, coupled with eslint rules to encourage better error handling.

The linting tools are available in the package eslint-plugin-ajel

Basic eslintrc

{
  plugins: ['ajel'],
  extends: [
    'plugin:ajel/recommended',
  ],
}

More Advanced Example

import { ajel, sjel } from 'ajel';

class CustomError extends Error { }
class CustomError2 extends Error { }
class CustomError3 extends Error { }

const foobarFn = async () => {
  const result = await ajel(Promise.resolve('result'));
  const result2 = sjel(JSON.parse, '{}');
  const result3 = await ajel(Promise.resolve('result'));
  const result5 = sjel((stringvar: string) => stringvar, '{}');

  //------ Test2 SJEL
  if (result2 instanceof CustomError) {
    //We can access the error here in BinaryExpression with var instanceof
    console.log(result2);
    return;
  }

  // Cant Access the result2 variable here
  // console.log(result2);

  if (result2 instanceof Error) {
    console.log(result2);
    // This return narrows the type of result2 to its Result
    return;
  }
  // Type is narrowed - no longer a union of Result | Error -> just Result
  console.log(result2);

  //------ Test AJEL
  // Cant Access the result variable here
  // console.log(result);

  switch (true) {
    case result instanceof CustomError3:
      //We can access the error here in BinaryExpression with var instanceof
      console.log(test);
      break;
    //We support fall through
    case result instanceof CustomError2:
    case result instanceof CustomError:
      console.log(result);
      break;
    case result instanceof Error:
      break;
  }
  console.log(result);

  //---- No handling of AJEL and SJEL returns reports Errors
  // console.log(result3);
  // console.log(result5);
};

What's inside this repo?

Apps and Packages

  • docs: A placeholder documentation site powered by Next.js
  • ajel: The core library (function)
  • eslint-plugin-ajel: Eslint rules for proper usage and error handling paradigm
  • benchmarks: Testing ajel's performance against other methods

About

Ajel is a Typescript Result<T,E> made from a set of thin wrappers around try-catch and an eslint plugin.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published