Skip to content

DWTechs/Gatelin-express.js

Repository files navigation

License: MIT npm version last version release date Jest:coverage

Open source Express.js middleware to extract and validate Gatelin gateway consumer headers.

  • 🪶 Very lightweight
  • 🧪 100% code coverage
  • 🚚 Shipped as ESM
  • 📝 Written in TypeScript
  • 🔒 Strict input validation

Installation

$ npm i @dwtechs/gatelin-express

Usage

// @ts-check
import express from "express";
const router = express.Router();

import { getConsumer } from "@dwtechs/gatelin-express";

// Routes
// Add new items
router.post("/", getConsumer, ...);

Use getConsumer to read and validate the consumer headers injected by the Gatelin gateway into each request. It stores the validated consumer information in res.locals.consumer ({ id, nickname }) for use by subsequent middleware in the request pipeline. Add it to any route that needs to identify the caller.

API Reference

/**
 * Middleware to extract and validate consumer information from request headers.
 * Retrieves consumer ID from 'x-consumer-id' header and consumer nickname from 
 * 'x-consumer-nickname' header.
 * Validates that the ID is a valid integer between 1 and 999999999, and the nickname
 * has at least 5 characters.
 * Stores the validated consumer information in res.locals.consumer ({ id, nickname })
 * for use by subsequent middleware in the request pipeline.
 *
 * @param {Request} req - The Express request object containing consumer headers 
 *                  (x-consumer-id and x-consumer-nickname).
 * @param {Response} res - The Express response object where consumer data will be stored 
 *                   in res.locals.consumer.
 * @param {NextFunction} next - The next middleware function in the Express.js 
 *                       request-response cycle.
 *
 * @returns {void} Calls the next middleware function with an error if consumer validation
 *                 fails, otherwise proceeds to the next middleware function.
 * 
 */
function getConsumer(req: Request, res: Response, next: NextFunction): void {}

The Consumer interface is exported and can be used to type res.locals.consumer in downstream middleware or route handlers:

import type { Consumer } from "@dwtechs/gatelin-express";

// Type res.locals.consumer in downstream middleware
const consumer = res.locals.consumer as Consumer;
// consumer.id       → number
// consumer.nickname → string

Support

Environment Version
Node.js >= 22

Logs

Gatelin-express.js uses @dwtechs/Winstan library for logging. All logs are in debug mode. Meaning they should not appear in production mode.

Stack

Purpose Choice Motivation
repository Github hosting for software development version control using Git
package manager npm default node.js package manager
language TypeScript static type checking along with the latest ECMAScript features
module bundler Rollup advanced module bundler for ES6 modules
unit testing Jest delightful testing with a focus on simplicity

About

Gatelin gateway toolset for Express.js

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors