Skip to content

Commit fae8a75

Browse files
authored
Merge pull request #798 from postmanlabs/feature/update-postman-collection-usage
Updated "postman-collection" usage to be specific to part of module being used.
2 parents 0427866 + b15aaee commit fae8a75

File tree

8 files changed

+71
-63
lines changed

8 files changed

+71
-63
lines changed

lib/schemaUtils.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
* This file contains util functions that need OAS-awareness
33
* utils.js contains other util functions
44
*/
5-
6-
const { ParseError } = require('./common/ParseError.js');
7-
85
const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'),
96
{ getConcreteSchemaUtils, isSwagger, validateSupportedVersion } = require('./common/versionUtils.js'),
107
async = require('async'),
11-
sdk = require('postman-collection'),
8+
{ Variable } = require('postman-collection/lib/collection/variable'),
9+
{ QueryParam } = require('postman-collection/lib/collection/query-param'),
10+
{ Header } = require('postman-collection/lib/collection/header'),
11+
{ ItemGroup } = require('postman-collection/lib/collection/item-group'),
12+
{ Item } = require('postman-collection/lib/collection/item'),
13+
{ FormParam } = require('postman-collection/lib/collection/form-param'),
14+
{ RequestAuth } = require('postman-collection/lib/collection/request-auth'),
15+
{ Response } = require('postman-collection/lib/collection/response'),
16+
{ RequestBody } = require('postman-collection/lib/collection/request-body'),
1217
schemaFaker = require('../assets/json-schema-faker.js'),
1318
deref = require('./deref.js'),
1419
_ = require('lodash'),
@@ -20,6 +25,7 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
2025
{ validateSchema } = require('./ajValidation/ajvValidation'),
2126
inputValidation = require('./30XUtils/inputValidation'),
2227
traverseUtility = require('traverse'),
28+
{ ParseError } = require('./common/ParseError.js'),
2329
SCHEMA_FORMATS = {
2430
DEFAULT: 'default', // used for non-request-body data and json
2531
XML: 'xml' // used for request-body XMLs
@@ -511,15 +517,15 @@ module.exports = {
511517
if (serverVariables) {
512518
_.forOwn(serverVariables, (value, key) => {
513519
let description = this.getParameterDescription(value);
514-
variables.push(new sdk.Variable({
520+
variables.push(new Variable({
515521
key: key,
516522
value: value.default || '',
517523
description: description
518524
}));
519525
});
520526
}
521527
if (keyName) {
522-
variables.push(new sdk.Variable({
528+
variables.push(new Variable({
523529
key: keyName,
524530
value: serverUrl,
525531
type: 'string'
@@ -739,7 +745,7 @@ module.exports = {
739745
addCollectionItemsFromWebhooks: function(spec, generatedStore, components, options, schemaCache) {
740746
let webhooksObj = this.generateTrieFromPaths(spec, options, true),
741747
webhooksTree = webhooksObj.tree,
742-
webhooksFolder = new sdk.ItemGroup({ name: 'Webhooks' }),
748+
webhooksFolder = new ItemGroup({ name: 'Webhooks' }),
743749
variableStore = {},
744750
webhooksVariables = [];
745751

@@ -752,7 +758,7 @@ module.exports = {
752758
webhooksTree.root.children.hasOwnProperty(child) &&
753759
webhooksTree.root.children[child].requestCount > 0
754760
) {
755-
webhooksVariables.push(new sdk.Variable({
761+
webhooksVariables.push(new Variable({
756762
key: this.cleanWebhookName(child),
757763
value: '/',
758764
type: 'string'
@@ -838,7 +844,7 @@ module.exports = {
838844
// variableStore contains all the kinds of variable created.
839845
// Add only the variables with type 'collection' to generatedStore.collection.variables
840846
if (variableStore[key].type === 'collection') {
841-
const collectionVar = new sdk.Variable(variableStore[key]);
847+
const collectionVar = new Variable(variableStore[key]);
842848
generatedStore.collection.variables.add(collectionVar);
843849
}
844850
}
@@ -961,7 +967,7 @@ module.exports = {
961967
// Add all folders created from tags and corresponding operations
962968
// Iterate from bottom to top order to maintain tag order in spec
963969
_.forEachRight(tagFolders, (tagFolder, tagName) => {
964-
var itemGroup = new sdk.ItemGroup({
970+
var itemGroup = new ItemGroup({
965971
name: tagName,
966972
description: tagFolder.description
967973
});
@@ -981,7 +987,7 @@ module.exports = {
981987
// Add only the variables with type 'collection' to generatedStore.collection.variables
982988
_.forEach(variableStore, (variable) => {
983989
if (variable.type === 'collection') {
984-
const collectionVar = new sdk.Variable(variable);
990+
const collectionVar = new Variable(variable);
985991
generatedStore.collection.variables.add(collectionVar);
986992
}
987993
});
@@ -997,7 +1003,7 @@ module.exports = {
9971003
* resolve references while generating params.
9981004
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
9991005
* @param {object} schemaCache - object storing schemaFaker and schmeResolution caches
1000-
* @returns {Array<object>} returns an array of sdk.Variable
1006+
* @returns {Array<object>} returns an array of Collection SDK Variable
10011007
*/
10021008
convertPathVariables: function(type, providedPathVars, commonPathVars, components, options, schemaCache) {
10031009
var variables = [];
@@ -1067,7 +1073,7 @@ module.exports = {
10671073
if (resource.requestCount > 1) {
10681074
// only return a Postman folder if this folder has>1 children in its subtree
10691075
// otherwise we can end up with 10 levels of folders with 1 request in the end
1070-
itemGroup = new sdk.ItemGroup({
1076+
itemGroup = new ItemGroup({
10711077
name: resource.name
10721078
// TODO: have to add auth here (but first, auth to be put into the openapi tree)
10731079
});
@@ -1308,9 +1314,9 @@ module.exports = {
13081314
*/
13091315
generateSdkParam: function (param, location) {
13101316
const sdkElementMap = {
1311-
'query': sdk.QueryParam,
1312-
'header': sdk.Header,
1313-
'path': sdk.Variable
1317+
'query': QueryParam,
1318+
'header': Header,
1319+
'path': Variable
13141320
};
13151321

13161322
let generatedParam = {
@@ -1913,7 +1919,7 @@ module.exports = {
19131919
convertedHeader = _.get(this.convertParamsWithStyle(header, fakeData, parameterSource,
19141920
components, schemaCache, options), '[0]');
19151921

1916-
reqHeader = new sdk.Header(convertedHeader);
1922+
reqHeader = new Header(convertedHeader);
19171923
reqHeader.description = this.getParameterDescription(header);
19181924

19191925
return reqHeader;
@@ -1936,7 +1942,7 @@ module.exports = {
19361942
originalParam,
19371943
paramArray = [],
19381944
updateOptions = {},
1939-
reqBody = new sdk.RequestBody(),
1945+
reqBody = new RequestBody(),
19401946
contentHeader,
19411947
contentTypes = {},
19421948
rDataMode,
@@ -2021,7 +2027,7 @@ module.exports = {
20212027
};
20222028

20232029
// add a content type header for each media type for the request body
2024-
contentHeader = new sdk.Header({
2030+
contentHeader = new Header({
20252031
key: 'Content-Type',
20262032
value: URLENCODED
20272033
});
@@ -2088,14 +2094,14 @@ module.exports = {
20882094
originalParam.type === 'string' &&
20892095
originalParam.format === 'binary'
20902096
) {
2091-
param = new sdk.FormParam({
2097+
param = new FormParam({
20922098
key: key,
20932099
value: '',
20942100
type: 'file'
20952101
});
20962102
}
20972103
else {
2098-
param = new sdk.FormParam({
2104+
param = new FormParam({
20992105
key: key,
21002106
value: value,
21012107
type: 'text'
@@ -2112,7 +2118,7 @@ module.exports = {
21122118
formdata: paramArray
21132119
};
21142120
// add a content type header for the pertaining media type
2115-
contentHeader = new sdk.Header({
2121+
contentHeader = new Header({
21162122
key: 'Content-Type',
21172123
value: FORM_DATA
21182124
});
@@ -2177,7 +2183,7 @@ module.exports = {
21772183
};
21782184
}
21792185

2180-
contentHeader = new sdk.Header({
2186+
contentHeader = new Header({
21812187
key: 'Content-Type',
21822188
value: bodyType
21832189
});
@@ -2246,7 +2252,7 @@ module.exports = {
22462252
responseMediaTypes = _.keys(response.content);
22472253

22482254
if (responseMediaTypes.length > 0) {
2249-
let acceptHeader = new sdk.Header({
2255+
let acceptHeader = new Header({
22502256
key: 'Accept',
22512257
value: responseMediaTypes[0]
22522258
});
@@ -2256,7 +2262,7 @@ module.exports = {
22562262
}
22572263
}
22582264

2259-
sdkResponse = new sdk.Response({
2265+
sdkResponse = new Response({
22602266
name: response.description,
22612267
code: code || 500,
22622268
header: responseHeaders,
@@ -2654,7 +2660,7 @@ module.exports = {
26542660
}
26552661

26562662
// creating the request object
2657-
item = new sdk.Item({
2663+
item = new Item({
26582664
name: reqName,
26592665
request: {
26602666
description: operation.description,
@@ -2672,7 +2678,7 @@ module.exports = {
26722678
};
26732679

26742680
thisAuthObject[authMap[authMeta.currentHelper]] = authMeta.helperAttributes;
2675-
item.request.auth = new sdk.RequestAuth(thisAuthObject);
2681+
item.request.auth = new RequestAuth(thisAuthObject);
26762682
}
26772683
else {
26782684
item.request.auth = authHelper;

lib/schemapack.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const { getConcreteSchemaUtils } = require('./common/versionUtils.js'),
66
Ajv = require('ajv'),
77
addFormats = require('ajv-formats'),
88
async = require('async'),
9-
sdk = require('postman-collection'),
9+
{ Collection } = require('postman-collection/lib/collection/collection'),
10+
{ Url } = require('postman-collection/lib/collection/url'),
1011
OasResolverOptions = {
1112
resolve: true, // Resolve external references
1213
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
@@ -340,7 +341,7 @@ class SchemaPack {
340341

341342
// Creating a new instance of a Postman collection
342343
// All generated folders and requests will go inside this
343-
generatedStore.collection = new sdk.Collection({
344+
generatedStore.collection = new Collection({
344345
info: {
345346
name: utils.getCollectionName(_.get(openapi, 'info.title'))
346347
}
@@ -530,7 +531,7 @@ class SchemaPack {
530531
});
531532

532533
// SDK URL object. Get raw string representation.
533-
requestUrl = (new sdk.Url(requestUrl)).toString();
534+
requestUrl = (new Url(requestUrl)).toString();
534535
}
535536

536537
// 1. Look at transaction.request.URL + method, and find matching request from schema

libV2/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable one-var */
22
const _ = require('lodash'),
3-
sdk = require('postman-collection'),
3+
{ Collection } = require('postman-collection/lib/collection/collection'),
44
GraphLib = require('graphlib'),
55
generateSkeletonTreeFromOpenAPI = require('./helpers/collection/generateSkeletionTreeFromOpenAPI'),
66
generateCollectionFromOpenAPI = require('./helpers/collection/generateCollectionFromOpenAPI'),
@@ -45,7 +45,7 @@ module.exports = {
4545
case 'collection': {
4646
// dummy collection to be generated.
4747
const { data, variables } = generateCollectionFromOpenAPI(context, node);
48-
collection = new sdk.Collection(data);
48+
collection = new Collection(data);
4949

5050
collection = collection.toJSON();
5151

libV2/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const sdk = require('postman-collection'),
2-
_ = require('lodash'),
1+
const _ = require('lodash'),
2+
{ Item } = require('postman-collection/lib/collection/item'),
3+
{ Response } = require('postman-collection/lib/collection/response'),
34

45
// This is the default collection name if one can't be inferred from the OpenAPI spec
56
COLLECTION_NAME = 'Imported from OpenAPI',
@@ -30,7 +31,7 @@ const sdk = require('postman-collection'),
3031
response.code = response.code.replace(/X|x/g, '0');
3132
response.code = response.code === 'default' ? 500 : _.toSafeInteger(response.code);
3233

33-
let sdkResponse = new sdk.Response({
34+
let sdkResponse = new Response({
3435
name: response.name,
3536
code: response.code,
3637
header: response.headers,
@@ -50,7 +51,7 @@ const sdk = require('postman-collection'),
5051
return sdkResponse;
5152
},
5253
generateRequestItemObject = (requestObject) => {
53-
const requestItem = new sdk.Item(requestObject),
54+
const requestItem = new Item(requestObject),
5455
queryParams = _.get(requestObject, 'request.params.queryParams'),
5556
pathParams = _.get(requestObject, 'request.params.pathParams', []),
5657
headers = _.get(requestObject, 'request.headers', []),

libV2/validationUtils.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// TODO: REMOVE THIS ☝🏻
33

44
const _ = require('lodash'),
5-
sdk = require('postman-collection'),
5+
{ Header } = require('postman-collection/lib/collection/header'),
6+
{ QueryParam } = require('postman-collection/lib/collection/query-param'),
7+
{ Url } = require('postman-collection/lib/collection/url'),
8+
{ Variable } = require('postman-collection/lib/collection/variable'),
69
async = require('async'),
710
crypto = require('crypto'),
811
schemaFaker = require('../assets/json-schema-faker.js'),
@@ -853,9 +856,9 @@ function checkContentTypeHeader (headers, transactionPathPrefix, schemaPathPrefi
853856
*/
854857
function generateSdkParam (param, location) {
855858
const sdkElementMap = {
856-
'query': sdk.QueryParam,
857-
'header': sdk.Header,
858-
'path': sdk.Variable
859+
'query': QueryParam,
860+
'header': Header,
861+
'path': Variable
859862
};
860863

861864
let generatedParam = {
@@ -1012,15 +1015,15 @@ function convertToPmCollectionVariables (serverVariables, keyName, serverUrl = '
10121015
if (serverVariables) {
10131016
_.forOwn(serverVariables, (value, key) => {
10141017
let description = getParameterDescription(value);
1015-
variables.push(new sdk.Variable({
1018+
variables.push(new Variable({
10161019
key: key,
10171020
value: value.default || '',
10181021
description: description
10191022
}));
10201023
});
10211024
}
10221025
if (keyName) {
1023-
variables.push(new sdk.Variable({
1026+
variables.push(new Variable({
10241027
key: keyName,
10251028
value: serverUrl,
10261029
type: 'string'
@@ -2535,7 +2538,7 @@ module.exports = {
25352538
queryParams = [...(requestUrl.query || [])];
25362539

25372540
// SDK URL object. Get raw string representation.
2538-
requestUrl = (new sdk.Url(requestUrl)).toString();
2541+
requestUrl = (new Url(requestUrl)).toString();
25392542
}
25402543

25412544
// 1. Look at transaction.request.URL + method, and find matching request from schema

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
"object-hash": "3.0.0",
129129
"graphlib": "2.1.8",
130130
"path-browserify": "1.0.1",
131-
"postman-collection": "4.2.1",
131+
"postman-collection": "^4.4.0",
132132
"swagger2openapi": "7.0.8",
133133
"traverse": "0.6.6",
134134
"yaml": "1.10.2"

0 commit comments

Comments
 (0)