All files / proxy/utils request.ts

93.54% Statements 29/31
66.66% Branches 4/6
85.71% Functions 6/7
91.66% Lines 22/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46  10x   10x   10x         10x 2x   10x 3x 3x     10x 69x 64x     5x       7x   7x   7x 18x 18x 4x   4x     3x     10x 66x    
import { CloudFrontRequest } from 'aws-lambda'
import { Region } from '../model'
 
export const getApiKey = (request: CloudFrontRequest): string | undefined => getQueryParameter(request, 'apiKey')
 
export const getVersion = (request: CloudFrontRequest): string => {
  const version = getQueryParameter(request, 'version')
  return version === undefined ? '3' : version
}
 
export const getLoaderVersion = (request: CloudFrontRequest): string | undefined =>
  getQueryParameter(request, 'loaderVersion')
 
export const getRegion = (request: CloudFrontRequest): Region => {
  const value = getQueryParameter(request, 'region')
  return getValidRegion(value)
}
 
export const getValidRegion = (value?: string | null): Region => {
  if (!value || !(value in Region)) {
    return Region.us
  }
 
  return value as Region
}
 
function getQueryParameter(request: CloudFrontRequest, key: string): string | undefined {
  const params = request.querystring.split('&')
 
  console.debug(`Attempting to extract ${key} from ${params}. Query string: ${request.querystring}`)
 
  for (let i = 0; i < params.length; i++) {
    const kv = params[i].split('=')
    if (kv[0] === key) {
      console.debug(`Found ${key} in ${params}: ${kv[1]}`)
 
      return kv[1]
    }
  }
  return undefined
}
 
export function isMethodAuthorized(method: string) {
  return method === 'POST'
}