From fee1f53d2710de02ef6a7d0cd1b5f79a2e15e988 Mon Sep 17 00:00:00 2001 From: Utkarsh Maheshwari Date: Thu, 22 Feb 2024 11:41:45 +0530 Subject: [PATCH 1/3] Add key `packages` in Script --- examples/collection-v2.json | 12 ++++++++++-- lib/collection/script.js | 15 +++++++++++++++ test/unit/event-list.test.js | 12 ++++++++---- test/unit/event.test.js | 31 ++++++++++++++++++++++++++++--- test/unit/script.test.js | 4 ++-- 5 files changed, 63 insertions(+), 11 deletions(-) diff --git a/examples/collection-v2.json b/examples/collection-v2.json index 0186aa964..a05791446 100644 --- a/examples/collection-v2.json +++ b/examples/collection-v2.json @@ -42,14 +42,22 @@ "id": "my-global-script-1", "script": { "type": "text/javascript", - "exec": "console.log(\"hello\");" + "exec": [ + "const package1 = pm.require(\"package1\");", + "console.log(\"hello\", package1);" + ], + "packages": { "package1": { "id": "script-package-1" } } } }, { "listen": "prerequest", "script": { "type": "text/javascript", - "exec": "console.log(\"hello\");" + "exec": [ + "const package1 = pm.require(\"package1\");", + "console.log(\"hello\", package1);" + ], + "packages": { "package1": { "id":"script-package-1" } } } } ], diff --git a/lib/collection/script.js b/lib/collection/script.js index 8ec0f6775..45d6e1871 100644 --- a/lib/collection/script.js +++ b/lib/collection/script.js @@ -6,6 +6,12 @@ var _ = require('../util').lodash, SCRIPT_NEWLINE_PATTERN = /\r?\n/g; +/** + * A map of package names to the IDs of the packages + * + * @typedef {Object.} Packages + */ + _.inherit(( /** @@ -49,6 +55,7 @@ _.assign(Script.prototype, /** @lends Script.prototype */ { * @param {String} [options.type] Script type * @param {String} [options.src] Script source url * @param {String[]|String} [options.exec] Script to execute + * @param {Packages} [options.packages] Packages required by the script */ update: function (options) { // 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 */ { * @type {string} */ this.type = options.type || 'text/javascript'; + + /** + * The packages required by the script + * + * @type {Packages} + */ + this.packages = options.packages; + _.has(options, 'src') && ( /** diff --git a/test/unit/event-list.test.js b/test/unit/event-list.test.js index abe404a63..91b35d182 100644 --- a/test/unit/event-list.test.js +++ b/test/unit/event-list.test.js @@ -10,7 +10,8 @@ describe('EventList', function () { id: 'my-global-script-1', script: { type: 'text/javascript', - exec: 'console.log("hello");' + exec: 'console.log("hello");', + packages: [{ id: 'script-package-1', name: 'package1' }] } }]; @@ -35,7 +36,8 @@ describe('EventList', function () { script: { id: 'test-script-1', type: 'text/javascript', - exec: 'console.log("hello");' + exec: 'console.log("hello");', + packages: { package1: { id: 'script-package-1' } } } }]), eventListJSON; @@ -46,7 +48,8 @@ describe('EventList', function () { script: { id: 'test-script-1', type: 'text/javascript', - exec: ['console.log("hello");'] + exec: ['console.log("hello");'], + packages: { package1: { id: 'script-package-1' } } } }]); @@ -63,7 +66,8 @@ describe('EventList', function () { script: { id: 'test-script-1', type: 'text/javascript', - exec: ['console.log("hello");'] + exec: ['console.log("hello");'], + packages: { package1: { id: 'script-package-1' } } } }); diff --git a/test/unit/event.test.js b/test/unit/event.test.js index b830ae1c3..801fecc16 100644 --- a/test/unit/event.test.js +++ b/test/unit/event.test.js @@ -11,7 +11,8 @@ describe('Event', function () { id: 'my-global-script-1', script: { type: 'text/javascript', - exec: 'console.log("hello");' + exec: 'console.log("hello");', + packages: {} } }; @@ -52,6 +53,7 @@ describe('Event', function () { expect(postmanEvent).to.have.property('script').that.is.an('object'); expect(postmanEvent.script).to.have.property('type', 'text/javascript'); expect(postmanEvent.script).to.have.property('exec').that.is.an('array'); + expect(postmanEvent.script).to.have.property('packages').that.is.an('object'); }); }); @@ -120,7 +122,8 @@ describe('Event', function () { expect(eventJSON).to.have.property('script').that.has.property('id'); expect(eventJSON.script).to.deep.include({ type: 'text/javascript', - exec: ['console.log("hello");'] + exec: ['console.log("hello");'], + packages: {} }); }); @@ -132,7 +135,8 @@ describe('Event', function () { expect(beforeJSON).to.have.property('script').that.has.property('id'); expect(beforeJSON.script).to.deep.include({ type: 'text/javascript', - exec: ['console.log("hello");'] + exec: ['console.log("hello");'], + packages: {} }); event.update({ script: { id: 'my-new-script' } }); @@ -142,5 +146,26 @@ describe('Event', function () { expect(afterJSON.script).to.have.property('id', 'my-new-script'); expect(afterJSON.script.exec).to.be.undefined; }); + + it('should not add packages key if not present', function () { + var rawEvent = { + listen: 'test', + id: 'my-global-script-1', + script: { + type: 'text/javascript', + exec: 'console.log("hello");' + } + }, + event = new Event(rawEvent), + beforeJSON = event.toJSON(), + afterJSON; + + expect(beforeJSON.script).to.not.have.property('packages'); + + event.update({ script: { exec: 'console.log("updated");' } }); + afterJSON = event.toJSON(); + + expect(afterJSON.script).to.not.have.property('packages'); + }); }); }); diff --git a/test/unit/script.test.js b/test/unit/script.test.js index 85d05c8b4..478bd137d 100644 --- a/test/unit/script.test.js +++ b/test/unit/script.test.js @@ -64,7 +64,7 @@ describe('Script', function () { var source = script.toSource(); expect(source).to.be.a('string'); - expect(source).to.equal(rawScript.exec); + expect(source).to.equal(rawScript.exec.join('\n')); }); }); }); @@ -201,7 +201,7 @@ describe('Script', function () { expect(jsonified).to.deep.include({ type: rawScript.type, - exec: rawScript.exec.split('\n') + exec: rawScript.exec }); expect(jsonified.src).to.eql(rawScript.src); }); From 04c79c109e5b47c718fc86abe456a72f70b107b7 Mon Sep 17 00:00:00 2001 From: Utkarsh Maheshwari Date: Fri, 23 Feb 2024 11:48:41 +0530 Subject: [PATCH 2/3] Generate types for script.packages --- types/index.d.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 7541f1aa6..7f799c0da 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2146,6 +2146,13 @@ declare module "postman-collection" { static createFromNode(response: any, cookies: any): any; } + /** + * A map of package names to the IDs of the packages + */ + export type Packages = { + [key: string]: { id: string; }; + }; + /** * Postman scripts that are executed upon events on a collection / request such as test and pre request. * @param options - - @@ -2162,13 +2169,19 @@ declare module "postman-collection" { * @param [options.type] - Script type * @param [options.src] - Script source url * @param [options.exec] - Script to execute + * @param [options.packages] - Packages required by the script */ update(options?: { type?: string; src?: string; exec?: string[] | string; + packages?: Packages; }): void; type: string; + /** + * The packages required by the script + */ + packages: Packages; src: Url; exec: string[]; /** From 07b7308ecaa2ed1794724ca05694df39690556bb Mon Sep 17 00:00:00 2001 From: Utkarsh Maheshwari Date: Tue, 27 Feb 2024 17:37:49 +0530 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 500102bdb..ec450c7bf 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,4 +1,6 @@ -develop: +unreleased: + new features: + - GH-1356 Add new key `packages` to Script chores: - >- GH-1357 Fixed a bug where invalid JSDoc prevented generating docs and