import {z} from 'zod' export type GetApiV2ImagesCollectionsIdItemsArgs = {id: string, page?: number | undefined, per_page?: number | undefined, share_code?: string | undefined, sort?: ('newest' | 'oldest') | undefined}; export type GetApiV2ImagesCollectionsIdItemsOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ImagesCollectionsIdItemsResponse = z.object({data: z.array(z.object({added_time: z.string().optional(), id: z.string(), media_type: 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().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 }; /** * Get the contents of image collections * * GET /v2/images/collections/{id}/items */ export const getApiV2ImagesCollectionsIdItems = async (args: GetApiV2ImagesCollectionsIdItemsArgs, options?: GetApiV2ImagesCollectionsIdItemsOptions) => { 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.share_code != null) query.append("share_code", String(args.share_code)) if (args.sort != null) query.append("sort", String(args.sort)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/images/collections/${encodeURIComponent(String(args.id))}/items${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(getApiV2ImagesCollectionsIdItemsResponse, await res.json()) };