npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-jsx-template-engine

v0.2.6

Published

JSX-based + typescript template engine for NestJS applications

Downloads

47

Readme

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.

:warning: WIP: this package is still in progress and that API is extremely volatile. Suggest not using in production.

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.

:warning: by default, the middleware will not render the template for requests that do not expect HTML in the header (e.g., Accept: 'text/html'). It will merely return the value from the controller. You can modify this behavior by extending the middleware class.

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(): IAppProps {
    return this.appService.getHello();
  }
}

Define your template with JSX:

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

export interface IAppProps  {
  name: string
}

/**
 * @param {any} data the value returned from the controller
 * @param {JSXTemplate.RenderProps} props framework provided props containing request.
 * 
 * See below on extending the props passed
 */
export function App(data: IAppProps, props: JSXTemplate.RenderProps): string {
  return <html>
    <body>
      <h1 class="foo">{data.name}</h1>
      <div>Request Path: {props.$req.path} from ip: {props.$req.ip}
    </body>
  </html>
}

See a working demo in the Github repo.

Extending Render behavior

The Render behavior provided by the middleware can be overriden in two key ways by sub-classing the middleware.

import { RenderMiddleware, JSXTemplate } from 'next-jsx-template-engine';

export interface CustomRenderProps extends JSXTemplate.RenderProps {
  $foo: boolean
}

export class CustomRenderMiddleware {

  // by default, the middleware will not render requests that do not expect HTML in the response.
  // returning false will disable this check. You can do this variably by introspecting the req object
  applyStrictAcceptHtmlCheck(req: Request): boolean {

    // returning false means that the template will be rendered for all requests,
    // regardless of client's accept headers.
    return false;
  }

  // apply additional request/response specific properties that will be passed as
  // the second parameter to the JSX template along with the data returned from 
  // the controller. 
  // 
  // Common use-cases might be supplying session data, CSRF tokens, etc.
  decorateRenderProps(req: Request, res: Response): Partial<CustomRenderProps> {
    return {
      $foo: false
    }
  }
}

A Note on React-Flavored JSX

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