Skip to content
/ trykit Public

πŸͺ„πŸͺ– Simple magic-like utilities to ease your development experience.

License

Notifications You must be signed in to change notification settings

m10rten/trykit

Repository files navigation

NPM Version NPM Downloads NPM License Bundle Size Type Definitions

trykit πŸͺ„πŸͺ–

Simple magic-like utilities to ease your development experience.

Installation

npm install trykit

Documentation

For more in depth documentation about the features, check out the full docs.


Functions & Classes

  • safetry: Call a function without wrapping in try-catch. Detects and handles errors.
  • tryparse: Parse data with a schema without throwing.
  • retry: Retry a function up to n times.
  • tryto: Evaluate input with fallback on error.
  • snag: Chainable error-handling for promises.
  • pipeline: Chain and execute multiple functions.

safetry

safetry<T>(callback: Promise<T> | (() => Promise<T>) | (() => T)): SafeTryResult<T>

  • Parameters:
    • callback: Function or promise to execute.
  • Returns:
    • SafeTryResult<T> with success flag and result or error.

Example:

import { safetry } from "trykit";

const result = await safetry(fetch("/hello"));
if (!result.success) console.error(result.error.message);
console.log(result.data);

tryparse

tryparse<T>(schema: TryParseSchema<T>, input: unknown, ...args: unknown[]): SafeTryResult<T>

  • Parameters:
    • schema: Schema with a parse method.
    • input: Data to parse.
  • Returns:
    • SafeTryResult<T> with parsed data or error.

Example:

import { tryparse } from "trykit";

const zodSchema = z.object({ hello: z.string() });
const zodResult = tryparse(zodSchema, { bye: "good day" });

retry

retry<T>(callback: Promise<T> | (() => Promise<T>), config?: RetryConfig): SafeTryResult<T>

  • Parameters:
    • callback: Function or promise to retry.
    • config: Retry settings (attempts, delay, factor).
  • Returns:
    • SafeTryResult<T> with final result or error.

Example:

const result = await retry(fetch("/unknown"), { attempts: 5, delay: 50 });
if (!result.success) console.error(result.error.message);
console.log(result.data);

tryto

tryto<T, D>(input: (() => T) | T, fallback: D | (() => D)): T | D

  • Parameters:
    • input: Value or function to evaluate.
    • fallback: Fallback value or function.
  • Returns:
    • T | D (result or fallback).

Example:

import { tryto } from "trykit";

const result = tryto(() => JSON.parse("invalid json"), "fallback");
console.log(result); // 'fallback'

pipeline

Chain multiple synchronous or asynchronous functions.

  • Methods:
    • .pipe: Add functions to the chain.
    • .execute: Run the chain.

Example:

import { pipeline } from "trykit";

const result = pipeline((n) => n + 1)
  .pipe((n) => n * 2)
  .execute(5);
console.log(result); // 12

snag

Chainable error handling utility.

  • Parameters:
    • promise: Operation to execute.
    • errorType: Specific error class to catch.
    • handler: Function to handle the error.

Example:

import { snag } from "trykit";

class DatabaseError extends Error {}
const result = await snag(fetchData)
  .on(DatabaseError, (err) => "Cached Data")
  .execute("some-id");

About

πŸͺ„πŸͺ– Simple magic-like utilities to ease your development experience.

Resources

License

Stars

Watchers

Forks

Packages

No packages published