import { ConseilServerInfo , ConseilQuery } from '../types/conseil/QueryTypes' ; import { ConseilRequestError } from '../types/conseil/ConseilErrorTypes' ; import FetchSelector from '../utils/FetchSelector' ; import LogSelector from '../utils/LoggerSelector' ; const log = LogSelector . log ; const fetch = FetchSelector . fetch ; /**
 * Utility functions for querying backend Conseil v2 API for metadata
 */ export namespace ConseilDataClient { param serverInfo param platform param network param entity param query export async function executeEntityQuery ( serverInfo , platform , network , entity , query ) { const url = `${ serverInfo . url }/v2/data/${ platform }/${ network }/${ entity }` ; log . debug ( `ConseilDataClient.executeEntityQuery request: ${ url }, ${ JSON . stringify ( query ) }` ) ; return fetch ( url , { method : 'post' , headers : { 'apiKey' : serverInfo . apiKey , 'Content-Type' : 'application/json' } , cache : 'no-store' , body : JSON . stringify ( query ) } ) . then ( r => { if ( ! r . ok ) { log . error ( `ConseilDataClient.executeEntityQuery request: ${ url }, ${ JSON . stringify ( query ) }, failed with ${ r . statusText }(${ r . status })` ) ; throw new ConseilRequestError ( r . status , r . statusText , url , query ) ; } return r ; } ) . then ( r => { const isJSONResponse = r . headers . get ( 'content-type' ) . toLowerCase ( ) . includes ( 'application/json' ) ; const response = isJSONResponse ? r . json ( ) : r . text ( ) ; log . debug ( `ConseilDataClient.executeEntityQuery response: ${ isJSONResponse ? JSON . stringify ( response ) : response }` ) ; return response ; } ) ; } }