import {z} from 'zod' export type GetApiV2EditorialImagesLivefeedsArgs = {country: unknown, page?: number | undefined, per_page?: number | undefined}; export type GetApiV2EditorialImagesLivefeedsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2EditorialImagesLivefeedsResponse = z.object({data: z.array(z.object({cover_item: z.object({height: z.number().int().optional(), id: z.string(), url: z.string(), width: z.number().int().optional()}).optional(), created_time: z.string().optional(), id: z.string(), name: z.string(), total_item_count: z.number().int()})), message: z.string().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), total_count: z.number().int()}); 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 editorial livefeed list * * GET /v2/editorial/images/livefeeds * * Also accepts auth: customer_accessCode */ export const getApiV2EditorialImagesLivefeeds = async (args: GetApiV2EditorialImagesLivefeedsArgs, options?: GetApiV2EditorialImagesLivefeedsOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country != null) query.append("country", String(args.country)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/editorial/images/livefeeds${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(getApiV2EditorialImagesLivefeedsResponse, await res.json()) };