import { inspect } from 'util' ; import fetch from 'isomorphic-unfetch' ; export interface VTEXOptions { tenant : string ; workspace : string ; environment : 'vtexcommercestable' | 'vtexcommercebeta' ; } const headers = { 'content-type' : 'application/json' , accept : 'application/json' , } ; const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; async function fetchRetry < T > ( path , init ? , retryCount = 3 ) { try { const response = await fetch ( path , init ) ; return await response . json ( ) ; } catch ( err ) { if ( retryCount === 1 ) { throw err ; } await delay ( 300 ) ; console . error ( `[gatsby-source-vtex]: Error while fetching. Retrying...` , err ) ; return fetchRetry ( path , init , retryCount - 1 ) ; } } export const fetchGraphQL = async < T > ( path , init ? ) : Promise < { data : T ; errors : any [ ] ; } > => { let retryCount = 0 ; let response ; while ( retryCount !== 3 ) { response = await fetchRetry < { data : T ; errors : any [ ] ; } > ( path , init ) ; if ( ! response . errors || response . errors . length === 0 ) { return response ; } retryCount += 1 ; console . error ( `[gatsby-source-vtex]: Error while fetching graphql data. Retry ${ retryCount }` ) ; } if ( response ) { for ( const error of response . errors ) { console . error ( inspect ( error , false , 100 , true ) ) ; } throw new Error ( `[gatsby-source-vtex]: GraphQL Error\n\n: ${ response . errors . map ( ( err ) => err . message ) . join ( '\n\n' ) }` ) ; } throw new Error ( '[gatsby-source-vtex]: Error while fetching graphql data' ) ; } ; export const fetchVTEX = async < T > ( path , options , init ? ) : Promise < T > => { try { const url = `https://${ options . tenant }.${ options . environment }.com.br${ path }` ; return await fetchRetry ( url , { ... init , headers : { ... headers , ... init ?. headers , } , } ) ; } catch ( err ) { console . error ( err ) ; throw err ; } } ;