import {z} from 'zod' export type GetApiV2ContributorsArgs = {id: Array}; export type GetApiV2ContributorsOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ContributorsResponse = z.object({data: z.array(z.object({about: z.string().optional(), contributor_type: z.array(z.string()).optional(), display_name: z.string().optional(), equipment: z.array(z.string()).optional(), id: z.string(), location: z.string().optional(), portfolio_url: z.string().optional(), social_media: z.object({facebook: z.string().optional(), google_plus: z.string().optional(), linkedin: z.string().optional(), pinterest: z.string().optional(), tumblr: z.string().optional(), twitter: z.string().optional()}).optional(), styles: z.array(z.string()).optional(), subjects: z.array(z.string()).optional(), website: 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 details about multiple contributors * * GET /v2/contributors * * Also accepts auth: basic */ export const getApiV2Contributors = async (args: GetApiV2ContributorsArgs, options?: GetApiV2ContributorsOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, customerAccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.id ?? []) query.append("id", String(item)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/contributors${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(getApiV2ContributorsResponse, await res.json()) };