import {z} from 'zod' export type Banks = string; export type AccountType = 'CONTA_CORRENTE_INDIVIDUAL' | 'CONTA_CORRENTE_CONJUNTA' | 'CONTA_POUPANCA_CONJUNTA' | 'CONTA_POUPANCA_INDIVIDUAL'; export type RequestTransfer = {value?: number | undefined, ccb?: string | undefined, bankAccount?: string | undefined, bankAccountDigit?: string | undefined, bankBranch?: string | undefined, bank?: Banks | undefined, accountType?: AccountType | undefined, document?: string | undefined}; export type CreateApiV21TransferRequestArgs = {body: RequestTransfer}; export type CreateApiV21TransferRequestOptions = {baseUrl?: string | undefined, partnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const banks = z.string(); export const accountType = z.enum(["CONTA_CORRENTE_INDIVIDUAL", "CONTA_CORRENTE_CONJUNTA", "CONTA_POUPANCA_CONJUNTA", "CONTA_POUPANCA_INDIVIDUAL"]); export const responseTransfer = z.object({id: z.string().optional(), ccbNumber: z.string().optional(), value: z.number().optional(), bankAccount: z.string().optional(), bankAccountDigit: z.string().optional(), bankBranch: z.string().optional(), bank: banks.optional(), accountType: accountType.optional(), document: z.string().optional(), status: z.string().optional(), creditId: z.number().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 }; /** * Transfer money between account * * POST /v2.1/transfer_request */ export const createApiV21TransferRequest = async (args: CreateApiV21TransferRequestArgs, options?: CreateApiV21TransferRequestOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, partnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (partnerAuth !== undefined) headers.set('authorization', `Bearer ${partnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2.1/transfer_request${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(responseTransfer, await res.json()) };