1.0.5 • Published 8 months ago

@trarn/middleware v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Avernix Technologies Middleware Library

This library provides a set of utility functions to simplify and standardize common tasks in your Node.js/Express applications.

Installation

To install the package, use npm or yarn:

npm install @trarn/middleware

or

yarn add @trarn/middleware

Usage

CommonJS (CJS) Example

If your project uses CommonJS, you can import and use the utilities as follows:

const {
    coerceBoolean,
    sanitizeParams,
    getParamValue,
    response,
    paramBuilder,
} = require('@trarn/middleware');

ECMAScript Modules (ESM) Example

If your project uses ECMAScript Modules, you can import and use the utilities as follows:

import {
    coerceBoolean,
    sanitizeParams,
    getParamValue,
    response,
    paramBuilder,
} from '@trarn/middleware';

1. coerceBoolean

Coerces a value to a boolean. This function is useful for standardizing the conversion of various types (like strings and numbers) to boolean values.

Example

// CommonJS
const { coerceBoolean } = require('@trarn/middleware');

// or ECMAScript Modules
import { coerceBoolean } from '@trarn/middleware';

console.log(coerceBoolean(true)); // returns true
console.log(coerceBoolean('true')); // returns true
console.log(coerceBoolean('1')); // returns true
console.log(coerceBoolean('false')); // returns false
console.log(coerceBoolean(0)); // returns false
console.log(coerceBoolean({})); // returns true

2. sanitizeParams

Middleware to sanitize parameters in an Express request object. This middleware removes potentially unsafe characters from query parameters, URL parameters, headers, and body parameters.

Example

// CommonJS
const express = require('express');
const { sanitizeParams } = require('@trarn/middleware');

// or ECMAScript Modules
import express from 'express';
import { sanitizeParams } from '@trarn/middleware';

const app = express();

// Use the sanitizeParams middleware
app.use(sanitizeParams());

app.post('/example', (req, res) => {
    res.json(req.body); // The body will be sanitized
});

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

3. getParamValue

Retrieves the value of a specified parameter from an Express request object. It searches through query parameters, route parameters, headers, body, and nested properties.

Example

// CommonJS
const express = require('express');
const { getParamValue } = require('@trarn/middleware');

// or ECMAScript Modules
import express from 'express';
import { getParamValue } from '@trarn/middleware';

const app = express();

app.post('/example', async (req, res) => {
    const value = await getParamValue(req, ['userId', 'custom-header']);
    res.send(`Parameter value: ${value}`);
});

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

4. Response

Sends a JSON response with the specified status and data, or redirects to a specified URL. Ensures that the response object is valid before attempting to send the response or perform the redirect.

Example

// CommonJS
const { response } = require('@trarn/middleware');

// or ECMAScript Modules
import { response } from '@trarn/middleware';

// Assuming the following response object:
const res = {
  status: (code) => ({
    json: (body) => console.log(`Status: ${code}, Body: ${JSON.stringify(body)}`),
    redirect: (url) => console.log(`Redirecting to: ${url}`)
  })
};

// Sending a successful response
response(res, 200, { message: 'Success' });
// Output: Status: 200, Body: {"ok":true,"success":true,"message":"Success"}

// Sending an error response
response(res, 404, { error: 'Not Found' });
// Output: Status: 404, Body: {"ok":false,"success":false,"error":"Not Found"}

// Redirecting to a URL
response(res, 302, { url: 'http://example.com' }, true);
// Output: Redirecting to: http://example.com

5. paramBuilder

Builds a query string from an object of parameters. This function is useful for creating URL query strings from an object of key-value pairs.

Example

// CommonJS
const { paramBuilder } = require('@trarn/middleware');

// or ECMAScript Modules
import { paramBuilder } from '@trarn/middleware';

const params = { name: 'John Doe', age: 25 };
const queryString = paramBuilder(params);
console.log(queryString); // Output: 'name=John%20Doe&age=25'

License

This project is licensed under the MIT License - see the LICENSE file for details.

About Avernix Technologies

Avernix Technologies is a company focused on delivering high-quality software solutions and tools to enhance productivity and security in modern web applications. For more information, visit our website.

1.0.5

8 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago