import {z} from 'zod' export type BoardPrivacyFilter = 'ALL' | 'PUBLIC' | 'PROTECTED' | 'SECRET' | 'PUBLIC_AND_SECRET'; export type GetApiBoardsArgs = {ad_account_id?: string | undefined, privacy?: BoardPrivacyFilter | undefined, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiBoardsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const boardMedia = z.object({image_cover_url: z.string().nullable().optional(), pin_thumbnail_urls: z.array(z.string()).optional()}); export const boardOwner = z.object({username: z.string().optional()}); export const board = z.object({board_pins_modified_at: z.string().optional(), collaborator_count: z.number().int().gte(0).optional(), created_at: z.string().optional(), description: z.string().nullable().optional(), follower_count: z.number().int().gte(0).optional(), id: z.string(), is_ads_only: z.boolean().optional(), media: boardMedia.optional(), name: z.string(), owner: boardOwner.optional(), pin_count: z.number().int().gte(0).optional()}); export const getApiBoardsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(board)}); 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 boards * * GET /boards * * Also accepts auth: client_credentials */ export const getApiBoards = async (args: GetApiBoardsArgs, options?: GetApiBoardsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ad_account_id != null) query.append("ad_account_id", String(args.ad_account_id)) if (args.privacy != null) query.append("privacy", String(args.privacy)) if (args.bookmark != null) query.append("bookmark", String(args.bookmark)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/boards${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(getApiBoardsResponse, await res.json()) };