import crossFetch from 'cross-fetch' ; import { stringify } from 'querystring' ; import { AuthError , DiscogsError } from '../errors' ; internal export enum HTTPVerbsEnum { GET = 'GET' , POST = 'POST' , PUT = 'PUT' , DELETE = 'DELETE' } param url param options returns throws internal export async function fetch < T > ( url , options ? , shouldReturnBlob ? ) { const response = await crossFetch ( url , options ) ; const { status , statusText } = response ; if ( status === 401 ) throw new AuthError ( ) ; if ( status < 200 || status >= 300 ) throw new DiscogsError ( statusText , status ) ; if ( status === 204 ) return Promise . resolve ( { } ) as Promise < T > ; if ( shouldReturnBlob ) { const blob = await response . blob ( ) ; return ( blob as unknown ) as T ; } const data = await response . json ( ) ; return data ; } param uri param query returns internal export function addQueryToUri ( uri , query ) { const definedKeys = Object . entries ( query ) . reduce ( ( acc , [ key , value ] ) => { if ( typeof value === 'undefined' ) return acc ; return { ... acc , [ key ] : value , } ; } , { } as Record < string , any > ) ; return `${ uri }?${ stringify ( definedKeys ) }` ; } param data returns internal export function transformData ( data ) { return Object . entries ( data ) . reduce ( ( acc , [ key , value ] ) => ( { ... acc , [ key === 'currency' ? 'curr_abbr' : key . replace ( /[A-Z]/g , ( letter ) => `_${ letter . toLowerCase ( ) }` ) ] : value , } ) , { } as Record < string , any > ) ; }