@jquintanadev/mailerlite-sdk
v0.0.4
Published
The `@jquintanadev/mailerlite-sdk` library provides a simple and efficient way to interact with the MailerLite API. Designed for scalability and ease of use, it encapsulates all API interactions, making it ideal for projects that require newsletter and ca
Readme
MailerLite SDK
The @jquintanadev/mailerlite-sdk library provides a simple and efficient way to interact with the MailerLite API. Designed for scalability and ease of use, it encapsulates all API interactions, making it ideal for projects that require newsletter and campaign management.
Building
Run nx build mailerlite-sdk to build the library.
Running unit tests
Run nx test mailerlite-sdk to execute the unit tests via Jest.
Benefits
Using the mailerlite-sdk library provides the following benefits:
- Centralized Logic: Encapsulates all interactions with the MailerLite API, reducing code duplication across projects.
- Reusability: Can be used in multiple applications within the monorepo or external projects.
- Error Handling: Provides consistent error handling for API interactions.
- Ease of Use: Simplifies the integration with MailerLite by abstracting API details.
- Scalability: Designed to support future API changes and additional features.
Setting Up Credentials
To use the @jquintanadev/mailerlite-sdk, you need to configure your MailerLite API key. Follow these steps:
Obtain Your API Key:
- Log in to your MailerLite account.
- Navigate to the Integrations section.
- Generate or copy your API key.
Store the API Key Securely:
- Create a
.envfile in the root of your project (if it doesn't already exist). - Add the following line to the
.envfile:MAILERLITE_API_KEY=your-api-key-here
- Create a
Load the API Key in Your Application:
- Use a library like
dotenvto load the environment variables:import dotenv from 'dotenv'; dotenv.config(); import { setApiKey } from '@jquintanadev/mailerlite-sdk'; setApiKey(process.env.MAILERLITE_API_KEY || '');
- Use a library like
Verify the Configuration:
- Ensure the API key is loaded correctly before making any API calls.
Encapsulated Methods
The SDK will include the following methods:
Subscriber Management
addSubscriber(listId: string, subscriber: Subscriber): Promise<Response>: Adds a new subscriber to a specific list.updateSubscriber(subscriberId: string, data: Partial<Subscriber>): Promise<Response>: Updates an existing subscriber.deleteSubscriber(subscriberId: string): Promise<Response>: Removes a subscriber.
List Management
getLists(): Promise<List[]>: Retrieves all available lists.getListDetails(listId: string): Promise<List>: Fetches details of a specific list.
Campaign Management
createCampaign(data: Campaign): Promise<Response>: Creates a new campaign.sendCampaign(campaignId: string): Promise<Response>: Sends a campaign to the specified list.
Utility Methods
setApiKey(apiKey: string): void: Configures the API key for authentication.handleApiError(error: any): void: Handles and logs API errors consistently.
Publishing
To publish the mailerlite-sdk library to npm, follow these steps:
Build the library:
npx nx build mailerlite-sdkIncrement the version (e.g., patch):
npm version patch --workspacesPublish the library:
npx nx run mailerlite-sdk:nx-release-publish
Ensure you are authenticated with npm and have the correct permissions to publish under the @jquintanadev scope.
Examples
Adding a Subscriber
import { addSubscriber } from '@jquintanadev/mailerlite-sdk';
const subscriber = {
email: '[email protected]',
name: 'John Doe',
};
addSubscriber('list-id', subscriber)
.then(response => console.log('Subscriber added:', response))
.catch(error => console.error('Error adding subscriber:', error));Fetching Lists
import { getLists } from '@jquintanadev/mailerlite-sdk';
getLists()
.then(lists => console.log('Available lists:', lists))
.catch(error => console.error('Error fetching lists:', error));Creating a Campaign
import { createCampaign } from '@jquintanadev/mailerlite-sdk';
const campaign = {
title: 'New Campaign',
content: '<h1>Hello World</h1>',
};
createCampaign(campaign)
.then(response => console.log('Campaign created:', response))
.catch(error => console.error('Error creating campaign:', error));Future Enhancements
- Support for additional MailerLite API endpoints.
- Improved caching for frequently accessed data.
- Integration with analytics tools for campaign tracking.
