Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / __unused / apolloDataSources.ts
Size: Mime:
function isSerializableJSON(body: any) {
  return (
    body !== undefined &&
    body !== null &&
    (body.constructor === Object ||
      ((body as any).toJSON && typeof (body as any).toJSON === 'function'))
  )
}

// overview of the reason this started to get forked
// var headers = new Headers()
// =>
// {
//     // node-fetch-vs-apollo
//     _headers: ['cookie=eh']
// }
// =>
// var headers new import('apollo-server-env').Headers(headers)
// =>
// // headers.entries() => { _headers: ['cookie=eh'] }
// headers.entries() => { _headers: ['[Object object]'] }
// // what it should be
// =>
// headers.entries => { cookies: 'eh' }

class {
  async _fetch<TResult>(
    init: RequestInit & {
      path: string
      params?: URLSearchParamsInit
    }
  ): Promise<TResult> {
    if (!(init.params instanceof URLSearchParams)) {
      init.params = new URLSearchParams(init.params)
    }

    if (!(init.headers && init.headers instanceof Headers)) {
      init.headers = new Headers(init.headers)
    }

    const options = init as RequestOptions

    if (this.willSendRequest) {
      await this.willSendRequest(options)
    }

    const url = await this.resolveURL(options)

    // Append params to existing params in the path
    for (const [name, value] of options.params) {
      url.searchParams.append(name, value)
    }

    // We accept arbitrary objects as body and serialize them as JSON
    if (isSerializableJSON(options.body)) {
      options.body = JSON.stringify(options.body)
      options.headers.set('Content-Type', 'application/json')
    }

    // @@note @@fork
    // const href = String(url)
    const href = url.href
    const request = new Request(href, options)

    // require('fliplog').quick(request)

    // @@ did add
    if (this.requestDidMount) {
      await this.requestDidMount(options)
    }

    const cacheKey = this.cacheKeyFor(request)

    console.log('beforeApolloRequest', { request })

    const performRequest = async () => {
      return (this as any).trace(
        `${options.method || 'GET'} ${url}`,
        async () => {
          const cacheOptions = options.cacheOptions
            ? options.cacheOptions
            : this.cacheOptionsFor && this.cacheOptionsFor.bind(this)

          try {
            // return fetch(request)

            const response = await this.httpCache.fetch(request, {
              cacheKey,
              cacheOptions,
            })
            // requestDidSend
            return await this.didReceiveResponse(response, request)
          } catch (error) {
            this.didEncounterError(error, request)
          }
        }
      )
    }

    if (request.method === 'GET') {
      let promise = this.memoizedResults.get(cacheKey)
      if (promise) return promise

      promise = performRequest()
      this.memoizedResults.set(cacheKey, promise)
      return promise
    } else {
      this.memoizedResults.delete(cacheKey)
      return performRequest()
    }
  }
}