import {z} from 'zod' export type CreateApiV2GenerativeImagesLicensesArgs = {subscription_id?: string | undefined, size?: ('small' | 'medium' | 'huge' | 'custom') | undefined, body: {images: Array<{asset_id: string, ai_use_acknowledgement: boolean, metadata?: Record | undefined, price?: number | undefined, size?: ('small' | 'medium' | 'huge' | 'custom') | undefined, custom_dimensions?: ({height?: number | undefined, width?: number | undefined}) | undefined, subscription_id?: string | undefined}>}}; export type CreateApiV2GenerativeImagesLicensesOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2GenerativeImagesLicensesResponse = z.object({data: z.array(z.object({asset_id: z.string(), image_id: z.string().optional(), allotment_charge: z.number().int().optional(), download: z.object({url: z.string()}).optional(), error: z.string().optional(), license_id: z.string().optional(), price: z.object({local_amount: z.number().optional(), local_currency: z.string().optional(), content_tiers: z.record(z.string(), z.object({local_amount: z.number(), local_currency: z.string()})).optional()}).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()}); 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 }; /** * License generated images * * POST /v2/generative/images/licenses */ export const createApiV2GenerativeImagesLicenses = async (args: CreateApiV2GenerativeImagesLicensesArgs, options?: CreateApiV2GenerativeImagesLicensesOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, customerAccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.subscription_id != null) query.append("subscription_id", String(args.subscription_id)) if (args.size != null) query.append("size", String(args.size)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/generative/images/licenses${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2GenerativeImagesLicensesResponse, await res.json()) };