@mdazad/common-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

this is a collection of common utils that you can use in your project. using this you can avoid writing the same code again and again. also you can build your crud api in seconds.

Install

npm install @mdazad/common-utils

or

yarn add @mdazad/common-utils

Usage

basic crud api using common-utils.

tag.controller.js

import { body } from 'express-validator';
import TagModel from '../models/tag.model.js';
import {
  createDocumentAndSendResponse,
  getAllDocumentAndSendResponse,
  validate,
  findByIdAndSendResponse,
  updateByIdAndSendResponse,
  deleteByIdAndSendResponse,
} from '@mdazad/common-utils';

export const createTag = [
  validate([body('name').notEmpty().withMessage('Name is required')]),
  async (req, res) => {
    await createDocumentAndSendResponse(req, res, TagModel, req.body);
  },
];

export const getTags = async (req, res) => {
  await getAllDocumentAndSendResponse(req, res, TagModel);
};

export const getTagById = async (req, res) => {
  await findByIdAndSendResponse(req, res, TagModel);
};

export const updateTagById = async (req, res) => {
  await updateByIdAndSendResponse(req, res, TagModel, req.body);
};

export const deleteTagById = async (req, res) => {
  await deleteByIdAndSendResponse(req, res, TagModel);
};

tag.route.js

import express from 'express';
import {
  createTag,
  getTags,
  getTagById,
  updateTagById,
  deleteTagById,
} from '../controllers/tag.controller.js';

import { authenticateToken } from '@mdazad/common-utils';

const router = express.Router();

// if you want to authenticate all the routes then use this
router.use(authenticateToken('JWT_SECRET', 'user_collection_name'));

router.post('/', createTag);
router.get('/', getTags);
router.get('/:id', getTagById);
router.put('/:id', updateTagById);
router.delete('/:id', deleteTagById);

export default router;

app.js

import express from 'express';

import tagRoutes from './routes/tag.route.js';

const app = express();

app.use(express.json());

app.use('/api/v1/tags', tagRoutes);

export default app;

Docs link

https://gitAzad.github.io/common-utils/

Docs are generated using typedoc.


Features

  • [x] Logger (winston) docs

  • [x] Authenticate Request docs

  • [x] Validate Request docs

  • [x] Crud Operations

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

MIT

Package Sidebar

Install

npm i @mdazad/common-utils

Weekly Downloads

11

Version

1.0.7

License

MIT

Unpacked Size

106 kB

Total Files

42

Last publish

Collaborators

  • mdazad