From de77a4c8b6a84e5532b0e6f7b0d817b666d99a6b Mon Sep 17 00:00:00 2001 From: Luiz Silva Date: Sun, 15 Sep 2024 13:02:33 -0300 Subject: [PATCH] melhorias --- dist/tipagemRotas.d.ts | 2 +- dist/tipagemRotas.js | 12 +++++++++++- dist/tipagemRotas.js.map | 2 +- package.json | 2 +- src/tipagemRotas.ts | 18 +++++++++++++++++- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/dist/tipagemRotas.d.ts b/dist/tipagemRotas.d.ts index cc6c976..0267412 100644 --- a/dist/tipagemRotas.d.ts +++ b/dist/tipagemRotas.d.ts @@ -31,7 +31,7 @@ export declare class TipagemRotas { ** "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 { 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 { ) 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 } }