import {z} from 'zod' export type ContratacaoAtivaRequest = {annualRate: string, averbacaoDate?: string | undefined, borrowerCpf: string, borrowerName: string, cetAnnual: string, cetMonthly: string, creditorName: string, eSocialCategory: string, employerCnpj: string, employerLegalName: string, extendedTermEligible?: boolean | undefined, fgtsGuaranteePercent?: string | undefined, installmentAmount: string, installmentCount: number, iofAmount: string, loanProductVersionId: string, matricula: string, monthlyRate: string, numeroContrato: string, principalAmount: string, vinculoId: string}; export type CreateApiApiV1BrConsignadoContractsArgs = {body: ContratacaoAtivaRequest}; export type CreateApiApiV1BrConsignadoContractsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const contratacaoAtivaResponse = z.object({annualRate: z.string(), biometricEvidenceRef: z.string(), cetAnnual: z.string(), cetMonthly: z.string(), createdAt: z.string(), firstDeductionCompetencia: z.string(), id: z.string(), installmentAmount: z.string(), installmentCount: z.number().int(), iofAmount: z.string(), loanProductVersionId: z.string(), monthlyRate: z.string(), numeroContrato: z.string(), principalAmount: z.string(), signatureSessionRef: z.string(), state: z.string(), vinculoId: z.string()}); 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 }; /** * Originate a consignado privado contract (contratação ativa) * * POST /api/v1/br/consignado/contracts */ export const createApiApiV1BrConsignadoContracts = async (args: CreateApiApiV1BrConsignadoContractsArgs, options?: CreateApiApiV1BrConsignadoContractsOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/br/consignado/contracts${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(contratacaoAtivaResponse, await res.json()) };