import { Operation } from 'apollo-link' ; import { print } from 'graphql/language/printer' ; import { InvariantError } from 'ts-invariant' ; declare var AbortController ; export type ServerError = Error & { response : Response ; result : Record < string , any > ; statusCode : number ; } ; export type ServerParseError = Error & { response : Response ; statusCode : number ; bodyText : string ; } ; export type ClientParseError = InvariantError & { parseError : Error ; } ; export interface HttpQueryOptions { includeQuery ? : boolean ; includeExtensions ? : boolean ; } export interface HttpConfig { http ? : HttpQueryOptions ; options ? : any ; headers ? : any ; credentials ? : any ; } export interface UriFunction { ( operation ) : string ; } export interface Body { query ? : string ; operationName ? : string ; variables ? : Record < string , any > ; extensions ? : Record < string , any > ; } export interface HttpOptions { /**
     * The URI to use when fetching operations.
     *
     * Defaults to '/graphql'.
     */ uri ? : string | UriFunction ; /**
     * Passes the extensions field to your graphql server.
     *
     * Defaults to false.
     */ includeExtensions ? : boolean ; /**
     * A `fetch`-compatible API to use when making requests.
     */ fetch ? : WindowOrWorkerGlobalScope [ 'fetch' ] ; /**
     * An object representing values to be sent as headers on the request.
     */ headers ? : any ; /**
     * The credentials policy you want to use for the fetch call.
     */ credentials ? : string ; /**
     * Any overrides of the fetch options argument to pass to the fetch call.
     */ fetchOptions ? : any ; } const defaultHttpOptions = { includeQuery : true , includeExtensions : false , } ; const defaultHeaders = { accept : '*/*' , 'content-type' : 'application/json' , } ; const defaultOptions = { method : 'POST' , } ; export const fallbackHttpConfig = { http : defaultHttpOptions , headers : defaultHeaders , options : defaultOptions , } ; export const throwServerError = ( response , result , message ) => { const error = new Error ( message ) as ServerError ; error . name = 'ServerError' ; error . response = response ; error . statusCode = response . status ; error . result = result ; throw error ; } ; export const parseAndCheckHttpResponse = operations => ( response ) => { return ( response . text ( ) . then ( bodyText => { try { return JSON . parse ( bodyText ) ; } catch ( err ) { const parseError = err as ServerParseError ; parseError . name = 'ServerParseError' ; parseError . response = response ; parseError . statusCode = response . status ; parseError . bodyText = bodyText ; return Promise . reject ( parseError ) ; } } ) . then ( ( result ) => { if ( response . status >= 300 ) { throwServerError ( response , result , `Response not successful: Received status code ${ response . status }` ) ; } if ( ! Array . isArray ( result ) && ! result . hasOwnProperty ( 'data' ) && ! result . hasOwnProperty ( 'errors' ) ) { throwServerError ( response , result , `Server response was missing for query '${ Array . isArray ( operations ) ? operations . map ( op => op . operationName ) : operations . operationName }'.` ) ; } return result ; } ) ) ; } ; export const checkFetcher = ( fetcher ) => { if ( ! fetcher && typeof fetch === 'undefined' ) { let library = 'unfetch' ; if ( typeof window === 'undefined' ) library = 'node-fetch' ; throw new InvariantError ( `
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/${ library }.

For example:
import fetch from '${ library }';
import { createHttpLink } from 'apollo-link-http';

const link = createHttpLink({ uri: '/graphql', fetch: fetch });` ) ; } } ; export const createSignalIfSupported = ( ) => { if ( typeof AbortController === 'undefined' ) return { controller : false , signal : false } ; const controller = new AbortController ( ) ; const signal = controller . signal ; return { controller , signal } ; } ; export const selectHttpOptionsAndBody = ( operation , fallbackConfig , ... configs ) => { let options = { ... fallbackConfig . options , headers : fallbackConfig . headers , credentials : fallbackConfig . credentials , } ; let http = fallbackConfig . http ; configs . forEach ( config => { options = { ... options , ... config . options , headers : { ... options . headers , ... config . headers , } , } ; if ( config . credentials ) options . credentials = config . credentials ; http = { ... http , ... config . http , } ; } ) ; const { operationName , extensions , variables , query } = operation ; const body = { operationName , variables } ; if ( http . includeExtensions ) ( body as any ) . extensions = extensions ; if ( http . includeQuery ) ( body as any ) . query = print ( query ) ; return { options , body , } ; } ; export const serializeFetchParameter = ( p , label ) => { let serialized ; try { serialized = JSON . stringify ( p ) ; } catch ( e ) { const parseError = new InvariantError ( `Network request failed. ${ label } is not serializable: ${ e . message }` ) as ClientParseError ; parseError . parseError = e ; throw parseError ; } return serialized ; } ; export const selectURI = ( operation , fallbackURI ? ) => { const context = operation . getContext ( ) ; const contextURI = context . uri ; if ( contextURI ) { return contextURI ; } else if ( typeof fallbackURI === 'function' ) { return fallbackURI ( operation ) ; } else { return ( fallbackURI as string ) || '/graphql' ; } } ;