import {z} from 'zod' export type GetApiSocialConnectionIdInsightArgs = {limit?: number | undefined, offset?: number | undefined, updated_gte?: string | undefined, sort?: string | undefined, order?: string | undefined, query?: string | undefined, profile_id?: string | undefined, post_id?: string | undefined, start_gte?: string | undefined, end_lt?: string | undefined, fields?: (Array<'id' | 'created_at' | 'profile_id' | 'post_id' | 'date' | 'period' | 'metric' | 'value' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string}; export type GetApiSocialConnectionIdInsightOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const socialMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertySocialInsightMetadata = z.array(socialMetadata); export const socialInsight = z.object({id: z.string().optional(), created_at: z.string().optional(), profile_id: z.string().optional(), post_id: z.string().optional(), date: z.string().optional(), period: z.enum(["DAY", "WEEK", "MONTH", "LIFETIME", "TOTAL", "OTHER"]).optional(), metric: z.enum(["FOLLOWERS", "FOLLOWING", "POSTS", "IMPRESSIONS", "REACH", "ENGAGEMENT", "PROFILE_VIEWS", "CLICKS", "VIDEO_VIEWS", "LIKES", "COMMENTS", "SHARES", "SAVES", "OTHER"]).optional(), value: z.number().optional(), metadata: propertySocialInsightMetadata.optional(), raw: z.record(z.string(), z.unknown()).optional()}); export const socialInsights = z.array(socialInsight); 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 }; /** * List all insights * * GET /social/{connection_id}/insight */ export const getApiSocialConnectionIdInsight = async (args: GetApiSocialConnectionIdInsightArgs, options?: GetApiSocialConnectionIdInsightOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.updated_gte != null) query.append("updated_gte", String(args.updated_gte)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.query != null) query.append("query", String(args.query)) if (args.profile_id != null) query.append("profile_id", String(args.profile_id)) if (args.post_id != null) query.append("post_id", String(args.post_id)) if (args.start_gte != null) query.append("start_gte", String(args.start_gte)) if (args.end_lt != null) query.append("end_lt", String(args.end_lt)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/social/${encodeURIComponent(String(args.connection_id))}/insight${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(socialInsights, await res.json()) };