digipinjs
Version:
A comprehensive TypeScript library for encoding and decoding Indian geographic coordinates into DIGIPIN format (Indian Postal Digital PIN system). Features CLI tools, caching, batch processing, and Express middleware for seamless integration.
27 lines (26 loc) • 773 B
JavaScript
import { getDigiPin } from './core';
/**
* Express middleware: reads x-lat & x-lng headers, adds X-DIGIPIN
*/
export function digiPinMiddleware(options = {}) {
const { silent = true, onError } = options;
return (req, res, next) => {
const lat = parseFloat(req.header('x-lat') || '');
const lng = parseFloat(req.header('x-lng') || '');
if (!isNaN(lat) && !isNaN(lng)) {
try {
res.setHeader('X-DIGIPIN', getDigiPin(lat, lng));
}
catch (error) {
if (onError) {
onError(error);
}
if (!silent) {
next(error);
return;
}
}
}
next();
};
}