63 lines
No EOL
2.1 KiB
JavaScript
63 lines
No EOL
2.1 KiB
JavaScript
"use strict";
|
|
/** Gerar uma classe que facilita a gestão de rotas com a tipagem das querys */
|
|
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")
|
|
*/
|
|
constructor(caminhoParcial, PREFIXO) {
|
|
/** Prefixo da url */
|
|
this.PREFIXO = undefined;
|
|
this._caminhoParcial = caminhoParcial;
|
|
if (PREFIXO)
|
|
this.PREFIXO = PREFIXO;
|
|
}
|
|
/** Retorna o caminho completo da rota
|
|
** console.log(mCaminho.caminho)
|
|
** "/caminho"
|
|
*/
|
|
get caminho() {
|
|
return this.PREFIXO + this._caminhoParcial;
|
|
}
|
|
/** Define o caminho completo da rota
|
|
** mCaminho.caminho = "/novoCaminho"
|
|
** console.log(mCaminho.caminho)
|
|
** "/novoCaminho"
|
|
** */
|
|
set caminho(caminhoParcial) {
|
|
this._caminhoParcial = caminhoParcial;
|
|
}
|
|
/** 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;
|
|
const queryKeys = typeof query == "object" && query ? Object.entries(query) : [[query, ""]];
|
|
for (const [key, value] of queryKeys) {
|
|
url.searchParams.set(String(key), value);
|
|
}
|
|
return url.href;
|
|
}
|
|
/** Vai para a url
|
|
** mCaminho.ir({q:"query"})
|
|
** window.location.href = "http://localhost:3000/caminho?q=query"
|
|
*/
|
|
ir(query) {
|
|
window.location.href = this.resolve(query);
|
|
}
|
|
/** Retorna os parametros da url
|
|
** console.log(mCaminho.parametros())
|
|
** {q:"query"}
|
|
*/
|
|
parametros() {
|
|
const url = new URL(window.location.href);
|
|
const query = url.searchParams;
|
|
const queryObj = Object.fromEntries(query.entries());
|
|
return queryObj;
|
|
}
|
|
}
|
|
exports.TipagemRotas = TipagemRotas;
|
|
//# sourceMappingURL=tipagemRotas.js.map
|