import {z} from 'zod' export type ApiContentReference = {ref_name: string, ref_url: string}; 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 ApiContestReward = {reward: string, place?: number | undefined, place_from?: number | undefined, place_to?: number | undefined}; export type ApiContestInfo = {title: string, description: string, references: Array, conditions: Array, rewards: Array, start_time_unix: number, end_time_unix: number, image_url: string, total_reward: string, hidden: boolean}; export type PatchApiV1ContestsIdArgs = {id: number, body: ApiContestInfo}; export type PatchApiV1ContestsIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiContentReference = z.object({ref_name: z.string(), ref_url: z.string()}); 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 apiContestReward = z.object({reward: z.string(), place: z.number().int().optional(), place_from: z.number().int().optional(), place_to: z.number().int().optional()}); export const apiContestInfo = z.object({title: z.string(), description: z.string(), references: z.array(apiContentReference), conditions: z.array(apiDexSwapCondition), rewards: z.array(apiContestReward), start_time_unix: z.number().int(), end_time_unix: z.number().int(), image_url: z.string(), total_reward: z.string(), hidden: z.boolean()}); export const apiContestInfoWithID = z.object({id: z.number().int(), info: apiContestInfo}); 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 }; /** * Update existing contest. For internal usage only * * PATCH /v1/contests/{id} */ export const patchApiV1ContestsId = async (args: PatchApiV1ContestsIdArgs, options?: PatchApiV1ContestsIdOptions) => { 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/contests/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(apiContestInfoWithID, await res.json()) };