import {z} from 'zod' export type GetApiApiCountryArgs = {max?: number | undefined}; export type GetApiApiCountryOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const countryCodeDto = z.object({allowRegistration: z.boolean().optional(), code: z.string().optional(), defaultCurrency: z.string().optional(), documents: z.array(z.object({code: z.string().optional(), description: z.string().optional(), id: z.number().int().optional(), required: z.boolean().optional()})).optional(), id: z.number().int().optional(), name: z.string().optional(), options: z.object({minimumOrderValue: z.number().int().optional(), limitValue: z.number().int().optional(), minimumTransferValue: z.number().int().optional(), minimumPartnerValue: z.number().int().optional(), requireProofOfPayment: z.string().optional(), supportInstantTransfer: z.boolean().optional(), unverifiedMaximum: z.string().optional(), orderMinimumValue: z.string().optional(), transferMinimumValue: z.string().optional(), withdrawalMaximum: z.string().optional(), withdrawalThreshold: z.string().optional()}).optional(), region: z.string().optional()}); export const getApiApiCountryResponse = z.array(countryCodeDto); 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 }; /** * Fetch country codes * * GET /api/country */ export const getApiApiCountry = async (args: GetApiApiCountryArgs, options?: GetApiApiCountryOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.max != null) query.append("max", String(args.max)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/country${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(getApiApiCountryResponse, await res.json()) };