67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import node_fetch from "cross-fetch"
|
|
import type { tipoResposta } from "p-respostas"
|
|
import { respostaComuns } from "p-respostas"
|
|
import { z } from "zod"
|
|
import { operadores_pilao, urlPilao, type zp_produto_conta } from "./variaveis"
|
|
import type { visoes } from "./visoes"
|
|
|
|
export const z_filtro = z.object({
|
|
coluna: z.string(),
|
|
valor: z.any(),
|
|
operador: operadores_pilao,
|
|
})
|
|
|
|
export const serie_consultar =
|
|
(cliente: z.infer<typeof zp_produto_conta>) =>
|
|
<T extends keyof typeof visoes>(
|
|
tipoVisao: T,
|
|
parametros: z.infer<(typeof visoes)[T]>,
|
|
) => {
|
|
const dados = async (): Promise<
|
|
tipoResposta<{
|
|
registros: any[]
|
|
legenda: string
|
|
serie: z.infer<(typeof visoes)[T]>
|
|
}>
|
|
> => {
|
|
const url = new URL(
|
|
`${urlPilao(cliente.emDesenvolvimento).api}/${
|
|
tipoVisao
|
|
}/${cliente.produto}/${cliente.conta}`,
|
|
)
|
|
|
|
const resp = await node_fetch(url.toString(), {
|
|
method: "POST",
|
|
body: JSON.stringify(parametros),
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
.then((r) => r.json())
|
|
.catch((e) =>
|
|
respostaComuns.erro("Erro ao enviar registros", [e.message]),
|
|
)
|
|
.then((r) => r as tipoResposta<any>)
|
|
|
|
if (cliente.ver_log)
|
|
console.log(
|
|
`[PILÃO]: buscar dados de "${JSON.stringify(parametros)}" para "${url.href}".`,
|
|
)
|
|
|
|
return resp
|
|
}
|
|
|
|
const url = (): string => {
|
|
const vUrl = new URL(
|
|
`${urlPilao(cliente.emDesenvolvimento).site}/${tipoVisao}/${cliente.produto}/${cliente.conta}`,
|
|
)
|
|
const serie = encodeURIComponent(JSON.stringify(parametros, null, 2))
|
|
if (cliente.ver_log)
|
|
console.log(
|
|
`[PILÃO]: Serie Consultar url de "${JSON.stringify(serie)}" para "${vUrl.href}".`,
|
|
)
|
|
return `${vUrl.href}?serie=${serie}`
|
|
}
|
|
return {
|
|
dados,
|
|
url,
|
|
}
|
|
}
|