import {z} from 'zod' export type GetApiProductsHelpDeskKnowledgeBaseAnalyticsArgs = {from: string, to: string}; export type GetApiProductsHelpDeskKnowledgeBaseAnalyticsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const knowledgeBaseAnalyticsFigures = z.object({views: z.number().int().gte(0), searches: z.number().int().gte(0), no_results: z.number().int().gte(0), helpful_yes: z.number().int().gte(0), helpful_no: z.number().int().gte(0), helpful_total: z.number().int().gte(0), helpful_pct: z.number().gte(0).lte(100).nullable(), no_results_pct: z.number().gte(0).lte(100).nullable()}); export const knowledgeBaseAnalytics = z.object({from: z.string(), to: z.string(), time_zone: z.string(), partial: z.boolean(), kpis: knowledgeBaseAnalyticsFigures, previous: knowledgeBaseAnalyticsFigures, series: z.array(z.object({day: z.string(), views: z.number().int().gte(0), searches: z.number().int().gte(0), no_results: z.number().int().gte(0), helpful_yes: z.number().int().gte(0), helpful_no: z.number().int().gte(0)})), articles: z.array(z.object({article_id: z.string().optional(), title: z.string().optional(), slug: z.string().optional(), views: z.number().int().gte(0).optional(), searches: z.number().int().gte(0).optional(), no_results: z.number().int().gte(0).optional(), helpful_yes: z.number().int().gte(0).optional(), helpful_no: z.number().int().gte(0).optional(), helpful_total: z.number().int().gte(0).optional(), helpful_pct: z.number().gte(0).lte(100).nullable().optional(), no_results_pct: z.number().gte(0).lte(100).nullable().optional()})), categories: z.array(z.object({category_id: z.string().optional(), name: z.string().optional(), views: z.number().int().gte(0).optional(), searches: z.number().int().gte(0).optional(), no_results: z.number().int().gte(0).optional(), helpful_yes: z.number().int().gte(0).optional(), helpful_no: z.number().int().gte(0).optional(), helpful_total: z.number().int().gte(0).optional(), helpful_pct: z.number().gte(0).lte(100).nullable().optional(), no_results_pct: z.number().gte(0).lte(100).nullable().optional()})), imported_totals: z.object({views: z.number().int().gte(0), helpful_yes: z.number().int().gte(0), helpful_no: z.number().int().gte(0)}).nullable()}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiProductsHelpDeskKnowledgeBaseAnalyticsResponse = z.object({data: knowledgeBaseAnalytics, meta: responseMeta.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 }; /** * Read knowledge base activity for a date range * * GET /products/help-desk/knowledge-base/analytics */ export const getApiProductsHelpDeskKnowledgeBaseAnalytics = async (args: GetApiProductsHelpDeskKnowledgeBaseAnalyticsArgs, options?: GetApiProductsHelpDeskKnowledgeBaseAnalyticsOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/help-desk/knowledge-base/analytics${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(getApiProductsHelpDeskKnowledgeBaseAnalyticsResponse, await res.json()) };