import {z} from 'zod' export type GetApiFileTokenArgs = {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 GetApiFileTokenOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const file = z.object({download_link: z.string().optional(), duration: z.number().nullable().optional(), filename: z.string().optional(), image_height: z.number().int().nullable().optional(), image_width: z.number().int().nullable().optional(), metadata: z.object({title: z.string().optional(), caption: z.string().optional(), copyright: z.string().optional(), created: z.string().optional(), city: z.string().optional(), country: z.string().optional(), state: z.string().optional(), camera: z.string().optional(), exposure: z.string().optional(), iso: z.string().optional(), fstop: z.string().optional(), focal_length: z.string().optional(), size: z.string().optional()}).optional(), mime: z.string().optional(), size: z.number().int().optional(), transcoding: z.array(z.object({basename: z.string().optional(), download_link: z.string().optional(), extension: z.string().optional(), timestamp: z.number().int().optional(), size: z.number().int().optional(), etag: z.string().optional()})).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 file information * * GET /file/{token} */ export const getApiFileToken = async (args: GetApiFileTokenArgs, options?: GetApiFileTokenOptions) => { 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(/\/+$/, '')}/file/${encodeURIComponent(String(args.token))}${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(file, await res.json()) };