UNPKG

@axway/amplify-sdk

Version:

Axway Amplify SDK for Node.js

43 lines (38 loc) 1.18 kB
import crypto from 'crypto'; /** * Appends query string parameters to a URL. * * @param {String} url - The URL. * @param {Object} params - A map of query string parameters. * @returns {String} */ function createURL(url, params) { return `${url}${url.includes('?') ? '&' : '?'}${prepareForm(params).toString()}`; } /** * Returns a hex encoded md5 hash of a string or object. * * @param {String|Object} it - The object to serialize and hash. * @returns {String} */ function md5(it) { return crypto.createHash('md5').update(typeof it === 'string' ? it : JSON.stringify(it)).digest('hex'); } /** * Copies all params into a new object and converts camelcase property names to underscore case. * * @param {Object} params - The query string parameters to stringify. * @returns {Object} */ function prepareForm(params) { const form = new URLSearchParams(); for (const prop of Object.keys(params).sort()) { if (params[prop] !== undefined) { const name = prop.replace(/[A-Z]/g, (m, i) => `${i ? '_' : ''}${m.toLowerCase()}`); form.append(name, params[prop]); } } return form; } export { createURL, md5, prepareForm }; //# sourceMappingURL=util.js.map