Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nest-jsx-template-engine

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nest-jsx-template-engine

JSX-based + typescript template engine for NestJS applications

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

nestjs-jsx-template-engine

This is a template engine for Nestjs applications that allows you to use JSX-based templates with strongly-typed params for server-side rendered HTML.

You don't need any extra templating engine like nunjucks or handlebars, and you get type-safety out of the box.

There's no additional compiler, since TypeScript compiles JSX natively.

Installation

Install the package:

npm install nest-jsx-template-engine

Update your tsconfig.json file to include jsx and jsxFactory options:

{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "h"
  }
}

Install the middleware in your Nest app.module:

// app.module.ts
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { RenderMiddleware } from 'nest-jsx-template-engine';
import { SomeModule } from './some/some.module';

@Module({
  imports: [SomeModule],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(RenderMiddleware)
      .forRoutes('*');
  }
}

Suggest installing it globally for all routes, but see Nest's documentation for other options on installing the middleware.

Rendering

// app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Render } from 'nest-jsx-template-engine';
import { App, IAppProps } from './app.view';
import { AppViewTransferObject } from './app.vto';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @Render<IAppProps>(App) // pass the App view directly to the Render decorator
  getHello(): Partial<IAppProps> {
    return this.appService.getHello();
  }
}

Define your template with JSX:

// app.view.tsx
import { h, JSXTemplate }  from 'nest-jsx-template-engine'
import { AppViewTransferObject } from './app.vto'

export interface IAppProps extends JSXTemplate.RenderProps {
  name: string
}

export function App(props: IAppProps): string {
  return <html>
    <body>
      <h1 class="foo">{props.name}</h1>
      <div>Request Path: {props.$req.path} from ip: {props.$req.ip}
    </body>
  </html>
}

See a working demo in the Github repo.

A Note on React-Flavored JSX

This package does not support React flavored JSX (e.g., className, htmlFor, etc). It expects basic HTML properties.

Keywords

FAQs

Package last updated on 26 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc