import { RelativeUrl } from "./feedback" ; import { HapiRequest , HapiResponseToolkit } from "server/types" ; export const feedbackReturnInfoKey = "f_t" ; const paramsToCopy = [ feedbackReturnInfoKey ] ; export function proceed ( request , h , nextUrl ) { const returnUrl = request . query . returnUrl ; if ( typeof returnUrl === "string" && returnUrl . startsWith ( "/" ) ) { return h . redirect ( returnUrl ) ; } else { return redirectTo ( request , h , nextUrl ) ; } } type Params = { num ? : number ; returnUrl : string ; } | { } ; export function nonRelativeRedirectUrl ( request , targetUrl , params = { } ) { const url = new URL ( targetUrl ) ; Object . entries ( params ) . forEach ( ( [ name , value ] ) => { url . searchParams . append ( name , `${ value }` ) ; } ) ; paramsToCopy . forEach ( ( key ) => { const value = request . query [ key ] ; if ( typeof value === "string" ) { url . searchParams . append ( key , value ) ; } } ) ; return url . toString ( ) ; } export function redirectUrl ( request , targetUrl , params = { } ) { const relativeUrl = new RelativeUrl ( targetUrl ) ; Object . entries ( params ) . forEach ( ( [ name , value ] ) => { relativeUrl . setParam ( name , `${ value }` ) ; } ) ; paramsToCopy . forEach ( ( key ) => { const value = request . query [ key ] ; if ( typeof value === "string" && ! relativeUrl . getParam ( key ) ) { relativeUrl . setParam ( key , value ) ; } } ) ; return relativeUrl . toString ( ) ; } export function redirectTo ( request , h , targetUrl , params = { } ) { if ( targetUrl . startsWith ( "http" ) ) { return h . redirect ( targetUrl ) ; } const url = redirectUrl ( request , targetUrl , params ) ; return h . redirect ( url ) ; } export const idFromFilename = ( filename ) => { return filename . replace ( /govsite\.|\.json|/gi , "" ) ; } ;