import {z} from 'zod' export type GetApiApiPublicEmailChannelIdArgs = {'x-api-key': string, id: string}; export type GetApiApiPublicEmailChannelIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dnsRecordResponseDto = z.object({id: z.string(), record_type: z.enum(["cname", "mx", "txt"]), host: z.string(), data: z.string(), valid: z.boolean(), purpose: z.enum(["spf", "dkim1", "dkim2", "inbound_mx"])}); export const plannedInboundMxResponseDto = z.object({host: z.string(), data: z.string()}); export const emailDomainResponseDto = z.object({id: z.string(), domain: z.string(), outbound_validation_status: z.enum(["not_configured", "pending", "verified", "failed"]), inbound_validation_status: z.enum(["not_configured", "pending", "verified", "failed"]), dns_records: z.array(dnsRecordResponseDto).optional(), planned_inbound_mx: plannedInboundMxResponseDto.optional(), connection_method: z.enum(["manual", "shared"]), subdomain_slug: z.string().nullable().optional()}); export const emailChannelResponseDto = z.object({id: z.string(), sender_address: z.string(), sender_name: z.string(), created_at: z.string(), email_domain: emailDomainResponseDto}); 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 a single email channel * * GET /api/public/email-channel/{id} */ export const getApiApiPublicEmailChannelId = async (args: GetApiApiPublicEmailChannelIdArgs, options?: GetApiApiPublicEmailChannelIdOptions) => { const {baseUrl = "https://api.vambe.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/email-channel/${encodeURIComponent(String(args.id))}${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(emailChannelResponseDto, await res.json()) };