Skip to main content

SMSTemplateEngine Interface

Package: @nauth-toolkit/core Type: Interface

Contract for template engines that can render SMS templates with variables.

import { SMSTemplateEngine } from '@nauth-toolkit/core';

Methods

MethodReturnsDescription
render(type, variables)Promise<SMSTemplate>Render a template with the provided variables
registerTemplate(type, template)voidRegister a custom template from inline string
registerTemplateFromSources(type, templateSource)Promise<void>Register a custom template from mixed sources (strings or files)

render()

Render a template with the provided variables.

Parameters:

ParameterTypeRequiredDescription
typeSMSTemplateType | stringYesType of template to render
variablesSMSTemplateVariablesYesVariables to inject into template

Returns: Promise<SMSTemplate>

Throws: NAuthException if template type is not found

Example:

const result = await engine.render(
SMSTemplateType.VERIFICATION,
{
appName: 'My App',
code: '123456',
expiryMinutes: 5,
}
);
// result.content: "My App: Your verification code is: 123456. Valid for 5 minutes."

registerTemplate()

Register a custom template from inline string.

Parameters:

ParameterTypeRequiredDescription
typeSMSTemplateType | stringYesTemplate type identifier
templateSMSTemplateYesTemplate definition with content

Example:

engine.registerTemplate(SMSTemplateType.VERIFICATION, {
content: '{{appName}}: Your code is {{code}}. Expires in {{expiryMinutes}} min.',
});

registerTemplateFromSources()

Register a custom template from mixed sources (strings or files).

Parameters:

ParameterTypeRequiredDescription
typeSMSTemplateType | stringYesTemplate type identifier
templateSource{ content: SMSTemplateSource }YesTemplate source (content or file path)

Throws: NAuthException if file not found or invalid source

Example:

// Inline content
await engine.registerTemplateFromSources(SMSTemplateType.VERIFICATION, {
content: { content: '{{appName}}: Your code is {{code}}.' },
});

// File-based
await engine.registerTemplateFromSources(SMSTemplateType.MFA, {
content: { filePath: './sms-templates/mfa.txt.hbs' },
});

Implementation

The default implementation is SMSTemplateEngineImpl:

import { SMSTemplateEngineImpl } from '@nauth-toolkit/core';

const engine = new SMSTemplateEngineImpl(process.cwd(), logger);

Error Handling

All methods throw NAuthException with AuthErrorCode.INTERNAL_ERROR when:

  • Template type is not found (render)
  • Template file does not exist (registerTemplateFromSources)
  • Invalid template source (registerTemplateFromSources)