1.2.4 • Published 3 months ago

@sleekify/sleekify-fastify v1.2.4

Weekly downloads
-
License
Apache License 2....
Repository
github
Last release
3 months ago

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

SleekifyFastifyNode.jsOpenAPI Specification
1.0.0+5.0.0+203.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.

1.2.4

3 months ago

1.2.3

3 months ago

1.2.2

4 months ago

1.2.1

4 months ago

1.2.0

5 months ago

1.1.0

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago