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.
npm install typed-rest-clientVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize `RestClient` and perform a basic GET request for a JSON resource, including error handling and null checks.
Upgrade your Node.js environment to version 16.0.0 or higher. If unable to upgrade, you must stick with `typed-rest-client` v1, which is End-of-Life and contains security vulnerabilities.
Always wrap `RestClient` calls in `try...catch` blocks to handle non-success HTTP responses. For `HttpClient`, inspect the `response.statusCode` property and `response.result` (or `response.message`) for details.
Migrate to `typed-rest-client` version 2.x, ensuring your Node.js environment meets the >=16.0.0 requirement.
Ensure you are using correct subpath imports, e.g., `import { RestClient } from 'typed-rest-client/RestClient';`Switch to ES module `import` syntax: `import { RestClient } from 'typed-rest-client/RestClient';`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or using `.mjs` file extension).Implement proper error handling with `try...catch` blocks around `RestClient` calls to gracefully manage non-success HTTP responses. Inspect `err.statusCode` for the exact HTTP status and `err.message` for details.
Always check if `response.result` is not `null` or `undefined` before accessing its properties: `if (response.statusCode === 200 && response.result) { ... }`.