notification-typescript-sdk
v1.0.1
Published
TypeScript SDK for notification service
Downloads
23
Maintainers
Readme
Notification TypeScript SDK
TypeScript SDK for sending notifications (email, SMS, WhatsApp) via the notification service API.
Installation
npm install notification-typescript-sdkFeatures
- Send emails and SMS messages
- Template management
- Connection pooling for optimal performance
- Automatic snake_case ↔ camelCase conversion
- Type-safe with full TypeScript support
- Winston logging integration
- Async and sync methods
Quick Start
import {
NotificationClient,
Notification,
PlatformEnum,
PathParameters
} from 'notification-typescript-sdk';
const client = new NotificationClient({
targetHost: 'http://api.example.com',
poolConfig: {
maxTotal: 10,
maxIdle: 8,
minIdle: 2
}
});
const notification = Notification.builder()
.withCategory('email')
.withMessage('Hello, this is a test email')
.withPlatformId(PlatformEnum.ONESTEP)
.withTo('[email protected]')
.withFrom('[email protected]')
.withSubject('Test Email')
.withMessageType(0)
.withNotifyType(1)
.buildNotificationRequest();
const pathParams = new PathParameters(
PlatformEnum.ONESTEP,
'testuser',
'0'
);
const result = await client.sendEmail(notification, pathParams);
if (result.data) {
console.log('Email sent successfully:', result.data);
} else if (result.error) {
console.error('Failed to send email:', result.error);
}Configuration
NotificationClientConfig
interface NotificationClientConfig {
targetHost: string; // API endpoint URL
poolConfig?: Partial<ClientPoolConfig>; // Optional pool configuration
}ClientPoolConfig
interface ClientPoolConfig {
maxTotal: number; // Maximum total connections
maxIdle: number; // Maximum idle connections
minIdle: number; // Minimum idle connections
blockWhenExhausted: boolean; // Block when pool is exhausted
maxWaitMillis?: number; // Maximum wait time in milliseconds
}API Reference
NotificationClient
sendEmail()
Send an email notification.
async sendEmail(
notification: NotificationRequest,
pathParameters: PathParameters
): Promise<HttpResponse<Notification>>sendSyncEmail()
Send an email notification synchronously.
async sendSyncEmail(
notification: NotificationRequest,
pathParameters: PathParameters
): Promise<HttpResponse<Notification>>sendSms()
Send an SMS notification.
async sendSms(
notification: NotificationRequest,
pathParameters: PathParameters
): Promise<HttpResponse<Notification>>getUnsentMessage()
Get unsent messages.
async getUnsentMessage(
pathParameters: PathParameters
): Promise<HttpResponse<Notification[]>>retryOldMessage()
Retry failed messages.
async retryOldMessage(
ids: number[],
pathParameters: PathParameters
): Promise<HttpResponse<any>>addTemplate()
Add or update a template.
async addTemplate(
platformType: PlatformEnum,
templateName: string,
type: TemplateType,
content: string
): Promise<HttpResponse<string>>Enums
PlatformEnum
enum PlatformEnum {
ONESTEP = 0,
BNPL = 1,
QHT = 2
}TemplateType
enum TemplateType {
SMS = 'sms',
EMAIL = 'email',
WHATAPPS = 'whatapps'
}BizType
enum BizType {
PAY_INTEREST = 'payInterst',
REGISTER = 'regiester'
}Usage Examples
Sending an Email
const notification = Notification.builder()
.withCategory('email')
.withMessage('Hello World')
.withPlatformId(PlatformEnum.ONESTEP)
.withTo('[email protected]')
.withFrom('[email protected]')
.withSubject('Welcome')
.withMessageType(0)
.withNotifyType(1)
.buildNotificationRequest();
const pathParams = new PathParameters(PlatformEnum.ONESTEP, 'username');
const result = await client.sendEmail(notification, pathParams);Sending an SMS
const notification = Notification.builder()
.withCategory('sms')
.withMessage('Your verification code is 123456')
.withPlatformId(PlatformEnum.ONESTEP)
.withTo('+1234567890')
.withMessageType(0)
.withNotifyType(1)
.buildNotificationRequest();
const pathParams = new PathParameters(PlatformEnum.ONESTEP, 'username');
const result = await client.sendSms(notification, pathParams);Adding a Template
const result = await client.addTemplate(
PlatformEnum.ONESTEP,
'welcome_email',
TemplateType.EMAIL,
'Hello ${username}, welcome to our service!'
);Using Callbacks
await client.sendEmailWithCallback(
notification,
pathParams,
(response) => {
if (response.data) {
console.log('Email sent:', response.data);
} else if (response.error) {
console.error('Error:', response.error);
}
}
);Getting Unsent Messages
const pathParams = new PathParameters(PlatformEnum.ONESTEP, 'username', '0');
const result = await client.getUnsentMessage(pathParams);
if (result.data) {
console.log('Unsent messages:', result.data);
}Retrying Failed Messages
const pathParams = new PathParameters(PlatformEnum.ONESTEP, 'username');
const result = await client.retryOldMessage([1, 2, 3], pathParams);Building
npm run buildDevelopment
npm run devLicense
MIT
