@api-buddy/sendgrid
Version:
API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails
352 lines (347 loc) • 7.97 kB
TypeScript
export { MailDataRequired } from '@sendgrid/mail';
/**
* Configuration options for the SendGrid client
*/
interface SendGridConfig {
/**
* Your SendGrid API key
*/
apiKey?: string;
/**
* Default "from" email address
*/
defaultFrom?: string;
/**
* Default "reply to" email address
*/
defaultReplyTo?: string;
/**
* Default mail settings for all emails
*/
mailSettings?: MailSettings;
/**
* Default tracking settings for all emails
*/
trackingSettings?: TrackingSettings;
/**
* Default ASM (Advanced Suppression Manager) settings
*/
asm?: ASMOptions;
/**
* Default IP pool name
*/
ipPoolName?: string;
}
/**
* Options for sending an email
*/
interface EmailOptions {
/**
* Recipient email address or array of email addresses
*/
to: string | string[];
/**
* Sender email address
*/
from?: string;
/**
* Email subject
*/
subject: string;
/**
* Plain text content
*/
text?: string;
/**
* HTML content
*/
html?: string;
/**
* SendGrid template ID
*/
templateId?: string;
/**
* Dynamic template data
*/
dynamicTemplateData?: Record<string, any>;
/**
* Email attachments
*/
attachments?: Attachment[];
/**
* Email categories for tracking
*/
categories?: string[];
/**
* Custom arguments
*/
customArgs?: Record<string, string>;
/**
* Custom headers
*/
headers?: Record<string, string>;
/**
* Mail settings
*/
mailSettings?: MailSettings;
/**
* Tracking settings
*/
trackingSettings?: TrackingSettings;
/**
* Reply-to email address
*/
replyTo?: string;
/**
* Send at timestamp (Unix timestamp)
*/
sendAt?: number;
/**
* Batch ID for scheduling
*/
batchId?: string;
/**
* ASM (Advanced Suppression Manager) settings
*/
asm?: ASMOptions;
/**
* IP pool name
*/
ipPoolName?: string;
}
/**
* Email attachment
*/
interface Attachment {
content: string | Buffer;
filename: string;
type?: string;
disposition?: string;
contentId?: string;
}
/**
* Personalization for sending to multiple recipients with different data
*/
interface Personalization {
to: {
email: string;
name?: string;
}[];
cc?: {
email: string;
name?: string;
}[];
bcc?: {
email: string;
name?: string;
}[];
subject?: string;
headers?: Record<string, string>;
substitutions?: Record<string, string>;
customArgs?: Record<string, string>;
sendAt?: number;
dynamicTemplateData?: Record<string, any>;
}
/**
* Mail settings
*/
interface MailSettings {
bcc?: BCCSettings;
bypassListManagement?: BypassListManagement;
bypassSpamManagement?: {
enable: boolean;
};
bypassBounceManagement?: {
enable: boolean;
};
bypassUnsubscribeManagement?: {
enable: boolean;
};
footer?: FooterSettings;
sandboxMode?: {
/**
* Whether to enable sandbox mode
*/
enable: boolean;
};
spamCheck?: {
enable: boolean;
threshold?: number;
post_to_url?: string;
};
}
/**
* BCC settings
*/
interface BCCSettings {
enable?: boolean;
email?: string;
}
/**
* Bypass list management settings
*/
interface BypassListManagement {
enable: boolean;
}
/**
* Footer settings
*/
interface FooterSettings {
enable?: boolean;
text?: string;
html?: string;
}
/**
* Tracking settings
*/
interface TrackingSettings {
clickTracking?: ClickTracking;
openTracking?: OpenTracking;
subscriptionTracking?: SubscriptionTracking;
ganalytics?: Ganalytics;
}
/**
* Click tracking settings
*/
interface ClickTracking {
enable?: boolean;
enableText?: boolean;
}
/**
* Open tracking settings
*/
interface OpenTracking {
enable?: boolean;
substitutionTag?: string;
}
/**
* Subscription tracking settings
*/
interface SubscriptionTracking {
enable?: boolean;
text?: string;
html?: string;
substitutionTag?: string;
}
/**
* Google Analytics settings
*/
interface Ganalytics {
enable?: boolean;
utmSource?: string;
utmMedium?: string;
utmTerm?: string;
utmContent?: string;
utmCampaign?: string;
}
/**
* ASM (Advanced Suppression Manager) settings
*/
interface ASMOptions {
groupId: number;
groupsToDisplay?: number[];
groupsToDisplayOnEmail?: boolean;
}
/**
* Response from SendGrid API
*/
interface SendGridResponse {
success: boolean;
statusCode: number;
headers: Record<string, string>;
body: any;
}
/**
* Response for multiple emails
*/
interface BatchSendGridResponse {
success: boolean;
responses: Array<{
statusCode: number;
headers: Record<string, string>;
body: any;
}>;
}
/**
* Error response from SendGrid
*/
interface SendGridError extends Error {
code: number;
response?: {
headers: Record<string, string>;
body: {
errors: Array<{
message: string;
field?: string;
help?: string;
}>;
};
};
}
declare class SendGridClient {
private config;
private initialized;
constructor(config: SendGridConfig);
/**
* Initialize the SendGrid client with the API key
*/
private initialize;
/**
* Send an email using SendGrid
* @param options Email options
* @returns Promise with the SendGrid response
*/
sendEmail(options: EmailOptions): Promise<{
success: boolean;
statusCode: number;
headers: any;
body: object;
}>;
/**
* Send multiple emails in a single request
* @param messages Array of email options
* @returns Promise with the SendGrid response
*/
sendMultipleEmails(messages: EmailOptions[]): Promise<BatchSendGridResponse>;
}
declare const sendGridClient: SendGridClient;
/**
* Send an email using the default SendGrid client
* @param options Email options
* @returns Promise with the SendGrid response
*/
declare const sendEmail: (options: EmailOptions) => Promise<{
success: boolean;
statusCode: number;
headers: any;
body: object;
}>;
/**
* Send multiple emails in a single request using the default SendGrid client
* @param messages Array of email options
* @returns Promise with the SendGrid response
*/
declare const sendMultipleEmails: (messages: EmailOptions[]) => Promise<BatchSendGridResponse>;
/**
* Hook for sending emails with SendGrid
* @returns Object containing the send function, loading state, and error state
*/
declare function useSendEmail(): {
send: (options: EmailOptions) => Promise<SendGridResponse>;
isLoading: boolean;
error: SendGridError | null;
response: SendGridResponse | null;
reset: () => void;
};
/**
* Hook for sending multiple emails with SendGrid
* @returns Object containing the send function, loading state, and error state
*/
declare function useSendMultipleEmails(): {
send: (messages: EmailOptions[]) => Promise<BatchSendGridResponse>;
isLoading: boolean;
error: SendGridError | null;
response: BatchSendGridResponse | null;
reset: () => void;
};
export { type ASMOptions, type Attachment, type BCCSettings, type BatchSendGridResponse, type BypassListManagement, type ClickTracking, type EmailOptions, type FooterSettings, type Ganalytics, type MailSettings, type OpenTracking, type Personalization, SendGridClient, type SendGridConfig, type SendGridError, type SendGridResponse, type SubscriptionTracking, type TrackingSettings, sendGridClient as default, sendEmail, sendGridClient, sendMultipleEmails, useSendEmail, useSendMultipleEmails };