import {z} from 'zod' export type GetApiV2EditorialVideosArgs = {id: Array, country: unknown, search_id?: string | undefined}; export type GetApiV2EditorialVideosOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2EditorialVideosResponse = z.object({data: z.array(z.object({aspect: z.number().optional(), assets: z.object({original: z.object({display_name: z.string().optional(), file_size: z.number().int().optional(), format: z.string().optional(), fps: z.number().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), preview_mp4: z.object({url: z.string()}).optional(), preview_webm: z.object({url: z.string()}).optional(), thumb_jpg: z.object({url: z.string()}).optional()}).optional(), byline: z.string().optional(), caption: z.string().optional(), categories: z.array(z.object({name: z.string().optional()})).optional(), date_taken: z.string().optional(), description: z.string().optional(), id: z.string(), keywords: z.array(z.string()).optional(), title: z.string().optional()})).optional(), search_id: z.string().optional(), total_count: z.number().int().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 }; /** * List editorial videos details by ID list * * GET /v2/editorial/videos * * Also accepts auth: customer_accessCode */ export const getApiV2EditorialVideos = async (args: GetApiV2EditorialVideosArgs, options?: GetApiV2EditorialVideosOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.id ?? []) query.append("id", String(item)) if (args.country != null) query.append("country", String(args.country)) if (args.search_id != null) query.append("search_id", String(args.search_id)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/editorial/videos${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(getApiV2EditorialVideosResponse, await res.json()) };