PitchHut logo
trembita
A lightweight TypeScript HTTP client for JSON APIs.
Pitch

Trembita is a small, efficient TypeScript ESM client designed for seamless interaction with third-party JSON APIs. Leveraging the fetch API with a strict Result-based error handling, it ensures reliable API calls without the baggage of legacy dependencies. Ideal for developers seeking a straightforward yet powerful solution.

Description

Trembita is a lightweight and efficient TypeScript HTTP client designed for consuming third-party JSON APIs. Built on the native fetch API, it provides a functional error handling mechanism through a Result<T, E> API structure, ensuring robust interactions with external services while eliminating the clutter of legacy dependencies.

Key Features:

  • Type-Safe Error Handling: Each response includes a categorized error structure, allowing TypeScript to assist in narrowing down issues without relying on exceptions.
  • Zero Runtime Dependencies: Utilizes built-in globals from the environment, making it suitable for both Node.js (≥ 20.10 LTS) and modern browsers with bundlers.
  • Minimal and Intuitive API: Simply create a client with createTrembita() which returns an API featuring only essential functions, promoting a cleaner coding experience.
  • Easily Testable: Designed to facilitate testing by allowing the injection of a custom fetch implementation.
  • ESM-Only and Strict TypeScript Compatibility: Ships with .d.ts files and source maps while ensuring tree-shakability.

Usage Examples:

Basic Setup for API Interaction:

import { createTrembita, HTTP_OK } from 'trembita';

const api = createTrembita({ endpoint: 'https://api.example.com/v1' });
if (!api.ok) throw new Error('Invalid endpoint');

const json = await api.value.request({
  path: '/resource',
  expectedCodes: [HTTP_OK]
});

if (!json.ok) {
  console.error(json.error.kind);
}

Learning Resources:

Choose your path:

Advanced Features:

  • The optional @trembita/openapi package provides helpful extensions such as path expansion templates and enhanced error handling.
  • Testing and Mocking: Supports unit testing strategies by allowing mock implementations of fetch, which helps isolate tests from actual network calls.

Conclusion:

Trembita streamlines interactions with JSON APIs through a type-safe, minimalistic, and efficient approach that emphasizes clarity and usability, making it an excellent choice for developers looking to elevate their HTTP request handling in TypeScript.

0 comments

No comments yet.

Sign in to be the first to comment.