import {z} from 'zod' export type InsightsRequest = Array<{network_id?: number | undefined, network_type?: (0 | 1 | 2) | undefined, client_user_network_id?: number | undefined, extern_post_id?: number | undefined}>; export type CreateApiNetworkPostInsightsTotalArgs = {body: InsightsRequest}; export type CreateApiNetworkPostInsightsTotalOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const insightsArray = z.array(z.object({intern_post_id: z.number().int().optional(), extern_post_id: z.number().int().optional(), network_id: z.number().int().optional(), error: z.number().int().optional(), insights: z.object({likes: z.number().int().optional(), comments: z.number().int().optional(), reshares: z.number().int().optional(), impressions: z.number().int().optional(), mentions: z.number().int().optional(), profile_views: z.number().int().optional(), video_views: z.number().int().optional()}).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 }; /** * V1 - Get Total Insights * * POST /network/post/insights/total */ export const createApiNetworkPostInsightsTotal = async (args: CreateApiNetworkPostInsightsTotalArgs, options?: CreateApiNetworkPostInsightsTotalOptions) => { const {baseUrl = "https://api.blog2social.com/rest/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/network/post/insights/total${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(insightsArray, await res.json()) };