UNPKG

@industriousoffice/mailchimp_marketing

Version:

This is a modified version of the official Node client library for the Mailchimp Marketing API meant to be usesd for both production and testing purposes.

139 lines (121 loc) 4.16 kB
/** * Example TypeScript usage of @industriousoffice/mailchimp_marketing * This file demonstrates how to use the package with full type safety */ import mailchimp from '@industriousoffice/mailchimp_marketing'; // or: const mailchimp = require('@industriousoffice/mailchimp_marketing'); // Configure the client mailchimp.setConfig({ apiKey: 'your-api-key-us1', server: 'us1' }); // Example: Ping the API async function pingAPI(): Promise<void> { try { const response = await mailchimp.ping.get(); console.log('API Health Status:', response.data.health_status); } catch (error) { console.error('Ping failed:', error); } } // Example: Get all lists async function getAllLists(): Promise<void> { try { const response = await mailchimp.lists.getAllLists({ count: 10, sort_field: 'date_created', sort_dir: 'DESC' }); console.log(`Found ${response.data.total_items} lists`); response.data.lists?.forEach((list: any) => { console.log(`- ${list.name} (${list.stats.member_count} members)`); }); } catch (error) { console.error('Failed to get lists:', error); } } // Example: Add a member to a list async function addMemberToList(listId: string, emailAddress: string): Promise<void> { try { const member = await mailchimp.lists.addListMember(listId, { email_address: emailAddress, status: 'subscribed', merge_fields: { FNAME: 'John', LNAME: 'Doe' } }); console.log(`Added member: ${member.data.email_address}`); } catch (error) { console.error('Failed to add member:', error); } } // Example: Create a campaign async function createCampaign(listId: string): Promise<void> { try { const campaign = await mailchimp.campaigns.create({ type: 'regular', recipients: { list_id: listId }, settings: { subject_line: 'Hello from TypeScript!', preview_text: 'This is a test campaign created with TypeScript', title: 'TypeScript Test Campaign', from_name: 'Your Company', reply_to: 'noreply@yourcompany.com', to_name: '*|FNAME|*' } }); console.log(`Campaign created with ID: ${campaign.data.id}`); return campaign.data.id; } catch (error) { console.error('Failed to create campaign:', error); } } // Example: Get campaign reports async function getCampaignReports(): Promise<void> { try { const reports = await mailchimp.reports.getAllCampaignReports({ count: 10, type: 'regular' }); console.log(`Found ${reports.data.total_items} campaign reports`); reports.data.reports?.forEach((report: any) => { console.log(`- ${report.campaign_title}: ${report.emails_sent} sent, ${report.opens.unique_opens} unique opens`); }); } catch (error) { console.error('Failed to get reports:', error); } } // Example usage with environment variable for testing async function exampleWithMockServer(): Promise<void> { // For testing, set the environment variable process.env.MAILCHIMP_BASE_URL = 'http://localhost:3001/3.0'; mailchimp.setConfig({ apiKey: 'test-key', server: 'us1' // This will be ignored when MAILCHIMP_BASE_URL is set }); try { const response = await mailchimp.ping.get(); console.log('Mock server response:', response.data); } catch (error) { console.error('Mock server not available:', error); } } // Export functions for use in other modules export { pingAPI, getAllLists, addMemberToList, createCampaign, getCampaignReports, exampleWithMockServer }; // Run examples if this file is executed directly if (require.main === module) { console.log('Running TypeScript examples...'); // Uncomment to test with your actual API key // pingAPI(); // getAllLists(); }