import { Request , Response , NextFunction } from 'express' ; import * as semver from 'semver' ; import config , { payIdServerVersions } from '../config' ; import { ParseError , ParseErrorType } from '../utils/errors' ; param req param res param next throws export default function checkPublicApiVersionHeaders ( req , res , next ) { res . header ( 'PayID-Server-Version' , config . app . payIdVersion ) ; const payIdVersionHeader = req . header ( 'PayID-Version' ) ; if ( ! payIdVersionHeader ) { throw new ParseError ( "A PayID-Version header is required in the request, of the form 'PayID-Version: {major}.{minor}'." , ParseErrorType . MissingPayIdVersionHeader ) ; } const semverRegex = /^\d+\.\d+$/u ; const regexResult = semverRegex . exec ( payIdVersionHeader ) ; if ( ! regexResult ) { throw new ParseError ( "A PayID-Version header must be in the form 'PayID-Version: {major}.{minor}'." , ParseErrorType . InvalidPayIdVersionHeader ) ; } const payIdRequestVersion = semver . coerce ( payIdVersionHeader ) as semver . SemVer ; const payIdServerVersion = semver . coerce ( config . app . payIdVersion ) as semver . SemVer ; if ( semver . gt ( payIdRequestVersion , payIdServerVersion ) ) { throw new ParseError ( `The PayID-Version ${ payIdVersionHeader } is not supported, please try downgrading your request to PayID-Version ${ config . app . payIdVersion }` , ParseErrorType . UnsupportedPayIdVersionHeader ) ; } if ( ! payIdServerVersions . includes ( payIdVersionHeader ) ) { throw new ParseError ( `The PayID Version ${ payIdVersionHeader } is not supported, try something in the range ${ payIdServerVersions [ 0 ] } - ${ payIdServerVersions [ payIdServerVersions . length - 1 ] }` , ParseErrorType . UnsupportedPayIdVersionHeader ) ; } res . header ( 'PayID-Version' , payIdVersionHeader ) ; res . header ( 'Cache-Control' , 'no-store' ) ; next ( ) ; }