This commit is contained in:
Luiz H. R. Silva 2024-06-19 17:47:48 -03:00
parent a6205f1ab6
commit e580643abc
40 changed files with 329 additions and 507 deletions

View file

@ -3,26 +3,28 @@ import type { z } from "zod"
import type { zAmbiente } from "../ts/ambiente"
import { urlAutenticacao } from "./_urlAutenticacao"
type tipoPostCodigoContaSite = { site: string }
import node_fetch from "cross-fetch"
export const codigoContaSite = async ({
ambiente,
post,
buscar,
}: {
ambiente: z.infer<typeof zAmbiente>
post: tipoPostCodigoContaSite
/** função que conecta com a API */
buscar: (
url: string,
post: tipoPostCodigoContaSite,
) => Promise<tipoResposta<string>>
}): Promise<tipoResposta<string>> => {
const url = `${urlAutenticacao(ambiente)}/api/codigo_prefeitura_site`
try {
const resp = await buscar(url, post).catch((e) =>
respostaComuns.erro(`erro ao buscar código do site: ${e}`),
)
const resp = await node_fetch(url, {
method: "POST",
body: JSON.stringify(post),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.catch((e) =>
respostaComuns.erro("Erro ao enviar registros", [e.message]),
)
.then((r) => r as tipoResposta<string>)
return resp
} catch (e) {

View file

@ -1,3 +1,4 @@
import node_fetch from "cross-fetch"
import { respostaComuns, type tipoResposta } from "p-respostas"
import type { z } from "zod"
import type { zAmbiente } from "../ts/ambiente"
@ -15,14 +16,9 @@ export type tipoUsuarioExterno = {
export const usuarios_quipo_governo = async ({
token_produto,
ambiente,
buscar,
}: {
ambiente: z.infer<typeof zAmbiente>
token_produto: string
buscar: (
url: string,
headers: { [k: string]: string },
) => Promise<tipoResposta<tipoUsuarioExterno[]>>
}): Promise<tipoResposta<tipoUsuarioExterno[]>> => {
const url = `${urlAutenticacao(ambiente)}/api/usuarios_quipo_governo`
@ -32,7 +28,12 @@ export const usuarios_quipo_governo = async ({
token: token_produto,
}
return buscar(url, headers).catch((e) =>
respostaComuns.erro(`erro ao buscar usuários quipo governo: ${e}`),
)
return node_fetch(url, {
headers,
})
.then((r) => r.json())
.catch((e) =>
respostaComuns.erro("Erro ao buscar usuários quipo governo", [e.message]),
)
.then((r) => r as tipoResposta<tipoUsuarioExterno[]>)
}

View file

@ -1,6 +1,7 @@
import type { tipoResposta } from "p-respostas"
import { urlAutenticacao } from "./_urlAutenticacao"
type tipoPostValidarTokem = { token: string }
import node_fetch from "cross-fetch"
import type { z } from "zod"
import type { zAmbiente } from "../ts/ambiente"
@ -8,20 +9,20 @@ import type { zAmbiente } from "../ts/ambiente"
export const validarToken = async ({
ambiente,
post,
buscar,
}: {
ambiente: z.infer<typeof zAmbiente>
post: tipoPostValidarTokem
/** função que conecta com a API */
buscar: (
url: string,
post: tipoPostValidarTokem,
) => Promise<tipoResposta<any>>
}): Promise<"valido" | "erro"> => {
const url = `${urlAutenticacao(ambiente)}/api/validar_token`
try {
const resposta = await buscar(url, post)
const resposta = await node_fetch(url, {
method: "POST",
body: JSON.stringify(post),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.then((r) => r as tipoResposta<any>)
.then((resposta) =>
resposta.eCerto ? ("valido" as const) : ("erro" as const),
)