import {z} from 'zod' export type GetApiV1NichesRankRadarsRankRadarIdArgs = {rankRadarId: string, startDate: string, endDate: string, currentPage?: number | undefined, pageSize?: number | undefined}; export type GetApiV1NichesRankRadarsRankRadarIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const externalKrtAsinRankDto = z.object({date: z.string(), organicRank: z.object({}), sponsoredRank: z.object({}).nullable(), impressionRank: z.object({})}); export const krtHighlightDto = z.object({id: z.string(), krtId: z.string(), krtKeywordId: z.string(), title: z.string(), description: z.object({}), color: z.object({}), type: z.string(), startAt: z.object({}), endAt: z.object({}), searchVolume: z.number(), svShare: z.number(), keywords: z.array(z.string())}); export const krtKeywordResponseDto = z.object({id: z.string(), keyword: z.string(), searchVolume: z.object({}), ranks: z.array(externalKrtAsinRankDto), highlights: z.array(krtHighlightDto), relevancy: z.object({}).optional()}); export const krtKeywordListResponseDto = z.object({currentPage: z.number(), pageSize: z.number(), hasNext: z.boolean(), hasPrev: z.boolean(), lastPage: z.number(), total: z.number(), data: z.array(krtKeywordResponseDto)}); 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 keyword rankings for a Rank Radar * * GET /v1/niches/rank-radars/{rankRadarId} */ export const getApiV1NichesRankRadarsRankRadarId = async (args: GetApiV1NichesRankRadarsRankRadarIdArgs, options?: GetApiV1NichesRankRadarsRankRadarIdOptions) => { const {baseUrl = "https://api.datadive.tools", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.currentPage != null) query.append("currentPage", String(args.currentPage)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/niches/rank-radars/${encodeURIComponent(String(args.rankRadarId))}${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(krtKeywordListResponseDto, await res.json()) };