All files / proxy/utils request.ts

92% Statements 23/25
75% Branches 6/8
87.5% Functions 7/8
91.3% Lines 21/23

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 47 48 49 50 51        10x   10x         10x 2x   10x 3x 3x     10x 65x 60x     5x       8x       7x   7x   7x 18x 18x 4x   4x     3x       62x    
import type { CloudFrontRequest } from 'aws-lambda'
import { Region } from '../model/index.ts'
import { isTruthy } from './is-truthy.ts'
 
export const getApiKey = (request: CloudFrontRequest): string | undefined => getQueryParameter(request, 'apiKey')
 
export const getVersion = (request: CloudFrontRequest): string => {
  const version = getQueryParameter(request, 'version')
  return version ?? '3'
}
 
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 (!isTruthy(value) || !isRegion(value)) {
    return Region.us
  }
 
  return value
}
 
function isRegion(value: string): value is Region {
  return Object.values<string>(Region).includes(value)
}
 
function getQueryParameter(request: CloudFrontRequest, key: string): string | undefined {
  const params = request.querystring.split('&')
 
  console.debug(`Attempting to extract ${key} from ${params.join(', ')}. 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.join(', ')}: ${kv[1]}`)
 
      return kv[1]
    }
  }
  return undefined
}
 
export function isMethodAuthorized(method: string) {
  return method === 'POST'
}