Skip to content

0.8.0

Choose a tag to compare

@realpha realpha released this 24 Jan 16:12
· 8 commits to main since this release
12e96fb

Features

By popular request: this is the first release shipping an adapter for commonly used Web APIs, namely fetch.

  • liftFetch().: Lifts fetch-like functions into a Task context. This comes with "sane" defaults for the construction of the possible error paths and is flexible enough to accomodate a variety of validation needs.
import { liftFetch } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

interface Company {
  name: string;
}

interface User {
  id: number;
  company: Company;
}

interface UserResponse extends Response {
  json(): Promise<User>
}

function fetchUserById(id: number): Promise<UserResponse> {
  return fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
}

const getUserById = liftFetch(fetchUserById);

const result: Company = await getUserById(1)
  .map((resp) => resp.json()) // inferred as User
  .inspect(console.log)
  .inspectErr(console.error) // FailedRequest<UserResponse> | FetchException
  .mapOr(user => user.company, { name: "Acme Corp." })
  .unwrap();

Full Changelog: 0.7.0...0.8.0