Skip to main content

guarded()

guarded<In, Out>(fn, check): (input) => Result<Out, string>

Guarded proxy - checks access before calling the function. The check returns true to allow, or a rejection reason string to deny. Returns a zygos Result: Ok(output) if allowed, Err(reason) if denied.


Type Parameters

In: In

The input type

Out: Out

The output type


Parameters

fn: (input) => Out

The function to guard

check: (input) => string | true

Access check. Return true to allow, or a string reason to deny.


Returns

A proxy returning Result<Out, string>


Since

2.4.0


Example

const deleteUser = guarded(
(id: string) => db.delete(id),
(id) => id !== "admin" ? true : "Cannot delete admin user",
);

deleteUser("user-1"); // Ok(void)
deleteUser("admin"); // Err("Cannot delete admin user")