npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cca-category-module

v0.0.28

Published

Category module

Readme

Below is an example of a Markdown documentation file for the Category Module:


Category Module Documentation

This module provides functionality to manage categories, including creating categories, retrieving categories and their associated posts, updating category details, updating the category tree structure, and deleting categories.

Note: This module is built on top of Express and leverages custom use cases for business logic. Custom errors such as ValidationError and NotFoundError are thrown when appropriate.


Installation

Install the module via npm (if published as a package):

npm install cca-category-module

Then, import the module into your project:

import { createCategoryContainer, CategoryController } from 'cca-category-module';

Usage Example

Below is an example on how to integrate the CategoryController with your Express application:

import express from 'express';
import { createCategoryContainer, CategoryController } from 'cca-category-module';

const app = express();
app.use(express.json());

// Create a container and resolve the CategoryController
const container = createCategoryContainer();
const categoryController: CategoryController = container.resolve(CategoryController);

// Routes for categories
app.post('/categories', categoryController.createCategory);
app.get('/categories', categoryController.getAllCategories);
app.get('/categories/:id', categoryController.getCategory);
app.get('/categories/:id/posts', categoryController.getAllCategoryPosts);
app.put('/categories/:id', categoryController.updateCategory);
app.put('/categories/tree', categoryController.updateCategoryTree);
app.delete('/categories/:id', categoryController.deleteCategory);

// Error handling middleware
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});

app.listen(3000, () => console.log('Server is running on port 3000'));

Endpoints

1. Create a Category

  • URL: /categories
  • Method: POST
  • Description: Create a new category.
  • Body Parameters:
    • name (string, required): The category name.
    • description (string, optional): Description of the category.
    • Other optional category data as required.
  • Success Response:
    • Code: 201 Created
    • Content: JSON object containing the created category details.
  • Errors:
    • Throws ValidationError if the required field name is missing.

2. Get All Categories

  • URL: /categories
  • Method: GET
  • Description: Retrieve all categories.
  • Success Response:
    • Code: 200 OK
    • Content: JSON array of categories.

3. Get a Single Category

  • URL: /categories/:id
  • Method: GET
  • Description: Retrieve a single category by its ID.
  • Parameters:
    • id (string): The category identifier.
  • Success Response:
    • Code: 200 OK
    • Content: JSON object with category details.
  • Errors:
    • Throws ValidationError if no ID is provided.
    • Throws NotFoundError if the category is not found.

4. Get All Posts for a Category

  • URL: /categories/:id/posts
  • Method: GET
  • Description: Retrieve posts associated with a specific category, with pagination support.
  • Parameters:
    • id (string): The category identifier.
  • Query Parameters:
    • page (number, optional): The page number (default: 1).
    • limit (number, optional): Number of posts per page (default: 10).
    • orderBy (string, optional): Field to order by (default: "createdAt").
    • order (string, optional): Order direction (ASC or DESC, default: "DESC").
  • Success Response:
    • Code: 200 OK
    • Content: JSON with paginated posts for the category.
  • Errors:
    • Throws ValidationError if no category ID is provided.

5. Update a Category

  • URL: /categories/:id
  • Method: PUT
  • Description: Update an existing category by its ID.
  • Parameters:
    • id (string): The category identifier.
  • Body Parameters:
    • JSON object with fields to update.
  • Success Response:
    • Code: 200 OK
    • Content: JSON object with the updated category details.
  • Errors:
    • Throws ValidationError if no category ID is provided.

6. Update Category Tree

  • URL: /categories/tree
  • Method: PUT
  • Description: Update the category tree structure. This endpoint expects an array representing the moves/changes in the category hierarchy.
  • Body Parameters:
    • An array of moves describing the new category tree structure.
  • Success Response:
    • Code: 204 No Content
  • Errors:
    • Throws ValidationError if the request body is not an array.

7. Delete a Category

  • URL: /categories/:id
  • Method: DELETE
  • Description: Delete a category by its ID.
  • Parameters:
    • id (string): The category identifier.
  • Success Response:
    • Code: 204 No Content
  • Errors:
    • Throws ValidationError if no category ID is provided.

Error Handling

Ensure your application has a proper error-handling middleware to catch and return errors:

app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});

License

This module is released under the MIT License.


This documentation provides an overview of the available endpoints and their usage. For more detailed information on the business logic and error handling, please refer to the source code and inline comments.