import {z} from 'zod' export type GetApiApiV1EmailsArgs = {personId?: string | undefined, projectId?: string | undefined, direction?: ('inbound' | 'outbound') | undefined, createdAfter?: (string | string) | undefined, createdBefore?: (string | string) | undefined, updatedAfter?: (string | string) | undefined, updatedBefore?: (string | string) | undefined, includeDeleted?: ('true' | 'false') | undefined, pageSize?: number | undefined, cursorDate?: string | undefined, cursorId?: string | undefined}; export type GetApiApiV1EmailsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1EmailsResponse = z.object({status: z.literal("ok"), data: z.array(z.object({id: z.string(), type: z.literal("email"), direction: z.enum(["inbound", "outbound", "null"]).nullable(), timestamp: z.string().nullable(), subject: z.string().nullable(), snippet: z.string().nullable(), threadId: z.string().nullable(), personId: z.string().nullable(), companyId: z.string().nullable(), projectId: z.string().nullable(), user: z.object({id: z.string(), name: z.string().nullable(), email: z.string().nullable()}).nullable(), createdAt: z.string().nullable(), updatedAt: z.string().nullable(), deletedAt: z.string().nullable()})), pagination: z.object({pageSize: z.number().int(), hasMore: z.boolean(), nextCursor: z.object({cursorDate: z.string(), cursorId: z.string()}).nullable()})}); 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 email activity * * GET /api/v1/emails */ export const getApiApiV1Emails = async (args: GetApiApiV1EmailsArgs, options: GetApiApiV1EmailsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.personId != null) query.append("personId", String(args.personId)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.updatedBefore != null) query.append("updatedBefore", String(args.updatedBefore)) if (args.includeDeleted != null) query.append("includeDeleted", String(args.includeDeleted)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.cursorDate != null) query.append("cursorDate", String(args.cursorDate)) if (args.cursorId != null) query.append("cursorId", String(args.cursorId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(getApiApiV1EmailsResponse, await res.json()) };