import {z} from 'zod' export type ApiDexSwapConditionType = 'unique' | 'listed' | 'partner_address' | 'tokens' | 'tokens-pair' | 'tokens-exact' | 'users-whitelist' | 'swap-tx-min-amount' | 'blacklist-pair' | 'blacklist-tokens' | 'dexes'; export type ApiDexSwapCondition = {type: ApiDexSwapConditionType, args?: Array | undefined}; export type ApiCashbackLimitCreateRequest = {interval_duration_seconds: number, max_distribute_at_interval: number}; export type ApiContentReference = {ref_name: string, ref_url: string}; export type ApiCashbackCreateRequest = {title: string, description: string, distributor: string, reward_token: string, conditions: Array, distribute: number, distributed: number, start_time_seconds: number, end_time_seconds: number, reward_denominator: number, is_hidden: boolean, limits: Array, image_url: string, references: Array}; export type CreateApiV1CashbackArgs = {body: ApiCashbackCreateRequest}; export type CreateApiV1CashbackOptions = {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 }; /** * Create new cashback distribution. For internal usage only * * POST /v1/cashback/ */ export const createApiV1Cashback = async (args: CreateApiV1CashbackArgs, options?: CreateApiV1CashbackOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) 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()) };