import * as url from "url" ; import * as querystring from "querystring" ; import * as express from "express" ; import { AuthorizationEndpointResponse , OAuthError , Result } from "oauth2-nodejs" ; export class Navigation { public static redirect ( resp , uri , parameters ? , fragments ? ) { const targetUrl = url . parse ( uri , true ) ; if ( parameters ) { const query = targetUrl . query ; Object . keys ( parameters ) . forEach ( ( key ) : void => { const value = parameters [ key ] ; query [ key ] = typeof value === "string" ? value : String ( value ) ; } ) ; } if ( fragments ) { targetUrl . hash = `#${ querystring . stringify ( fragments ) }` ; } resp . redirect ( url . format ( targetUrl ) ) ; } public static backTo ( resp , result , redirectUri ) { if ( result . isSuccess ( ) ) { const response = result . value ; this . redirect ( resp , redirectUri , response . query , response . fragment ) ; } else { this . redirect ( resp , redirectUri , { "error" : result . error . getType ( ) } , { } ) ; } } public static sendError ( resp , error ) { resp . set ( "Content-Type" , "application/json; charset=UTF-8" ) ; resp . status ( error . code ) . send ( error . toJson ( ) ) ; } }