import {z} from 'zod' export type GetApiEmailsArgs = {direction?: ('in' | 'out') | undefined, status?: ('pending' | 'succeeded' | 'failed') | undefined, domain?: string | undefined, inbox_id?: string | undefined, label?: string | undefined, archived?: boolean | undefined, category?: string | undefined, important?: boolean | undefined, include_spam?: boolean | undefined, from?: string | undefined, to?: string | undefined, unread?: boolean | undefined, after?: string | undefined, before?: string | undefined, query?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiEmailsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiEmailsResponse = z.object({data: z.array(z.unknown()), count: z.number().int()}); 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 }; /** * List sent and received email * * GET /emails */ export const getApiEmails = async (args: GetApiEmailsArgs, options?: GetApiEmailsOptions) => { const {baseUrl = "https://sendhq.cc/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.direction != null) query.append("direction", String(args.direction)) if (args.status != null) query.append("status", String(args.status)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.inbox_id != null) query.append("inbox_id", String(args.inbox_id)) if (args.label != null) query.append("label", String(args.label)) if (args.archived != null) query.append("archived", String(args.archived)) if (args.category != null) query.append("category", String(args.category)) if (args.important != null) query.append("important", String(args.important)) if (args.include_spam != null) query.append("include_spam", String(args.include_spam)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.unread != null) query.append("unread", String(args.unread)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/emails${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(getApiEmailsResponse, await res.json()) };