import {z} from 'zod' export type ContestTemplateUpdateRequest = {name?: (string | null) | undefined, description?: (string | null) | undefined, prize_type?: (string | null) | undefined, prize_value?: (string | null) | undefined, max_winners?: (number | null) | undefined, attempts_per_user?: (number | null) | undefined, times_per_day?: (number | null) | undefined, schedule_times?: (string | null) | undefined, cooldown_hours?: (number | null) | undefined, payload?: ({[key: string]: unknown} | null) | undefined, is_enabled?: (boolean | null) | undefined}; export type PatchApiContestsDailyTemplatesTemplateIdArgs = {template_id: number, body: ContestTemplateUpdateRequest}; export type PatchApiContestsDailyTemplatesTemplateIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const contestTemplateResponse = z.object({id: z.number().int(), name: z.string(), slug: z.string(), description: z.string().nullable().optional(), prize_type: z.string(), prize_value: z.string(), max_winners: z.number().int(), attempts_per_user: z.number().int(), times_per_day: z.number().int(), schedule_times: z.string().nullable().optional(), cooldown_hours: z.number().int(), payload: z.record(z.string(), z.unknown()).optional(), is_enabled: z.boolean(), 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 }; /** * Update Daily Template * * PATCH /contests/daily/templates/{template_id} */ export const patchApiContestsDailyTemplatesTemplateId = async (args: PatchApiContestsDailyTemplatesTemplateIdArgs, options: PatchApiContestsDailyTemplatesTemplateIdOptions) => { 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))}${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(contestTemplateResponse, await res.json()) };