import {z} from 'zod' export type CreateApiV2VideosLicensesArgs = {subscription_id?: string | undefined, size?: ('web' | 'sd' | 'hd' | '4k') | undefined, search_id?: string | undefined, body: {videos: Array<{auth_cookie?: {name: string, value: string} | undefined, editorial_acknowledgement?: boolean | undefined, metadata?: Record | undefined, price?: number | undefined, search_id?: string | undefined, show_modal?: boolean | undefined, size?: ('web' | 'sd' | 'hd' | '4k') | undefined, subscription_id?: string | undefined, video_id: string}>}}; export type CreateApiV2VideosLicensesOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2VideosLicensesResponse = z.object({data: z.array(z.object({allotment_charge: z.number().int().optional(), download: z.object({url: z.string()}).optional(), license_id: z.string().optional(), error: 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(), video_id: 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(), total_count: z.number().int().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 }; /** * License videos * * POST /v2/videos/licenses */ export const createApiV2VideosLicenses = async (args: CreateApiV2VideosLicensesArgs, options?: CreateApiV2VideosLicensesOptions) => { 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 (args.search_id != null) query.append("search_id", String(args.search_id)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/videos/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(createApiV2VideosLicensesResponse, await res.json()) };