forked from postmanlabs/postman-collection-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (85 loc) · 4.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var semver = require('semver'),
util = require('./util'),
converter = require('./converters'),
normalizers = require('./normalizers');
module.exports = {
/**
* Converts a Collection between different versions, based on the given input.
*
* @param {Object} collection - The collection object to be converted.
* @param {Object} options - The set of conversion options.
* @param {String} options.outputVersion - The version to convert to.
* @param {String} options.inputVersion - The version to convert from.
* @param {Function} callback - The function invoked to mark the completion of the conversion process.
*/
convert: function (collection, options, callback) {
if (!options.outputVersion || !semver.valid(options.outputVersion, true)) {
return callback(new Error('Output version not specified or invalid: ' + options.outputVersion));
}
if (!options.inputVersion || !semver.valid(options.inputVersion, true)) {
return callback(new Error('Input version not specified or invalid: ' + options.inputVersion));
}
return converter.convert(collection, options, callback);
},
normalize: normalizers.normalize,
normalizeSingle: normalizers.normalizeSingle,
normalizeResponse: normalizers.normalizeResponse,
/**
* Export the utilities
*/
util: util,
/**
* Checks whether the given object is a v1 collection
*
* @param {Object} object - The Object to check for v1 collection compliance.
* @returns {Boolean} - A boolean result indicating whether or not the passed object was a v1 collection.
*/
isv1: function (object) {
return Boolean(object.name && object.order && object.requests);
},
/**
* Checks whether the given object is a v2 collection
*
* @param {Object} object - The Object to check for v2 collection compliance.
* @returns {Boolean} - A boolean result indicating whether or not the passed object was a v2 collection.
*/
isv2: function (object) {
return Boolean(object && object.info && object.info.schema);
},
/**
* Converts a single V1 request to a V2 Item, or a V2 item to a single V1 request.
*
* @param {Object} object - A V1 request or a V2 item.
* @param {Object} options - The set of options for response conversion.
* @param {String} options.outputVersion - The version to convert to.
* @param {String} options.inputVersion - The version to convert from.
* @param {Function} callback - The function invoked to mark the completion of the conversion process.
*/
convertSingle: function (object, options, callback) {
if (!options.outputVersion || !semver.valid(options.outputVersion, true)) {
return callback(new Error('Output version not specified or invalid: ' + options.outputVersion));
}
if (!options.inputVersion || !semver.valid(options.inputVersion, true)) {
return callback(new Error('Input version not specified or invalid: ' + options.inputVersion));
}
return converter.convertSingle(object, options, callback);
},
/**
* Converts a single V1 request to a V2 Item, or a V2 item to a single V1 request.
*
* @param {Object} object - A V1 request or a V2 item.
* @param {Object} options - The set of options for response conversion.
* @param {String} options.outputVersion - The version to convert to.
* @param {String} options.inputVersion - The version to convert from.
* @param {Function} callback - The function invoked to mark the completion of the conversion process.
*/
convertResponse: function (object, options, callback) {
if (!options.outputVersion || !semver.valid(options.outputVersion, true)) {
return callback(new Error('Output version not specified or invalid: ' + options.outputVersion));
}
if (!options.inputVersion || !semver.valid(options.inputVersion, true)) {
return callback(new Error('Input version not specified or invalid: ' + options.inputVersion));
}
return converter.convertResponse(object, options, callback);
}
};