import {z} from 'zod' export type CreateApiTransferRequestsArgs = {body: {transferRequest: {transferMethod?: ('pix' | 'ted' | 'tef') | undefined, apiPartnerTransactionUuid: string, value: number, callbackUrl?: string | undefined}, beneficiary: {name?: string | undefined, documentNumber?: string | undefined, bankCode?: string | undefined, agency?: string | undefined, account?: string | undefined, uuid?: string | undefined, keyType?: ('evp' | 'cpf' | 'cnpj' | 'email' | 'phone') | undefined, key?: string | undefined}}}; export type CreateApiTransferRequestsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiTransferRequestsResponse = z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({apiPartnerTransactionUuid: z.string().optional(), originBankAccount: z.object({agency: z.string().optional(), account: z.string().optional(), name: z.string().optional()}).optional(), associatedFund: z.unknown(), beneficiary: z.object({companyName: z.unknown(), destinationAccount: z.object({agency: z.string().optional(), account: z.string().optional(), name: z.string().optional()}).optional()}).optional(), transferDetails: z.object({createdAt: z.string().optional(), value: z.string().optional(), source: z.string().optional(), transferMethod: z.string().optional(), approvalStatus: z.string().optional(), transferStatus: z.unknown()}).optional()}).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 }; /** * Nova transferĂȘncia * * POST /transfer_requests */ export const createApiTransferRequests = async (args: CreateApiTransferRequestsArgs, options?: CreateApiTransferRequestsOptions) => { const {baseUrl = "https://pagamentos.grafeno.be/api/v2", fetch: fetchFn = fetch, sec0, sec1} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/transfer_requests${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(createApiTransferRequestsResponse, await res.json()) };