melhorias

This commit is contained in:
Luiz Silva 2024-09-15 13:02:33 -03:00
parent 7971b508c6
commit de77a4c8b6
5 changed files with 31 additions and 5 deletions

View file

@ -48,7 +48,7 @@ export class TipagemRotas<T = { [q: string]: string | undefined }> {
** "http://localhost:3000/caminho?q=query"
*/
endereco(query: T) {
endereco(query: T, usarComoHash?: boolean) {
const url = new URL(
typeof window !== "undefined" ? window.location.href : "http://localhost",
)
@ -61,6 +61,12 @@ export class TipagemRotas<T = { [q: string]: string | undefined }> {
for (const [key, value] of queryKeys) {
url.searchParams.set(String(key), value)
}
if (usarComoHash) {
url.hash = `#${url.search}`
url.search = ""
}
return url.href
}
@ -85,6 +91,16 @@ export class TipagemRotas<T = { [q: string]: string | undefined }> {
)
const query = url.searchParams
const queryObj = Object.fromEntries(query.entries())
// pegar hash
const hash = url.hash
if (hash) {
const hashObj = Object.fromEntries(
new URLSearchParams(hash.slice(1)).entries(),
)
return { ...queryObj, ...hashObj } as T
}
return queryObj as T
}
}