import {z} from 'zod' export type Locale = 'ar' | 'be' | 'bg' | 'bn' | 'ca' | 'cs' | 'da' | 'de' | 'el' | 'en-GB' | 'en-US' | 'es-ES' | 'es-419' | 'fi' | 'fil' | 'fr' | 'he' | 'hi' | 'hr' | 'hu' | 'id' | 'it' | 'ja' | 'ko' | 'lt' | 'lv' | 'nl' | 'no' | 'pl' | 'pt-BR' | 'pt-PT' | 'ro' | 'ru' | 'sk' | 'sl' | 'sq' | 'sv-SE' | 'th' | 'tok' | 'tr' | 'uk' | 'vi' | 'zh-CN' | 'zh-TW'; export type GetApiTenorTrendingGifsArgs = {locale?: Locale | undefined}; export type GetApiTenorTrendingGifsOptions = {baseUrl?: string | undefined, sessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const int32Type = z.number().int().gte(0).lte(2147483647); export const gifMediaFormat = z.object({src: z.string(), proxy_src: z.string(), width: int32Type, height: int32Type}); export const gifResponse = z.object({id: z.string(), slug: z.string(), provider: z.string().min(1).max(32), title: z.string(), url: z.string(), src: z.string(), proxy_src: z.string(), width: int32Type, height: int32Type, media: z.record(z.string(), gifMediaFormat), placeholder: z.string().nullable().optional()}); export const gifListResponse = z.array(gifResponse); 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 }; /** * Get trending GIFs (deprecated alias) * * GET /tenor/trending-gifs */ export const getApiTenorTrendingGifs = async (args: GetApiTenorTrendingGifsArgs, options?: GetApiTenorTrendingGifsOptions) => { const {baseUrl = "https://web.fluxer.app/api/v1", fetch: fetchFn = fetch, sessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.locale != null) query.append("locale", String(args.locale)) if (sessionToken !== undefined) headers.set("Authorization", sessionToken) const url = `${baseUrl.replace(/\/+$/, '')}/tenor/trending-gifs${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(gifListResponse, await res.json()) };