A workaround for using libraries in Postman

Hi!

I was pretty unsatisfied with the functionality regarding loading libraries into Postman. I really disliked having to go into my environment file, copy the code out, paste it into another editor, then add it back to the environment. So, I came up with a solution! LOAD!

Now, this DOES require a necessary evil. The following LOAD function needs to be stored in your globals, at least once.
(arg I can’t figure out how to format code here…)

Script = require(‘postman-collection’).Script;
function LOAD(library) {

const pullLibrary = {
    url: 'https://api.getpostman.com/collections/' + pm.environment.get(collection),
    method: 'GET',
    header: {
        'X-Api-Key': environment.postmanApiKey
    }
};
let x;
if (library == 'all') {
pm.sendRequest(pullLibrary, function(err, res) {
  for(x in res.json().collection.item[0].item){
script = new Script(res.json().collection.item[0].item[x].event[0].script);
pm.environment.set('' + res.json().collection.item[0].item[x].name, script.toSource());
console.log('loaded ' + res.json().collection.item[0].item[x].name);
   } 
});
} else {
    pm.sendRequest(pullLibrary, function(err, res) {
        let foundLibrary = (_.findWhere(res.json().collection.item[0].item, {
            name: library
        }));
        if(foundLibrary){
        script = new Script(foundLibrary.event[0].script);
        pm.environment.set('' + foundLibrary.name, script.toSource());
        console.log('loaded ' + foundLibrary.name);
        } else {
            throw new Error('library ' + library + ' does not exist!');
        }
    });
};

};

  1. First, you need to save the LOAD script into your globals,
  2. Then, create a new collection in your workspace called “Libraries”
  3. Add a new environment variable named collection, and set it to use the ‘Library’ collection’s uuid provided by the Postman API, e.g. 1234123-abc12345-aaaa-bbbb-cccc-123456789123
  4. Create a subfolder called “Scripts”
  5. This is where you would create new requests. Put any code you want to reuse in the “Tests” tab. You can add as many requests as you like, each containing their own special code.
  6. Create another environment variable, postmanApiKey, which will contain your postman api key.
  7. Run eval(globals.LOAD) in any pre-request, then LOAD(‘all’)
  8. After that, you can call any script saved this way just by using eval(environment.yourRequestScriptsName).

While this is still using the old, typical solution of cluttering up the environment file, it allows you to edit your scripts in an actual editor, without having to copy and paste them in and out of the environment. LOAD only really needs to be run once at the beginning of a run, just to pick up any changes made to your library.

If anyone has any questions, tips, or improvements, please let me know! I’m still new to javascript and have a lot to learn.

5 Likes

Nice work @akaczinski!

If I understand correctly, is the main aim here to ease sharing of common snippets between workspaces since global/environment variable scope is limited to a workspace?

It’s a clever approach to use the Postman API to fetch resources from one part of Postman for use in another!

Along the same lines as this, I put together a pre-request script and local server that allows you to pull in external modules not included in the sandbox:

I’m going to review your snippet a little more to see if there’s improvements I can make to the way I’m executing my script, and, perhaps you might like to flick through mine for the same.

1 Like

I really like your approach, your code is much neater than mine. I really dig the approach using the global variable require, makes it much more intuitive to use, it’s very clever! I think I might implement some of your methods, specifically using require and storing all packages in one variable.

I should probably go through mine again anyways and see if there’s a way to neaten it up or download all at once (as opposed to a bunch of requests). Just realized it is missing the Postman Scripts require at the top, somehow the post deleted it, will fix that in my op. Might be a way to use that to process the scripts without using eval, actually.

Only thing I can really think of is maybe adding an update check to the local server when pkgs returns installed, but I’m not really sure how important checking for updates would be.

1 Like

Wow I was going to post a use case of as a Tess developer I want to create a postman library so I can maintain common routines efficiently,

But I think hopefully you have answered it - will study and feedback!!!