trykit
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

trykit

🪄🪖 Simple magic-like utilities to ease your development experience.

installation

npm install trykit

functions

  • safetry - call your function without wrapping it in a try-catch block, and check if it throws;
  • tryparse - parse data in a schema, without it erroring.
  • retry - retry a function n-times;

safetry

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

parameters

  • callback: Promise<T> | (() => Promise<T> | () => T - function or promise returntype like with infered T;

returns

  • SafeTryResult<T> - result object with success property to indicate if there is an error or you can get the data;

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: TryParseSchema<T> - schema with a parse function in it that returns type T;
  • input: unknown - first argument passed to the schema.parse function, use args for rest parameters;

returns

  • SafeTryResult<T> - see the safetry function;

example

import { tryparse } from "trykit";

const zodSchema = z.object({
  hello: z.string(),
});

const zodResult = tryparse(zodSchema, { bye: "good day" });
const jsonResult = tryparse(JSON, jsonString);

retry

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

parameters

  • callback: Promise<T> | (() => Promise<T> | () => T - function or promise returntype like with infered T;
  • config?: RetryConfig - (optional) configuration for the retries of type RetryConfig;

RetryConfig:

  • attempts?: number - (optional) number of times the callback can be called before returning the error;
  • delay?: number - (optional) delay in milliseconds to wait for the next iteration;
  • factor?: number - (optional) multiply the delay with a factor (default 2);

returns

  • SafeTryResult<T> - see the safetry function;

example

const result = await retry(fetch("/unknown"), { attempts: 5, delay: 50, factor: 1 });

if (!result.success) console.error(result.error.message);
console.log(result.data);

Readme

Keywords

none

Package Sidebar

Install

npm i trykit

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

18.9 kB

Total Files

24

Last publish

Collaborators

  • m10rten