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

@rotomeca/rop

v5.0.2

Published

A robust, lightweight, and type-safe implementation of Railway Oriented Programming (ROP) for TypeScript.

Downloads

895

Readme

🚂 TS Railway Result

A robust, lightweight, and type-safe implementation of Railway Oriented Programming (ROP) for TypeScript. Eliminate try/catch spaghetti code and handle errors elegantly using Results and Decorators.

TypeScript License

📦 Installation

npm install @rotomeca/rop
# or
yarn add @rotomeca/rop

🚀 Quick Start

Transform your risky operations into safe "Rails" using decorators.

import { Risky, HappyPath, ErrorPath, Result } from '@rotomeca/rop';

class UserService {
  
  @ErrorPath((err) => console.error(`[Admin Alert] Failed to create user: ${err.message}`))
  @HappyPath((user) => console.log(`[Analytics] User created: ${user.name}`))
  @Risky() // Automatically catches exceptions and converts return value to Result
  async createUser(name: string, age: number): Promise<Result<{ name: string }>> {
    
    if (name.length < 3) {
      throw new Error("Name too short"); // Will be converted to Fail(Error)
    }

    // Simulate DB call
    return { name }; // Will be converted to Success({ name })
  }
}

// Usage
async function main() {
  const service = new UserService();
  
  const result = await service.createUser("Alice", 25);

  result.match({
    Ok: (user) => console.log(`Welcome ${user.name}!`),
    Err: (error) => console.log(`Oops: ${error.message}`)
  });
}

✨ Features

  • 🛡️ Type-Safe Error Handling: No more guessing if a function throws. Returns Result<T, E>.
  • ✨ Powerful Decorators:
    • @Risky: Wraps methods to catch exceptions and return Success or Fail.
    • @HappyPath: Execute side effects (logging, events) only on success.
    • @ErrorPath: Execute side effects (monitoring, alerts) only on failure.
  • ⛓️ Fluent API: Chain operations with map, andThen (flatMap), and unwrapOr.
  • 🔗 Sync & Async Support: Works seamlessly with both synchronous functions and Promises.

📖 Core Concepts

1. The Result Object (Result)

Instead of throwing exceptions, your functions return an Result. It has two states:

  • Success<T>: The operation succeeded and contains a value of type T.
  • Fail<E>: The operation failed and contains an error of type E (default is Error).
import { Success, Fail, Result } from '@rotomeca/rop';

function divide(a: number, b: number): Result<number> {
  if (b === 0) {
    return Fail.Create(new Error("Cannot divide by zero"));
  }
  return Success.Create(a / b);
}

2. Matching (Pattern Matching)

Force yourself to handle both success and failure cases.

const result = divide(10, 2);

const message = result.match({
  Ok: (val) => `Result is ${val}`,
  Err: (err) => `Calculation failed: ${err.message}`
});

3. Decorators

@Risky()

Automatically wraps the method execution in a try/catch block.

  • If the method returns a value, it becomes Success(value).
  • If the method throws, it becomes Fail(error).
  • Works with async/await and synchronous code.

@RiskyPath()

Same as Risky, but use it with ErrorPath and HappyPath.

@HappyPath(fn) & @ErrorPath(fn)

Perfect for Side Effects (logging, notifications) without polluting your business logic. These decorators act as "Taps": they inspect the result but do not modify the return value.

@ErrorPath((e) => Sentry.captureException(e)) // Only if fails
@HappyPath((data) => Analytics.track('success', data)) // Only if success
@RiskyPath()
saveData(data: any) { ... }

Note: To use the decorators, ensure you have disabled experimentalDecorators in your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": false
  }
}

📚 API Reference

Result<T, E> Methods

| Method | Description | | :--- | :--- | | match({ Ok, Err }) | Executes Ok fn if success, Err fn if failure. Returns the result of the function. | | map(fn) | Transforms the inner value T to U only if Success. | | mapError(fn) | Transforms the inner error E to NewError only if Fail. | | andThen(fn) | Chains operations. fn must return a new Result. (Often called flatMap). | | unwrapOr(default) | Returns the value if Success, otherwise returns default. | | throwIfError() | Unsafe. Returns value if Success, throws the error if Fail. | | isSuccess() / isError() | Boolean checks for the state. |

🧩 Advanced Usage: Chaining

You can build complex pipelines that stop at the first error:

const result = validateInput(input)           // returns Result<Input>
  .andThen(parsed => processData(parsed))     // returns Result<Data>
  .andThen(data => saveToDb(data))            // returns Result<Id>
  .map(id => `Created object with ID: ${id}`); // returns Result<string>

// If any step failed, 'result' is a Fail with the error of that step.
// If all succeeded, 'result' is a Success with the final string.

📄 License

MIT