import {z} from 'zod' export type CriarPessoaRequest = {nome: string, codigointerno?: string | undefined, documento: string, documentoTipo: 'cpf' | 'cnpj', dataNasc?: string | undefined, rg?: string | undefined, rgOrgaoEmissor?: string | undefined, rgDataEmissao?: string | undefined, sexo?: ('M' | 'F' | 'NDI' | 'A') | undefined, email: string, telefone: string, celular?: string | undefined, rendaFamiliar?: number | undefined, estadoCivil?: number | undefined, dataCasamento?: string | undefined, pais?: string | undefined, estado?: string | undefined, cidade?: string | undefined, logradouro?: string | undefined, cep?: string | undefined, endereco?: string | undefined, bairro?: string | undefined, numero?: string | undefined, complemento?: string | undefined, naturalidade?: string | undefined, filiacaoPai?: string | undefined, filiacaoMae?: string | undefined, profissao?: string | undefined, observacoes?: string | undefined, inscEstadual?: string | undefined, inscMunicipal?: string | undefined, ativoLogin?: boolean | undefined, idclassificacao?: number | undefined, dataPrimeiraHabilitacaoCnh?: string | undefined, dataFimValidadeCnh?: string | undefined, trabalhoNomeEmpresa?: string | undefined, trabalhoCep?: string | undefined, trabalhoEndereco?: string | undefined, trabalhoBairro?: string | undefined, trabalhoNumero?: string | undefined, complementoTrabalho?: string | undefined, trabalhoEstado?: string | undefined, trabalhoCidade?: string | undefined, logradouroTrabalho?: string | undefined, trabalhoTelefone?: string | undefined, spcScore?: number | undefined, spcTaxaInadimplencia?: number | undefined, spcClasseRisco?: string | undefined, spcValorRestricao?: number | undefined, spcStatus?: ('A' | 'R' | 'P') | undefined, identificador_contato?: string | undefined}; export type CreateApiV3CadastrosPessoasArgs = {body: CriarPessoaRequest}; export type CreateApiV3CadastrosPessoasOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const criarPessoaResponse = z.object({status: z.string().optional(), code: z.number().int().optional(), data: z.object({id: z.number().int().optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Cadastra uma pessoa. * * POST /v3/cadastros/pessoas */ export const createApiV3CadastrosPessoas = async (args: CreateApiV3CadastrosPessoasArgs, options?: CreateApiV3CadastrosPessoasOptions) => { const {baseUrl = "https://integracao.cvcrm.com.br/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/cadastros/pessoas${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(criarPessoaResponse, await res.json()) };