digest auth request plugin for fetch/node-fetch.
// dependencies for node
npm install crypto-js 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>
const DigestFetch = require('digest-fetch')
const digestOptions = {
cnonceSize: 32, // length of cnonce, default: 32
logger: console, // logger for debug, default: none
algorithm: 'MD5' // only 'MD5' is supported now
}
const client = new DigestFetch('user', 'password', digestOptions)
// 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.
// etc: 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))