npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alten-safe

v0.0.4

Published

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

Downloads

4

Readme

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);