import {z} from 'zod' export type CreateApiV2CatalogCollectionsArgs = {body: {name: string, visibility?: ('private' | 'public') | undefined, items?: Array<{asset: {id: string, type: string}}> | undefined}}; export type CreateApiV2CatalogCollectionsOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2CatalogCollectionsResponse = z.object({id: z.string(), name: z.string(), cover_asset: 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()}).optional(), total_item_count: z.number(), created_time: z.string(), updated_time: z.string(), visibility: z.enum(["private", "public"]), role_assignments: z.object({collection_id: z.string(), roles: z.object({owners: z.array(z.object({id: z.string(), type: z.literal("USER"), email: z.string()})).optional(), editors: z.array(z.object({id: z.string(), type: z.literal("USER"), email: z.string()})).optional(), viewers: z.array(z.object({id: z.string(), type: z.literal("USER"), email: 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 }; /** * Create catalog collections * * POST /v2/catalog/collections */ export const createApiV2CatalogCollections = async (args: CreateApiV2CatalogCollectionsArgs, options?: CreateApiV2CatalogCollectionsOptions) => { 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 (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/catalog/collections${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(createApiV2CatalogCollectionsResponse, await res.json()) };