import {z} from 'zod' export type GetApiSocialConnectionIdInsightIdArgs = {fields?: (Array<'id' | 'created_at' | 'profile_id' | 'post_id' | 'date' | 'period' | 'metric' | 'value' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, id: string}; export type GetApiSocialConnectionIdInsightIdOptions = {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 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 }; /** * Retrieve an insight * * GET /social/{connection_id}/insight/{id} */ export const getApiSocialConnectionIdInsightId = async (args: GetApiSocialConnectionIdInsightIdArgs, options?: GetApiSocialConnectionIdInsightIdOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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/${encodeURIComponent(String(args.id))}${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(socialInsight, await res.json()) };