import {z} from 'zod' export type GetApiEmailLogsArgs = {search?: string | undefined, searchBy?: ('byEmail' | 'bySubject') | undefined, fromDate?: string | undefined, toDate?: string | undefined, status?: ('Sent' | 'Processed' | 'Delivered' | 'Opened' | 'Clicked' | 'Bounced' | 'Deferred' | 'Dropped' | 'Spam Complaint' | 'Spam Report' | 'Unsubscribed' | 'Group Unsubscribe' | 'Group Resubscribe' | 'Subscription Change' | 'Failed') | undefined, orderById?: number | undefined, sortBy?: ('createdAt' | 'sentAt') | undefined, sortType?: ('ASC' | 'DESC') | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiEmailLogsOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const emailLogListItemDto: z.ZodType = z.object({id: z.number(), messageId: z.string(), recipient: z.string(), sender: z.string(), subject: z.string(), status: z.enum(["Sent", "Processed", "Delivered", "Opened", "Clicked", "Bounced", "Deferred", "Dropped", "Spam Complaint", "Spam Report", "Unsubscribed", "Group Unsubscribe", "Group Resubscribe", "Subscription Change", "Failed"]), sentAt: z.string(), customerOrderCount: z.number().optional(), attachments: z.array(z.string()).optional(), attachmentsCount: z.number().optional(), opens: z.number().optional(), clicks: z.number().optional(), isOpened: z.boolean().optional(), isClicked: z.boolean().optional(), clickedLink: z.string().optional(), createdAt: z.string(), webhookResponse: z.string().optional(), ipAddress: z.string().optional(), browser: z.string().optional(), location: z.string().optional(), history: z.array(z.lazy(() => emailLogListItemDto)).optional()}); export const emailLogListResponseDto = z.object({totalItems: z.number(), items: z.array(emailLogListItemDto), page: z.number(), size: z.number()}); 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 all email logs * * GET /email-logs * * Also accepts auth: access-token */ export const getApiEmailLogs = async (args: GetApiEmailLogsArgs, options?: GetApiEmailLogsOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.searchBy != null) query.append("searchBy", String(args.searchBy)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.status != null) query.append("status", String(args.status)) if (args.orderById != null) query.append("orderById", String(args.orderById)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortType != null) query.append("sortType", String(args.sortType)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/email-logs${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(emailLogListResponseDto, await res.json()) };