import {z} from 'zod' export type GetApiApiInboxesChannelIdTrendsArgs = {channelId: string, dateFrom: string, dateTo: string, granularity?: ('day' | 'week' | 'month' | 'quarter') | undefined, includeJunk?: boolean | undefined}; export type GetApiApiInboxesChannelIdTrendsOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxTrendPeriod = z.object({period: z.string().optional(), volume: z.object({in: z.number().int().optional(), out: z.number().int().optional(), total: z.number().int().optional()}).optional(), threadCount: z.number().int().optional(), avgResponseHrs: z.number().nullable().optional(), responseCount: z.number().int().optional(), avgFirstResponseHrs: z.number().nullable().optional(), oneTouchPct: z.number().nullable().optional(), reopenRatePct: z.number().nullable().optional()}); export const getApiApiInboxesChannelIdTrendsResponse = z.object({data: z.object({granularity: z.enum(["day", "week", "month", "quarter"]).optional(), window: z.object({from: z.string().optional(), to: z.string().optional()}).optional(), data: z.array(inboxTrendPeriod).optional()}).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 and response-time trends for an inbox * * GET /api/inboxes/{channelId}/trends * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiInboxesChannelIdTrends = async (args: GetApiApiInboxesChannelIdTrendsArgs, options?: GetApiApiInboxesChannelIdTrendsOptions) => { 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.granularity != null) query.append("granularity", String(args.granularity)) 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))}/trends${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(getApiApiInboxesChannelIdTrendsResponse, await res.json()) };