Skip to content

Instantly share code, notes, and snippets.

@codeami
Forked from prashantagarwal/ReuseRequest.js
Created July 19, 2022 12:22
Show Gist options
  • Save codeami/06480211d49585f5f2fdefd62faff999 to your computer and use it in GitHub Desktop.
Save codeami/06480211d49585f5f2fdefd62faff999 to your computer and use it in GitHub Desktop.

Revisions

  1. @prashantagarwal prashantagarwal renamed this gist May 4, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @prashantagarwal prashantagarwal created this gist May 4, 2020.
    104 changes: 104 additions & 0 deletions .js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    function executeReferencedRequest({ collectionUUID, requestName }, callback) {
    var _ = require('lodash');

    //Please refer Postman docs on how to generate an API KEY https://go.pstmn.io/docs-pro-api-intro
    const API_KEY = "<API-KEY>";

    /*
    Here we are making an API call to Postman (Refer: https://api.getpostman.com) using pm.sendRequest from the script,
    to fetch the collection from which we need to reuse the request.
    */
    pm.sendRequest({
    "method": "GET",
    "header": [{
    "key": "x-api-key",
    "value": API_KEY,
    "type": "text"
    }],
    "url": {
    "raw": `https://api.getpostman.com/collections/${collectionUUID}`,
    "protocol": "https",
    "host": [
    "api",
    "getpostman",
    "com"
    ],
    "path": [
    "collections",
    collectionUUID
    ]
    }
    }, function (err, response) {
    if(err) {
    console.error("Unable to fetch the referenced request", err);
    callback && callback(err, null);
    return;
    }
    let collection = response.json().collection;

    /*
    This function will find a given request in a collection using request name.
    It will also inherit Authorization and Scripts from its parent chaing accordingly.
    */
    function processCollectionRequest(tree, identifier) {
    if(tree.name === identifier){
    return tree;
    }
    else if (!_.isEmpty(tree.item)){
    var result;
    for(let i = 0; !result && i < tree.item.length; i++) {
    result = processCollectionRequest(tree.item[i], identifier);

    if(result) {

    // Extend prerequest and test scripts with collection and folder level scripts
    let nodePreRequestScript = _.find(result.event, {listen: "prerequest"}),
    parentPreRequestScript = _.find(tree.event, {listen: "prerequest"}),
    nodeTestScript = _.find(result.event, {listen: "test"}),
    parentTestScript = _.find(tree.event, {listen: "test"});

    nodePreRequestScript ?
    (nodePreRequestScript.script.exec = _.concat(
    parentPreRequestScript && parentPreRequestScript.script.exec,
    nodePreRequestScript.script.exec
    )) :
    (nodePreRequestScript = parentPreRequestScript);

    nodeTestScript ?
    (nodeTestScript.script.exec = _.concat(
    parentTestScript && parentTestScript.script.exec,
    nodeTestScript.script.exec
    )) :
    (nodeTestScript = parentTestScript);

    result.event = _.compact(_.concat(nodePreRequestScript, nodeTestScript));

    //Inherit authorization from parent chain
    if(!result.request.auth && tree.auth) {
    result.request.auth = tree.auth
    }
    }
    }
    return result;
    }
    return;
    }

    //Handle any collection level variables in the referenced collection
    _.forEach(collection.variables, function (variable) {
    pm.collectionVariables.set(variable.key, variable.value);
    });

    let referencedRequest = processCollectionRequest(collection, requestName),
    preRequestScript = _.find(referencedRequest.event, {listen: "prerequest"}),
    testScript = _.find(referencedRequest.event, {listen: "test"});


    // Store pre-request script, test script & request as local variables so that they can be used later
    pm.variables.set("referencedPreRequestScript", preRequestScript && preRequestScript.script.exec.join("\n"));
    pm.variables.set("referencedTestScript", testScript && testScript.script.exec.join("\n"));
    pm.variables.set("referencedRequest", referencedRequest && referencedRequest.request);

    callback && callback(null, collection);
    });
    }