import { Request , Response , NextFunction } from 'express' ; import config , { adminApiVersions } from '../config' ; import { ParseError , ParseErrorType , ContentTypeError } from '../utils/errors' ; param req param res param next throws export function checkRequestAdminApiVersionHeaders ( req , res , next ) { res . header ( 'PayID-Server-Version' , config . app . payIdVersion ) ; res . header ( 'PayID-API-Server-Version' , config . app . adminApiVersion ) ; const payIdApiVersionHeader = req . header ( 'PayID-API-Version' ) ; if ( ! payIdApiVersionHeader ) { throw new ParseError ( "A PayID-API-Version header is required in the request, of the form 'PayID-API-Version: YYYY-MM-DD'." , ParseErrorType . MissingPayIdApiVersionHeader ) ; } const dateRegex = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/u ; const regexResult = dateRegex . exec ( payIdApiVersionHeader ) ; if ( ! regexResult ) { throw new ParseError ( "A PayID-API-Version header must be in the form 'PayID-API-Version: YYYY-MM-DD'." , ParseErrorType . InvalidPayIdApiVersionHeader ) ; } if ( payIdApiVersionHeader < adminApiVersions [ 0 ] ) { throw new ParseError ( `The PayID-API-Version ${ payIdApiVersionHeader } is not supported, please try upgrading your request to at least 'PayID-API-Version: ${ adminApiVersions [ 0 ] }'` , ParseErrorType . UnsupportedPayIdApiVersionHeader ) ; } next ( ) ; } param req param _res param next throws export function checkRequestContentType ( req , _res , next ) { type MimeType = 'application/json' | 'application/merge-patch+json' ; let mediaType = 'application/json' ; if ( req . method === 'PATCH' ) { /**
         * The required Content-Type header for the PATCH endpoints is 'application/merge-patch+json'.
         *
         * The merge patch format is primarily intended for use with the HTTP PATCH method
         * as a means of describing a set of modifications to a target resource’s content.
         * Application/merge-patch+json is a Type Specific Variation of the "application/merge-patch" Media Type that uses a
         * JSON data structure to describe the changes to be made to a target resource.
         */ mediaType = 'application/merge-patch+json' ; } if ( req . header ( 'Content-Type' ) !== mediaType && [ 'POST' , 'PUT' , 'PATCH' ] . includes ( req . method ) ) { throw new ContentTypeError ( mediaType ) ; } next ( ) ; } param _req param res param next export function addAcceptPatchResponseHeader ( _req , res , next ) { /**
     * Add this header to the response.
     * Accept-Patch in response to any method means that PATCH is allowed on the resource identified by the Request-URI.
     * The Accept-Patch response HTTP header advertises which media-type the server is able to understand for a PATCH request.
     */ res . header ( 'Accept-Patch' , 'application/merge-patch+json' ) ; next ( ) ; }