import {z} from 'zod' export type RequestInfo = {requestId: string}; export type DisplayName = string; export type CompanyName = string; export type AccountId = string; export type AccountName = string; export type PrimaryEmail = string; export type OtherAlias = string; export type IdentityType = 'internal' | 'external' | 'bot'; export type PhoneNumber = string; export type Sender = {displayName: DisplayName, companyName?: CompanyName | undefined, accountId?: AccountId | undefined, accountName?: AccountName | undefined, primaryEmail: PrimaryEmail, otherAlias?: OtherAlias | undefined, identityType?: IdentityType | undefined, phoneNumber?: PhoneNumber | undefined, senderType: 'sender' | 'sent_on_behalf_of' | 'original_sender' | 'shared_messenger'}; export type RecipientUser = {deliveryTypes: Array<'to' | 'cc' | 'bcc'>, displayName: DisplayName, companyName?: CompanyName | undefined, accountId?: AccountId | undefined, accountName?: AccountName | undefined, primaryEmail: PrimaryEmail, otherAlias?: OtherAlias | undefined, phoneNumber?: PhoneNumber | undefined, identityType?: IdentityType | undefined}; export type Content = {textType?: ('plain' | 'html' | 'markdown') | undefined, priority?: string | undefined, sensitivity?: string | undefined, classification?: string | undefined, message: string, signature?: string | undefined, disclaimer?: string | undefined}; export type File = {filename?: string | undefined, fileKey: string, fileHref?: string | undefined, contentId?: string | undefined}; export type EmailProperty = {name: string, value: string, xheader?: boolean | undefined, customHeader?: boolean | undefined, hidden?: boolean | undefined}; export type EmailOverview = {messageId: string, time: number, subject?: string | undefined, senders: Array, recipients?: Array | undefined, content: Content, files?: Array | undefined, emailProperties?: Array | undefined, eventType?: ('email_sent' | 'email_received' | 'email_forwarded' | 'email_replied' | 'email_replied_all' | 'email_bounced' | 'email_draft' | 'email_deleted' | 'email_read' | 'email_unread' | 'email_archived' | 'email_recalled' | 'email_sent_on_behalf' | 'auto_reply' | 'email_scheduled' | 'email_system_generated') | undefined}; export type EmailPayload = {requestInfo?: RequestInfo | undefined, emailOverview?: EmailOverview | undefined}; export type CreateApiV2EmailArgs = {'X-SubType': string, body: EmailPayload}; export type CreateApiV2EmailOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createEmailResponse = z.object({reconciliationId: z.string()}); 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 }; /** * POST a single email message * * POST /v2/email */ export const createApiV2Email = async (args: CreateApiV2EmailArgs, options?: CreateApiV2EmailOptions) => { const {baseUrl = "https://swaggerhub.globalrelay.net/virts/Gateway-APIs/oc-email-api/1.2", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-SubType'] != null) headers.set("X-SubType", String(args['X-SubType'])) if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/email${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(createEmailResponse, await res.json()) };