Skip to content
zsumzPublic

Repository files navigation

avirx

Typed, inspectable HTTP requests on top of fetch.

CI

Build requests as immutable values, preview them, render cURL, validate responses, then execute.

Install · Example · Model · Features · Runtime · Guides


Install

npm install avirx

Example

import { avirx } from 'avirx';

const Book = {
    parse(input: unknown): { id: string; title: string } {
        if (typeof input !== 'object' || input === null) throw new Error('Expected a book.');
        const value = input as Record<string, unknown>;
        if (typeof value.id !== 'string' || typeof value.title !== 'string') {
            throw new Error('Expected a book id and title.');
        }
        return { id: value.id, title: value.title };
    },
};

const api = avirx('https://api.example.com');

const request = api
    .get('/books/{id}')
    .path({ id: 'book_123' })
    .query({ include: ['author', 'reviews'] });

console.log(await request.preview());
console.log(await request.curl());

const book = await request.as(Book);

Model

Avirx treats requests as values. Build a request once, then inspect, clone, or execute it without hidden mutation.

const result = await api
    .get('/books/{id}')
    .path({ id: 'missing' })
    .result(Book);

if (!result.ok) {
    console.log(result.status);
}

Features

  • Immutable fluent request builders
  • Typed path parameters from route strings
  • Query, JSON body, multipart upload, and download helpers
  • Runtime schema validation with any parser that exposes parse
  • Result mode for expected HTTP failures
  • Redacted preview and cURL output
  • Auth, timeout, retry, hooks, cache, concurrency, and rate-limit policies
  • Zero runtime dependencies

Runtime

Avirx requires Node.js 18.17 or newer, or any runtime with compatible fetch, Request, Response, Headers, Blob, FormData, ReadableStream, performance.now(), and AbortSignal.any APIs. The package is ESM-only; TypeScript consumers require TypeScript 5.0.4 or newer.

Guides

Releases

Packages

Contributors

Languages