
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

A practical Result type inspired by Rust Result.
Errors are normal in an applications lifecycle, they should be regular values and we should be able to know them by looking at function type signatures.
try/catch should be reserved to unexpected events recovery.
npm i okerr
import { Ok, Err } from "okerr";
or
import "okerr/globals";
will import the Ok and Err global functions, Result type and add the toResult method to Promises.
import { Ok, Err } from "okerr";
// import 'okerr/globals';
enum ValidationErrors {
NameEmpty = "NameEmpty",
EmailEmpty = "EmailEmpty",
}
interface Input {
name: string;
email: string;
}
// function validate(input: Input): Err<ValidationErrors> | Ok<Input>
function validate(input: Input) {
if (!input.name) {
return Err(ValidationErrors.NameEmpty);
}
if (!input.email) {
return Err(ValidationErrors.EmailEmpty);
}
return Ok(input);
}
catch exceptions from a Promise into a Promise<Ok | Err>
import { toResult } from 'okerr';
// or
import 'okerr/globals';
async function someAsyncFunction(value: string): string {
...
}
const result = await toResult<ApiErrors>(someAsyncFunction(value));
// or
const result = await toResult(someAsyncFunction(value), e => e as ApiErrors);
// or if import 'okerr/globals'
const result = await someAsyncFunction(value).toResult<ApiErrors>();
// result: Ok<string> | Err<ApiErrors>
naturally bubble errors up the callstack until you want to deal with them.
notice how the following function do not throw and instead describe precisely all errors it might return without any visible error handling.
// function getItemsFromApi(input: Input): Promise<Err<ValidationErrors> | Ok<string> | Err<ApiErrors>>
async function getItemsFromApi(input: Input) {
const validateResult = validate(input);
// validateResult: Err<ValidationErrors> | Ok<Input>
const apiCallResult = await validateResult.mapOk(async (value) => {
return await someAsyncFunction(value).toResult<ApiErrors>();
});
// apiCallResult: Err<ValidationErrors> | Ok<string> | Err<ApiErrors>
return apiCallResult;
}
const result = validate({ name: "John", email: "john@email.com" });
// result: Err<ValidationErrors> | Ok<Input>
if (result.isErr()) {
// result.error: ValidationErrors
toast(translate(result.error));
return;
}
// result.data: Input
console.log(result.data);
const { error, data } = validate({ name: "John", email: "john@email.com" });
// error: ValidationErrors | undefined
// data: Input | undefined
if (error) {
// error: ValidationErrors
toast(translate(error));
return;
}
// data: Input
console.log(data);
transform a function returning Promise into a function returning Promise<Ok | Err>
import { resultify } from "okerr";
const someAsyncResultFunction = resultify(someAsyncFunction);
// someAsyncResultFunction: <E = unknown>(value: string) => Promise<Ok<string> | Err<E>>
const result = await someAsyncResultFunction<ApiErrors>(value);
// result: Ok<string> | Err<ApiErrors>
// or
const someAsyncResultFunction = resultify(someAsyncFunction)<ApiErrors>;
// someAsyncResultFunction: (value: string) => Promise<Ok<string> | Err<ApiErrors>>
const result = await someAsyncResultFunction(value);
// result: Ok<string> | Err<ApiErrors>
keep return types readable by merging Ok and Err types
import { Result } from "okerr";
// import 'okerr/globals';
// function getItemsFromApi(input: Input): Promise<Result<string, ValidationErrors | ApiErrors>>
async function getItemsFromApi(
input: Input
): Result<string, ValidationErrors | ApiErrors> {
const validateResult = validate(input);
// validateResult: Err<ValidationErrors> | Ok<Input>
const apiCallResult = await validateResult.mapOk(async (value) => {
return await someAsyncFunction(value).toResult<ApiErrors>();
});
// apiCallResult: Err<ValidationErrors> | Ok<string> | Err<ApiErrors>
return apiCallResult;
}
FAQs
A practical Result type inspired by Rust Result.
We found that okerr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.