import {z} from 'zod' export type StartRoundRequest = {starts_at?: (string | null) | undefined, ends_at?: (string | null) | undefined, cooldown_hours?: (number | null) | undefined, payload?: ({[key: string]: unknown} | null) | undefined, force?: boolean | undefined}; export type CreateApiContestsDailyTemplatesTemplateIdStartRoundArgs = {template_id: number, body: StartRoundRequest}; export type CreateApiContestsDailyTemplatesTemplateIdStartRoundOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const contestRoundResponse = z.object({id: z.number().int(), template_id: z.number().int(), template_slug: z.string(), template_name: z.string().nullable().optional(), starts_at: z.string(), ends_at: z.string(), status: z.string(), payload: z.record(z.string(), z.unknown()).optional(), winners_count: z.number().int(), max_winners: z.number().int(), attempts_per_user: z.number().int(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Start Round Now * * POST /contests/daily/templates/{template_id}/start-round */ export const createApiContestsDailyTemplatesTemplateIdStartRound = async (args: CreateApiContestsDailyTemplatesTemplateIdStartRoundArgs, options: CreateApiContestsDailyTemplatesTemplateIdStartRoundOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/contests/daily/templates/${encodeURIComponent(String(args.template_id))}/start-round${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(contestRoundResponse, await res.json()) };