import {z} from 'zod' export type GetApiV1EmailsArgs = {emailId?: string | undefined, include?: string | undefined, status?: ('streaming' | 'complete' | 'error') | undefined, createdAtFrom?: string | undefined, createdAtTo?: string | undefined, updatedAtFrom?: string | undefined, updatedAtTo?: string | undefined, limit?: (number | string) | undefined, cursor?: string | undefined}; export type GetApiV1EmailsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailsListResponse = z.object({data: z.array(z.object({emailId: z.string().min(1), emailVersionId: z.string().min(1).optional(), title: z.string().min(1), status: z.enum(["streaming", "complete", "error"]), previewImage: z.string().optional(), updatedAt: z.string(), previewText: z.string().optional(), html: z.string().optional(), versions: z.array(z.object({version: z.union([z.number().int().gte(0), z.literal("latest")]), emailVersionId: z.string().min(1)})).optional()})), pagination: z.object({limit: z.number().int().gte(1).lte(100), cursor: z.string().nullable(), hasMore: z.boolean()}).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 }; /** * Get emails * * GET /v1/emails * * Also accepts auth: apiKeyAuth */ export const getApiV1Emails = async (args: GetApiV1EmailsArgs, options?: GetApiV1EmailsOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.emailId != null) query.append("emailId", String(args.emailId)) if (args.include != null) query.append("include", String(args.include)) if (args.status != null) query.append("status", String(args.status)) if (args.createdAtFrom != null) query.append("createdAtFrom", String(args.createdAtFrom)) if (args.createdAtTo != null) query.append("createdAtTo", String(args.createdAtTo)) if (args.updatedAtFrom != null) query.append("updatedAtFrom", String(args.updatedAtFrom)) if (args.updatedAtTo != null) query.append("updatedAtTo", String(args.updatedAtTo)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(emailsListResponse, await res.json()) };