import {z} from 'zod' export type AddWallet = {amount: number, payment_gateway_id: number, gateway_id: number, buyer_name: string, buyer_email: string, buyer_address: string, buyer_city: string, buyer_state: string, buyer_country_iso2: string, buyer_phone: number, buyer_zipcode: number, credit_card_code: number, credit_card_expire: string, credit_card_name_on_card: string, credit_card_number: number}; export type CreateApiWalletsArgs = {token: unknown, body: AddWallet}; export type CreateApiWalletsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const wallet = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional(), amount: z.number().optional(), payment_gateway_id: z.number().int().optional(), is_payment_completed: z.number().int().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 }; /** * Creates a new wallet * * POST /wallets */ export const createApiWallets = async (args: CreateApiWalletsArgs, options: CreateApiWalletsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) const url = `${baseUrl.replace(/\/+$/, '')}/wallets${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(wallet, await res.json()) };