import {z} from 'zod' export type InterestsEnum = 'ALL' | 'ANIMALS' | 'ARCHITECTURE' | 'ART' | 'BEAUTY' | 'DIY_AND_CRAFTS' | 'EDUCATION' | 'EVENT_PLANNING' | 'FASHION' | 'FOOD_AND_DRINKS' | 'GARDENING' | 'HEALTH' | 'HOME_DECOR' | 'PARENTING' | 'TRAVEL' | 'WEDDING'; export type ProductCategoryRegion = 'US' | 'GB+IE' | 'CA'; export type GetApiTrendsTopicsFeaturedArgs = {interest?: InterestsEnum | undefined, region: ProductCategoryRegion}; export type GetApiTrendsTopicsFeaturedOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const interestsEnum = z.enum(["ALL", "ANIMALS", "ARCHITECTURE", "ART", "BEAUTY", "DIY_AND_CRAFTS", "EDUCATION", "EVENT_PLANNING", "FASHION", "FOOD_AND_DRINKS", "GARDENING", "HEALTH", "HOME_DECOR", "PARENTING", "TRAVEL", "WEDDING"]); export const productCategoryRegion = z.enum(["US", "GB+IE", "CA"]); export const trendingPin = z.object({color: z.string(), height: z.number().int(), id: z.string(), src: z.string(), vertical_offset: z.number().optional(), width: z.number().int()}); export const trendingTopic = z.object({description: z.string(), id: z.string(), percent_growth_mom: z.number().int().optional(), pins: z.array(trendingPin), related_interests: z.array(z.string()), related_searches: z.array(z.string()), time_series: z.record(z.string(), z.number()), title: z.string()}); export const featuredTrend = z.object({interest: interestsEnum, market: productCategoryRegion.optional(), trends: z.array(trendingTopic).optional()}); export const getApiTrendsTopicsFeaturedResponse = z.array(featuredTrend); 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 featured topics * * GET /trends/topics/featured * * Also accepts auth: client_credentials */ export const getApiTrendsTopicsFeatured = async (args: GetApiTrendsTopicsFeaturedArgs, options?: GetApiTrendsTopicsFeaturedOptions) => { 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.interest != null) query.append("interest", String(args.interest)) if (args.region != null) query.append("region", String(args.region)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/trends/topics/featured${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(getApiTrendsTopicsFeaturedResponse, await res.json()) };