import {z} from 'zod' export type GetApiV1GiveawayGiveawayIdArgs = {giveaway_id: string}; export type GetApiV1GiveawayGiveawayIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const giveawayStatsResponse = z.object({total_entries: z.number().int(), unique_participants: z.number().int(), total_tips_usd: z.string()}); export const giveawayResponse = z.object({id: z.string(), creator_account_id: z.string(), earning_address: z.string().nullable().optional(), creator_handle: z.string().nullable().optional(), creator_ens_name: z.string().nullable().optional(), creator_base_name: z.string().nullable().optional(), title: z.string(), description: z.string(), minimum_tip_usd: z.string(), allow_multiple_entries: z.boolean(), num_winners: z.number().int(), selection_mode: z.string(), network: z.string(), token_symbol: z.string(), start_at: z.string(), end_at: z.string(), status: z.string(), created_at: z.string(), updated_at: z.string(), drawn_at: z.string().nullable().optional(), is_winner: z.boolean().nullable().optional(), stats: giveawayStatsResponse.nullable().optional(), creator_address: z.string().nullable()}); export const giveawayWithStatsResponse = z.object({giveaway: giveawayResponse, stats: giveawayStatsResponse}); 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 giveaway details * * GET /v1/giveaway/{giveaway_id} */ export const getApiV1GiveawayGiveawayId = async (args: GetApiV1GiveawayGiveawayIdArgs, options: GetApiV1GiveawayGiveawayIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/giveaway/${encodeURIComponent(String(args.giveaway_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(giveawayWithStatsResponse, await res.json()) };