melhoria de rotas
This commit is contained in:
parent
a5ca1748af
commit
7971b508c6
5 changed files with 86 additions and 41 deletions
46
dist/tipagemRotas.js
vendored
46
dist/tipagemRotas.js
vendored
|
|
@ -1,24 +1,38 @@
|
|||
"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 });
|
||||
exports.TipagemRotas = void 0;
|
||||
class TipagemRotas {
|
||||
/** 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) {
|
||||
/** 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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue