import {z} from 'zod' export type GetApiV2EditorialVideosSearchArgs = {query?: string | undefined, sort?: ('relevant' | 'newest' | 'oldest') | undefined, category?: string | undefined, country: unknown, supplier_code?: Array | undefined, date_start?: string | undefined, date_end?: string | undefined, resolution?: ('4k' | 'high_definition' | 'standard_definition') | undefined, fps?: number | undefined, per_page?: number | undefined, cursor?: string | undefined}; export type GetApiV2EditorialVideosSearchOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2EditorialVideosSearchResponse = 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()})), message: z.string().optional(), next: z.string().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), prev: z.string().optional(), search_id: z.string().optional(), total_count: z.number().int()}); 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 }; /** * Search editorial video content * * GET /v2/editorial/videos/search * * Also accepts auth: customer_accessCode */ export const getApiV2EditorialVideosSearch = async (args: GetApiV2EditorialVideosSearchArgs, options?: GetApiV2EditorialVideosSearchOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.category != null) query.append("category", String(args.category)) if (args.country != null) query.append("country", String(args.country)) for (const item of args.supplier_code ?? []) query.append("supplier_code", String(item)) if (args.date_start != null) query.append("date_start", String(args.date_start)) if (args.date_end != null) query.append("date_end", String(args.date_end)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.fps != null) query.append("fps", String(args.fps)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/editorial/videos/search${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(getApiV2EditorialVideosSearchResponse, await res.json()) };