JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 61
  • Score
    100M100P100Q71394F
  • License Apache License 2.0

A TypeScript decorator driven approach for developing Fastify web applications.

Package Exports

  • @sleekify/sleekify-fastify
  • @sleekify/sleekify-fastify/dist/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sleekify/sleekify-fastify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Sleekify Fastify Integration

This project's goal is to simplify the development of REST web services in NodeJS by bringing the best of the Java API for RESTful Web Services (JAX-RS) to TypeScript. This is possible since TypeScript decorators may be used in a manner similar to Java annotations. This project allows you to apply decorators to your resource classes to identify your REST resources and associate them with OpenAPI definitions. This allows you to maintain your API documentation alongside your code using typed definitions. If you ever had to maintain a large OpenAPI specification by hand, this should grab your attention. Your API documentation will both match the OpenAPI specification's schema and be generated from your code.

Versions

Sleekify Fastify Node.js OpenAPI Specification
1.0.0+ 5.0.0+ 20 3.1.1

Getting Started

  1. First, install Sleekify and the Fastify integration

    npm install @sleekify/sleekify
    npm install @sleekify/sleekify-fastify
  2. Create REST resources using the provided decorators
    src/v1/users.ts

    import { type FastifyReply, type FastifyRequest } from 'fastify';
    import { DELETE, GET, Path, POST, PUT, Schema } from '@sleekify/sleekify';
    
    @Path('/v1/users')
    export class UsersResource {
      @POST()
      async createOne (request: FastifyRequest, reply: FastifyReply) {
        // TODO: your create user code here
      }
    
      @GET()
      async getMany (request: FastifyRequest, reply: FastifyReply) {
        // TODO: your query users code here
      }
    }
    
    @Path({
      path: '/v1/users/{id}',
      parameters: [
        {
          $ref: '#/components/parameters/id'
        }
      ]
    })
    @Schema({
      $ref: '#/components/schemas/user'
    })
    export class UsersIdResource {
      @GET()
      async getOne (request: FastifyRequest, reply: FastifyReply) {
        // TODO: your read user code here
      }
    
      @PUT()
      async updateOne (request: FastifyRequest, reply: FastifyReply) {
        // TODO: your update user code here
      }
    
      @DELETE()
      async deleteOne (request: FastifyRequest, reply: FastifyReply) {
        // TODO: your delete user code here
      }
    }
  3. Create your base OpenAPI specification
    src/openapi.ts

    import { OpenAPIObject } from '@sleekify/sleekify';
    
    export const specification: OpenAPIObject = {
      openapi: '3.1.1',
      info: {
        title: 'Test API',
        version: '1.0.0'
      },
      components: {
        parameters: {
          id: {
            description: 'The identifier of the resource',
            name: 'id',
            in: 'path',
            required: true,
            schema: {
            $ref: '#/components/schemas/id'
          }
        },
        schemas: {
          id: {
            description: 'The id path parameter schema',
            type: ['integer']
          },
          user: {
            type: ['object'],
            properties: {
              id: {
                type: ['number']
              },
              name: {
                type: ['string']
              }
            }
          }
        }
      }
    };
  4. Register your OpenAPI specification and resources with Fastify
    src/index.ts

    import Fastify from 'fastify';
    import { Annotation, Path } from '@sleekify/sleekify';
    import { Sleekify } from '@sleekify/sleekify-fastify';
    import { specification } from './openapi';
    
    const fastify = Fastify({
      logger: true
    });
    
    async function run () {
      // 1. Find all of your resource classes
      const classList = await Annotation.getClassesAnnotatedWith('./v1', Path);
      // 2. Create a Sleekify instance
      const sleekify = new Sleekify(specification, classList);
    
      // 3. Register Sleekify with Fastify
      await fastify.register(sleekify.getPlugin());
    
      fastify.listen({ port: 3000 }, function (error) {
        if (error != null) {
          fastify.log.error(error);
          process.exit(1);
        }
      });
    }
    
    void run();

API Documentation

For more information on the provided decorators, OpenAPI types, and web application errors see the Sleekify API Reference.