import {z} from 'zod' export type Category = 'CRYPTO_ONRAMP' | 'CRYPTO_OFFRAMP' | 'CRYPTO_TRANSFER' | 'BANK_LINKING' | 'FIAT_PAYMENTS' | 'CRYPTO_VIRTUAL_ACCOUNT_ONRAMP' | 'CRYPTO_VIRTUAL_ACCOUNT_OFFRAMP' | 'CUSTOMER_KYC' | 'CRYPTO_ONRAMP_SWAP' | 'CRYPTO_OFFRAMP_SWAP'; export type GetApiNetworkPartnerDefaultsCountryCodeCategoryArgs = {countryCode: string, category: Category, 'Meld-Account-Id'?: string | undefined}; export type GetApiNetworkPartnerDefaultsCountryCodeCategoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const category = z.enum(["CRYPTO_ONRAMP", "CRYPTO_OFFRAMP", "CRYPTO_TRANSFER", "BANK_LINKING", "FIAT_PAYMENTS", "CRYPTO_VIRTUAL_ACCOUNT_ONRAMP", "CRYPTO_VIRTUAL_ACCOUNT_OFFRAMP", "CUSTOMER_KYC", "CRYPTO_ONRAMP_SWAP", "CRYPTO_OFFRAMP_SWAP"]); export const countryDefaults = z.object({countryCode: z.string().optional(), category: category.optional(), currencyCode: z.string().optional(), paymentMethods: z.array(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 }; /** * Get country defaults * * GET /network-partner/defaults/{countryCode}/{category} */ export const getApiNetworkPartnerDefaultsCountryCodeCategory = async (args: GetApiNetworkPartnerDefaultsCountryCodeCategoryArgs, options?: GetApiNetworkPartnerDefaultsCountryCodeCategoryOptions) => { const {baseUrl = "https://api-sb.meld.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Meld-Account-Id'] != null) headers.set("Meld-Account-Id", String(args['Meld-Account-Id'])) const url = `${baseUrl.replace(/\/+$/, '')}/network-partner/defaults/${encodeURIComponent(String(args.countryCode))}/${encodeURIComponent(String(args.category))}${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(countryDefaults, await res.json()) };