Package Exports
- digest-fetch
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (digest-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
digest-fetch
digest auth request plugin for fetch/node-fetch, supports http basic authentication as well
Installation
// dependencies for node
npm install node-fetch
// for browers, if to use it directly, please indcude file `digest-fetch.js` in a <script/>
<script type="application/javascript" src="path-to-digest-fetch.js'></script>Get Started
const DigestFetch = require('digest-fetch')
// In browser: const DigestFetch = window.DigestFetch;Initialize with option and credential
const digestOptions = {
cnonceSize: 32, // length of cnonce, default: 32
logger: console, // logger for debug, default: none
algorithm: 'MD5', // algorithm to be used, 'MD5' or 'MD5-sess'
// Custom authentication failure code for avoiding browser prompt:
// https://stackoverflow.com/questions/9859627/how-to-prevent-browser-to-invoke-basic-auth-popup-and-handle-401-error-using-jqu
statusCode: 401, // default 401
basic: false // we support http basic authentication as well, enable it here if you want, default: false
}
const client = new DigestFetch('user', 'password', digestOptions)
const basic_auth_client = DigestFetch('user', 'password', { basic: true })
Do request same way as fetch or node-fetch
const url = ''
const options = {}
client.fetch(url, options)
.then(resp=>resp.json())
.then(data=>console.log(data))
.catch(e=>console.error(e))Pass in refresh request options factory function for conditions options needs be refreshed when trying again. For example when posting with file stream:
const factory = () => ({ method: 'post', body: fs.createReadStream('path-to-file') })
client.fetch(url, {factory})
.then(resp=>resp.json())
.then(data=>console.log(data))
.catch(e=>console.error(e))
About
Digest authentication: https://en.wikipedia.org/wiki/Digest_access_authentication This plugin is implemented following RFC2069 and RFC2617, supports http basic authentication as well!
Please open issues if you find bugs and meet problems during using this plugin.