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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@minimal-effort/safe-try

v1.2.0

Published

Safe-try is a Typescript type-safe error handling library for both sync and async operations

Readme

Basic Usage

import { Try } from "@minimal-effort/safe-try"

// Synchronous operations
const { data, error } = Try.catch(() => JSON.parse('{"name": "Asep"}'))
if (error) {
    console.error("Parse failed:", error)

    if (error instanceof Error) {
        // handle error
    }
}

console.log("Parsed data:", data) // { name: "Asep" }

// Asynchronous operations
const fetchUser = async () => {
    const result = await fetch('/api')
    if (!result.ok) throw new Error('Api Error')
    return result
}

const { data, error } = await Try.catch(async () => fetchUser())
if (error) {
    console.error("failed:", error)
    // Handle error
}

const { data, error } = await Try.catch(fetchUser)
if (error) {
    console.error("failed:", error)
    // Handle error
}

Advanced Configuration

// Resilient API call with timeout, retry, and exponential backoff
const result = await Try.catch(fetchUser)
    .withTimeout(5000)                          // 5 second timeout
    .withRetry(3)                              // Retry up to 3 times
    .withBackoff({                             // Exponential backoff
        baseDelayMs: 1000,                       // Start with 1 second
        exponential: true,                       // Double delay each retry
        maxDelayMs: 10000,                       // Cap at 10 seconds
        jitter: true                             // Add randomness
    })

if (result.error) {
    console.error("Operation failed after all retries:", result.error)
    // handle error
}

console.log("Success:", result.data)

Try.sleep(ms)

Utility method for creating delays.

await Try.sleep(1000) // Wait 1 second

File Operations

import { readFile } from 'fs/promises'

const result = await Try.catch(async () => readFile('config.json', 'utf-8'))
    .withRetry(2)

const config = result.error
    ? getDefaultConfig()
    : JSON.parse(result.data)

Parsing Operations

function parseJsonSafely(jsonString: string) {
    const result = Try.catch(() => JSON.parse(jsonString))

    return result.error
        ? { valid: false, error: result.error.message }
        : { valid: true, data: result.data }
}

Advanced Patterns

Chaining Operations

async function processUserData(userId: string) {
  // Fetch user
    const userResult = await Try.catch(async () => fetchUser(userId))
        .withTimeout(5000)
        .withRetry(2)

  if (userResult.error) return userResult

  // Process user data
  const processResult = Try.catch(() => processUser(userResult.data))
  if (processResult.error) return processResult

  // Save processed data
  const saveResult = await Try.catch(async () => saveUser(processResult.data))
        .withRetry(3)

  return saveResult
}

Custom Error Types

class ApiError extends Error {
    constructor(public status: number, message: string) {
        super(message)
    }
}

const result = await Try.catch<User, ApiError>(fetchUser)
if (result.error) {
    console.error(`API Error ${result.error.status}: ${result.error.message}`)
    // handle error
}

Star ⭐ this repo if you find it useful!