melhoria de rotas
This commit is contained in:
parent
a5ca1748af
commit
7971b508c6
5 changed files with 86 additions and 41 deletions
|
|
@ -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