melhoria de rotas

This commit is contained in:
Luiz Silva 2024-09-15 12:45:30 -03:00
parent a5ca1748af
commit 7971b508c6
5 changed files with 86 additions and 41 deletions

View file

@ -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
export declare class TipagemRotas<T = { *
[q: string]: string; * Definições:
}> { *
/** Prefixo da url */ * caminho = "/aplicacao/funcionalidade"
PREFIXO: string | undefined; *
_caminhoParcial: string; * endereco = "http://localhost:3000/aplicacao/funcionalidade"
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial *
** export const mCaminho = new TipagemRotas<{q:string}>("/caminho") * parametros = {nome:"José"}
*/ */
constructor(caminhoParcial: string, PREFIXO: string | undefined); export declare class TipagemRotas<T = {
[q: string]: string | undefined;
}> {
_partesCaminho: string[];
/** 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[]);
/** Retorna o caminho completo da rota /** Retorna o caminho completo da rota
** console.log(mCaminho.caminho) ** console.log(mCaminho.caminho)
** "/caminho" ** "/caminho"
@ -24,7 +31,7 @@ export declare class TipagemRotas<T = {
** console.log(mCaminho.resolve({q:"query"})) ** console.log(mCaminho.resolve({q:"query"}))
** "http://localhost:3000/caminho?q=query" ** "http://localhost:3000/caminho?q=query"
*/ */
resolve(query: T): string; endereco(query: T): string;
/** Vai para a url /** Vai para a url
** mCaminho.ir({q:"query"}) ** mCaminho.ir({q:"query"})
** window.location.href = "http://localhost:3000/caminho?q=query" ** window.location.href = "http://localhost:3000/caminho?q=query"

46
dist/tipagemRotas.js vendored
View file

@ -1,24 +1,38 @@
"use strict"; "use strict";
/** 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é"}
*/
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.TipagemRotas = void 0; exports.TipagemRotas = void 0;
class TipagemRotas { class TipagemRotas {
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial /** 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, PREFIXO) { constructor(...caminhos) {
/** Prefixo da url */ this._partesCaminho = [];
this.PREFIXO = undefined; caminhos.forEach((caminho) => {
this._caminhoParcial = caminhoParcial; String(caminho)
if (PREFIXO) .split("/")
this.PREFIXO = PREFIXO; .forEach((parte) => {
if (parte) {
this._partesCaminho.push(parte);
}
});
});
} }
/** Retorna o caminho completo da rota /** Retorna o caminho completo da rota
** console.log(mCaminho.caminho) ** console.log(mCaminho.caminho)
** "/caminho" ** "/caminho"
*/ */
get caminho() { get caminho() {
return this.PREFIXO + this._caminhoParcial; return `/${this._partesCaminho.join("/")}`;
} }
/** Define o caminho completo da rota /** Define o caminho completo da rota
** mCaminho.caminho = "/novoCaminho" ** mCaminho.caminho = "/novoCaminho"
@ -26,15 +40,15 @@ class TipagemRotas {
** "/novoCaminho" ** "/novoCaminho"
** */ ** */
set caminho(caminhoParcial) { set caminho(caminhoParcial) {
this._caminhoParcial = caminhoParcial; this._partesCaminho = caminhoParcial.split("/").filter((parte) => parte);
} }
/** Retorna o caminho completo da rota com a query /** Retorna o caminho completo da rota com a query
** console.log(mCaminho.resolve({q:"query"})) ** console.log(mCaminho.resolve({q:"query"}))
** "http://localhost:3000/caminho?q=query" ** "http://localhost:3000/caminho?q=query"
*/ */
resolve(query) { endereco(query) {
const url = new URL(window.location.href); const url = new URL(typeof window !== "undefined" ? window.location.href : "http://localhost");
url.pathname = this.PREFIXO + this._caminhoParcial; url.pathname = this.caminho;
const queryKeys = typeof query == "object" && query ? Object.entries(query) : [[query, ""]]; const queryKeys = typeof query == "object" && query ? Object.entries(query) : [[query, ""]];
for (const [key, value] of queryKeys) { for (const [key, value] of queryKeys) {
url.searchParams.set(String(key), value); url.searchParams.set(String(key), value);
@ -46,14 +60,16 @@ class TipagemRotas {
** window.location.href = "http://localhost:3000/caminho?q=query" ** window.location.href = "http://localhost:3000/caminho?q=query"
*/ */
ir(query) { ir(query) {
window.location.href = this.resolve(query); if (typeof window != "undefined") {
window.location.href = this.endereco(query);
}
} }
/** Retorna os parametros da url /** Retorna os parametros da url
** console.log(mCaminho.parametros()) ** console.log(mCaminho.parametros())
** {q:"query"} ** {q:"query"}
*/ */
parametros() { 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 query = url.searchParams;
const queryObj = Object.fromEntries(query.entries()); const queryObj = Object.fromEntries(query.entries());
return queryObj; return queryObj;

View file

@ -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,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QA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,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"}

View file

@ -1,6 +1,6 @@
{ {
"name": "p-comuns", "name": "p-comuns",
"version": "0.42.0", "version": "0.43.0",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View file

@ -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
*
export class TipagemRotas<T = { [q: string]: string }> { * Definições:
/** Prefixo da url */ *
PREFIXO: string | undefined = undefined * caminho = "/aplicacao/funcionalidade"
*
_caminhoParcial: string * endereco = "http://localhost:3000/aplicacao/funcionalidade"
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial *
** export const mCaminho = new TipagemRotas<{q:string}>("/caminho") * parametros = {nome:"José"}
*/ */
constructor(caminhoParcial: string, PREFIXO: string | undefined) {
this._caminhoParcial = caminhoParcial export class TipagemRotas<T = { [q: string]: string | undefined }> {
if (PREFIXO) this.PREFIXO = PREFIXO _partesCaminho: string[] = []
/** 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) => {
String(caminho)
.split("/")
.forEach((parte) => {
if (parte) {
this._partesCaminho.push(parte)
}
})
})
} }
/** Retorna o caminho completo da rota /** Retorna o caminho completo da rota
@ -18,7 +32,7 @@ export class TipagemRotas<T = { [q: string]: string }> {
** "/caminho" ** "/caminho"
*/ */
get caminho() { get caminho() {
return this.PREFIXO + this._caminhoParcial return `/${this._partesCaminho.join("/")}`
} }
/** Define o caminho completo da rota /** Define o caminho completo da rota
** mCaminho.caminho = "/novoCaminho" ** mCaminho.caminho = "/novoCaminho"
@ -26,7 +40,7 @@ export class TipagemRotas<T = { [q: string]: string }> {
** "/novoCaminho" ** "/novoCaminho"
** */ ** */
set caminho(caminhoParcial: string) { set caminho(caminhoParcial: string) {
this._caminhoParcial = caminhoParcial this._partesCaminho = caminhoParcial.split("/").filter((parte) => parte)
} }
/** Retorna o caminho completo da rota com a query /** Retorna o caminho completo da rota com a query
@ -34,9 +48,13 @@ export class TipagemRotas<T = { [q: string]: string }> {
** "http://localhost:3000/caminho?q=query" ** "http://localhost:3000/caminho?q=query"
*/ */
resolve(query: T) { endereco(query: T) {
const url = new URL(window.location.href) const url = new URL(
url.pathname = this.PREFIXO + this._caminhoParcial typeof window !== "undefined" ? window.location.href : "http://localhost",
)
url.pathname = this.caminho
const queryKeys = const queryKeys =
typeof query == "object" && query ? Object.entries(query) : [[query, ""]] typeof query == "object" && query ? Object.entries(query) : [[query, ""]]
@ -51,7 +69,9 @@ export class TipagemRotas<T = { [q: string]: string }> {
** window.location.href = "http://localhost:3000/caminho?q=query" ** window.location.href = "http://localhost:3000/caminho?q=query"
*/ */
ir(query: T) { ir(query: T) {
window.location.href = this.resolve(query) if (typeof window != "undefined") {
window.location.href = this.endereco(query)
}
} }
/** Retorna os parametros da url /** Retorna os parametros da url
@ -60,7 +80,9 @@ export class TipagemRotas<T = { [q: string]: string }> {
*/ */
parametros() { 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 query = url.searchParams
const queryObj = Object.fromEntries(query.entries()) const queryObj = Object.fromEntries(query.entries())
return queryObj as T return queryObj as T