fix(types): Fix TypeScript build errors and improve API type safety across platformservice interfaces

This commit is contained in:
2025-05-08 10:39:43 +00:00
parent 4624fdbe10
commit 39b634b6bb
9 changed files with 445 additions and 37 deletions

View File

@ -5,6 +5,10 @@ import { logger } from '../../logger.js';
// Import MTA classes
import { MtaService } from './classes.mta.js';
import { Email as MtaEmail } from '../core/classes.email.js';
import { DeliveryStatus } from './classes.emailsendjob.js';
// Re-export for use in index.ts
export { DeliveryStatus };
// Import Email types
export interface IEmailOptions {
@ -19,14 +23,6 @@ export interface IEmailOptions {
headers?: { [key: string]: string };
}
// Reuse the DeliveryStatus from the email send job
export enum DeliveryStatus {
PENDING = 'pending',
PROCESSING = 'processing',
DELIVERED = 'delivered',
DEFERRED = 'deferred',
FAILED = 'failed'
}
// Reuse the IAttachment interface
export interface IAttachment {
@ -37,6 +33,66 @@ export interface IAttachment {
encoding?: string;
}
/**
* Email status details
*/
export interface IEmailStatusDetails {
/** Number of delivery attempts */
attempts?: number;
/** Timestamp of last delivery attempt */
lastAttempt?: Date;
/** Timestamp of next scheduled attempt */
nextAttempt?: Date;
/** Error message if delivery failed */
error?: string;
/** Message explaining the status */
message?: string;
}
/**
* Email status response
*/
export interface IEmailStatusResponse {
/** Current status of the email */
status: DeliveryStatus | 'unknown' | 'error';
/** Additional status details */
details?: IEmailStatusDetails;
}
/**
* Options for sending an email via MTA
*/
export interface ISendEmailOptions {
/** Whether to use MIME format conversion */
useMimeFormat?: boolean;
/** Whether to track clicks */
trackClicks?: boolean;
/** Whether to track opens */
trackOpens?: boolean;
/** Message priority (1-5, where 1 is highest) */
priority?: number;
/** Message scheduling options */
schedule?: {
/** Time to send the email */
sendAt?: Date | string;
/** Time the message expires */
expireAt?: Date | string;
};
/** DKIM signing options */
dkim?: {
/** Whether to sign the message */
sign?: boolean;
/** Domain to use for signing */
domain?: string;
/** Key selector to use */
selector?: string;
};
/** Additional headers */
headers?: Record<string, string>;
/** Message tags for categorization */
tags?: string[];
}
export class MtaConnector {
public emailRef: EmailService;
private mtaService: MtaService;
@ -46,6 +102,13 @@ export class MtaConnector {
this.mtaService = mtaService || this.emailRef.mtaService;
}
/**
* Send an email using the MTA service
* @param smartmail The email to send
* @param toAddresses Recipients (comma-separated or array)
* @param options Additional options
*/
/**
* Send an email using the MTA service
* @param smartmail The email to send
@ -55,7 +118,7 @@ export class MtaConnector {
public async sendEmail(
smartmail: plugins.smartmail.Smartmail<any>,
toAddresses: string | string[],
options: any = {}
options: ISendEmailOptions = {}
): Promise<string> {
// Check if recipients are on the suppression list
const recipients = Array.isArray(toAddresses)
@ -101,7 +164,7 @@ export class MtaConnector {
const emailOptions: Record<string, any> = { ...options };
// Check if we should use MIME format
const useMimeFormat = options.useMimeFormat ?? true;
const useMimeFormat = options.useMimeFormat !== false; // Default to true
if (useMimeFormat) {
// Use smartmail's MIME conversion for improved handling
@ -521,28 +584,28 @@ export class MtaConnector {
/**
* Check the status of a sent email
* @param emailId The email ID to check
* @returns Current status and details
*/
public async checkEmailStatus(emailId: string): Promise<{
status: string;
details?: any;
}> {
public async checkEmailStatus(emailId: string): Promise<IEmailStatusResponse> {
try {
const status = this.mtaService.getEmailStatus(emailId);
if (!status) {
return {
status: 'unknown',
status: 'unknown' as const,
details: { message: 'Email not found' }
};
}
return {
status: status.status,
// Use type assertion to ensure this passes type check
status: status.status as DeliveryStatus,
details: {
attempts: status.attempts,
lastAttempt: status.lastAttempt,
nextAttempt: status.nextAttempt,
error: status.error?.message
error: status.error?.message,
message: `Status: ${status.status}${status.error ? `, Error: ${status.error.message}` : ''}`
}
};
} catch (error) {
@ -554,7 +617,7 @@ export class MtaConnector {
});
return {
status: 'error',
status: 'error' as const,
details: { message: error.message }
};
}