import {z} from 'zod' export type CreateApiV1RoutinesCardToAccountArgs = {'Idempotency-Key'?: unknown, body: {name: string, amount: number, client_correlation_id: string, start_date?: string | undefined, source: {id: string}, destination: {id: string, user_id?: string | undefined}, debit_fee_percent?: number | undefined, ultimate_receiver_id?: string | undefined, ultimate_sender_id?: string | undefined}}; export type CreateApiV1RoutinesCardToAccountOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1RoutinesCardToAccountResponse = z.object({id: z.string().optional(), status: z.string().optional(), name: z.string().optional(), amount: z.number().optional(), source: z.object({id: z.string().optional()}).optional(), destination: z.object({id: z.string().optional(), user_id: z.string().optional()}).optional(), start_date: z.string().optional(), created: z.string().optional(), active: z.boolean().optional(), payment_route: z.literal("card_to_account").optional(), type: z.literal("one-time").optional(), blocked: z.boolean().optional(), debit_fee_percent: z.number().optional(), ultimate_receiver_id: z.string().optional(), ultimate_sender_id: z.string().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 }; /** * Create a card to account routine * * POST /v1/routines/card-to-account */ export const createApiV1RoutinesCardToAccount = async (args: CreateApiV1RoutinesCardToAccountArgs, options?: CreateApiV1RoutinesCardToAccountOptions) => { const {baseUrl = "https://api-sandbox.astra.finance", 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 (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/routines/card-to-account${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(createApiV1RoutinesCardToAccountResponse, await res.json()) };