melhoria de rotas
This commit is contained in:
parent
a5ca1748af
commit
7971b508c6
5 changed files with 86 additions and 41 deletions
23
dist/tipagemRotas.d.ts
vendored
23
dist/tipagemRotas.d.ts
vendored
|
|
@ -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<T = {
|
||||
[q: string]: string;
|
||||
[q: string]: string | undefined;
|
||||
}> {
|
||||
/** 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<T = {
|
|||
** console.log(mCaminho.resolve({q:"query"}))
|
||||
** "http://localhost:3000/caminho?q=query"
|
||||
*/
|
||||
resolve(query: T): string;
|
||||
endereco(query: T): string;
|
||||
/** Vai para a url
|
||||
** mCaminho.ir({q:"query"})
|
||||
** window.location.href = "http://localhost:3000/caminho?q=query"
|
||||
|
|
|
|||
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;
|
||||
|
|
|
|||
2
dist/tipagemRotas.js.map
vendored
2
dist/tipagemRotas.js.map
vendored
|
|
@ -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;QA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AM,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,GAAG,CAAC,IAAI,CAAA;IACjB,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"}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "p-comuns",
|
||||
"version": "0.42.0",
|
||||
"version": "0.43.0",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -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<T = { [q: string]: string }> {
|
||||
/** Prefixo da url */
|
||||
PREFIXO: string | undefined = undefined
|
||||
export class TipagemRotas<T = { [q: string]: string | undefined }> {
|
||||
_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<T = { [q: string]: string }> {
|
|||
** "/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<T = { [q: string]: string }> {
|
|||
** "/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<T = { [q: string]: string }> {
|
|||
** "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<T = { [q: string]: string }> {
|
|||
** 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<T = { [q: string]: string }> {
|
|||
*/
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue