1.0.1 • Published 2 years ago

nestjs-inertia v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

nestjs-inertia

nestjs-inertia is a simple NestJS adapter/middleware with express-platform for Inertia.js to create server-driven single page applications.

Installation

You can run command below to install nestjs-inertia to your NestJS project.

npm i nestjs-inertia

Applying adapter

Based on official NestJS documentation of their middleware, you can apply nestjs-inertia middleware to global or module level like below:

  1. Global Level

    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    import { inertiaMiddleware } from 'nestjs-inertia'
    
    const app = await NestFactory.create(AppModule);
    app.use(inertiaMiddleware);
    await app.listen(3000);
  2. Module Level

    import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { InertiaMiddleware } from 'nestjs-inertia'
    
    @Module({
      imports: [],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule implements NestModule {
      configure(consumer: MiddlewareConsumer) {
        consumer
          .apply(InertiaMiddleware)
          .forRoutes('*');
      }
    }

Usage

You can use this adapter from express' Response api.

Basic Usage

  • Rendering a component
    res.inertia.render('Users/AllUser', {
      users: [
        {
          id: 1,
          name: 'John Doe'
        }
      ]
    });
  • Share data
    res.inertia.share({
      isLoggedIn: true
    });
  • Redirection
    res.inertia.redirect('/user');

Example usage at controller

import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('users')
export class UserController {
  @Get()
  findAll(@Res() res: Response): Promise<void> {
    return res.inertia.render('Users/AllUser', {
      users: [
        {
          id: 1,
          name: 'John Doe'
        }
      ]
    });
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT