import {z} from 'zod' export type GetApiV1TiktokAdsSearchArgs = {query?: string | undefined, advertiser_id?: string | undefined, region?: string | undefined, days?: number | undefined, sort?: string | undefined, offset?: number | undefined, search_id?: string | undefined, count?: number | undefined}; export type GetApiV1TiktokAdsSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tikTokAdVideo = z.object({video_url: z.string().optional(), cover_img: z.string().optional()}); export const tikTokAd = z.object({id: z.string(), name: z.string().optional(), audit_status: z.string().optional(), type: z.string().optional(), first_shown_date: z.number().int().optional(), last_shown_date: z.number().int().optional(), videos: z.array(tikTokAdVideo).optional()}); export const adLibraryPage = z.object({has_more: z.boolean().optional(), total: z.number().int().optional(), search_id: z.string().optional(), offset: z.number().int().optional()}); export const adLibrarySearchResponse = z.object({ads: z.array(tikTokAd).optional(), pagination: adLibraryPage, region: z.string()}); 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 }; /** * Search the TikTok Ad Library * * GET /v1/tiktok/ads/search */ export const getApiV1TiktokAdsSearch = async (args: GetApiV1TiktokAdsSearchArgs, options?: GetApiV1TiktokAdsSearchOptions) => { const {baseUrl = "https://scrapebadger.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.advertiser_id != null) query.append("advertiser_id", String(args.advertiser_id)) if (args.region != null) query.append("region", String(args.region)) if (args.days != null) query.append("days", String(args.days)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search_id != null) query.append("search_id", String(args.search_id)) if (args.count != null) query.append("count", String(args.count)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tiktok/ads/search${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(adLibrarySearchResponse, await res.json()) };