import {z} from 'zod' export type GetApiV2EditorialIdArgs = {id: string, country: unknown, search_id?: string | undefined}; export type GetApiV2EditorialIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2EditorialIdResponse = z.object({aspect: z.number().optional(), assets: z.object({original: 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(), thumb_170: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), thumb_220: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), watermark_450: z.object({height: z.number().int(), url: z.string(), width: z.number().int()}).optional(), watermark_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(), 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()}).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(), special_instructions: z.string().optional(), title: 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 }; /** * (Deprecated) Get editorial content details * * GET /v2/editorial/{id} * * Also accepts auth: customer_accessCode */ export const getApiV2EditorialId = async (args: GetApiV2EditorialIdArgs, options?: GetApiV2EditorialIdOptions) => { 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.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/${encodeURIComponent(String(args.id))}${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(getApiV2EditorialIdResponse, await res.json()) };