Skip to content

Commit 28b67b1

Browse files
authored
Add key packages in Script (#1356)
1 parent 13f7ebd commit 28b67b1

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed

CHANGELOG.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
develop:
1+
unreleased:
2+
new features:
3+
- GH-1356 Add new key `packages` to Script
24
chores:
35
- >-
46
GH-1357 Fixed a bug where invalid JSDoc prevented generating docs and

examples/collection-v2.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,22 @@
4242
"id": "my-global-script-1",
4343
"script": {
4444
"type": "text/javascript",
45-
"exec": "console.log(\"hello\");"
45+
"exec": [
46+
"const package1 = pm.require(\"package1\");",
47+
"console.log(\"hello\", package1);"
48+
],
49+
"packages": { "package1": { "id": "script-package-1" } }
4650
}
4751
},
4852
{
4953
"listen": "prerequest",
5054
"script": {
5155
"type": "text/javascript",
52-
"exec": "console.log(\"hello\");"
56+
"exec": [
57+
"const package1 = pm.require(\"package1\");",
58+
"console.log(\"hello\", package1);"
59+
],
60+
"packages": { "package1": { "id":"script-package-1" } }
5361
}
5462
}
5563
],

lib/collection/script.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var _ = require('../util').lodash,
66

77
SCRIPT_NEWLINE_PATTERN = /\r?\n/g;
88

9+
/**
10+
* A map of package names to the IDs of the packages
11+
*
12+
* @typedef {Object.<string, { id: string }>} Packages
13+
*/
14+
915
_.inherit((
1016

1117
/**
@@ -49,6 +55,7 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
4955
* @param {String} [options.type] Script type
5056
* @param {String} [options.src] Script source url
5157
* @param {String[]|String} [options.exec] Script to execute
58+
* @param {Packages} [options.packages] Packages required by the script
5259
*/
5360
update: function (options) {
5461
// no splitting is being done here, as string scripts are split right before assignment below anyway
@@ -62,6 +69,14 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
6269
* @type {string}
6370
*/
6471
this.type = options.type || 'text/javascript';
72+
73+
/**
74+
* The packages required by the script
75+
*
76+
* @type {Packages}
77+
*/
78+
this.packages = options.packages;
79+
6580
_.has(options, 'src') && (
6681

6782
/**

test/unit/event-list.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('EventList', function () {
1010
id: 'my-global-script-1',
1111
script: {
1212
type: 'text/javascript',
13-
exec: 'console.log("hello");'
13+
exec: 'console.log("hello");',
14+
packages: [{ id: 'script-package-1', name: 'package1' }]
1415
}
1516
}];
1617

@@ -35,7 +36,8 @@ describe('EventList', function () {
3536
script: {
3637
id: 'test-script-1',
3738
type: 'text/javascript',
38-
exec: 'console.log("hello");'
39+
exec: 'console.log("hello");',
40+
packages: { package1: { id: 'script-package-1' } }
3941
}
4042
}]),
4143
eventListJSON;
@@ -46,7 +48,8 @@ describe('EventList', function () {
4648
script: {
4749
id: 'test-script-1',
4850
type: 'text/javascript',
49-
exec: ['console.log("hello");']
51+
exec: ['console.log("hello");'],
52+
packages: { package1: { id: 'script-package-1' } }
5053
}
5154
}]);
5255

@@ -63,7 +66,8 @@ describe('EventList', function () {
6366
script: {
6467
id: 'test-script-1',
6568
type: 'text/javascript',
66-
exec: ['console.log("hello");']
69+
exec: ['console.log("hello");'],
70+
packages: { package1: { id: 'script-package-1' } }
6771
}
6872
});
6973

test/unit/event.test.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Event', function () {
1111
id: 'my-global-script-1',
1212
script: {
1313
type: 'text/javascript',
14-
exec: 'console.log("hello");'
14+
exec: 'console.log("hello");',
15+
packages: {}
1516
}
1617
};
1718

@@ -52,6 +53,7 @@ describe('Event', function () {
5253
expect(postmanEvent).to.have.property('script').that.is.an('object');
5354
expect(postmanEvent.script).to.have.property('type', 'text/javascript');
5455
expect(postmanEvent.script).to.have.property('exec').that.is.an('array');
56+
expect(postmanEvent.script).to.have.property('packages').that.is.an('object');
5557
});
5658
});
5759

@@ -120,7 +122,8 @@ describe('Event', function () {
120122
expect(eventJSON).to.have.property('script').that.has.property('id');
121123
expect(eventJSON.script).to.deep.include({
122124
type: 'text/javascript',
123-
exec: ['console.log("hello");']
125+
exec: ['console.log("hello");'],
126+
packages: {}
124127
});
125128
});
126129

@@ -132,7 +135,8 @@ describe('Event', function () {
132135
expect(beforeJSON).to.have.property('script').that.has.property('id');
133136
expect(beforeJSON.script).to.deep.include({
134137
type: 'text/javascript',
135-
exec: ['console.log("hello");']
138+
exec: ['console.log("hello");'],
139+
packages: {}
136140
});
137141

138142
event.update({ script: { id: 'my-new-script' } });
@@ -142,5 +146,26 @@ describe('Event', function () {
142146
expect(afterJSON.script).to.have.property('id', 'my-new-script');
143147
expect(afterJSON.script.exec).to.be.undefined;
144148
});
149+
150+
it('should not add packages key if not present', function () {
151+
var rawEvent = {
152+
listen: 'test',
153+
id: 'my-global-script-1',
154+
script: {
155+
type: 'text/javascript',
156+
exec: 'console.log("hello");'
157+
}
158+
},
159+
event = new Event(rawEvent),
160+
beforeJSON = event.toJSON(),
161+
afterJSON;
162+
163+
expect(beforeJSON.script).to.not.have.property('packages');
164+
165+
event.update({ script: { exec: 'console.log("updated");' } });
166+
afterJSON = event.toJSON();
167+
168+
expect(afterJSON.script).to.not.have.property('packages');
169+
});
145170
});
146171
});

test/unit/script.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Script', function () {
6464
var source = script.toSource();
6565

6666
expect(source).to.be.a('string');
67-
expect(source).to.equal(rawScript.exec);
67+
expect(source).to.equal(rawScript.exec.join('\n'));
6868
});
6969
});
7070
});
@@ -201,7 +201,7 @@ describe('Script', function () {
201201

202202
expect(jsonified).to.deep.include({
203203
type: rawScript.type,
204-
exec: rawScript.exec.split('\n')
204+
exec: rawScript.exec
205205
});
206206
expect(jsonified.src).to.eql(rawScript.src);
207207
});

types/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,13 @@ declare module "postman-collection" {
21462146
static createFromNode(response: any, cookies: any): any;
21472147
}
21482148

2149+
/**
2150+
* A map of package names to the IDs of the packages
2151+
*/
2152+
export type Packages = {
2153+
[key: string]: { id: string; };
2154+
};
2155+
21492156
/**
21502157
* Postman scripts that are executed upon events on a collection / request such as test and pre request.
21512158
* @param options - -
@@ -2162,13 +2169,19 @@ declare module "postman-collection" {
21622169
* @param [options.type] - Script type
21632170
* @param [options.src] - Script source url
21642171
* @param [options.exec] - Script to execute
2172+
* @param [options.packages] - Packages required by the script
21652173
*/
21662174
update(options?: {
21672175
type?: string;
21682176
src?: string;
21692177
exec?: string[] | string;
2178+
packages?: Packages;
21702179
}): void;
21712180
type: string;
2181+
/**
2182+
* The packages required by the script
2183+
*/
2184+
packages: Packages;
21722185
src: Url;
21732186
exec: string[];
21742187
/**

0 commit comments

Comments
 (0)