( location: string, status = 302, headers?: Record<string, string> | Headers, )
| 33 | * @returns A `Response` object representing the HTTP redirect. |
| 34 | */ |
| 35 | export function createRedirectResponse( |
| 36 | location: string, |
| 37 | status = 302, |
| 38 | headers?: Record<string, string> | Headers, |
| 39 | ): Response { |
| 40 | if (ngDevMode && !isValidRedirectResponseCode(status)) { |
| 41 | throw new Error( |
| 42 | `Invalid redirect status code: ${status}. ` + |
| 43 | `Please use one of the following redirect response codes: ${[...VALID_REDIRECT_RESPONSE_CODES.values()].join(', ')}.`, |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | const resHeaders = headers instanceof Headers ? headers : new Headers(headers); |
| 48 | if (ngDevMode && resHeaders.has('location')) { |
| 49 | // eslint-disable-next-line no-console |
| 50 | console.warn( |
| 51 | `Location header "${resHeaders.get('location')}" will be ignored and set to "${location}".`, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | // Ensure unique values for Vary header |
| 56 | const varyArray = resHeaders.get('Vary')?.split(',') ?? []; |
| 57 | const varySet = new Set(['X-Forwarded-Prefix']); |
| 58 | for (const vary of varyArray) { |
| 59 | const value = vary.trim(); |
| 60 | |
| 61 | if (value) { |
| 62 | varySet.add(value); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | resHeaders.set('Vary', [...varySet].join(', ')); |
| 67 | resHeaders.set('Location', location); |
| 68 | |
| 69 | return new Response(null, { |
| 70 | status, |
| 71 | headers: resHeaders, |
| 72 | }); |
| 73 | } |
no test coverage detected