import {z} from 'zod' export type GetApiV1AccountsAccountIdLinkedinAggregateAnalyticsArgs = {accountId: string, aggregation?: ('TOTAL' | 'DAILY') | undefined, fromDate?: string | undefined, toDate?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, metrics?: string | undefined}; export type GetApiV1AccountsAccountIdLinkedinAggregateAnalyticsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const linkedInAggregateAnalyticsTotalResponse = z.object({accountId: z.string().optional(), platform: z.string().optional(), accountType: z.string().optional(), username: z.string().optional(), aggregation: z.literal("TOTAL").optional(), dateRange: z.object({startDate: z.string().optional(), endDate: z.string().optional()}).nullable().optional(), analytics: z.object({impressions: z.number().int().optional(), reach: z.number().int().optional(), reactions: z.number().int().optional(), comments: z.number().int().optional(), shares: z.number().int().optional(), saves: z.number().int().optional(), sends: z.number().int().optional(), engagementRate: z.number().optional()}).optional(), note: z.string().optional(), lastUpdated: z.string().optional()}); export const linkedInAggregateAnalyticsDailyResponse = z.object({accountId: z.string().optional(), platform: z.string().optional(), accountType: z.string().optional(), username: z.string().optional(), aggregation: z.literal("DAILY").optional(), dateRange: z.object({startDate: z.string().optional(), endDate: z.string().optional()}).nullable().optional(), analytics: z.object({impressions: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional(), reactions: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional(), comments: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional(), shares: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional(), saves: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional(), sends: z.array(z.object({date: z.string().optional(), count: z.number().int().optional()})).optional()}).optional(), skippedMetrics: z.array(z.string()).optional(), note: z.string().optional(), lastUpdated: z.string().optional()}); export const getApiV1AccountsAccountIdLinkedinAggregateAnalyticsResponse = z.union([linkedInAggregateAnalyticsTotalResponse, linkedInAggregateAnalyticsDailyResponse]); 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 LinkedIn aggregate stats * * GET /v1/accounts/{accountId}/linkedin-aggregate-analytics */ export const getApiV1AccountsAccountIdLinkedinAggregateAnalytics = async (args: GetApiV1AccountsAccountIdLinkedinAggregateAnalyticsArgs, options?: GetApiV1AccountsAccountIdLinkedinAggregateAnalyticsOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.aggregation != null) query.append("aggregation", String(args.aggregation)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.metrics != null) query.append("metrics", String(args.metrics)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}/linkedin-aggregate-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(getApiV1AccountsAccountIdLinkedinAggregateAnalyticsResponse, await res.json()) };