digest-fetch is a JavaScript/TypeScript library that provides digest and basic HTTP authentication capabilities for both the standard `fetch` API and `node-fetch` in Node.js environments. The current stable version is 3.1.1. This library distinguishes itself by strictly adhering to RFC2069, RFC2617, and RFC7616 for digest access authentication, supporting various algorithms like MD5, SHA-256, and SHA-512-256, including their session variants. It primarily acts as a plugin, wrapping the `fetch` function to handle the authentication challenge-response cycle seamlessly. Major version 3.x transitioned the package to an ES module, requiring changes in project configuration for both JavaScript and TypeScript users. It allows for customizable options such as algorithm, status codes for failure, and cnonce size, and can also be configured to perform basic HTTP authentication.
npm install digest-fetchVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `digest-fetch` for digest authentication, create a `DigestClient`, and make an authenticated request in a TypeScript Node.js environment.
For Node.js projects, add `"type": "module"` to your `package.json`. For TypeScript, ensure `tsconfig.json` includes `"module": "ESNext"` and `"moduleResolution": "node"`.
Update `node-fetch` to its latest version (`npm install node-fetch@latest`). Review `node-fetch` v3 upgrade guides for its specific breaking changes, such as the removal of the `timeout` option.
The library suggests using a custom alternate authentication failure code (`statusCode` option) to avoid the browser prompt. Alternatively, ensure the server returns a different status for initial challenges or handle the 401 in a way that doesn't trigger the browser UI.
Ensure your project is configured as an ES module by adding `"type": "module"` to your `package.json`, and use `import DigestClient from 'digest-fetch';`. Alternatively, downgrade `digest-fetch` to a v2.x version if your project must remain CommonJS.
Ensure your project's `package.json` has `"type": "module"`. If you intend to use CommonJS, you must downgrade `digest-fetch` to v2.x.
Install `node-fetch` (`npm install node-fetch`) and ensure it's made available globally, for instance, by adding `globalThis.fetch = fetch;` after importing `node-fetch` in your entry file.