koka-ts
v0.1.4
Published
A TypeScript library to handle errors.
Downloads
4
Readme
Kōka helps you craft elegant and secure TypeScript code. It enables easy error handling, streamlines development processes, and enhances code maintainability.
効果 (こうか): This means "effect" or "effectiveness." It can be used to talk about the impact or result of something.
npm i koka-ts
Usage/Examples
trySync and tryAsync
async function getData(): Promise<string> {
const res = await tryAsync(fetch("https://api.sampleapis.com/coffee/hot"));
if (res.isErr()) {
const err = res.getErr();
return err.message;
}
const data = await tryAsync(res.unwrap().json());
if (data.isErr()) {
const err = data.getErr();
return err.message;
}
return data.unwrap()[0].title;
}
guardSync and guardAsync
async function getUser(userId: number): Promise<User> {
const user = await guardAsync(db.users.findOne(userId)).throw();
return user;
}
async function getUserName(userId: number): Promise<string> {
const userName = (await db.users.findOne(1)).name;
return guardSync(userName).or("John Doe");
}