0.1.3 • Published 1 year ago
url-md5 v0.1.3
URL MD5
URL MD5 是一个将 url 中的路径和查询参数使用 md5 加密的工具。
使用指南
使用条件
该项目需要在 node 环境下使用。
安装
npm install url-md5
使用示例
import urlMd5 from "url-md5";
// 传入完整 url
urlMd5.encode("https://www.hostname.com/pathname?a=1&b=2");
// 仅传入 pathname 和 search,适用于 axios 一类使用 basichURL
urlMd5.encode("/pathname?a=1&b=2");
// 还可以直接传入请求 options
urlMd5.encode({url: "/pathname", method: "POST", params: {a: 1, b: 2}});
详细说明
urlMd5.encode(url,pathname+querystring,options)
encode 函数会将传入的参数格式化成标准的 url 然后截取 pathname+search 进行 md5 加密。
自定义配置
urlMd5.config.salt
你可以定义配置 salt,md5 加密时会将 salt 拼接到 pathname+search 之后。
import urlMd5 from "url-md5";
// 设置 salt
urlMd5.config.salt = "Your salt";
// 示例
urlMd5.encode("https:/www.hostname.com/pathname?a=1&b=2"); // => md5("/pathname?a=1&b=2" + "Your salt");
urlMd5.encode("/pathname?a=1&b=2"); // => md5("/pathname?a=1&b=2" + "Your salt");
urlMd5.encode({url: "/pathname", params: {a: 1, b: 2}}); // => md5("/pathname?a=1&b=2" + "Your salt");
urlMd5.config.pathRewriter(pathname, raw)
如在使用 axios 时,你需要替换加密的 pathname 则可以自定义该函数。
import urlMd5 from "url-md5";
// 设置 pathRewriter
urlMd5.config.pathRewriter = function (pathname, raw) {
return pathname.replace("/prefix", "") // 将 /prefix/pathname 替换为 /pathname
}
// 示例
urlMd5.encode("https://www.hostname.com/prefix/pathname?a=1&b=2") // => md5("/pathname/?a=1&b=2");
urlMd5.encode("/prefix/pathname?a=1&b=2") // => md5("/pathname/?a=1&b=2");
urlMd5.encode({url: "/prefix/pathname", params: {a: 1, b: 2}}) // => md5("/pathname/?a=1&b=2");