Skip to content

feat(nest): add Fastify support to NestJS application generator #30894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/generated/packages/nest/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
"type": "string",
"x-priority": "important"
},
"framework": {
"description": "The framework to use for the application.",
"type": "string",
"enum": ["express", "fastify"],
"default": "express",
"x-priority": "important"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
Expand Down
26 changes: 26 additions & 0 deletions packages/nest/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ describe('application generator', () => {
expect(tree.exists(`${appDirectory}/src/app/app.service.ts`)).toBeTruthy();
});

it('should generate files for Express framework', async () => {
await applicationGenerator(tree, {
directory: appDirectory,
addPlugin: true,
framework: 'express',
});

expect(tree.exists(`${appDirectory}/src/main.ts`)).toBeTruthy();
expect(tree.read(`${appDirectory}/src/main.ts`, 'utf-8')).toContain(
'NestFactory.create(AppModule)'
);
});

it('should generate files for Fastify framework', async () => {
await applicationGenerator(tree, {
directory: appDirectory,
addPlugin: true,
framework: 'fastify',
});

expect(tree.exists(`${appDirectory}/src/main.ts`)).toBeTruthy();
expect(tree.read(`${appDirectory}/src/main.ts`, 'utf-8')).toContain(
'NestFactory.create<NestFastifyApplication>'
);
});

it('should configure tsconfig correctly', async () => {
await applicationGenerator(tree, {
directory: appDirectory,
Expand Down
9 changes: 6 additions & 3 deletions packages/nest/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { applicationGenerator as nodeApplicationGenerator } from '@nx/node';

import { initGenerator } from '../init/init';
import {
createFiles,
createFilesForFramework,
normalizeOptions,
toNodeApplicationGeneratorOptions,
updateTsConfig,
Expand Down Expand Up @@ -40,11 +40,14 @@ export async function applicationGeneratorInternal(
toNodeApplicationGeneratorOptions(options)
);
tasks.push(nodeApplicationTask);
createFiles(tree, options);

// Dynamically create files based on the selected framework
createFilesForFramework(tree, options);

updateTsConfig(tree, options);

if (!options.skipPackageJson) {
tasks.push(ensureDependencies(tree));
tasks.push(ensureDependencies(tree, options));
}

if (!options.skipFormat) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
*/

import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';

async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3000;
await app.listen(port);
Logger.log(`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`);
}
bootstrap();
23 changes: 21 additions & 2 deletions packages/nest/src/generators/application/lib/create-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ import type { Tree } from '@nx/devkit';
import { generateFiles, joinPathFragments } from '@nx/devkit';
import type { NormalizedOptions } from '../schema';

export function createFiles(tree: Tree, options: NormalizedOptions): void {
export function createFilesForFramework(
tree: Tree,
options: NormalizedOptions
): void {
const templatePath =
options.framework === 'fastify'
? joinPathFragments(__dirname, '..', 'files', 'fastify')
: joinPathFragments(__dirname, '..', 'files', 'express');

generateFiles(
tree,
templatePath,
joinPathFragments(options.appProjectRoot, 'src'),
{
tmpl: '',
name: options.appProjectName,
root: options.appProjectRoot,
}
);

generateFiles(
tree,
joinPathFragments(__dirname, '..', 'files'),
joinPathFragments(__dirname, '..', 'files', 'common'),
joinPathFragments(options.appProjectRoot, 'src'),
{
tmpl: '',
Expand Down
1 change: 1 addition & 0 deletions packages/nest/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Linter, LinterType } from '@nx/eslint';
export interface ApplicationGeneratorOptions {
directory: string;
name?: string;
framework?: 'express' | 'fastify';
frontendProject?: string;
linter?: Linter | LinterType;
skipFormat?: boolean;
Expand Down
7 changes: 7 additions & 0 deletions packages/nest/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
"type": "string",
"x-priority": "important"
},
"framework": {
"description": "The framework to use for the application.",
"type": "string",
"enum": ["express", "fastify"],
"default": "express",
"x-priority": "important"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
Expand Down
8 changes: 6 additions & 2 deletions packages/nest/src/utils/ensure-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import {
rxjsVersion,
tsLibVersion,
} from './versions';
import { NormalizedOptions } from '../generators/application/schema';

export function ensureDependencies(tree: Tree): GeneratorCallback {
export function ensureDependencies(
tree: Tree,
options: NormalizedOptions
): GeneratorCallback {
return addDependenciesToPackageJson(
tree,
{
'@nestjs/common': nestJsVersion,
'@nestjs/core': nestJsVersion,
'@nestjs/platform-express': nestJsVersion,
[`@nestjs/platform-${options.framework}`]: nestJsVersion,
'reflect-metadata': reflectMetadataVersion,
rxjs: rxjsVersion,
tslib: tsLibVersion,
Expand Down