/fastify-mongo-swagger-auth0-boilerplate

fastify mongo swagger auth0 boilerplate

Primary LanguageJavaScript

Server

Fastify + mongo + swagger + auth0 boilerplate

It uses internally:

It requires:

  • Node 14

Environment variables

  • Please look into src/config.js, you will see all the ENV variables there.

How to?

CRUD route

index.js

import { commonHandlers } from '../core/index.js';

const getRouteOpts = (baseUrl) => ({ baseUrl, commonHandlers });

export default { getRouteOpts };

your-route.js

import fp from 'fastify-plugin';

import { isInvalidMongoId } from '../../db-utils.js';
import { commonTasks, routes } from '../core/index.js';

export default fp((app, opts, next) => {
    const { url, urlById } = routes.getRouteUrls(opts, "todo-item");
    //tells what url to use
    const getCommonTasks = () => commonTasks(app.dbExtensions, "todos");
    // tells what collection to use

    const { commonHandlers } = opts;

    app.route({
      method: 'POST',
      url,
      handler: async ({ body }, reply) => {
        const { name, languages } = body;
        const insertedId = await getCommonTasks().insertOne({ text: body.text });
        return commonHandlers.handleCommonPostOne(insertedId, reply);
      },
      preValidation: app.authenticate, // USES AUTH0 Authentication
    });

    app.route({
      method: 'GET',
      url,
      handler: () => getCommonTasks().find(),
      preValidation: app.authenticate,
    });

    app.route({
      method: 'GET',
      url: urlById,
      handler: async ({ params: { id } }, reply) => {
        if (isInvalidMongoId(id)) {
          return commonHandlers.handleInvalidObjectId(id, reply);
        }
        const todo = await getCommonTasks().findById({ id });
        return commonHandlers.handleCommonGetOne(todo, reply);
      },
      preValidation: app.authenticate,
    });

    app.route({
      method: 'PUT',
      url: urlById,
      handler: async ({ body, params: { id } }, reply) => {
        if (isInvalidMongoId(id)) {
          return commonHandlers.handleInvalidObjectId(id, reply);
        }
        const { text } = body;
        const result = await getCommonTasks().updateOne({ id }, { text });
        return commonHandlers.handleCommonPutDelete(result, reply);
      },
      preValidation: app.authenticate,
    });

    app.route({
      method: 'DELETE',
      url: urlById,
      handler: async ({ params: { id } }, reply) => {
        if (isInvalidMongoId(id)) {
          return commonHandlers.handleInvalidObjectId(id, reply);
        }
        const result = await getCommonTasks().deleteOne({ id });
        return commonHandlers.handleCommonPutDelete(result, reply);
      },
      preValidation: app.authenticate,
    });

    next();
  },
);

Managing jsonSchemas

import {
  makeFeatureConstants,
  makeCreateJsonSchema,
  makePutOneJsonSchema,
  makeDeleteJsonSchema,
} from '../core/index.js';
import { HttpStatusCodes } from '../../constants.js';

const featureName = 'todo';
const todoConstants = makeFeatureConstants(featureName);

const baseSchema = {
  swaggerTag: todoConstants.swaggerTag,
};

const postUpdateConfig = {
  itemType: {
    text: { type: 'string', minLength: 3 },
  },
  required: ['text'],
  ...baseSchema,
};

const todoCreateSchema = makeCreateJsonSchema({
  ...postUpdateConfig,
  responses: {
    [HttpStatusCodes.notFound]: {
      type: 'null',
    },
  },
});

const todoUpdateSchema = makePutOneJsonSchema({
  ...postUpdateConfig,
  params: postUpdateConfig.params.properties,
  paramsRequired: [projectIdField],
});

const todoDeleteSchema = makeDeleteJsonSchema({
  ...baseSchema,
  params: postUpdateConfig.params.properties,
  paramsRequired: [projectIdField],
});

export default {
  ...todoConstants,
  todoCreateSchema,
  todoDeleteSchema,
  todoUpdateSchema,
};