X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=7a918bb9f4eaf1d7b588152d35290268ced89aa5;hb=ddf28d2e4816d7c4e188f8dd510b48ad87aee478;hp=3bf7e55cc7e85a4cd050fd2708dc2726fa9f9d94;hpb=3d41f4dd33d59d0c11c97ddeb51ab22d851b93e6;p=squeep-indieauth-helper diff --git a/lib/common.js b/lib/common.js index 3bf7e55..7a918bb 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,36 +1,29 @@ '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) => { - 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 {AxiosResponse} res + * Pick out useful got response fields. + * @param {GotResponse} res * @returns {Object} */ -const axiosResponseLogData = (res) => { +const gotResponseLogData = (res) => { const data = pick(res, [ - 'status', - 'statusText', + 'statusCode', + 'statusMessage', 'headers', - 'elapsedTimeMs', - 'data', + 'body', + 'error', ]); - if (data.data) { - data.data = logTruncate(data.data, 100); + if (typeof res.body === 'string') { + data.body = logTruncate(data.body, 100); + } else if (res.body instanceof Buffer) { + data.body = ``; + } + data.elapsedTimeMs = res?.timings?.phases?.total; + if (res?.redirectUrls?.length) { + data.redirectUrls = res.redirectUrls; + } + if (res?.retryCount) { + data.retryCount = res.retryCount; } return data; }; @@ -98,23 +91,10 @@ 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, + gotResponseLogData, logTruncate, pick, setSymmetricDifference, properURLComponentName, - formData, -}; \ No newline at end of file +};