import {z} from 'zod' export type CreateApiV3SequencesStatsArgs = {body: {filters?: ({from?: string | undefined, to?: string | undefined, dateRangePreset?: ('lastWeek' | 'lastMonth') | undefined, userIds?: Array | undefined}) | undefined}}; export type CreateApiV3SequencesStatsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV3SequencesStatsResponse = z.array(z.object({sequenceId: z.number().int().optional(), name: z.string().optional(), status: z.enum(["new", "active", "paused", "archived"]).optional(), emailOverview: z.object({contacted: z.number().int().optional(), delivered: z.number().int().optional(), opened: z.number().int().optional(), replied: z.number().int().optional(), interested: z.number().int().optional(), notReached: z.number().int().optional(), optedOut: z.number().int().optional(), outOfOffice: z.number().int().optional(), bounced: z.number().int().optional(), autoReplied: z.number().int().optional(), meetingsBooked: z.number().int().optional(), deliveredPercentage: z.number().optional(), openedPercentage: z.number().optional(), repliedPercentage: z.number().optional(), interestedPercentage: z.number().optional(), notReachedPercentage: z.number().optional(), optedOutPercentage: z.number().optional(), outOfOfficePercentage: z.number().optional(), bouncedPercentage: z.number().optional(), autoRepliedPercentage: z.number().optional(), meetingsBookedPercentage: z.number().optional()}).optional(), linkedInOverview: z.object({connectionsSent: z.number().int().optional(), connectionsAccepted: z.number().int().optional(), connectionsAcceptedPercentage: z.number().optional(), messagesSent: z.number().int().optional(), replied: z.number().int().optional(), repliedPercentage: z.number().optional(), inMailsSent: z.number().int().optional(), inMailsReplied: z.number().int().optional(), inMailsRepliedPercentage: z.number().optional(), connectionNotesSent: z.number().int().optional(), connectionNotesReplied: z.number().int().optional(), connectionNotesRepliedPercentage: z.number().optional(), profileViews: z.number().int().optional(), likes: z.number().int().optional(), follows: z.number().int().optional(), endorses: z.number().int().optional(), regularMessagesSent: z.number().int().optional(), regularMessagesReplied: z.number().int().optional(), regularMessagesRepliedPercentage: z.number().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 }; /** * Get stats for all sequences * * POST /v3/sequences/stats */ export const createApiV3SequencesStats = async (args: CreateApiV3SequencesStatsArgs, options?: CreateApiV3SequencesStatsOptions) => { const {baseUrl = "https://api.reply.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/sequences/stats${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(createApiV3SequencesStatsResponse, await res.json()) };