X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=3bf7e55cc7e85a4cd050fd2708dc2726fa9f9d94;hb=03cdf23c601ed191fb3d23f5d5bd7b3c7198e578;hp=dc0585471303780c5720ab0d5f62fdf55d11d059;hpb=cf9590ecbcd4b0a7c01f153cacade619518f84f0;p=squeep-indieauth-helper diff --git a/lib/common.js b/lib/common.js index dc05854..3bf7e55 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,24 +1,25 @@ 'use strict'; const path = require('path'); +const { name: packageName, version: packageVersion } = require('../package'); + +const libraryIdentifier = `${packageName}@${packageVersion}`; /** * Return a function which combines a part of the filename with a scope, for use in logging. * @param {string} filename */ const fileScope = (filename) => { - let fScope = path.basename(filename, '.js'); - if (fScope === 'index') { - fScope = path.basename(path.dirname(filename)); - } - return (scope) => `${fScope}:${scope}`; + const shortFilename = path.basename(filename, '.js'); + const fScope = (shortFilename === 'index') ? path.basename(path.dirname(filename)) : shortFilename; + return (scope) => [libraryIdentifier, fScope, scope].join(':'); } /** * Pick out useful axios response fields. - * @param {*} res - * @returns + * @param {AxiosResponse} res + * @returns {Object} */ const axiosResponseLogData = (res) => { const data = pick(res, [ @@ -97,6 +98,17 @@ const properURLComponentName = (component) => { } +/** + * Encodes single-level object as form data string. + * @param {Object} data + */ +const formData = (data) => { + const formData = new URLSearchParams(); + Object.entries(data).forEach(([name, value]) => formData.set(name, value)); + return formData.toString(); +}; + + module.exports = { fileScope, axiosResponseLogData, @@ -104,4 +116,5 @@ module.exports = { pick, setSymmetricDifference, properURLComponentName, + formData, }; \ No newline at end of file