melhorias de tipagem

This commit is contained in:
Luiz H. R. Silva 2024-06-20 08:11:17 -03:00
parent 1d5d3a48b4
commit afa28a0699
32 changed files with 435 additions and 486 deletions

View file

@ -0,0 +1,47 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import node_fetch from "cross-fetch";
import { respostaComuns } from "p-respostas";
import { z } from "zod";
import { PREFIXO, baseUrlPilao, z_tipo_coluna_base_dados, } from "./variaveis";
export const zp_registrar_base_dados = z.object({
tabela: z.string(),
colunas: z.array(z.object({
coluna: z.string(),
tipo: z_tipo_coluna_base_dados,
})),
});
//enviar registros para base de dados
export const zp_enviar_registros = z.object({
tabela: z.string(),
registros: z.array(z.record(z.string(), z.object({
valor: z.any(),
tipo: z_tipo_coluna_base_dados.optional().nullable(),
}))),
});
export const enviar_registros = ({ conta, produto, emDesenvolvimento }) => ({ registros, tabela, }) => __awaiter(void 0, void 0, void 0, function* () {
const url = new URL(`${baseUrlPilao(emDesenvolvimento)}${PREFIXO}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`);
const tamanhoBlocos = 1000;
while (registros.length > 0) {
const bloco = registros.splice(0, tamanhoBlocos);
const resp = yield node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela, registros: bloco }),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message]))
.then((r) => r);
if (resp.eErro) {
return resp;
}
}
return respostaComuns.valor(true);
});