Skip to content

marten-cz/postman-libraries

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Postman Helpers

1. Setup in Postman

Create a global variable in Postman called require. Provide a comma separated listed of packages you need e.g. graphql. It will load all the files from this repository which are in the list.

Add the following script (install.js) to the Pre-Request Script of your collection:

const reqs = pm.globals.get('require');
const pkgs = pm.globals.get('packages');
const installed = pm.globals.get('installedPackages');
let helpers = [];

if (pkgs && (reqs === installed)) {
  postman.setEnvironmentVariable("helpers", pkgs);
} else {
  const req = reqs.split(',')

  for (let i = 0; i < req.length; i++) {
    pm.sendRequest({
      async: false,
      url: `https://raw.githubusercontent.com/marten-cz/postman-libraries/main/${req[i]}.js`,
      method: 'GET'
    }, (err, res) => {
      if (!err) {
        helpers.push(`${req[i]}: ${res.text()}`);
        pm.globals.set('installedPackages', pm.globals.get('installedPackages', reqs));
        pm.globals.set('packages', `() => { return { ${helpers.join(', ')} }; }`);

        postman.setEnvironmentVariable("helpers", pm.globals.get('packages'));
      }
    });
  }
}

2. How to use

Evalute content of helpers variable. The methods are accessible under ().().

let helpers = eval(environment.helpers)();
console.log(helpers.graphql().isGraphql());

About

Import additional prepared tests into the Postman

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published