All files / proxy/utils/customer-variables types.ts

100% Statements 20/20
100% Branches 3/3
100% Functions 5/5
100% Lines 18/18

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 52 53 54 55 56 5715x   15x 15x 15x 15x 15x 15x 15x     15x             227x   15x 42x   42x 38x     4x     15x                 15x 269x                                
import { isNonNegativeInteger } from '../validation'
 
export enum CustomerVariableName {
  GetResultPath = 'fpjs_get_result_path',
  BehaviorPathNestLevel = 'fpjs_integration_path_depth',
  PreSharedSecret = 'fpjs_pre_shared_secret',
  AgentDownloadPath = 'fpjs_agent_download_path',
  FpCdnUrl = 'fpjs_cdn_url',
  FpIngressBaseHost = 'fpjs_ingress_base_host',
}
 
export const internalVariables: Set<CustomerVariableName> = new Set<CustomerVariableName>([
  CustomerVariableName.FpCdnUrl,
  CustomerVariableName.FpIngressBaseHost,
])
 
type ParserValidator<T> = (value: T) => boolean
 
const stringParser = (value: string) => value
 
const intParser = (fallbackValue: number, validation: ParserValidator<number>) => (value: string) => {
  const parsed = parseInt(value)
 
  if (validation(parsed)) {
    return parsed
  }
 
  return fallbackValue
}
 
export const customerVariableParsers = {
  [CustomerVariableName.GetResultPath]: stringParser,
  [CustomerVariableName.BehaviorPathNestLevel]: intParser(1, isNonNegativeInteger),
  [CustomerVariableName.PreSharedSecret]: stringParser,
  [CustomerVariableName.AgentDownloadPath]: stringParser,
  [CustomerVariableName.FpCdnUrl]: stringParser,
  [CustomerVariableName.FpIngressBaseHost]: stringParser,
}
 
export function parseCustomerVariable<T extends CustomerVariableName>(variable: T, value: string) {
  return customerVariableParsers[variable](value) as CustomerVariableType<T>
}
 
export type CustomerVariableType<T extends CustomerVariableName> = ReturnType<(typeof customerVariableParsers)[T]>
 
export type CustomerVariableReturn = string | null | undefined
 
export type CustomerVariablesRecord = {
  [Key in CustomerVariableName]: CustomerVariableType<Key> | undefined | null
}
 
export interface CustomerVariableProvider {
  readonly name: string
 
  getVariable: (variable: CustomerVariableName) => Promise<CustomerVariableReturn>
}