
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
Resultat is a TypeScript library designed to simplify error handling and provide a structured approach for dealing with success and error scenarios. The library introduces a custom Result type along with utility functions to deal with the outcome. By leveraging Resultat, you can enhance the reliability and clarity of error handling in your TypeScript projects.
To start using Resultat in your TypeScript project, you can install it via npm or yarn:
npm install resultat
# or
yarn add resultat
Consider a simple use case of dividing two numbers. The divideNumbers function returns either an Ok result containing the division result or an Err result with an error message if division by zero occurs.
import { Ok, Err } from "resultat";
import type { Result } from "resultat";
// Simple example: Divide Two Numbers
function divideNumbers(a: number, b: number): Result<number> {
return b === 0 ? Err("Cannot divide by 0") : Ok(a / b);
}
result.okBefore retrieving the value from the result, you have to confirm whether the result returned Ok or Err, using type narrowing. Here's how you can achieve this:
const result = divideNumbers(10, 0);
if (result.ok) {
console.log("Division result:", result.val);
} else {
// Handle the Err case
console.error("Error:", result.err);
}
unwrap: Extracting Values from OkThe unwrap function allows you to extract the value from an Ok result. If the result is an Err, it throws an error. Use it when you need to assert that result is Ok.
// Success
const result = divideNumbers(10, 2);
const value = result.unwrap(); // Returns 5
// Failure
const result = divideNumbers(10, 0);
const value = result.unwrap(); // Throws the error
unwrapOr: Providing a Default ValueThe unwrapOr function extracts the value from an Ok result or returns a provided default value if the result is an Err. This is useful when you want to ensure a fallback value in case of errors.
const result = divideNumbers(10, 0);
const num = result.unwrapOr(0); // Returns 0 since division by 0 results in an Err
unwrapOrElse: Providing a CallbackThe unwrapOrElse function is used to extract the value from an Ok result or execute a provided callback to handle the Err result and provide a fallback value.
const result = divideNumbers(10, 0);
const num = result.unwrapOrElse((errorMessage) => {
console.error(errorMessage, "Defaulting to 0");
return 0; // Fallback value provided by the callback
});
In real-world applications, fetching user data from a database or API can result in various outcomes. The Result type provided by Resultat can simplify error handling and result management. Consider the following example:
The following function attempts to retrieve user data based on a provided username.
function findUser(username: string) {
try {
const user = UserModel.findByUsername(username);
if (user === null) {
return Err("User not found");
}
return Ok(user);
} catch (e) {
return Err("Something went wrong");
}
}
Resultat allows you to annotate return types explicitly, ensuring clarity in intentions and type of the ResultOk and ResultErr path.
type UserData = {
name: string;
isAdmin: boolean;
};
function findUser(username: string): Result<UserData, string> {
if (x === 1) {
return Err("Error 1");
}
if (x === 2) {
return Err("Error 2");
}
return Ok({ name: "John", isAdmin: false ));
}
FAQs
## Description
The npm package resultat receives a total of 0 weekly downloads. As such, resultat popularity was classified as not popular.
We found that resultat 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.