Socket
Socket
Sign inDemoInstall

alten-safe

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    alten-safe

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


Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Install size
8.58 kB
Created
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

FAQs

Last updated on 02 May 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc