import {z} from 'zod' export type GetApiApiInboxesChannelIdHealthArgs = {channelId: string, dateFrom: string, dateTo: string, baselinePeriods?: number | undefined, includeJunk?: boolean | undefined}; export type GetApiApiInboxesChannelIdHealthOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxHealth = z.object({window: z.object({from: z.string().optional(), to: z.string().optional()}).optional(), baseline: z.object({from: z.string().optional(), to: z.string().optional(), periods: z.number().int().optional(), avgVolume: z.number().nullable().optional(), sufficient: z.boolean().optional()}).optional(), volume: z.number().int().optional(), volumeVsBaselinePct: z.number().nullable().optional(), trend: z.enum(["falling", "rising", "flat", "unknown"]).optional(), severityScore: z.number().int().nullable().optional(), severityLabel: z.enum(["critical", "high", "moderate", "low", "none", "insufficient_history"]).optional(), tags: z.array(z.string()).optional()}); export const getApiApiInboxesChannelIdHealthResponse = z.object({data: inboxHealth.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 }; /** * Volume health for an inbox against its own baseline * * GET /api/inboxes/{channelId}/health * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiInboxesChannelIdHealth = async (args: GetApiApiInboxesChannelIdHealthArgs, options?: GetApiApiInboxesChannelIdHealthOptions) => { const {baseUrl = "https://core-api.getaptly.com", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.baselinePeriods != null) query.append("baselinePeriods", String(args.baselinePeriods)) if (args.includeJunk != null) query.append("includeJunk", String(args.includeJunk)) if (ApiKeyHeader !== undefined) headers.set("x-token", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/inboxes/${encodeURIComponent(String(args.channelId))}/health${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(getApiApiInboxesChannelIdHealthResponse, await res.json()) };