import {z} from 'zod' export type GetApiV2GenerativeImagesHistoryArgs = {page?: number | undefined, per_page?: number | undefined, sort_order?: ('asc' | 'desc') | undefined, prompt?: string | undefined, status?: ('COMPLETED' | 'FAILED' | 'GENERATING' | 'PENDING') | undefined, generation_type?: ('image-generation' | 'image-variation' | 'image-smart-edit') | undefined}; export type GetApiV2GenerativeImagesHistoryOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2GenerativeImagesHistoryResponse = z.object({data: z.array(z.object({id: z.string(), status: z.enum(["COMPLETED", "FAILED", "GENERATING", "PENDING", "NOT_FOUND"]), request_time: z.string(), is_private: z.boolean(), can_register_assets: z.boolean().nullable().optional(), error_code: z.string().nullable().optional(), generation_type: z.enum(["image-generation", "image-variation", "image-smart-edit"]).optional(), prompt: z.string().optional(), aspect_ratio: z.string().optional(), style: z.object({id: z.string().optional(), name: z.string().optional()}).nullable(), model: z.string().nullable(), assets: z.array(z.object({id: z.string(), description: z.string(), image_id: z.string().optional(), ai_ip_review: z.object({status: z.string().optional(), rejection_reasons: z.array(z.string()).optional()}).optional(), small: z.object({url: z.string(), height: z.number().int(), width: z.number().int()}), watermarked: z.object({url: z.string(), height: z.number().int(), width: z.number().int()})}))})), page: z.number().int(), per_page: z.number().int(), 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 }; /** * List image generation history * * GET /v2/generative/images/history */ export const getApiV2GenerativeImagesHistory = async (args: GetApiV2GenerativeImagesHistoryArgs, options?: GetApiV2GenerativeImagesHistoryOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.prompt != null) query.append("prompt", String(args.prompt)) if (args.status != null) query.append("status", String(args.status)) if (args.generation_type != null) query.append("generation_type", String(args.generation_type)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/generative/images/history${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(getApiV2GenerativeImagesHistoryResponse, await res.json()) };