import fetch from 'isomorphic-unfetch' ; import { BAD_REQUEST } from './responses' ; internal private param { string } endpoint param options param { string } options . baseUrl param { string } options . userAgent returns { Promise < string > } export function fetchFromApi ( endpoint , { baseUrl = 'https://api.pwnedpasswords.com' , userAgent , } = { } ) { const config = Object . assign ( { } , userAgent ? { headers : { 'User-Agent' : userAgent , } , } : { } ) ; const url = `${ baseUrl . replace ( /\/$/g , '' ) }${ endpoint }` ; return fetch ( url , config ) . then ( ( res ) => { if ( res . ok ) return res . text ( ) ; if ( res . status === BAD_REQUEST . status ) { return res . text ( ) . then ( ( text ) => { throw new Error ( text ) ; } ) ; } throw new Error ( res . statusText ) ; } ) ; }