mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-15 01:21:09 +02:00
Only include emailOptions object when from or replyTo is provided, preventing validation errors when these optional fields are not set. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| src | ||
| .eslintignore | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
| tsup.config.ts | ||
@manacore/notify-client
Client SDK for the mana-notify notification service.
Installation
pnpm add @manacore/notify-client
Usage
Basic Usage
import { NotifyClient } from '@manacore/notify-client';
const notify = new NotifyClient({
serviceUrl: 'http://localhost:3040',
serviceKey: process.env.MANA_NOTIFY_SERVICE_KEY,
appId: 'your-app-id',
});
// Send email
await notify.sendEmail({
to: 'user@example.com',
template: 'auth-password-reset',
data: { resetUrl: 'https://...', userName: 'Max' },
});
// Send push notification
await notify.sendPush({
userId: 'user-uuid',
title: 'New Message',
body: 'You have a new message',
data: { messageId: 'xxx' },
});
// Send to specific token
await notify.sendPush({
token: 'ExponentPushToken[xxx]',
title: 'Hello',
body: 'World',
});
// Schedule notification
await notify.scheduleEmail({
to: 'user@example.com',
template: 'calendar-reminder',
data: { eventTitle: 'Meeting' },
scheduledFor: new Date('2024-12-20T13:45:00Z'),
});
NestJS Integration
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NotifyModule } from '@manacore/notify-client/nestjs';
@Module({
imports: [
ConfigModule.forRoot(),
NotifyModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
serviceUrl: config.get('MANA_NOTIFY_URL', 'http://localhost:3040'),
serviceKey: config.get('MANA_NOTIFY_SERVICE_KEY'),
appId: config.get('APP_ID'),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
Then inject the client:
import { Injectable, Inject } from '@nestjs/common';
import { NOTIFY_CLIENT, NotifyClient } from '@manacore/notify-client/nestjs';
@Injectable()
export class NotificationService {
constructor(@Inject(NOTIFY_CLIENT) private readonly notify: NotifyClient) {}
async sendWelcomeEmail(email: string, name: string) {
await this.notify.sendEmail({
to: email,
template: 'auth-welcome',
data: { userName: name },
});
}
}
API Reference
NotifyClient
Constructor
new NotifyClient({
serviceUrl: string; // mana-notify service URL
serviceKey: string; // Service authentication key
appId: string; // Your application ID
timeout?: number; // Request timeout in ms (default: 30000)
});
Methods
sendEmail(options)- Send an email immediatelyscheduleEmail(options)- Schedule an email for later
Push Notifications
sendPush(options)- Send a push notificationschedulePush(options)- Schedule a push notification
Other Channels
sendMatrix(options)- Send a Matrix messagesendWebhook(options)- Send a webhook
Batch & Management
sendBatch(notifications)- Send multiple notificationsgetNotification(id)- Get notification statuscancelNotification(id)- Cancel a pending notification
Templates
listTemplates(appId?)- List available templatesgetTemplate(slug, locale?)- Get a templatepreviewTemplate(slug, data, locale?)- Preview a rendered template
Environment Variables
| Variable | Description |
|---|---|
MANA_NOTIFY_URL |
mana-notify service URL |
MANA_NOTIFY_SERVICE_KEY |
Service authentication key |
APP_ID |
Your application ID |