import {z} from 'zod' export type GetApiWorkspacesWorkspaceIdStatsArgs = {workspace_id: string, resource: 'contact' | 'order' | 'form_submission', count?: boolean | undefined, count_by?: {property: string} | undefined, created_at_range?: boolean | undefined, sum?: {property: string} | undefined, filter?: {[key: string]: string} | undefined, stable_id?: string | undefined, stored_filter_id?: string | undefined}; export type GetApiWorkspacesWorkspaceIdStatsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWorkspacesWorkspaceIdStatsResponse = z.object({resource: z.string().optional(), workspace_id: z.number().int().optional(), stats: z.object({count: z.number().int().optional(), count_capped_at: z.number().int().optional(), count_by: z.object({property: z.string().optional(), values: z.record(z.string(), z.number().int()).optional(), capped: z.boolean().optional()}).optional(), created_at_range: z.object({min: z.string().nullable().optional(), max: z.string().nullable().optional()}).optional(), sum: z.object({property: z.string().optional(), value: z.string().nullable().optional()}).optional()}).optional(), unavailable: z.array(z.string()).optional(), unavailable_reason: z.string().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 }; /** * Fetch Workspace Stats * * GET /workspaces/{workspace_id}/stats * * Also accepts auth: OAuth2 */ export const getApiWorkspacesWorkspaceIdStats = async (args: GetApiWorkspacesWorkspaceIdStatsArgs, options?: GetApiWorkspacesWorkspaceIdStatsOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.resource != null) query.append("resource", String(args.resource)) if (args.count != null) query.append("count", String(args.count)) if (args.count_by != null) query.append("count_by", JSON.stringify(args.count_by)) if (args.created_at_range != null) query.append("created_at_range", String(args.created_at_range)) if (args.sum != null) query.append("sum", JSON.stringify(args.sum)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.stable_id != null) query.append("stable_id", String(args.stable_id)) if (args.stored_filter_id != null) query.append("stored_filter_id", String(args.stored_filter_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspace_id))}/stats${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(getApiWorkspacesWorkspaceIdStatsResponse, await res.json()) };