core-utils
    Preparing search index...

    Client for sending notifications through third-party providers.

    Index

    Constructors

    • Creates a new NotificationClient instance.

      Parameters

      • params: { logger: Logger; tracer: Tracer } & (
            | { apiKey: string; provider?: undefined }
            | { apiKey?: undefined; provider: IdempotentKnock }
        )
        • logger: Logger

          Logger instance for structured logging.

        • tracer: Tracer

          Tracer instance for distributed tracing.

        • { apiKey: string; provider?: undefined }
          • apiKey: string

            API key for the third-party provider.

          • Optionalprovider?: undefined
        • { apiKey?: undefined; provider: IdempotentKnock }
          • OptionalapiKey?: undefined

            API key for the third-party provider.

          • provider: IdempotentKnock

      Returns NotificationClient

    Methods

    • Triggers a notification through third-party providers.

      This method handles:

      • Stale notifications prevention through expiration checks.
      • Logging with sensitive data redaction.
      • Distributed tracing with notification metadata.
      • Idempotency to prevent duplicate notifications.
      • Comprehensive error handling and logging.

      Parameters

      Returns Promise<ServiceResult<TriggerResponse>>

      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 });