import {z} from 'zod' export type CreateApiEmailSearchArgs = {body: {start_date?: string | undefined, end_date?: string | undefined, limit?: number | undefined, status_counts?: boolean | undefined, opened_only?: boolean | undefined, clicked_only?: boolean | undefined, ignore_case?: boolean | undefined, filter_query?: string | undefined, email_id?: Array | undefined, username?: string | undefined, headers?: Array | undefined, continue_token?: string | undefined}}; export type CreateApiEmailSearchOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEmailSearchResponse = z.object({request_id: z.string().optional(), data: z.object({count: z.number().int().optional(), emails: z.array(z.object({sender: z.string().optional(), email_id: z.string().optional(), smtpcode: z.number().int().optional(), response: z.string().optional(), process_status: z.string().optional(), host: z.string().optional(), recipient: z.string().optional(), delivered_at: z.string().optional(), opens: z.array(z.object({user_agent: z.string().optional(), opened_at: z.string().optional(), read_secs: z.string().optional(), recipient: z.string().optional(), ip_address: z.string().optional()})).optional(), total_opens: z.number().int().optional(), status: z.string().optional(), email_ts: z.string().optional()})).optional(), continue_token: z.string().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 }; /** * View and search sent emails * * POST /email/search * * Also accepts auth: sec1 */ export const createApiEmailSearch = async (args: CreateApiEmailSearchArgs, options?: CreateApiEmailSearchOptions) => { 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(/\/+$/, '')}/email/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(createApiEmailSearchResponse, await res.json()) };