import {z} from 'zod' export type BusinessID = string; export type GetApiPresenceAnalyticsKeywordsArgs = {metrics: 'count' | 'threshold', dimensions?: ('business' | 'keyword' | 'month' | 'year' | 'branded') | undefined, order_by?: ('' | '-') | undefined, business__in?: Array | undefined, filter_date__gte?: unknown | undefined, filter_date__lte?: unknown | undefined, branded?: boolean | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiPresenceAnalyticsKeywordsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const page = z.number().int(); export const totalItemsCount = z.number().int(); export const maxPage = z.number().int(); export const keyword = z.string(); export const keywordsCount = z.number().int(); export const threshold = z.number().int(); export const keywordsAnalyticsDate = z.string(); export const keywordsAnalyticsBucket = z.string(); export const businessID = z.string(); export const branded = z.enum(["branded", "discovery"]); export const presenceAnalyticsKeywordsResults = z.array(z.object({keyword: keyword.optional(), count: keywordsCount.optional(), threshold: threshold.optional(), date: keywordsAnalyticsDate.optional(), bucket: keywordsAnalyticsBucket.optional(), business_id: businessID.optional(), branded: branded.optional()})); export const presenceAnalyticsKeywordsResponse = z.object({page: page.optional(), count: totalItemsCount.optional(), max_page: maxPage.optional(), keywords: presenceAnalyticsKeywordsResults.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 }; /** * Profile keywords for your business * * GET /presence_analytics/keywords */ export const getApiPresenceAnalyticsKeywords = async (args: GetApiPresenceAnalyticsKeywordsArgs, options?: GetApiPresenceAnalyticsKeywordsOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metrics != null) query.append("metrics", String(args.metrics)) if (args.dimensions != null) query.append("dimensions", String(args.dimensions)) if (args.order_by != null) query.append("order_by", String(args.order_by)) for (const item of args.business__in ?? []) query.append("business__in", String(item)) if (args.filter_date__gte != null) query.append("filter_date__gte", String(args.filter_date__gte)) if (args.filter_date__lte != null) query.append("filter_date__lte", String(args.filter_date__lte)) if (args.branded != null) query.append("branded", String(args.branded)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/presence_analytics/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(presenceAnalyticsKeywordsResponse, await res.json()) };