File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ export function redirect ( url : string , init : ResponseInit | number = 302 ) {
2+ let responseInit = init ;
3+
4+ if ( typeof responseInit === "number" ) {
5+ responseInit = { status : responseInit , headers : { } } ;
6+ } else if ( ! ( "status" in responseInit ) ) {
7+ responseInit = { ...responseInit , status : 302 } ;
8+ }
9+
10+ let headers = new Headers ( responseInit . headers ) ;
11+ headers . set ( "Location" , url ) ;
12+
13+ return new Response ( null , { ...responseInit , headers } ) ;
14+ }
Original file line number Diff line number Diff line change 1+ function isResponse ( value : unknown ) : value is Response {
2+ return value instanceof Response ;
3+ }
4+
5+ export async function catchResponse ( promise : Promise < unknown > ) {
6+ try {
7+ await promise ;
8+ throw new Error ( "Should have failed." ) ;
9+ } catch ( error ) {
10+ if ( isResponse ( error ) ) return error ;
11+ throw error ;
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments