SimpleNS LogoDocs

API Reference

Complete API reference for @simplens/sdk interfaces, schemas, and utilities.


Overview

This page provides a quick reference for all exports from @simplens/sdk.

import {
  // Interfaces (types only)
  type SimpleNSProvider,
  type ProviderManifest,
  type ProviderConfig,
  type RateLimitConfig,
  type DeliveryResult,
  type BaseNotification,
  type FileAttachmentConfig,
  type FileAttachmentItem,

  // Constants
  STANDARD_ATTACHMENT_OPTIONAL_KEYS,
  
  // Schemas & Helpers
  baseNotificationSchema,
  baseRecipientSchema,
  emailRecipientSchema,
  smsRecipientSchema,
  whatsappRecipientSchema,
  pushRecipientSchema,
  emailContentSchema,
  smsContentSchema,
  whatsappContentSchema,
  createNotificationSchema,
  fileAttachmentItemSchema,
  fileAttachmentArraySchema,
  createFileAttachmentArraySchema,
  parseByteSize,
  resolveFileAttachmentConfig,
  
  // Utilities
  replaceVariables,
  isHtmlContent,
  truncate,
  sleep,
  retryWithBackoff,
  
  // Re-exported
  z,  // Zod
} from '@simplens/sdk';

Interfaces

SimpleNSProvider<T>

The main contract all providers must implement.

MemberTypeDescription
manifestProviderManifestProvider metadata (readonly)
getNotificationSchema()() => z.ZodSchema<T>Returns notification validation schema
getRecipientSchema()() => z.ZodObjectReturns recipient fields schema
getContentSchema()() => z.ZodObjectReturns content fields schema
getRateLimitConfig()() => RateLimitConfigReturns rate limit settings
getFileAttachmentConfig()() => FileAttachmentConfig(Optional) Declares attachment support & limits
initialize(config)(ProviderConfig) => Promise<void>Setup credentials
healthCheck()() => Promise<boolean>Verify connectivity
send(notification)(T) => Promise<DeliveryResult>Deliver notification
shutdown()() => Promise<void>Cleanup resources

View full documentation →


ProviderManifest

Provider metadata for registration and display.

FieldTypeRequiredDescription
namestring✅Package name (e.g., @simplens/twilio)
versionstring✅Semantic version
channelstring✅Channel type: email, sms, push, whatsapp, chat
displayNamestring✅Human-readable name
descriptionstring✅Provider description
authorstring✅Maintainer name
homepagestring❌Documentation URL
requiredCredentialsstring[]✅Required credential keys
optionalConfigstring[]❌Optional config keys
fileAttachmentFileAttachmentConfig❌Attachment support & limits

View full documentation →


ProviderConfig

Configuration passed to initialize().

FieldTypeDescription
idstringUnique provider instance ID
credentialsRecord<string, string>API keys, tokens
optionsRecord<string, unknown>?Optional settings

RateLimitConfig

Token bucket rate limiting configuration.

FieldTypeDescription
maxTokensnumberMaximum burst capacity
refillRatenumberTokens added per interval
refillInterval'second' | 'minute' | 'hour' | 'day'?Refill period (default: 'second')

FileAttachmentConfig & FileAttachmentItem

  • FileAttachmentConfig: { enabled?: boolean, maxFileSize?: number, maxCount?: number, allowedMimeTypes?: string[] }
  • FileAttachmentItem: { id: string, filename: string, mime_type: string, size: number, hash: string, url?: string, metadata?: Record<string, unknown>, buffer?: Buffer, stream?: NodeJS.ReadableStream | Buffer }
  • STANDARD_ATTACHMENT_OPTIONAL_KEYS: ['ATTACHMENT_MAX_FILE_SIZE', 'ATTACHMENT_MAX_COUNT', 'ATTACHMENT_ALLOWED_MIME_TYPES', 'ATTACHMENT_ENABLED'] as const

DeliveryResult

Return type from send().

FieldTypeDescription
successbooleanWhether delivery succeeded
messageIdstring?Provider's message ID
providerResponseunknown?Raw API response
errorobject?Error details (if failed)
error.codestringError code
error.messagestringHuman-readable message
error.retryablebooleanWhether to retry

BaseNotification

Common fields in all notifications.

FieldTypeDescription
notification_idstringMongoDB ObjectId
request_idstringUUID v4 identifier
client_idstringUUID v4 client ID
channelstringChannel name
providerstring?Provider override
recipientRecord<string, unknown>Recipient data
contentRecord<string, unknown>Message content
variablesRecord<string, string>?Template variables
webhook_urlstringCallback URL
retry_countnumberCurrent retry count
created_atDateCreation timestamp

Schemas

Base Schemas

SchemaDescription
baseNotificationSchemaCommon notification fields
baseRecipientSchemaBase recipient with user_id

Recipient Schemas

SchemaExtendsAdditional Fields
emailRecipientSchemabaseRecipientSchemaemail
smsRecipientSchemabaseRecipientSchemaphone
whatsappRecipientSchemabaseRecipientSchemaphone
pushRecipientSchemabaseRecipientSchemadevice_token

Content Schemas

SchemaFieldsNotes
emailContentSchemasubject?, html?, text?Requires html or text
smsContentSchemamessageMax 1600 characters
whatsappContentSchemamessage-

Schema Factory

createNotificationSchema(channelName, recipientSchema, contentSchema)

Creates a complete notification schema combining base fields with channel-specific schemas.

View full documentation →


Utilities

FunctionSignatureDescription
replaceVariables(template: string, variables: Record<string, string>) => stringReplace {{var}} placeholders
isHtmlContent(content: string) => booleanCheck for HTML tags
truncate(str: string, maxLength: number, suffix?: string) => stringTruncate with suffix
sleep(ms: number) => Promise<void>Async delay
retryWithBackoff<T>(fn: () => Promise<T>, maxRetries?: number, baseDelayMs?: number) => Promise<T>Exponential backoff retry

View full documentation →


Quick Import Examples

import {
  SimpleNSProvider,
  ProviderManifest,
  ProviderConfig,
  DeliveryResult,
  RateLimitConfig,
  createNotificationSchema,
  emailRecipientSchema,
  emailContentSchema,
  replaceVariables,
  z,
} from '@simplens/sdk';

const schema = createNotificationSchema('email', emailRecipientSchema, emailContentSchema);
type EmailNotification = z.infer<typeof schema>;

class EmailProvider implements SimpleNSProvider<EmailNotification> {
  // ...implementation
}
import {
  SimpleNSProvider,
  ProviderManifest,
  DeliveryResult,
  createNotificationSchema,
  smsRecipientSchema,
  smsContentSchema,
  truncate,
  z,
} from '@simplens/sdk';

const schema = createNotificationSchema('sms', smsRecipientSchema, smsContentSchema);
type SMSNotification = z.infer<typeof schema>;

class SMSProvider implements SimpleNSProvider<SMSNotification> {
  // ...implementation
}
import {
  SimpleNSProvider,
  ProviderManifest,
  DeliveryResult,
  createNotificationSchema,
  baseRecipientSchema,
  z,
} from '@simplens/sdk';

// Custom recipient
const myRecipientSchema = baseRecipientSchema.extend({
  channel_id: z.string(),
});

// Custom content
const myContentSchema = z.object({
  text: z.string(),
  blocks: z.array(z.unknown()).optional(),
});

const schema = createNotificationSchema('chat', myRecipientSchema, myContentSchema);
type MyNotification = z.infer<typeof schema>;

class MyProvider implements SimpleNSProvider<MyNotification> {
  // ...implementation
}

On this page