liaisev5.3.1

Playground

This is the real liaise build, running in your browser. Pick an example, change it and run it again: the Network and Console panels show what happened. The examples call a fake API at api.example.com, so every failure is repeatable. Point baseUrl or endpoint anywhere else and the request goes out for real (the server has to allow cross-origin requests).

import { createApi, defineRequest } from 'liaise'

type User = { id: string; name: string; email: string }

// The path decides which params are required.
const getUser = defineRequest<User>()({ method: 'GET', path: '/users/:id' })

const api = createApi({
  baseUrl: 'https://api.example.com',
  requests: { getUser },
})

// This never throws: you always get { data, error }.
const { data, error } = await api.getUser({ id: '42' })

if (error) {
  console.log(error.kind, error.status)
} else {
  console.log(`Hello, ${data.name}`) // data is a User here
}

Network

    Console

      Change the id to '404', '500' or 'offline' and run it again. Hover over data or error to see the types.

      Editing works on a larger screen.

      esc