UNPKG

mapnests-node-sdk

Version:

TypeScript Node Supported SDK for Mapnests API integration (Distance Matrix, Distance Matrix Details, Geocode, Reverse Geocode)

23 lines (22 loc) 868 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateToken = generateToken; const crypto_1 = __importDefault(require("crypto")); async function generateToken(apiKey) { const randInt = Math.floor(Math.random() * 1000000); const expires = Math.floor(Date.now() / 1000) + 30; const input = `${expires}:${randInt}:${apiKey}`; // Node.js native crypto hashing const hash = crypto_1.default.createHash('sha256').update(input).digest('hex'); const jsonPayload = { random: randInt, expires: expires, sign: hash, }; const jsonStr = JSON.stringify(jsonPayload); const base64 = Buffer.from(jsonStr).toString('base64'); return base64; }