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/graphql / src / bootstrapper / api / correlationId.ts
Size: Mime:
import uuid from 'uuid'
import { Request as ExpressRequest } from 'express'
import { isFunction } from 'exotic'

// this we can output to a file
// and then get all the logs for a specific deploy
export const idList: string[] = []
export const correlationIdNamespace = process.env.SKCID || '@@graphql'
export const cookieNamespace = process.env.COOKIE_NAMESPACE || '@@cookie'

const fromReqToCookie = (request: ExpressRequest) => {
  return isFunction(request.headers.get)
    ? request.headers.get('Cookie')
    : isFunction(request.get)
      ? request.get('Cookie')
      : request.headers.Cookie || request.Headers.cookie || undefined
}

/**
 * @todo add this req param if we want to use it explicitly,
 * since we should set it on the req
 */
export const toCorrelationId = (req?: ExpressRequest) => {
  // index?: number
  // return at an index, or the last one
  // if (index !== undefined) {
  //   return idList[index] || idList[idList.length - 1]
  // }

  /**
   * @note BREAKING CHANGE
   * BACKWARDS API,
   * NOW NOTHING GIVES THE LAST ONE,
   * REQUIRES A REQUEST TO GENERATE A NEW ONE
   */
  if (req === undefined) {
    return idList[idList.length - 1]
  }

  const idForRequest =
    correlationIdNamespace +
    uuid() +
    cookieNamespace +
    '=' +
    fromReqToCookie(req)

  req.log.info('[gql] toCorrelationId: ', idForRequest)
  idList.push(idForRequest)

  return idForRequest
}