Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

alten-safe

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Issues
File Explorer

Advanced tools

alten-safe

A set of function can that be wrapped around promises to simplify the process of catching errors and handling results.

    0.0.4latest

Version published
Maintainers
1
Yearly downloads
228

Weekly downloads

Readme

Source

Alten Safe

An NPM package which allows users to wrap their promises in a function that handles error handling, and optionally manages type checking.

Examples

Let's start with an easy example, Simply fetching some data:

import { safe } from "alten-safe"; interface Todo { id: number; message: string; } const fetchSomeData = async (): Promise<Todo | undefined> => { const request = await safe<Todo>(axios.get("/my-todo/url/1")); if (request.error || !request.result) { console.log("Woops, looks like something went wrong!"); } return request.result; }

Destructuring the result from safe() may be nicer to read:

const { result, error } = await safe<Todo>(axios.get("/my-todo/url/1"));

Type checking

Let's add some type checking to our request. For this example we'll use some of Typescripts build in typechecking features, but you can use any function that returns a boolean to indicate if an object has passed the type check or not.

import { safe } from "alten-safe"; interface Todo { id: number; message: string; } // Our type checking function. const isATodo = (obj: any): obj is Todo => { return 'id' in obj && 'message' in obj; } const fetchSomeData = async (): Promise<Todo | undefined> => { // We pass the type checking function as the second argument. // If the response for our passed promise does not pass the type check, // the result is empty and the error is filled. const { result, error } = await safe<Todo>(axios.get("/my-todo/url/1"), isATodo); if (error) { console.log("Either the request came back with an error, or the response object did not pass the type check.") return; } return result; }

It is only possible to use this package to perform a type check:

import { isType } from "alten-safe"; interface Todo { id: number; message: string; } // Our type checking function. function isATodo(obj: any): obj is Todo { return 'id' in obj && 'message' in obj; } // The given object will not pass the test. const { result, error } = isType<Todo>({id: 3, message: 27}, isATodo); // The given object will pass the test. const { result, error } = isType<Todo>({id: 3, message: "A real todo"}, isATodo);

Keywords

Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc