Created
March 12, 2025 17:23
-
-
Save tcartwright/7af864048ed944985f3cfb8055c9065a to your computer and use it in GitHub Desktop.
POSTMAN: Global functions from CDN javascript file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// can be used at a collection level pre-request script to pull a js from a cdn, and expose the methods to all your requests | |
// can also use a script package instead of a CDN. | |
await (async () => { | |
const requestPromise = new Promise((resolve, reject) => { | |
pm.sendRequest("https://cdn.foo.com/foo_utils.js", (err, res) => { | |
if (err) { | |
console.error(err); | |
return reject(err); | |
} | |
resolve(res.text()); | |
}); | |
}); | |
try { | |
const response = await requestPromise; | |
//console.log(`response = ${response}`); | |
// execute the response so the functions are available | |
eval(response); | |
// wrap the library functions as a global so we dont have to import the library into every request. | |
// otherwise the requests cannot call the functions directly | |
utils = { | |
GetValue: async function() { | |
return await GetValue(); | |
}, | |
GetValue2: function(param1, param2) { | |
return GetValue2(param1, param2); | |
} | |
}; | |
} catch (error) { | |
console.warn(`error.stack = ${error.stack}`); | |
console.error('Error:', error); | |
throw error; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment