fst-fastify
v0.0.7
Published

Readme
Fastify runtime

Current Library is a wrapper around fastify
Main Features
Fastify service default configuration
- Logging (TBD)
- Metrics (TBD)
- Event Loop Health (TBD)
- Basic endpoints:
- healthcheck under /api/health
- swagger under /docs
- Default plugins set:
- Swagger
- Registered Routes Logging
Plugin autodiscovery
Library do perform plugins autodiscovery under the src/server/plugins
The following example describe fastify-sensible plugin registration
Proper export default is important
import fp from 'fastify-plugin'
import sensible, { SensibleOptions } from 'fastify-sensible'
import { FastifyInstance } from 'fastify'
/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
export default fp<SensibleOptions>(async (fastify: FastifyInstance) => {
await fastify.register(sensible, {
errorHandler: false
})
})RouteOptions - Primarily Fastapi Interface. Importing it gives you types definition. Detailed documentation can be found here
export default route - is important to perform autodiscovery
RestAPI routes autodiscovery
Library do perform routes autodiscovery under the src/server/api and src/server/webhook paths
iac
└── ...
src
└── server
└── api
└── health
└── get.ts
└── carriers
└── {carrierId}
└── put.ts
└── get.ts
└── webhook
└── test
└── patch.ts
├── index.ts
├── ...
└── ...With the example above, the next routes will be added
/GET api/health
/GET api/carriers/
/PUT api/carriers/{carrierId}
/PATCH webhook/testRoute definition example:
import { RouteOptions } from 'fst-fastify'
const route: RouteOptions = {
handler: async function (request, reply) {
reply.send({ status: 'ok' })
}
}
export default routeRouteOptions - Primarily Fastapi Interface. Importing it gives you types definition. Detailed documentation can be found here
export default route - is important to perform autodiscovery
Jobs autodiscovery
Jobs are autoloaded from the src/server/jobs folder
Example:
import { CronOptions } from 'fst-fastify'
const job: CronOptions = {
cronTime: '0 0 0 * * * ',
onTick: (server) => {
// Do something
}
}
export default jobPreparation to work
Before start working with the tool you need to generate a personalised gitlab token and add it to your system. Guideline can be found below
Install
npm i -D fst-fastifyConfiguration
Add it to your index.ts in order to run application
import { start } from 'fst-fastify'
start()Testing
Test Example using jest
import { app } from 'fst-fastify'
describe('Basic endpoints test', () => {
it('Health check for service', async () => {
let server = await app
const response = await server.inject({
method: 'GET',
url: '/api/health'
})
expect(response.statusCode).toBe(200)
expect(JSON.parse(response.body)).toStrictEqual({
status: 'ok'
})
})
it('Test swagger file initiallisation', async () => {
let server = await app
const response = await server.inject({
method: 'GET',
url: '/docs'
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toStrictEqual('./docs/static/index.html')
})
})