import {z} from 'zod' export type GetApiV1CashbackIdArgs = {id: number}; export type GetApiV1CashbackIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiDexSwapConditionType = z.enum(["unique", "listed", "partner_address", "tokens", "tokens-pair", "tokens-exact", "users-whitelist", "swap-tx-min-amount", "blacklist-pair", "blacklist-tokens", "dexes"]); export const apiDexSwapCondition = z.object({type: apiDexSwapConditionType, args: z.array(z.string()).optional()}); export const apiCashbackLimit = z.object({limit_id: z.number().int(), interval_duration_seconds: z.number().int(), max_distribute_at_interval: z.number()}); export const apiContentReference = z.object({ref_name: z.string(), ref_url: z.string()}); export const apiCashbackInfo = z.object({id: z.number().int(), title: z.string(), description: z.string(), distributor: z.string(), reward_token: z.string(), conditions: z.array(apiDexSwapCondition), distribute: z.number(), distributed: z.number(), start_time_seconds: z.number().int(), end_time_seconds: z.number().int(), reward_denominator: z.number(), is_active: z.boolean(), limits: z.array(apiCashbackLimit), image_url: z.string(), references: z.array(apiContentReference), is_hidden: 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 }; /** * Get cashback info * * GET /v1/cashback/{id} */ export const getApiV1CashbackId = async (args: GetApiV1CashbackIdArgs, options?: GetApiV1CashbackIdOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cashback/${encodeURIComponent(String(args.id))}${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(apiCashbackInfo, await res.json()) };