import { ParsedAcceptHeader } from '../types/headers' ; import { ParseError , ParseErrorType } from '../utils/errors' ; const badAcceptHeaderErrorMessage = `Must have an Accept header of the form "application/{payment_network}(-{environment})+json".
      Examples:
      - 'Accept: application/xrpl-mainnet+json'
      - 'Accept: application/btc-testnet+json'
      - 'Accept: application/ach+json'
      - 'Accept: application/payid+json'
      ` ; param acceptHeaders returns throws export function parseAcceptHeaders ( acceptHeaders ) { if ( ! acceptHeaders . length ) { throw new ParseError ( `Missing Accept Header. ${ badAcceptHeaderErrorMessage }` , ParseErrorType . InvalidMediaType ) ; } const parsedAcceptHeaders = acceptHeaders . map ( ( type ) => parseAcceptHeader ( type ) ) ; return parsedAcceptHeaders ; } param acceptHeader returns throws export function parseAcceptHeader ( acceptHeader ) { const ACCEPT_HEADER_REGEX = /^(?:application\/)(?<paymentNetwork>\w+)-?(?<environment>\w+)?(?:\+json)$/u ; const lowerCaseMediaType = acceptHeader . toLowerCase ( ) ; const regexResult = ACCEPT_HEADER_REGEX . exec ( lowerCaseMediaType ) ; if ( ! regexResult || ! regexResult . groups ) { throw new ParseError ( `Invalid Accept Header. ${ badAcceptHeaderErrorMessage }` , ParseErrorType . InvalidMediaType ) ; } return { mediaType : lowerCaseMediaType , ... ( regexResult . groups . environment && { environment : regexResult . groups . environment . toUpperCase ( ) , } ) , paymentNetwork : regexResult . groups . paymentNetwork . toUpperCase ( ) , } ; }