5.2.2 • Published 1 year ago

@forrestjs/service-fastify v5.2.2

Weekly downloads
50
License
MIT
Repository
github
Last release
1 year ago

@forrestjs/service-fastify

ForrestJS service which helps setting up an FastifyJS server.

DEMO:
https://codesandbox.io/s/service-fastify-th8dq

Install & Setup

npm add @forrestjs/service-fastify

Set it up in your FastifyJS App:

// index.js
const forrestjs = require('@forrestjs/core');
const fastifyService = require('@forrestjs/service-fastify');

// Create an Home Page for the Web App
const homePage = () => ({
  target: '$FASTIFY_ROUTE',
  handler: [
    {
      method: 'GET',
      url: '/',
      handler: async () => `Hello World`,
    },
  ],
});

// Run the App:
forrestjs.run({
  settings: {
    fastify: {
      port: 8080,
    },
  },
  services: [fastifyService],
  features: [homePage],
});

Configuration & Environment

šŸ“ fastify.port

Sets up the port on which the server will listen for requests.

It falls back to environment variables:

  • process.env.REACT_APP_PORT
  • process.env.PORT
  • 8080 (default value)

šŸ“ fastify.instance.options

Let you pass arbitrary configuration to the Fastify instance.

šŸ“ fastify.tdd.scope

Let you setup the root of the testing APIs.

Default: /test


Context

The ForrestJS App will be decorated with:

šŸŒŽ fastify

A reference to the Fastify instance as configured during the booting of the App. It allows to fully manipulate the running server.

const fastify = getContext('fastify');

šŸŒŽ axios

A reference to the static instance of Axios.

const axios = getContext('axios');
await axios.get('/');

Testing Support

Service Fastify ships testing facilities with the goal of making end-2-end testing easy for you.

The idea is to let the testing enviornment interact with the Fastify instance using REST APIs that are available exclusively whith process.env.NODE_ENV set to:

  • development
  • test

šŸ”„ You must also pass the environmental variable šŸ”„

DANGEROUSLY_ENABLE_FASTIFY_TDD_ENDPOINT=yes

Those test-specific APIs are exposed under a /test/... root that you can change by setting fastify.tdd.root.

Test Healthcheck

The root Route /test acts as healthcheck for running tests. Any test client should await for a 200 reply from this endpoint before attempting to run any call.

By default there is no logic to this healthcheck, but you can interact with by implementing two extensions:

  • $FASTIFY_TDD_CHECK
  • $FASTIFY_TDD_ROOT

Test APIs

get://test/reset

Runs the $FASTIFY_TDD_RESET handlers to reset the App's state.

get://test/config?key=foobar&value=123

Reads a configuration value from the App

post://test/config { key: 'foobar', value: 555 }

Writes a configuration value into the App

post://test/axios/stubs

delete://test/axios/stubs


Extensions

All the extensions exposed by service-fastify are synchronous and executes in serie.

🧩 $FASTIFY_OPTIONS

It allows to programmatically modify the options that are given to the Fastify's instance, it works in waterfall.

registerAction({
  target: '$FASTIFY_OPTIONS',
  handler: (defaultOptions) => ({
    ...defaultOptions,
    logger: true,
  }),
});

🧩 $FASTIFY_HACKS_BEFORE

This hook fires before any other step.
It receives a direct reference to the fastify instance.

registerAction({
  target: '$FASTIFY_HACKS_BEFORE',
  handler: ({ fastify }) => {
    // Do something with the Fastify's instance
    fastify.register();
  },
});

🧩 $FASTIFY_HACKS_AFTER

This hook fires after any other step.
It receives a direct reference to the fastify instance.

registerAction({
  target: '$FASTIFY_HACKS_AFTER',
  handler: ({ fastify }) => {
    // Do something with the Fastify's instance
    fastify.register();
  },
});

🧩 $FASTIFY_PLUGIN

Let register Fastify plugins or decorate the instance.

registerAction({
  hook: '$FASTIFY_PLUGIN',
  handler: ({ registerPlugin }) => {
    registerPlugin(/* fastify plugin */);
    registerPlugin(/* fastify plugin */);
  },
});

It also receives API for decorating the Fastify instance:

registerAction({
  hook: '$FASTIFY_PLUGIN',
  handler: ({ decorateRequest }) => {
    const user = { name: 'Marco' };
    decorateRequest('user', user);
  },
});

🧩 $FASTIFY_ROUTE

Lets implement first level routes in your Fastify instance:

// With the API:
registerAction({
  target: '$FASTIFY_ROUTE',
  handler: ({ registerRoute }) =>
    registerRoute({
      method: 'GET',
      url: '/',
      handler: (req, res) => res.send('Hello World'),
    }),
});

// With direct values:
registerAction({
  target: '$FASTIFY_ROUTE',
  handler: () => ({
    method: 'GET',
    url: '/',
    handler: (req, res) => res.send('Hello World'),
  }),
});

// With multiple routes
registerAction({
  target: '$FASTIFY_ROUTE',
  handler: () => [
    {
      method: 'GET',
      url: '/p1',
      handler: (req, res) => res.send('Page1'),
    },
    {
      method: 'GET',
      url: '/p2',
      handler: (req, res) => res.send('Page2'),
    },
  ],
});

🧩 $FASTIFY_TDD_RESET

Let you inject state reset logic from any Service or Feature.

Tests will then call /test/reset in order to trigger an App's state reset.

registerAction({
  target: '$FASTIFY_TDD_CHECK',
  handler: ({ registerTddReset }) => {
    registerTddCheck(async ({ getContext }) => {
      const pg = getContext('pg');
      return pg.query('DROP SCHEMA "public" CASCADE');
    });
  },
});

🧩 $FASTIFY_TDD_CHECK

Let you inject preHandlers to the /test route.

Use this to await for database connections or other preconditions.

registerAction({
  target: '$FASTIFY_TDD_CHECK',
  handler: ({ registerTddCheck }) => {
    // Middleware style
    registerTddCheck((request, reply, next) => {
      console.log('Healthcheck with callback');
      next();
    });

    // Async style
    registerTddCheck(async (request, reply) => {
      console.log('Asynchronous healthcheck');
    });
  },
});

🧩 $FASTIFY_TDD_ROOT

Let you implement the returning body of the /test route.

registerAction({
  target: '$FASTIFY_TDD_ROOT',
  name: 'registerTddRoute',
  handler: () => (request, reply) => reply.send('ok'),
});

🧩 $FASTIFY_TDD_ROOT

Let you extend the /test API with custom sub-routes.

It works exactly as $FASTIFY_ROUTE, but all the routes you define are scoped with the testing prefix.

5.2.2

1 year ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0-alpha.39

2 years ago

5.0.0-alpha.37

2 years ago

5.0.0-alpha.38

2 years ago

5.0.0-alpha.35

2 years ago

5.0.0-alpha.36

2 years ago

5.0.0-alpha.33

2 years ago

5.0.0-alpha.34

2 years ago

5.0.0-alpha.40

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.0.0-alpha.31

3 years ago

5.0.0-alpha.32

3 years ago

5.0.0-alpha.30

3 years ago

5.0.0-alpha.28

3 years ago

5.0.0-alpha.29

3 years ago

5.0.0-alpha.26

3 years ago

5.0.0-alpha.27

3 years ago

5.0.0-alpha.25

3 years ago

5.0.0-alpha.9

3 years ago

5.0.0-y.0

3 years ago

5.0.0-alpha.19

3 years ago

5.0.0-alpha.17

3 years ago

5.0.0-alpha.18

3 years ago

5.0.0-alpha.15

3 years ago

5.0.0-alpha.16

3 years ago

5.0.0-alpha.13

3 years ago

5.0.0-alpha.14

3 years ago

5.0.0-alpha.11

3 years ago

5.0.0-alpha.12

3 years ago

5.0.0-alpha.10

3 years ago

5.0.0-alpha.24

3 years ago

5.0.0-alpha.22

3 years ago

5.0.0-alpha.23

3 years ago

5.0.0-alpha.20

3 years ago

5.0.0-alpha.21

3 years ago

5.0.0-alpha.7

3 years ago

5.0.0-alpha.6

3 years ago

5.0.0-alpha.5

3 years ago

5.0.0-alpha.4

3 years ago

5.0.0-alpha.8

3 years ago

5.0.0-alpha.3

3 years ago

5.0.0-alpha.2

3 years ago

5.0.0-alpha.1

3 years ago

5.0.0-alpha.0

3 years ago

4.6.0-alpha.0

3 years ago

4.7.0

3 years ago

4.7.2

3 years ago

4.7.1

3 years ago

4.6.0

3 years ago

4.7.1-alpha.0

3 years ago

4.7.1-alpha.1

3 years ago

4.5.3

3 years ago

4.5.2

3 years ago

4.5.1

3 years ago

4.5.0-alpha.7

3 years ago

4.5.0

3 years ago

4.4.2

3 years ago

4.5.0-alpha.6

3 years ago

4.5.0-alpha.5

3 years ago

4.5.0-alpha.1

3 years ago

4.5.0-alpha.0

3 years ago

4.4.2-alpha.0

3 years ago

4.4.1-alpha.2

3 years ago

4.4.1-alpha.1

3 years ago

4.4.1-alpha.0

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.4.0-alpha.22

3 years ago

4.4.0-alpha.21

3 years ago

4.4.0-alpha.20

3 years ago

4.4.0-alpha.17

3 years ago

4.4.0-alpha.19

3 years ago

4.4.0-alpha.18

3 years ago

4.4.0-alpha.0

3 years ago

4.4.0-alpha.4

3 years ago

4.4.0-alpha.3

3 years ago

4.4.0-alpha.2

3 years ago

4.4.0-alpha.1

3 years ago

4.4.0-alpha.7

3 years ago

4.4.0-alpha.6

3 years ago

4.0.0-alpha.19

3 years ago

4.4.0-alpha.5

3 years ago

4.2.0-y.0

3 years ago

4.4.0-alpha.9

3 years ago

4.3.0-alpha.0

3 years ago

4.3.0-alpha.1

3 years ago

4.0.0

3 years ago

4.2.0

3 years ago

4.2.0-alpha.0

3 years ago

4.2.0-alpha.2

3 years ago

4.2.0-alpha.1

3 years ago

4.4.0-8.0

3 years ago

4.4.0-alpha.11

3 years ago

4.4.0-alpha.10

3 years ago

4.4.0-alpha.13

3 years ago

4.4.0-alpha.12

3 years ago

4.2.0-alpha.3

3 years ago

4.1.0

3 years ago

4.3.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.0.0-alpha.20

3 years ago

4.1.0-alpha.1

3 years ago

4.1.0-alpha.0

3 years ago

4.0.0-alpha.22

3 years ago

4.0.0-alpha.21

3 years ago

4.0.0-alpha.18

4 years ago

4.0.0-alpha.17

4 years ago

4.0.0-alpha.16

4 years ago

4.0.0-alpha.15

4 years ago

4.0.0-alpha.14

4 years ago

4.0.0-alpha.13

4 years ago

4.0.0-alpha.9

4 years ago

4.0.0-alpha.7

4 years ago

4.0.0-alpha.8

4 years ago

4.0.0-alpha.5

4 years ago

4.0.0-alpha.6

4 years ago

4.0.0-alpha.3

4 years ago

4.0.0-alpha.1

4 years ago

4.0.0-alpha.2

4 years ago

4.0.0-alpha.12

4 years ago

4.0.0-alpha.0

4 years ago

4.0.0-alpha.11

4 years ago

4.0.0-alpha.10

4 years ago

3.20.6

4 years ago

3.21.0-alpha.3.0

4 years ago

3.21.0-alpha.3.1

4 years ago

3.21.0-alpha.3.2

4 years ago

3.20.2

4 years ago

3.20.1

4 years ago

3.20.0

4 years ago

3.19.8

4 years ago

3.19.7

4 years ago

3.19.4

4 years ago

3.19.3

4 years ago

3.19.6

4 years ago

3.19.5

4 years ago

3.19.1

5 years ago

3.19.0

5 years ago

3.18.2

5 years ago

3.18.1

5 years ago

3.18.0

5 years ago

3.17.0

5 years ago

3.16.1

5 years ago

3.16.0

5 years ago

3.15.0

5 years ago

3.14.0

5 years ago

3.13.8

5 years ago

3.13.4

5 years ago

3.13.3

5 years ago

3.13.6

5 years ago

3.13.5

5 years ago

3.13.7

5 years ago

3.13.2

5 years ago

3.13.1

5 years ago

3.13.0

5 years ago

3.12.0

5 years ago

3.11.0

5 years ago

3.10.1

5 years ago