|
1 | 1 | import { actionAsyncStorage } from '../../server/app-render/action-async-storage.external'
|
2 | 2 | import { RedirectStatusCode } from './redirect-status-code'
|
3 |
| - |
4 |
| -const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT' |
5 |
| - |
6 |
| -export enum RedirectType { |
7 |
| - push = 'push', |
8 |
| - replace = 'replace', |
9 |
| -} |
10 |
| - |
11 |
| -export type RedirectError = Error & { |
12 |
| - digest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};` |
13 |
| -} |
| 3 | +import { |
| 4 | + RedirectType, |
| 5 | + type RedirectError, |
| 6 | + isRedirectError, |
| 7 | + REDIRECT_ERROR_CODE, |
| 8 | +} from './redirect-error' |
14 | 9 |
|
15 | 10 | export function getRedirectError(
|
16 | 11 | url: string,
|
@@ -68,39 +63,6 @@ export function permanentRedirect(
|
68 | 63 | throw getRedirectError(url, type, RedirectStatusCode.PermanentRedirect)
|
69 | 64 | }
|
70 | 65 |
|
71 |
| -/** |
72 |
| - * Checks an error to determine if it's an error generated by the |
73 |
| - * `redirect(url)` helper. |
74 |
| - * |
75 |
| - * @param error the error that may reference a redirect error |
76 |
| - * @returns true if the error is a redirect error |
77 |
| - */ |
78 |
| -export function isRedirectError(error: unknown): error is RedirectError { |
79 |
| - if ( |
80 |
| - typeof error !== 'object' || |
81 |
| - error === null || |
82 |
| - !('digest' in error) || |
83 |
| - typeof error.digest !== 'string' |
84 |
| - ) { |
85 |
| - return false |
86 |
| - } |
87 |
| - |
88 |
| - const digest = error.digest.split(';') |
89 |
| - const [errorCode, type] = digest |
90 |
| - const destination = digest.slice(2, -2).join(';') |
91 |
| - const status = digest.at(-2) |
92 |
| - |
93 |
| - const statusCode = Number(status) |
94 |
| - |
95 |
| - return ( |
96 |
| - errorCode === REDIRECT_ERROR_CODE && |
97 |
| - (type === 'replace' || type === 'push') && |
98 |
| - typeof destination === 'string' && |
99 |
| - !isNaN(statusCode) && |
100 |
| - statusCode in RedirectStatusCode |
101 |
| - ) |
102 |
| -} |
103 |
| - |
104 | 66 | /**
|
105 | 67 | * Returns the encoded URL from the error if it's a RedirectError, null
|
106 | 68 | * otherwise. Note that this does not validate the URL returned.
|
|
0 commit comments