I try to figure out how to add custom code to my request similar way to pre-request script in Postman like:
const items = JSON.parse(pm.collectionVariables.get('items'))
const randomItem = _.sample(items)
pm.variables.set('randomItemId', randomItem.id)
pm.variables.set('randomItem', JSON.stringify(randomItem));
I know that for 03 2023 importing external libraries is in beta, but let's assume it works ok.
I understand that I need to create filter
:
First question: where is option to create one? I wish it will be separated form my project code.
Second question: can I have filter code directly in my request (visible to quickly check what happening in request)? Or I need to create filters for every request where I need new custom code 😔
I tried follow docs but there is no clear information where add files for filters: https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md
I partially figured it out because it looks like scripts are injected on Collection level.
Prerequisites - navigate to Thunder Client:
v
variable and SaveCreate script:
async function testFunc() {
console.log("Test log message");
return "TEST";
}
module.exports = [testFunc];
To add custom script to Collection:
Use script:
A-test
with value: {{v | testFunc}}
If script is not loaded and function was not executed try:
{{v | testFunc}}
and saveExtend scripts: