Options
All
  • Public
  • Public/Protected
  • All
Menu

@thermopylae/core.adapter.fastify

@thermopylae/core.adapter.fastify

Version Node Version Documentation License: MIT

Fastify adapter for Thermopylae framework.

Install

npm install @thermopylae/core.adapter.fastify

Description

:information_source: TL;DR;
Thermopylae framework allows you to use it's core.* modules with any of the existing web frameworks, such as express, fastify etc.
All you need to do is implementing HttpRequest and HttpResponse interfaces from @thermopylae/core.declarations.

This package contains implementations for HttpRequest and HttpResponse from @thermopylae/core.declarations.
These implementations are wrappers over Fastify request and response objects.

Usage

Bellow is a simple example of how this package can be used:

import { FastifyRequestAdapter, FastifyResponseAdapter, LOCATION_SYM } from '@thermopylae/core.adapter.fastify';
import { HttpStatusCode } from '@thermopylae/core.declarations';
import type { FastifyRequest, FastifyReply } from 'fastify';

function handler(req: FastifyRequest, res: FastifyReply) {
    const request = new FastifyRequestAdapter(req);
    const response = new FastifyResponseAdapter(res);

    // location needs to be computed by yourself and attached to Fastify request object, 
    // before passing it to Thermopylae functions
    request.raw[LOCATION_SYM] = {
        countryCode: 'MD',
        regionCode: 'K',
        city: 'Chisinau',
        latitude: null,
        longitude: null,
        timezone: null
    };
    
    console.log('New request received from ip ', request.ip, ' and device ', JSON.stringify(request.device));
    
    response.status(HttpStatusCode.Ok).send('Hello World!');
}

API Reference

API documentation is available here.

It can also be generated by issuing the following commands:

git clone git@github.com:marinrusu1997/thermopylae.git
cd thermopylae
yarn install
yarn workspace @thermopylae/core.adapter.fastify run doc

Author

👤 Rusu Marin

📝 License

Copyright © 2021 Rusu Marin.
This project is MIT licensed.