import {z} from 'zod' export type GetApiPayoutServiceDestinationTokenAlternativesArgs = {destination_token: string, 'Idempotency-Key'?: string | undefined, destination_currency?: string | undefined, show_dynamic_tokens_only?: boolean | undefined}; export type GetApiPayoutServiceDestinationTokenAlternativesOptions = {baseUrl?: string | undefined, AUTHORIZERNAME?: string | undefined, fetch?: typeof fetch | undefined}; export const service = z.object({country_code: z.string().min(3).max(3), delivery_type: z.enum(["CASH_PICKUP", "BANK_DEPOSIT", "HOME_DELIVERY", "MOBILE_WALLET", "MASSPAY_CARD", "PAPER_CHECK", "BILL", "CRYPTOCURRENCY"]), payers: z.array(z.object({destination_token: z.string(), payer_logo: z.string().optional(), payer_name: z.string(), exchange_rate: z.array(z.object({currency_symbol: z.string(), exchange_rate: z.number()})), fee: z.number(), max_limit: z.number(), min_limit: z.number(), source_amount: z.number().optional(), number_of_locations: z.number().int().gte(0).optional(), estimated_availability: z.string().optional(), additional_description: z.string().optional(), is_dynamic_token: z.boolean(), is_variable_fee: z.boolean(), is_generic: z.boolean()}))}); 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 }; /** * Returns list of alternative service to a provided service * * GET /payout/service/{destination_token}/alternatives * * Also accepts auth: api_key */ export const getApiPayoutServiceDestinationTokenAlternatives = async (args: GetApiPayoutServiceDestinationTokenAlternativesArgs, options?: GetApiPayoutServiceDestinationTokenAlternativesOptions) => { const {baseUrl = "https://api.masspay.io/v1.0.0", fetch: fetchFn = fetch, AUTHORIZERNAME} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args.destination_currency != null) query.append("destination_currency", String(args.destination_currency)) if (args.show_dynamic_tokens_only != null) query.append("show_dynamic_tokens_only", String(args.show_dynamic_tokens_only)) if (AUTHORIZERNAME !== undefined) headers.set("Authorization", AUTHORIZERNAME) const url = `${baseUrl.replace(/\/+$/, '')}/payout/service/${encodeURIComponent(String(args.destination_token))}/alternatives${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(service, await res.json()) };