Skip to content

Commit

Permalink
feat(node): create fastify setup closer to what fastify-cli creates
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Feb 17, 2023
1 parent 40007a1 commit 36dc171
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
2 changes: 2 additions & 0 deletions e2e/node/src/node-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ describe('Node Applications + webpack', () => {
// Only Fastify generates with unit tests since it supports them without additional libraries.
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow();

addLibImport(fastifyApp, utilLib);

await runE2eTests(expressApp);
await runE2eTests(fastifyApp);
await runE2eTests(koaApp);
Expand Down
18 changes: 17 additions & 1 deletion packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
esbuildVersion,
expressTypingsVersion,
expressVersion,
fastifyAutoloadVersion,
fastifySensibleVersion,
fastifyVersion,
koaTypingsVersion,
koaVersion,
Expand Down Expand Up @@ -99,6 +101,7 @@ function getEsBuildConfig(
),
// Use CJS for Node apps for widest compatibility.
format: ['cjs'],
bundle: false,
main: joinPathFragments(
project.sourceRoot,
'main' + (options.js ? '.js' : '.ts')
Expand Down Expand Up @@ -293,13 +296,26 @@ function addProjectDependencies(
},
fastify: {
fastify: fastifyVersion,
'@fastify/autoload': fastifyAutoloadVersion,
'@fastify/sensible': fastifySensibleVersion,
},
};
const frameworkDevDependencies = {
express: {
'@types/express': expressTypingsVersion,
},
koa: {
'@types/koa': koaTypingsVersion,
},
fastify: {},
};
return addDependenciesToPackageJson(
tree,
{},
{
...frameworkDependencies[options.framework],
},
{
...frameworkDevDependencies[options.framework],
...bundlers[options.bundler],
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import * as path from 'path';
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import AutoLoad from '@fastify/autoload';

/* eslint-disable-next-line */
export interface AppOptions {
}
export interface AppOptions { }

export async function app(fastify: FastifyInstance, opts: AppOptions) {
fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) {
return { message: 'Hello API' };
// Place here your custom code!

// Do not touch the following lines

// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: { ...opts },
});

// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: { ...opts },
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import fp from 'fastify-plugin';
import sensible from '@fastify/sensible';

/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
export default fp(async function(fastify: FastifyInstance, opts: {}) {
fastify.register(sensible);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';

export default async function(fastify: FastifyInstance, opts: {}) {
fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) {
return { message: 'Hello API' };
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ RUN addgroup --system <%= project %> && \
COPY <%= buildLocation %> <%= project %>
RUN chown -R <%= project %>:<%= project %> .

# You can remove this install step if you build with `--bundle` option.
# The bundled output will include external dependencies.
RUN npm --prefix <%= project %> --omit=dev -f install

CMD [ "node", "<%= project %>" ]
14 changes: 8 additions & 6 deletions packages/node/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ export const nxVersion = require('../../package.json').version;

export const tslibVersion = '^2.3.0';

export const typesNodeVersion = '18.7.1';
export const typesNodeVersion = '~18.7.1';

export const esbuildVersion = '^0.17.5';

export const expressVersion = '^4.18.1';
export const expressTypingsVersion = '4.17.13';
export const expressVersion = '~4.18.1';
export const expressTypingsVersion = '~4.17.13';

export const koaVersion = '2.14.1';
export const koaTypingsVersion = '2.13.5';
export const koaVersion = '~2.14.1';
export const koaTypingsVersion = '~2.13.5';

export const fastifyVersion = '4.11.0';
export const fastifyVersion = '~4.13.0';
export const fastifyAutoloadVersion = '~5.7.1';
export const fastifySensibleVersion = '~5.2.0';

export const axiosVersion = '^1.0.0';

0 comments on commit 36dc171

Please sign in to comment.