1.0.11 • Published 4 months ago

returnit v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

returnit

Stop throwing errors, and return type safe results!

Features

  • Inferred const types without any explicit returns!
  • Golang style returns

Notes

  • Please have strictNullChecks to true in your tsconfig.json

Install

  • npm i returnit

How to use

import { Ok, Err } from 'returnit'

export const login = (username: string, password: string, ip: string) => {
  const dbUser = await db.getUser(username)
  if(!dbUser) {
    return Err('WRONG_PASSWORD')
  }

  const isEqual = compareHash(dbUser.password, password)

  if(!isEqual) {
    return Err('WRONG_PASSWORD')
  }

  if(loginRateLimited(username, ip)) {
    return Err('RATE_LIMITED')
  }

  return Ok('SUCCESS')
}


// ...

const [result, err] = await login('user', 'password', 127.0.0.0)

if(err) {
  /* 
    Type narrowing means the will be automatically typed
    result equals type: undefined
    err equals a string literal type union:  WRONG_PASSWORD' | 'RATE_LIMITED'
  */
} else {
  /*
    result is a string literal union. 'SUCCESS'
    err equals type undefined
  */
}

All

Works similar to Promise.all, it accepts returnit type functions and executes them concurrently. It will return immediately if one of them returns an error, otherwise will return an array of the successful result.

import { Ok, Err, All } from 'returnit'


const [result, err] = await All([returnIt1(),returnIt2()])

if(err) {
  /* 
    err returns the first error encountered.
  */
} else {
  /*
    result is an array of the success results
  */
}
1.0.9

4 months ago

1.0.11

4 months ago

1.0.10

4 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago