import {z} from 'zod' export type GetApiRoundRoundSlugArgs = {Accept: 'application/vnd.Creative Force.v2.3+json' | 'application/vnd.Creative Force.v2.3+xml', 'x-api-language'?: (Array<'all' | 'ar_AR' | 'bg_BG' | 'bn_IN' | 'ca_ES' | 'cs_CZ' | 'cy_GB' | 'da_DK' | 'de_DE' | 'en_GB' | 'en_US' | 'el_GR' | 'es_NN' | 'es_LA' | 'et_EE' | 'fa_NN' | 'fi_FI' | 'fr_FR' | 'fr_CA' | 'he_IL' | 'hi_IN' | 'hr_HR' | 'hu_HU' | 'hy_AM' | 'it_IT' | 'ja_JP' | 'ko_KR' | 'lt_LT' | 'ms_MY' | 'nl_NL' | 'nb_NO' | 'pl_PL' | 'pt_BR' | 'ro_RO' | 'ru_RU' | 'sk_SK' | 'sl_SI' | 'sq_AL' | 'sr_RS' | 'sv_SE' | 'sw_NN' | 'th_TH' | 'tr_TR' | 'uk_UA' | 'vi_VN' | 'zh_CN' | 'zh_HK'>) | undefined}; export type GetApiRoundRoundSlugOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const round = z.object({chapters: z.array(z.object({slug: z.string().optional(), link: z.string().optional(), name: z.record(z.string(), z.string()).optional()})).optional(), created: z.string().optional(), end_date: z.string().nullable().optional(), form: z.string().optional(), name: z.record(z.string(), z.string()).optional(), season: z.object({slug: z.string().optional(), link: z.string().optional(), name: z.record(z.string(), z.string()).optional()}).optional(), slug: z.string().optional(), start_date: z.string().nullable().optional(), type: z.enum(["entry", "judge", "feedback"]).optional(), updated: z.string().optional()}); 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 }; /** * Retrieve a round * * GET /round/{round_slug} */ export const getApiRoundRoundSlug = async (args: GetApiRoundRoundSlugArgs, options?: GetApiRoundRoundSlugOptions) => { const {baseUrl = "https://api.us.cr4ce.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args['x-api-language'] != null) headers.set("x-api-language", args['x-api-language'].join(',')) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/round/${encodeURIComponent(String(args.round_slug))}${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(round, await res.json()) };