nestjs-inertia
is a simple NestJS adapter/middleware with express-platform
for Inertia.js to create server-driven single page applications.
You can run command below to install nestjs-inertia
to your NestJS project.
npm i nestjs-inertia
Based on official NestJS documentation of their middleware, you can apply nestjs-inertia
middleware to global or module level like below:
- 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);
- 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('*'); } }
You can use this adapter from express' Response
api.
- 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');
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'
}
]
});
}
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.