import {z} from 'zod' export type GetApiV2CatalogSearchArgs = {sort?: ('newest' | 'oldest') | undefined, page?: number | undefined, per_page?: number | undefined, query?: string | undefined, collection_id?: Array | undefined, asset_type?: (Array<'image' | 'video' | 'audio' | 'elements' | 'editorial-image' | 'editorial-video'>) | undefined}; export type GetApiV2CatalogSearchOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2CatalogSearchResponse = z.object({page: z.number(), per_page: z.number(), total_count: z.number(), data: z.array(z.object({id: z.string(), asset: z.object({id: z.string().optional(), type: z.enum(["image", "video", "audio", "editorial-image", "editorial-video"]), name: z.string().optional()}), created_time: z.string(), collection_ids: z.array(z.string()).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 }; /** * Search catalogs for assets * * GET /v2/catalog/search */ export const getApiV2CatalogSearch = async (args: GetApiV2CatalogSearchArgs, options?: GetApiV2CatalogSearchOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, customerAccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", String(args.sort)) 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.query != null) query.append("query", String(args.query)) for (const item of args.collection_id ?? []) query.append("collection_id", String(item)) for (const item of args.asset_type ?? []) query.append("asset_type", String(item)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/catalog/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(getApiV2CatalogSearchResponse, await res.json()) };