import axios from 'axios' ; import getStream from 'get-stream' ; import { Readable } from 'stream' ; import { pkgName } from '../index' ; export function getDocumentStream ( url , token ? ) { const headers = { 'User-Agent' : `${ pkgName }` , ... ( token ? { Authorization : `Bearer ${ token }` } : { } ) , } ; return axios ( { method : 'get' , responseType : 'stream' , url , headers , } ) . then ( ( res ) => res . data ) . catch ( async ( err ) => { if ( ! err . response || ! err . response . data ) { throw err ; } let error = err ; try { const data = await getStream ( err . response . data ) ; error = new Error ( JSON . parse ( data ) . message ) ; } catch ( jsonErr ) { } throw error ; } ) as Promise < Readable > ; }