import {z} from 'zod' export type GetApiV2ImagesSearchArgs = {library?: (Array<'shutterstock' | 'offset'>) | undefined, added_date?: string | undefined, added_date_start?: string | undefined, aspect_ratio_min?: number | undefined, aspect_ratio_max?: number | undefined, aspect_ratio?: number | undefined, added_date_end?: string | undefined, category?: string | undefined, color?: string | undefined, contributor?: Array | undefined, contributor_country?: (Array | Array) | undefined, fields?: string | undefined, height?: number | undefined, height_from?: number | undefined, height_to?: number | undefined, image_type?: (Array<'photo' | 'illustration' | 'vector'>) | 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' | 'enhanced'>) | undefined, model?: Array | undefined, orientation?: ('horizontal' | 'vertical') | undefined, page?: number | undefined, per_page?: number | undefined, people_model_released?: boolean | 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' | 'NOT african' | 'NOT african_american' | 'NOT black' | 'NOT brazilian' | 'NOT chinese' | 'NOT caucasian' | 'NOT east_asian' | 'NOT hispanic' | 'NOT japanese' | 'NOT middle_eastern' | 'NOT native_american' | 'NOT pacific_islander' | 'NOT south_asian' | 'NOT southeast_asian' | 'NOT other'>) | undefined, people_gender?: ('male' | 'female' | 'both') | undefined, people_number?: number | undefined, query?: string | undefined, exact_match?: boolean | undefined, region?: (unknown | string) | undefined, safe?: boolean | undefined, sort?: ('newest' | 'popular' | 'relevance' | 'random' | 'oldest') | undefined, spellcheck_query?: boolean | undefined, view?: ('minimal' | 'full') | undefined, width?: number | undefined, width_from?: number | undefined, width_to?: number | undefined}; export type GetApiV2ImagesSearchOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ImagesSearchResponse = z.object({data: z.array(z.object({added_date: z.string().optional(), affiliate_url: z.string().optional(), aspect: z.number().optional(), assets: z.object({huge_jpg: z.object({display_name: z.string().optional(), dpi: z.number().int().optional(), file_size: z.number().int().optional(), format: z.string().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), huge_thumb: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), large_thumb: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), medium_jpg: z.object({display_name: z.string().optional(), dpi: z.number().int().optional(), file_size: z.number().int().optional(), format: z.string().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), preview: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), preview_600: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), preview_600_unwatermarked: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), preview_1000: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), preview_1500: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), small_jpg: z.object({display_name: z.string().optional(), dpi: z.number().int().optional(), file_size: z.number().int().optional(), format: z.string().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), small_thumb: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), supersize_jpg: z.object({display_name: z.string().optional(), dpi: z.number().int().optional(), file_size: z.number().int().optional(), format: z.string().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), vector_eps: z.object({display_name: z.string().optional(), dpi: z.number().int().optional(), file_size: z.number().int().optional(), format: z.string().optional(), height: z.number().int().optional(), is_licensable: z.boolean().optional(), width: z.number().int().optional()}).optional(), mosaic: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).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(), has_model_release: z.boolean().optional(), has_property_release: z.boolean().optional(), id: z.string(), image_type: z.string().optional(), is_adult: z.boolean().optional(), is_editorial: z.boolean().optional(), is_illustration: z.boolean().optional(), asset_collection: z.enum(["Starter", "Essentials", "Choice", "Offset"]).optional(), ai_ip_review: z.object({status: z.string().optional(), rejection_reasons: z.array(z.string()).optional()}).optional(), keywords: z.array(z.string()).optional(), media_type: z.string(), model_releases: z.array(z.object({id: z.string().optional()})).optional(), models: z.array(z.object({id: z.string()})).optional(), releases: z.array(z.string()).optional(), url: z.string().optional()})), errors: z.array(z.object({statusCode: z.number().optional(), code: z.string().optional(), data: z.string().optional(), items: z.array(z.object({})).optional(), message: z.string(), path: z.string().optional()})).optional(), message: z.string().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), search_id: z.string(), spellcheck_info: z.object({}).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 for images * * GET /v2/images/search * * Also accepts auth: customer_accessCode */ export const getApiV2ImagesSearch = async (args: GetApiV2ImagesSearchArgs, options?: GetApiV2ImagesSearchOptions) => { 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.library ?? []) query.append("library", String(item)) 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.aspect_ratio_min != null) query.append("aspect_ratio_min", String(args.aspect_ratio_min)) if (args.aspect_ratio_max != null) query.append("aspect_ratio_max", String(args.aspect_ratio_max)) if (args.aspect_ratio != null) query.append("aspect_ratio", String(args.aspect_ratio)) if (args.added_date_end != null) query.append("added_date_end", String(args.added_date_end)) if (args.category != null) query.append("category", String(args.category)) if (args.color != null) query.append("color", String(args.color)) for (const item of args.contributor ?? []) query.append("contributor", String(item)) if (args.contributor_country != null) query.append("contributor_country", String(args.contributor_country)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.height != null) query.append("height", String(args.height)) if (args.height_from != null) query.append("height_from", String(args.height_from)) if (args.height_to != null) query.append("height_to", String(args.height_to)) for (const item of args.image_type ?? []) query.append("image_type", String(item)) 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_model_released != null) query.append("people_model_released", String(args.people_model_released)) 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.query != null) query.append("query", String(args.query)) if (args.exact_match != null) query.append("exact_match", String(args.exact_match)) if (args.region != null) query.append("region", String(args.region)) if (args.safe != null) query.append("safe", String(args.safe)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.spellcheck_query != null) query.append("spellcheck_query", String(args.spellcheck_query)) if (args.view != null) query.append("view", String(args.view)) if (args.width != null) query.append("width", String(args.width)) if (args.width_from != null) query.append("width_from", String(args.width_from)) if (args.width_to != null) query.append("width_to", String(args.width_to)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/images/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(getApiV2ImagesSearchResponse, await res.json()) };