import {z} from 'zod' export type CreateApiInternalAnalyticsArgs = {body: {userId: string, environment: string}}; export type CreateApiInternalAnalyticsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const commonResponseStatus = z.enum(["ok", "error"]); export const commonConnectedApps = z.object({appName: z.string(), imageSrc: z.string()}); export const commonRecentConnections = z.object({id: z.string(), createdAt: z.string()}); export const commonRecentApiCalls = z.object({method: z.string(), path: z.string(), status: z.number().int()}); export const commonSummaryApiCalls = z.object({date: z.string(), numberOfCalls: z.number().int()}); export const commonAnalytics = z.object({totalConnections: z.number().int(), connectedApps: z.array(commonConnectedApps).nullable().optional(), recentConnections: z.array(commonRecentConnections).nullable().optional(), recentApiCalls: z.array(commonRecentApiCalls).nullable().optional(), totalApiCalls: z.number().int().nullable().optional(), summaryApiCalls: z.array(commonSummaryApiCalls).nullable().optional()}); export const internalGetAnalyticsResponse = z.object({status: commonResponseStatus, result: commonAnalytics}); 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 Analytics of your revert account * * POST /internal/analytics */ export const createApiInternalAnalytics = async (args: CreateApiInternalAnalyticsArgs, options?: CreateApiInternalAnalyticsOptions) => { const {baseUrl = "https://api.revert.dev", 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(/\/+$/, '')}/internal/analytics${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(internalGetAnalyticsResponse, await res.json()) };