melhorias na tipagem de rotas

This commit is contained in:
Luiz Silva 2025-03-23 08:47:15 -03:00
parent 7d4fe4ad61
commit 2cdac5c970
5 changed files with 30 additions and 11 deletions

View file

@ -11,12 +11,17 @@
export class TipagemRotas<T extends { [q: string]: string | undefined }> {
_partesCaminho: string[] = []
_acaoIr: undefined | ((endereco: string) => undefined)
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial
** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade")
*/
constructor(...caminhos: string[]) {
caminhos.forEach((caminho) => {
constructor({
caminhos,
acaoIr,
}: { caminhos: string[] | string; acaoIr: (endereco: string) => undefined }) {
this._acaoIr = acaoIr
;[Array.isArray(caminhos) ? caminhos : [caminhos]].forEach((caminho) => {
String(caminho)
.split("/")
.forEach((parte) => {
@ -86,8 +91,12 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
** window.location.href = "http://localhost:3000/caminho?q=query"
*/
ir(query: T) {
if (typeof window != "undefined") {
window.location.href = this.endereco(query)
if (this._acaoIr) {
this._acaoIr(this.endereco(query))
} else {
if (typeof window != "undefined") {
window.location.href = this.endereco(query)
}
}
}