Skip to main content
Home
Works with
This package works with Browsers
This package works with Browsers
JSR Score94%
Downloads33/wk
Published6 days ago (0.1.6)

Type-safe HTTP API Client.

Type-safe HTTP API Client.

Builds a strongly-typed client from server route definitions. For each declared route it provides:

  • fetch(input?, options?): Promise
  • signal(): a reactive RequestState wrapper powered by @preact/signals

Features:

  • End-to-end input/output typing from your route defs
  • Abortable requests with .reset() and deduping via a shared AbortController
  • Helpful errors: ErrorWithData (JSON error payload) and ErrorWithBody (invalid JSON)

Examples

Basic usage

import { makeClient } from '@01edu/api-client'
import type { RoutesDefinitions } from '/api/routes.ts'
// Important, only import types from your backend

const api = makeClient<RoutesDefinitions>('/api')
const res = await api['GET/hello'].fetch({ name: 'Ada' })

Reactive usage

const hello = api['GET/hello'].signal()
hello.fetch({ name: 'Ada' })
hello.$.subscribe(v => console.log(v.pending, v.data, v.error))

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.