Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port useSearchParams and usePathname to pages
- Loading branch information
Showing
6 changed files
with
125 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,81 @@ | ||
import type { ParsedUrlQuery } from 'node:querystring' | ||
import { AppRouterInstance } from '../app-router-context' | ||
import { NextRouter } from './router' | ||
|
||
/** | ||
* adaptForAppRouterInstance implements the AppRouterInstance with a NextRouter. | ||
* | ||
* @param router the NextRouter to adapt | ||
* @returns an AppRouterInstance | ||
*/ | ||
export function adaptForAppRouterInstance( | ||
nextRouter: NextRouter | ||
router: NextRouter | ||
): AppRouterInstance { | ||
return { | ||
back(): void { | ||
nextRouter.back() | ||
router.back() | ||
}, | ||
forward(): void { | ||
nextRouter.forward() | ||
router.forward() | ||
}, | ||
refresh(): void { | ||
nextRouter.reload() | ||
router.reload() | ||
}, | ||
push(href: string): void { | ||
void nextRouter.push(href) | ||
void router.push(href) | ||
}, | ||
replace(href: string): void { | ||
void nextRouter.replace(href) | ||
void router.replace(href) | ||
}, | ||
prefetch(href: string): void { | ||
void nextRouter.prefetch(href) | ||
void router.prefetch(href) | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* transforms the ParsedUrlQuery into a URLSearchParams. | ||
* | ||
* @param query the query to transform | ||
* @returns URLSearchParams | ||
*/ | ||
function transformQuery(query: ParsedUrlQuery): URLSearchParams { | ||
const params = new URLSearchParams() | ||
|
||
for (const [name, value] of Object.entries(query)) { | ||
if (Array.isArray(value)) { | ||
for (const val of value) { | ||
params.append(name, val) | ||
} | ||
} else if (typeof value !== 'undefined') { | ||
params.append(name, value) | ||
} | ||
} | ||
|
||
return params | ||
} | ||
|
||
/** | ||
* adaptForSearchParams transforms the ParsedURLQuery into URLSearchParams. | ||
* | ||
* @param router the router that contains the query. | ||
* @returns the search params in the URLSearchParams format | ||
*/ | ||
export function adaptForSearchParams(router: NextRouter): URLSearchParams { | ||
if (!router.isReady || !router.query) { | ||
return new URLSearchParams() | ||
} | ||
|
||
return transformQuery(router.query) | ||
} | ||
|
||
/** | ||
* adaptForPathname adapts the `asPath` parameter from the router to a pathname. | ||
* | ||
* @param asPath the asPath parameter to transform that comes from the router | ||
* @returns pathname part of `asPath` | ||
*/ | ||
export function adaptForPathname(asPath: string): string { | ||
const url = new URL(asPath, 'http://f') | ||
return url.pathname | ||
} |