1.0.15 • Published 1 year ago

fastifox v1.0.15

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

🦊FastiFox

Fastifox is a mini framework for Fastify to facilitate the creation of APIs and CRUD operations with just one file.

🦊FastiFox

Prerequisites

To use Fastifox, you need to have TypeORM for database operations and Zod for data validation.

Install the necessary dependencies:

npm install fastify typeorm zod

🚀 Installation

Install the package via npm:

npm install fastifox

🛠️ Usage

Setup

Start Fastifox by passing the correct parameters in your application's bootstrap.

  import { FastiFox } from 'fastifox'

  const fastifox = new FastiFox()
  fastifox.initialize({
    directory: './src/**/*.module{.ts,.js}',
    context: {
      datasource: typeormSouce,
      server: fastifyInstance
    }
  })

CRUD Generator

To automatically generate a CRUD, you need to have the TypeORM entity configured and Zod schemas in place if you want to add validations to the routes. Import the CrudGenerator from Fastifox and make the necessary definitions.

import { FastifyInstance } from 'fastify';
import { DataSource } from 'typeorm';
import { CrudGenerator } from 'fastifox'
import { YourEntity } from './entities/YourEntity';
import { z } from 'zod';

class MyCrudGenerator extends CrudGenerator {
  init(){
    this.generate({
      entity: YourEntity,
      route: {
        create: {
          findableFields: ['email'],
          schema: z.object({
            name: z.string(),
            // other fields...
          }),
        },
        update: {
          schema: updateUserSchema,
        },
        listMany: {
          withPagination: true,
          findableFields: ['email', 'name']
        },
        listOne: {
          inputPlugins // plugins to execute on input
        },
        delete: {
          inputPlugins
        }
    }})
  }
}

Using the CrudGenerator and correctly mapping the file at the start of the application will automatically generate the routes.

FieldDescription
inputPluginsAn array of plugins to be executed at the start of the request (route).
findableFieldsThe entity fields that should be used to search or validate data.
withPaginationEnables pagination for routes that list multiple data entries.
schemaIt's the Zod schema object.

Additional details | Field | Description | | ------------- | ------------- | | searchTerm | Use in GET routes to fetch data. | | fields | Use to select the fields that should be returned in the request. |

Auth JWT (Plugin)

This plugin generates JWT token authentication as well as the Sign in and Sign up routes.

Usage

Instantiate the plugin and add the necessary data for authentication configuration.

  import { JWTAuthPlugin } from "fastify";

  const foxFlowAuth = new JWTAuthPlugin({
    datasource: this.datasource,
    server: this.server,
    entity: User,
  })
  
  foxFlowAuth.register({
    secret: enviroment.jwt.secret
  })

  foxFlowAuth.execute({
    findableField: 'email',
    hashedField: 'password',
    signUp: {
      schema: signUpSchema,
      defaultValues: {
        roleSlug: 'superuser',
      }
    },
    signIn: {
      schema: signInSchema,
      fieldsToken: ['id', 'email', 'roleSlug'],
      returnFields: ['id', 'email', 'roleSlug', 'name'],
    },
    defaultValues: {
      roleSlug: 'superuser'
    }
  })

After the configuration, the /sign-in and /sign-up routes will be generated.

Using the authentication middleware

  // ... other code
    update: {
    schema: updateUserSchema,
    inputPlugins: [
      JWTAuthPlugin.verifyToken()
    ]
  },
  // ... other code

📚 Detailed Explanation

  1. Create an Entity: Define your TypeORM entity.
  2. Generate CRUD Routes: Use CrudGenerator to generate CRUD routes for your entity.
  3. Run the Server: Initialize Fastify and TypeORM, then start the server.

🤝 Contributing

We welcome contributions! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/my-feature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/my-feature).
  5. Open a Pull Request.

Setting Up for Development

  1. Clone the repository:

    git clone https://github.com/dotbruu/fastifox
    cd fastifox
  2. Install dependencies:

    npm install
  3. Build the project:

    npm run build
  4. Run tests (if applicable):

    npm test

🙌 Acknowledgments

  • TypeORM: A powerful ORM for TypeScript and JavaScript.
  • Zod: A TypeScript-first schema declaration and validation library.
  • Fastify: A fast and low overhead web framework for Node.js.
1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago