mercredi 3 avril 2019

How to source a js script file using postman collections sdk

OVERVIEW

I am trying to use a separate script file using the postman collections sdk.

In the docs, it says you can reference the file location using src and point it relative to the script. Every variation I have tried has failed so far though:

'script.js'
'/script.js'
'./script.js'

If I use exec, it works fine though i.e when I open up postman and edit the folder, I can see my code where I want it (in the folder), so I know it is working in that regard.

COLLECTION.JS

// variables
var fs = require("fs"), // for IO
    Collection = require('postman-collection').Collection,
    Item = require('postman-collection').Item,
    ItemGroup = require('postman-collection').ItemGroup,
    Script = require('postman-collection').Script,
    myCollection,
    myScript;

// create the collection
myCollection = new Collection({
    "name": "collection",
    "id": "collection",
});

// add the folder
myCollection.items.add(new ItemGroup({
    "id": "folder",
    "name": "folder"
}));

// add requests to the search folder
myCollection.items.one("folder").items.add(new Item({
    "name": "request",
    "id": "request",
    "request": {
        "url": "https://postman-echo.com/",
        "method": "GET"
    }
}));

// add scripts to the search folder
myScript = new Script();
myScript.update({
    // exec: 'console.log("inline")'
    src: './script.js'
});

myCollection.items.one("folder").events.add({
    listen: "test",
    script: myScript
});

// export the collection
fs.writeFileSync("collection.postman_collection.json", JSON.stringify(myCollection, null, 2));

SCRIPT.JS (IN SAME FOLDER AS COLLECTION.JS)

console.log('from file');

QUESTION

How can I point to a separate file to bring in my js code? I am wondering if I need to use fs somehow to read the file in?

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire