import {z} from 'zod' export type GetApiAdAccountsAdAccountIdBulkBulkRequestIdArgs = {ad_account_id: string, bulk_request_id: string, include_details?: boolean | undefined}; export type GetApiAdAccountsAdAccountIdBulkBulkRequestIdOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkRequestStatus = z.enum(["RUNNING", "SUCCEEDED", "FAILED"]); export const bulkJobData = z.object({result_url: z.string().optional(), status: bulkRequestStatus, workload_id: 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 }; /** * Download advertiser entities in bulk * * GET /ad_accounts/{ad_account_id}/bulk/{bulk_request_id} * * Also accepts auth: client_credentials */ export const getApiAdAccountsAdAccountIdBulkBulkRequestId = async (args: GetApiAdAccountsAdAccountIdBulkBulkRequestIdArgs, options?: GetApiAdAccountsAdAccountIdBulkBulkRequestIdOptions) => { 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.include_details != null) query.append("include_details", String(args.include_details)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/bulk/${encodeURIComponent(String(args.bulk_request_id))}${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(bulkJobData, await res.json()) };