1.0.2 • Published 10 months ago

valid-response-hiteshfolio v1.0.2

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
10 months ago

Valid Response HiteshFolio

A utility for handling API responses with consistent error and success messaging. This package simplifies the process of sending structured responses in your Node.js applications.

Table of Contents

  1. Installation
  2. Usage
  3. Error Handling
  4. License
  5. Contributing
  6. Author

Installation

You can install the package using npm:

npm install valid-response-hiteshfolio

Usage

To use this package, you need to require it in your Node.js application. You can then use the provided functions to send error or success responses.

Importing the Package

For CommonJS:

const { resError, resSuccess } = require('valid-response-hiteshfolio');

For ES Modules:

import { resError, resSuccess } from 'valid-response-hiteshfolio';

Functions

resError(res, errors, message, dynamicStatus)

Sends an error response with a structured format.

Parameters:

  • res: The response object from your Express app.
  • errors: Additional error information.
  • message: Custom error message (optional).
  • dynamicStatus: Boolean indicating whether to adjust status based on the message (default: false).

Example

app.get('/api/some-route', (req, res) => {
    const errors = { field: 'exampleField' };
    return resError(res, errors, 'Unauthorized access', true);
});

resSuccess(res, data, message, dynamicStatus)

Sends a success response with a structured format.

Parameters:

  • res: The response object from your Express app.
  • data: The data to be returned in the response.
  • message: Custom success message (optional).
  • dynamicStatus: Boolean indicating whether to adjust status based on the data (default: false).

Example

app.post('/api/create-user', (req, res) => {
    const userData = { id: 1, name: 'John Doe' };
    return resSuccess(res, userData, 'User created successfully', true);
});

Error Handling

This package automatically logs errors to the console, allowing for easier debugging during development. Ensure to check your console for any logged error messages.

License

This package is licensed under the Apache-2.0 License.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss changes. Ensure to follow the coding standards and include relevant tests for new features.

Author

Hitesh Saini 🧩💻 GitHub | LinkedIn | Portfolio

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago