Skip to content

Commit 2a18d01

Browse files
authored
chore: document the RootRedirectConfig interface (#570)
1 parent 01d33c6 commit 2a18d01

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

packages/root/src/core/config.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,27 @@ export interface RootBuildConfig {
134134
}
135135

136136
export interface RootRedirectConfig {
137+
/**
138+
* The source path to redirect. Accepts placeholders in the format
139+
* `[key]` or `[...key]`. Use `[key]` for single segments and
140+
* `[...key]` for multi-segment wildcards.
141+
* @example "/old-path/[id]" or "/old-path/[...wildcard]"
142+
*/
137143
source: string;
144+
145+
/**
146+
* The destination to redirect to. Placeholders from the source can
147+
* optionally be inserted into the destination using the same
148+
* placeholder format.
149+
* @example "/new-path/[id]" or "/new-path/[...wildcard]"
150+
*/
138151
destination: string;
139-
type?: number;
152+
153+
/**
154+
* The redirect type (`301` = permanent, `302` = temporary). If unspecified,
155+
* defaults to `302` (temporary).
156+
*/
157+
type?: 301 | 302;
140158
}
141159

142160
export interface RootHeaderConfig {
@@ -218,7 +236,22 @@ export interface RootServerConfig {
218236
sessionCookieSecret?: string | string[];
219237

220238
/**
221-
* List of redirects.
239+
* List of redirects. Supports optional wildcards.
240+
*
241+
* @example
242+
* ```ts
243+
* redirects: [
244+
* {
245+
* source: '/old-path/[id]',
246+
* destination: '/new-path/[id]',
247+
* type: 301,
248+
* },
249+
* {
250+
* source: '/old-path/[...wildcard]',
251+
* destination: '/new-path/[...wildcard]',
252+
* },
253+
* ]
254+
* ```
222255
*/
223256
redirects?: RootRedirectConfig[];
224257

packages/root/src/middleware/redirects.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ function verifyRedirectConfig(redirect: RootRedirectConfig) {
4444
return true;
4545
}
4646

47+
/**
48+
* Replaces placeholders in a URL path format string with actual values.
49+
*
50+
* @param urlPathFormat The URL path format string containing parameter placeholders in the format `[key]` or `[...key]`.
51+
* @param params A map of parameter names to their corresponding values.
52+
* @returns The URL path with all parameter placeholders replaced by their corresponding values.
53+
*/
4754
function replaceParams(urlPathFormat: string, params: Record<string, string>) {
4855
const urlPath = urlPathFormat.replaceAll(
4956
/\[\[?(\.\.\.)?([\w\-_]*)\]?\]/g,

0 commit comments

Comments
 (0)