import {z} from 'zod' export type GetApiV2VideosSearchArgs = {added_date?: string | undefined, added_date_start?: string | undefined, added_date_end?: string | undefined, aspect_ratio?: ('4_3' | '16_9' | 'nonstandard') | undefined, category?: string | undefined, contributor?: Array | undefined, contributor_country?: Array | undefined, duration_from?: number | undefined, duration_to?: number | undefined, fps_from?: number | undefined, fps_to?: number | undefined, keyword_safe_search?: boolean | undefined, language?: ('ar' | 'bg' | 'bn' | 'cs' | 'da' | 'de' | 'el' | 'en' | 'es' | 'fi' | 'fr' | 'gu' | 'he' | 'hi' | 'hr' | 'hu' | 'id' | 'it' | 'ja' | 'kn' | 'ko' | 'ml' | 'mr' | 'nb' | 'nl' | 'or' | 'pl' | 'pt' | 'ro' | 'ru' | 'sk' | 'sl' | 'sv' | 'ta' | 'te' | 'th' | 'tr' | 'uk' | 'ur' | 'vi' | 'zh' | 'zh-Hant') | undefined, license?: (Array<'commercial' | 'editorial'>) | undefined, model?: Array | undefined, orientation?: ('vertical' | 'horizontal') | undefined, page?: number | undefined, per_page?: number | undefined, people_age?: ('infants' | 'children' | 'teenagers' | '20s' | '30s' | '40s' | '50s' | '60s' | 'older') | undefined, people_ethnicity?: (Array<'african' | 'african_american' | 'black' | 'brazilian' | 'chinese' | 'caucasian' | 'east_asian' | 'hispanic' | 'japanese' | 'middle_eastern' | 'native_american' | 'pacific_islander' | 'south_asian' | 'southeast_asian' | 'other'>) | undefined, people_gender?: ('male' | 'female' | 'both') | undefined, people_number?: number | undefined, people_model_released?: boolean | undefined, query?: string | undefined, exact_match?: boolean | undefined, resolution?: ('4k' | 'standard_definition' | 'high_definition') | undefined, safe?: boolean | undefined, sort?: ('newest' | 'oldest' | 'popular' | 'relevance' | 'random') | undefined, view?: ('minimal' | 'full') | undefined, library?: (Array<'shutterstock' | 'footage_select'>) | undefined}; export type GetApiV2VideosSearchOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2VideosSearchResponse = z.object({data: z.array(z.object({added_date: z.string().optional(), affiliate_url: z.string().optional(), aspect: z.number().optional(), aspect_ratio: z.string().optional(), assets: z.object({"4k": 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(), hd: 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_jpg: z.object({url: z.string()}).optional(), preview_mp4: z.object({url: z.string()}).optional(), preview_webm: z.object({url: z.string()}).optional(), sd: 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(), thumb_jpg: z.object({url: z.string()}).optional(), thumb_jpgs: z.object({urls: z.array(z.string())}).optional(), thumb_mp4: z.object({url: z.string()}).optional(), thumb_webm: z.object({url: z.string()}).optional(), web: 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()}).optional(), categories: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), contributor: z.object({id: z.string(), name: z.string().optional()}), description: z.string().optional(), duration: z.number().optional(), has_model_release: z.boolean().optional(), has_property_release: z.boolean().optional(), id: z.string(), is_adult: z.boolean().optional(), is_editorial: z.boolean().optional(), is_select: z.boolean().optional(), asset_collection: z.enum(["", "Essentials", "Select"]).optional(), keywords: z.array(z.string()).optional(), media_type: z.string(), models: z.array(z.object({id: z.string()})).optional(), releases: z.array(z.object({id: z.string().optional()})).optional(), url: z.string().optional()})), message: z.string().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), total_count: z.number().int(), search_id: z.string()}); 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 for videos * * GET /v2/videos/search * * Also accepts auth: customer_accessCode */ export const getApiV2VideosSearch = async (args: GetApiV2VideosSearchArgs, options?: GetApiV2VideosSearchOptions) => { 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.added_date != null) query.append("added_date", String(args.added_date)) if (args.added_date_start != null) query.append("added_date_start", String(args.added_date_start)) if (args.added_date_end != null) query.append("added_date_end", String(args.added_date_end)) if (args.aspect_ratio != null) query.append("aspect_ratio", String(args.aspect_ratio)) if (args.category != null) query.append("category", String(args.category)) for (const item of args.contributor ?? []) query.append("contributor", String(item)) for (const item of args.contributor_country ?? []) query.append("contributor_country", String(item)) if (args.duration_from != null) query.append("duration_from", String(args.duration_from)) if (args.duration_to != null) query.append("duration_to", String(args.duration_to)) if (args.fps_from != null) query.append("fps_from", String(args.fps_from)) if (args.fps_to != null) query.append("fps_to", String(args.fps_to)) if (args.keyword_safe_search != null) query.append("keyword_safe_search", String(args.keyword_safe_search)) if (args.language != null) query.append("language", String(args.language)) for (const item of args.license ?? []) query.append("license", String(item)) for (const item of args.model ?? []) query.append("model", String(item)) if (args.orientation != null) query.append("orientation", String(args.orientation)) 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.people_age != null) query.append("people_age", String(args.people_age)) for (const item of args.people_ethnicity ?? []) query.append("people_ethnicity", String(item)) if (args.people_gender != null) query.append("people_gender", String(args.people_gender)) if (args.people_number != null) query.append("people_number", String(args.people_number)) if (args.people_model_released != null) query.append("people_model_released", String(args.people_model_released)) if (args.query != null) query.append("query", String(args.query)) if (args.exact_match != null) query.append("exact_match", String(args.exact_match)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.safe != null) query.append("safe", String(args.safe)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.view != null) query.append("view", String(args.view)) for (const item of args.library ?? []) query.append("library", String(item)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(getApiV2VideosSearchResponse, await res.json()) };