Creates a new NotificationClient instance.
Logger instance for structured logging.
Tracer instance for distributed tracing.
API key for the third-party provider.
Optional
provider?: undefinedOptional
apiKey?: undefinedAPI key for the third-party provider.
Append to a user's push tokens.
Promise resolving to either an error or successful response.
Triggers a notification through third-party providers.
This method handles:
Promise resolving to either an error or successful response.
import { NotificationClient, type Span } from "@clipboard-health/notifications";
import { isSuccess } from "@clipboard-health/util-ts";
const client = new NotificationClient({
apiKey: "test-api-key",
logger: {
info: console.log,
warn: console.warn,
error: console.error,
} as const,
tracer: {
trace: <T>(_name: string, _options: unknown, fun: (span?: Span | undefined) => T): T => fun(),
},
});
async function triggerNotification(job: { attemptsCount: number }) {
const result = await client.trigger({
attempt: (job?.attemptsCount ?? 0) + 1,
body: {
recipients: ["user-1"],
data: { favoriteColor: "blue", secret: "2" },
},
expiresAt: new Date(Date.now() + 300_000), // 5 minutes
idempotencyKey: "welcome-user-4",
key: "welcome-email",
keysToRedact: ["secret"],
});
if (isSuccess(result)) {
console.log("Notification sent:", result.value.id);
}
}
// eslint-disable-next-line unicorn/prefer-top-level-await
void triggerNotification({ attemptsCount: 0 });
Updates or creates a workplace (tenant) in Knock.
Promise resolving to either an error or successful response.
Client for sending notifications through third-party providers.