import {z} from 'zod' export type GetApiAttachmentArgs = {category?: string | undefined, chapter?: string | undefined, entry?: string | undefined, season?: string | undefined, tab?: string | undefined, page?: number | undefined, per_page?: number | undefined, order?: ('created' | 'entry' | 'order' | 'tab' | 'updated') | undefined, dir?: ('asc' | 'desc') | undefined, 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 GetApiAttachmentOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attachment = z.object({entry: z.object({slug: z.string().optional(), link: z.string().optional(), title: z.string().optional()}).optional(), attachment_fields: z.array(z.object({slug: z.string().optional(), link: z.string().optional(), label: z.record(z.string(), z.string()).optional(), title: z.record(z.string(), z.string()).optional(), value: z.string().optional(), translated: z.record(z.string(), z.string()).optional(), download: z.string().optional()})).optional(), created: z.string().optional(), file: z.object({token: z.string().optional(), link: z.string().optional(), download: z.string().nullable().optional(), filename: z.string().optional()}).optional(), order: z.number().int().optional(), tab: z.object({slug: z.string().optional(), link: z.string().optional(), name: z.record(z.string(), z.string()).optional()}).optional(), token: z.string().optional(), updated: z.string().optional()}); export const attachmentList = z.object({current_page: z.number().int().optional(), first_page_url: z.string().optional(), from: z.number().int().nullable().optional(), last_page: z.number().int().optional(), last_page_url: z.string().optional(), next_page_url: z.string().optional(), path: z.string().optional(), per_page: z.number().int().gte(1).lte(100).optional(), prev_page_url: z.string().optional(), to: z.number().int().nullable().optional(), total: z.number().int().optional(), data: z.array(attachment).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 attachments * * GET /attachment */ export const getApiAttachment = async (args: GetApiAttachmentArgs, options?: GetApiAttachmentOptions) => { 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.category != null) query.append("category", String(args.category)) if (args.chapter != null) query.append("chapter", String(args.chapter)) if (args.entry != null) query.append("entry", String(args.entry)) if (args.season != null) query.append("season", String(args.season)) if (args.tab != null) query.append("tab", String(args.tab)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.order != null) query.append("order", String(args.order)) if (args.dir != null) query.append("dir", String(args.dir)) 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(/\/+$/, '')}/attachment${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(attachmentList, await res.json()) };