import {z} from 'zod' export type GetApiSettingsExchangeRatesArgs = {page?: number | undefined, per_page?: number | undefined, target_currency?: number | undefined, rate_from?: number | undefined, rate_to?: number | undefined, sort?: ('id' | 'target_currency' | 'rate') | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiSettingsExchangeRatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const adminSettingsExchangeRate = z.object({id: z.number().int().nullable().optional(), targetCurrency: z.number().int().nullable().optional(), targetCurrencyCode: z.string().nullable().optional(), targetCurrencyName: z.string().nullable().optional(), rate: z.number().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), message: z.string().nullable().optional()}); export const getApiSettingsExchangeRatesResponse = z.array(adminSettingsExchangeRate); 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 }; /** * List exchange rates * * GET /settings/exchange-rates */ export const getApiSettingsExchangeRates = async (args: GetApiSettingsExchangeRatesArgs, options?: GetApiSettingsExchangeRatesOptions) => { const {baseUrl = "http://localhost:8000/api/admin", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.target_currency != null) query.append("target_currency", String(args.target_currency)) if (args.rate_from != null) query.append("rate_from", String(args.rate_from)) if (args.rate_to != null) query.append("rate_to", String(args.rate_to)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/settings/exchange-rates${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(getApiSettingsExchangeRatesResponse, await res.json()) };