diff --git a/dist/tipagemRotas.d.ts b/dist/tipagemRotas.d.ts index a624209..cc6c976 100644 --- a/dist/tipagemRotas.d.ts +++ b/dist/tipagemRotas.d.ts @@ -1,14 +1,21 @@ -/** Gerar uma classe que facilita a gestão de rotas com a tipagem das querys */ +/** Gerar uma classe que facilita a gestão de rotas com a tipagem das querys + * + * Definições: + * + * caminho = "/aplicacao/funcionalidade" + * + * endereco = "http://localhost:3000/aplicacao/funcionalidade" + * + * parametros = {nome:"José"} + */ export declare class TipagemRotas { - /** Prefixo da url */ - PREFIXO: string | undefined; - _caminhoParcial: string; + _partesCaminho: string[]; /** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial - ** export const mCaminho = new TipagemRotas<{q:string}>("/caminho") + ** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade") */ - constructor(caminhoParcial: string, PREFIXO: string | undefined); + constructor(...caminhos: string[]); /** Retorna o caminho completo da rota ** console.log(mCaminho.caminho) ** "/caminho" @@ -24,7 +31,7 @@ export declare class TipagemRotas("/caminho") + ** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade") */ - constructor(caminhoParcial, PREFIXO) { - /** Prefixo da url */ - this.PREFIXO = undefined; - this._caminhoParcial = caminhoParcial; - if (PREFIXO) - this.PREFIXO = PREFIXO; + constructor(...caminhos) { + this._partesCaminho = []; + caminhos.forEach((caminho) => { + String(caminho) + .split("/") + .forEach((parte) => { + if (parte) { + this._partesCaminho.push(parte); + } + }); + }); } /** Retorna o caminho completo da rota ** console.log(mCaminho.caminho) ** "/caminho" */ get caminho() { - return this.PREFIXO + this._caminhoParcial; + return `/${this._partesCaminho.join("/")}`; } /** Define o caminho completo da rota ** mCaminho.caminho = "/novoCaminho" @@ -26,15 +40,15 @@ class TipagemRotas { ** "/novoCaminho" ** */ set caminho(caminhoParcial) { - this._caminhoParcial = caminhoParcial; + this._partesCaminho = caminhoParcial.split("/").filter((parte) => parte); } /** Retorna o caminho completo da rota com a query ** console.log(mCaminho.resolve({q:"query"})) ** "http://localhost:3000/caminho?q=query" */ - resolve(query) { - const url = new URL(window.location.href); - url.pathname = this.PREFIXO + this._caminhoParcial; + endereco(query) { + const url = new URL(typeof window !== "undefined" ? window.location.href : "http://localhost"); + url.pathname = this.caminho; const queryKeys = typeof query == "object" && query ? Object.entries(query) : [[query, ""]]; for (const [key, value] of queryKeys) { url.searchParams.set(String(key), value); @@ -46,14 +60,16 @@ class TipagemRotas { ** window.location.href = "http://localhost:3000/caminho?q=query" */ ir(query) { - window.location.href = this.resolve(query); + if (typeof window != "undefined") { + window.location.href = this.endereco(query); + } } /** Retorna os parametros da url ** console.log(mCaminho.parametros()) ** {q:"query"} */ parametros() { - const url = new URL(window.location.href); + const url = new URL(typeof window !== "undefined" ? window.location.href : "http://localhost"); const query = url.searchParams; const queryObj = Object.fromEntries(query.entries()); return queryObj; diff --git a/dist/tipagemRotas.js.map b/dist/tipagemRotas.js.map index 2abfef2..c2b3a34 100644 --- a/dist/tipagemRotas.js.map +++ b/dist/tipagemRotas.js.map @@ -1 +1 @@ -{"version":3,"file":"tipagemRotas.js","sourceRoot":"","sources":["../src/tipagemRotas.ts"],"names":[],"mappings":";AAAA,qFAAqF;;;AAErF,MAAa,YAAY;IAKvB;;OAEG;IACH,YAAY,cAAsB,EAAE,OAA2B;QAP/D,qBAAqB;QACrB,YAAO,GAAuB,SAAS,CAAA;QAOrC,IAAI,CAAC,eAAe,GAAG,cAAc,CAAA;QACrC,IAAI,OAAO;YAAE,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACrC,CAAC;IAED;;;OAGG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,eAAe,CAAA;IAC5C,CAAC;IACD;;;;UAIM;IACN,IAAI,OAAO,CAAC,cAAsB;QAChC,IAAI,CAAC,eAAe,GAAG,cAAc,CAAA;IACvC,CAAC;IAED;;;OAGG;IAEH,OAAO,CAAC,KAAQ;QACd,MAAM,GAA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CAAC;IAED;;;OAGG;IACH,EAAE,CAAC,KAAQ;QACT,IAAI,OAAO,MAAM,IAAI,WAAW,EAAE,CAAC;YACjC,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC7C,CAAC;IACH,CAAC;IAED;;;OAGG;IAEH,UAAU;QACR,MAAM,GAAG,GAAG,IAAI,GAAG,CACjB,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,kBAAkB,CAC1E,CAAA;QACD,MAAM,KAAK,GAAG,GAAG,CAAC,YAAY,CAAA;QAC9B,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;QACpD,OAAO,QAAa,CAAA;IACtB,CAAC;CACF;AA9ED,oCA8EC"} \ No newline at end of file diff --git a/package.json b/package.json index 20491db..06d9d7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "p-comuns", - "version": "0.42.0", + "version": "0.43.0", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/tipagemRotas.ts b/src/tipagemRotas.ts index e4fd4f6..f4e4632 100644 --- a/src/tipagemRotas.ts +++ b/src/tipagemRotas.ts @@ -1,16 +1,30 @@ -/** Gerar uma classe que facilita a gestão de rotas com a tipagem das querys */ +/** Gerar uma classe que facilita a gestão de rotas com a tipagem das querys + * + * Definições: + * + * caminho = "/aplicacao/funcionalidade" + * + * endereco = "http://localhost:3000/aplicacao/funcionalidade" + * + * parametros = {nome:"José"} + */ -export class TipagemRotas { - /** Prefixo da url */ - PREFIXO: string | undefined = undefined +export class TipagemRotas { + _partesCaminho: string[] = [] - _caminhoParcial: string /** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial - ** export const mCaminho = new TipagemRotas<{q:string}>("/caminho") + ** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade") */ - constructor(caminhoParcial: string, PREFIXO: string | undefined) { - this._caminhoParcial = caminhoParcial - if (PREFIXO) this.PREFIXO = PREFIXO + constructor(...caminhos: string[]) { + caminhos.forEach((caminho) => { + String(caminho) + .split("/") + .forEach((parte) => { + if (parte) { + this._partesCaminho.push(parte) + } + }) + }) } /** Retorna o caminho completo da rota @@ -18,7 +32,7 @@ export class TipagemRotas { ** "/caminho" */ get caminho() { - return this.PREFIXO + this._caminhoParcial + return `/${this._partesCaminho.join("/")}` } /** Define o caminho completo da rota ** mCaminho.caminho = "/novoCaminho" @@ -26,7 +40,7 @@ export class TipagemRotas { ** "/novoCaminho" ** */ set caminho(caminhoParcial: string) { - this._caminhoParcial = caminhoParcial + this._partesCaminho = caminhoParcial.split("/").filter((parte) => parte) } /** Retorna o caminho completo da rota com a query @@ -34,9 +48,13 @@ export class TipagemRotas { ** "http://localhost:3000/caminho?q=query" */ - resolve(query: T) { - const url = new URL(window.location.href) - url.pathname = this.PREFIXO + this._caminhoParcial + endereco(query: T) { + const url = new URL( + typeof window !== "undefined" ? window.location.href : "http://localhost", + ) + + url.pathname = this.caminho + const queryKeys = typeof query == "object" && query ? Object.entries(query) : [[query, ""]] @@ -51,7 +69,9 @@ export class TipagemRotas { ** window.location.href = "http://localhost:3000/caminho?q=query" */ ir(query: T) { - window.location.href = this.resolve(query) + if (typeof window != "undefined") { + window.location.href = this.endereco(query) + } } /** Retorna os parametros da url @@ -60,7 +80,9 @@ export class TipagemRotas { */ parametros() { - const url = new URL(window.location.href) + const url = new URL( + typeof window !== "undefined" ? window.location.href : "http://localhost", + ) const query = url.searchParams const queryObj = Object.fromEntries(query.entries()) return queryObj as T