Typed REST and HTTP Client

2.3.0 · active · verified Sun Apr 19

typed-rest-client is a lightweight REST and HTTP client library designed for Node.js, with a strong emphasis on TypeScript support, including generics and async/await capabilities. The current stable version is 2.3.0, building on a release history that includes a significant v2.0.0 update in late 2023 that raised the minimum Node.js requirement to version 16.0.0. The library provides built-in TypeScript typings, eliminating the need for separate type installations. It differentiates itself by offering both a low-level HttpClient, which returns response objects for all HTTP statuses (including 4xx/5xx), and a higher-level RestClient, which automatically deserializes JSON and throws errors for non-success status codes. Key features include support for Basic, Bearer, and NTLM authentication, proxy configurations, client/server certificates, and automatic handling of HTTP redirects. It maintains a regular, but not rapid, release cadence, focusing on stability and functionality for enterprise Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `RestClient` and perform a basic GET request for a JSON resource, including error handling and null checks.

import { RestClient } from 'typed-rest-client/RestClient';

interface Todo {
  userId: number;
  id: number;
  title: string;
  completed: boolean;
}

async function fetchTodo() {
  // The user-agent string is required for RestClient initialization
  const rest = new RestClient('my-app-user-agent');
  try {
    const response = await rest.get<Todo>('https://jsonplaceholder.typicode.com/todos/1');

    if (response.statusCode === 200 && response.result) {
      console.log(`Fetched Todo (ID: ${response.result.id}): ${response.result.title} - Completed: ${response.result.completed}`);
    } else if (response.statusCode === 404) {
      // RestClient does not throw for 404 if no error body is returned and result will be null
      console.log('Todo not found.');
    } else {
      console.error(`Request failed with status: ${response.statusCode}`);
    }
  } catch (err: any) {
    // RestClient throws for other 4xx/5xx errors
    if (err.statusCode) {
      console.error(`RestClient threw an error for status ${err.statusCode}: ${err.message}`);
    } else {
      console.error(`An unexpected network or client error occurred: ${err.message}`);
    }
  }
}

fetchTodo();

view raw JSON →