import {z} from 'zod' export type GetApiV2CatalogCollectionsArgs = {page?: number | undefined, per_page?: number | undefined, sort?: ('newest' | 'oldest') | undefined, shared?: boolean | undefined}; export type GetApiV2CatalogCollectionsOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2CatalogCollectionsResponse = z.object({page: z.number(), per_page: z.number(), total_count: z.number(), data: z.array(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 }; /** * List catalog collections * * GET /v2/catalog/collections */ export const getApiV2CatalogCollections = async (args: GetApiV2CatalogCollectionsArgs, options?: GetApiV2CatalogCollectionsOptions) => { 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 != null) query.append("sort", String(args.sort)) if (args.shared != null) query.append("shared", String(args.shared)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2CatalogCollectionsResponse, await res.json()) };