import {z} from 'zod' export type CreateApiActivitySearchArgs = {body: {start_date?: string | undefined, end_date?: string | undefined, search?: string | undefined, search_email_id?: string | undefined, search_subject?: string | undefined, search_sender?: string | undefined, search_recipient?: string | undefined, search_usernames?: Array | undefined, subaccounts?: Array | undefined, limit?: number | undefined, continue_token?: string | undefined, only_latest?: boolean | undefined, only_latest_by_sent?: boolean | undefined, event_types?: Array | undefined, include_headers?: boolean | undefined, custom_headers?: Array | undefined, region?: ('us' | 'eu' | 'au') | undefined}}; export type CreateApiActivitySearchOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiActivitySearchResponse = z.object({data: z.object({continue_token: z.string().nullable(), events: z.array(z.object({from: z.string(), recipient: z.string(), subaccount_name: z.string(), email_id: z.string(), date: z.string(), event: z.string(), recipients: z.array(z.string()), subject: z.string(), username: z.string(), reply_to: z.string(), sender: z.string(), sender_full: z.string(), to: z.string(), cc: z.string(), bcc: z.string(), smtp_response: z.string(), reason: z.string(), host: z.string(), originating_host: z.string().optional(), error: z.string(), email_client: z.object({}), metadata: z.object({}), outbound_ip: z.string(), byte_size: z.number().int(), headers: z.string(), custom_headers: z.object({}), delivery_attempts: z.array(z.object({smtptime: z.string().optional(), host: z.string().optional(), smtpresponse: z.string().optional()})).optional()})), total_events: z.number().int()}), request_id: z.string()}); 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 }; /** * Search activity * * POST /activity/search * * Also accepts auth: sec1 */ export const createApiActivitySearch = async (args: CreateApiActivitySearchArgs, options?: CreateApiActivitySearchOptions) => { const {baseUrl = "https://api.smtp2go.com/v3", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("X-Smtp2go-Api-Key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/activity/search${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(createApiActivitySearchResponse, await res.json()) };