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 / dist / bootstrapper / setup / requestMiddleware.ts
Size: Mime:
import { OneRequestStore } from '@skava/request'
import { setConfig, config } from '@skava/request/dist/config'
import { fromMapToObj } from 'exotic'
import { oneStorage } from '@skava/persistence'
import {
  partnerId,
  campaignId,
  skavaHeaderSecret,
  shouldIncludeCampaignId,
  storeId,
  appid,
  toCorrelationId,
  locale,
} from '../../bootstrapper/config'
import { checkAndGetSessionId } from '../../deps/checkAPIResponse'
import { logger } from '../../log'

// Todo isTruish is not available in uxui-module
const isTruish = x => x === true || x === 'true' || /^["|']?true["|']?$/.test(x)

const storeKey = isTruish(process.env.SHOULD_USE_LOWERCASE_STORE_ID)
  ? 'storeid'
  : 'storeId'

function fromRequestToLocale(store: OneRequestStore) {
  const { req = DEFAULT_EXPRESS_REQ, res } = store.get('context')
  const localeValue = req.headers.locale
  const localeFromContext = store.get('context').req
    ? store.get('context').req.get('locale') || locale
    : locale

  return localeValue || localeFromContext || 'en_US'
}

const constantAndDefault = {
  // Orchestration wont require these params
  // partnerId,
  // [storeKey]: storeId,
  // appid,
  // locale,
  // === issue with "Unprocessable" ===
  // @todo
  locale(store: OneRequestStore) {
    return fromRequestToLocale(store)
  },
}

export const DEFAULT_EXPRESS_REQ = Object.freeze({
  headers: {},
})

const constantHeaders = {
  /**
   * @todo !!! SEEMINGLY AN ISSUE WHEN USING THIS
   */
  // get skcid() {
  //   return toCorrelationId() || '@@EMPTY'
  // },

  // const { context, ...rest } = fromMapToObj(store)
  // 'x-sk-session-id'(store: OneRequestStore) {
  //   const { req = DEFAULT_EXPRESS_REQ, res } = store.get('context')
  //   const existingSession = req.headers['x-sk-session-id']
  //   if (existingSession) {
  //     return existingSession
  //   } else {
  //     logger.debug('[x-sk-session-id] bad apis will always be bad')
  //     // return 'eeh'
  //     return checkAndGetSessionId(store)
  //   }
  // },

  // locale(store: OneRequestStore) {
  //   return fromRequestToLocale(store)
  // },

  // Orchestration API layer requires storeId, contentType and SessionId to be header params
  'x-store-id': storeId,
  'content-type': 'json',
  'x-version': '8.0.0',
  'x-skava': skavaHeaderSecret,
}

if (isTruish(shouldIncludeCampaignId)) {
  constantAndDefault.campaignId = campaignId
}

setConfig({
  // should not need this..
  // partnerId,
  // campaignId,
  // [storeKey]: storeId,
  // appid,
  // locale,
  // //
  // constantParams: constantAndDefault,
  // defaultParams: constantAndDefault,
  ...constantHeaders,
  ...constantAndDefault,
})

config.set('constantParams', constantAndDefault)
config.set('constantHeaders', constantHeaders)