πŸ”¨wrap

wrap can secure your functions. This means even if your functions throw errors, it will still be safe to call them without worrying that they will cause your program to crash.

A simple example of wrapping a synchronous function

import { wrap } from 'unwrapit'

const tryParseJson = wrap(() => JSON.parse(`{"package": "unwrapit!"}`))
const json = tryParseJson()
if (!json.ok) {
    return json.error
}
console.log(json.value)

If you are sure that the wrapped function won't panic anyway, or just for debug usage, you can call unwrap method to force unwrap the value.

tryParseJson().unwrap()

This will unwrap the value without checking whether it will throw an error or not.

You can wrap an asynchronous function as well.

import {wrap} from 'unwrapit'

const fetchWrapper = wrap(fetch)
const ret = (await fetchWrapper('www.google.com')).unwrap()
const text = await ret.text()

Note that you need to await or call then to resolve the Promise result first because we don't know if the function will throw errors or not without executing it.

Type

wrap will infer the given function type and return it as is except wrapping the return type of the given function into Result.

For asynchronous functions, the return type looks like below

Specify Error Type

By default, wrap won't know what error type the function is going to throw, so it will infer it as unknown, so that you can cast it when you are handling it. However, if you are developing a common function that want to share with others, you probably need to specify the specific error type. With the wrap function type definition, you can specify the error type easily.

Let's say we have a function with a signature below

It will infer the wrapped type like below

We want to specify the error type as Error. Just do below

Now the inferred type will be

Specify Both Error & Return Type

Sometimes, the function we wrapped may return any, we want to narrow down the return type to make it more precise. You may think of casting the function to be wrapped to the desired signature. Well, with wrap function type, we can just specify the return type like below

Note that the given returned type should be a subtype of the function's return type, otherwise, TypeScript will complain.

Last updated