UNPKG

@znode/sign

Version:
23 lines (22 loc) 723 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sign = void 0; const hmac_1 = require("@zodash/hmac"); const qs = require("@zcorky/query-string"); function sign(data, secret) { const timestamp = new Date(data.timestamp).toISOString(); const method = data.method.toUpperCase(); const path = data.path; const body = method === 'GET' ? '' : stringify(data.body, qs.stringify); const text = [timestamp, method, path, body].join(''); return hmac_1.hmacSHA256(text, secret); } exports.sign = sign; function stringify(v, stringifyFn) { if (!v) return ''; if (typeof v === 'string') return v; return stringifyFn(v); } exports.default = sign;