import {z} from 'zod' export type GetApiV1CashbackArgs = {distributor?: string | undefined, token?: string | undefined, active?: boolean | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiV1CashbackOptions = {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 apiCashbackInfoResponse = z.object({count: z.number().int(), infos: z.array(apiCashbackInfo)}); 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 cashbacks info * * GET /v1/cashback/ */ export const getApiV1Cashback = async (args: GetApiV1CashbackArgs, options?: GetApiV1CashbackOptions) => { 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 (args.distributor != null) query.append("distributor", String(args.distributor)) if (args.token != null) query.append("token", String(args.token)) if (args.active != null) query.append("active", String(args.active)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cashback/${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(apiCashbackInfoResponse, await res.json()) };