import {z} from 'zod' export type PinterestLibIntegerFormatType = string; export type MatchType = 'BROAD' | 'PHRASE' | 'EXACT' | 'EXACT_NEGATIVE' | 'PHRASE_NEGATIVE'; export type GetApiAdAccountsAdAccountIdKeywordsArgs = {campaign_id?: string | undefined, ad_group_id?: string | undefined, ad_group_ids?: Array | undefined, match_types?: Array | undefined, ad_account_id: string, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiAdAccountsAdAccountIdKeywordsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const matchType = z.enum(["BROAD", "PHRASE", "EXACT", "EXACT_NEGATIVE", "PHRASE_NEGATIVE"]); export const keyword = z.object({archived: z.boolean().optional(), bid: z.number().int().nullable().optional(), id: z.string(), match_type: matchType.nullable(), parent_id: z.string(), parent_type: z.string().optional(), type: z.string().optional(), value: z.string()}); export const getApiAdAccountsAdAccountIdKeywordsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(keyword)}); 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 keywords * * GET /ad_accounts/{ad_account_id}/keywords * * Also accepts auth: client_credentials */ export const getApiAdAccountsAdAccountIdKeywords = async (args: GetApiAdAccountsAdAccountIdKeywordsArgs, options?: GetApiAdAccountsAdAccountIdKeywordsOptions) => { 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.campaign_id != null) query.append("campaign_id", String(args.campaign_id)) if (args.ad_group_id != null) query.append("ad_group_id", String(args.ad_group_id)) for (const item of args.ad_group_ids ?? []) query.append("ad_group_ids", String(item)) for (const item of args.match_types ?? []) query.append("match_types", String(item)) 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(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/keywords${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(getApiAdAccountsAdAccountIdKeywordsResponse, await res.json()) };