@formatr/nestjs
v0.1.0
Published
NestJS integration for formatr template engine
Maintainers
Readme
@formatr/nestjs
NestJS module for formatr template engine with dependency injection support.
Installation
npm install @formatr/nestjs @timur_manjosov/formatr @nestjs/common @nestjs/coreUsage
Module Registration
import { Module } from '@nestjs/common';
import { FormatrModule } from '@formatr/nestjs';
@Module({
imports: [
FormatrModule.register({
templatesDir: './templates',
cache: true,
filters: {
currency: (value) => `$${Number(value).toFixed(2)}`,
},
}),
],
})
export class AppModule {}Using the Service
import { Injectable } from '@nestjs/common';
import { FormatrService } from '@formatr/nestjs';
@Injectable()
export class EmailService {
constructor(private readonly formatr: FormatrService) {}
async sendWelcomeEmail(userId: number, userName: string) {
const html = await this.formatr.render('welcome-email', {
userId,
userName,
});
// Send email...
}
}Using the Decorator
import { Controller, Get } from '@nestjs/common';
import { FormatrResponse } from '@formatr/nestjs';
@Controller('reports')
export class ReportsController {
@Get('monthly')
@FormatrResponse('monthly-report')
getMonthlyReport() {
return {
month: 'December',
revenue: 150000,
expenses: 75000,
};
}
}Async Filters
FormatrModule.register({
templatesDir: './templates',
asyncFilters: {
fetchUser: async (userId) => {
return await userService.findById(userId);
},
},
}),Then use renderAsync:
const html = await this.formatr.renderAsync('user-profile', { userId: 123 });API
FormatrModule.register(options)
Options:
templatesDir(string, required): Directory containing template filesextension(string, optional): File extension for templatescache(boolean | object, optional): Enable cachingfilters(object, optional): Synchronous custom filtersasyncFilters(object, optional): Asynchronous custom filters
FormatrService
Methods:
render(templateName, context): Render template synchronouslyrenderAsync(templateName, context): Render with async filter support
@FormatrResponse(templateName)
Decorator to automatically render controller responses using a template.
FormatrInterceptor
Interceptor for automatic template rendering (use with @FormatrResponse).
License
MIT
