-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathconverter-v21-to-v1.js
More file actions
132 lines (111 loc) · 4.68 KB
/
converter-v21-to-v1.js
File metadata and controls
132 lines (111 loc) · 4.68 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable object-shorthand */
var _ = require('lodash').noConflict(),
util = require('../../util'),
inherits = require('inherits'),
v2Common = require('../../common/v2'),
BaseBuilders = require('../v2.0.0/converter-v2-to-v1').Builders,
Builders;
inherits(Builders = function () {
Builders.super_.apply(this, arguments);
}, BaseBuilders);
_.assign(Builders.prototype, {
/**
* Converts arrays of v2.1 style auth params to their v1.0.0 equivalent objects.
*
* @param {Object} entity - A v2.1 compliant wrapped auth manifest.
* @param {?Object} options - The set of options for the current auth cleansing operation.
* @param {?Boolean} [options.includeNoauth=false] - When set to true, noauth is set to null.
* @returns {Object} - A v1 compliant set of auth helper attributes.
*/
auth: function (entity, options) {
return util.sanitizeAuthArray(entity, options);
}
});
module.exports = {
input: '2.1.0',
output: '1.0.0',
Builders: Builders,
/**
* Converts a single V2 item to a V1 request.
*
* @param {Object} request - The request to be converted.
* @param {Object} options - The set of options for request conversion.
* @param {Function} callback - The function to be invoked after conversion has completed.
*/
convertSingle: function (request, options, callback) {
var err,
converted,
clone = _.cloneDeep(request),
builders = new Builders(options);
try {
clone = v2Common.populateIds(clone);
converted = builders.request(clone);
}
catch (e) { err = e; }
if (callback) { return callback(err, converted); }
if (err) { throw err; }
return converted;
},
/**
* Converts a single V2 item to a V1 request.
*
* @param {Object} response - The response to be converted.
* @param {Object} options - The set of options for request conversion.
* @param {Function} callback - The function to be invoked after conversion has completed.
*/
convertResponse: function (response, options, callback) {
var builders = new Builders(options),
converted,
err;
try { converted = builders.response(_.cloneDeep(response)); }
catch (e) { err = e; }
if (callback) { return callback(err, converted); }
if (err) { throw err; }
return converted;
},
/**
* Converts a V2 collection to a V1 collection (performs ID replacement, etc as necessary).
*
* @param {Object} collection - The collection to be converted.
* @param {Object} options - The set of options for request conversion.
* @param {Function} callback - The function to be invoked after conversion has completed.
*/
convert: function (collection, options, callback) {
collection = _.cloneDeep(collection);
var auth,
events,
variables,
builders = new Builders(options),
authOptions = { excludeNoauth: true },
units = ['order', 'folders_order', 'folders', 'requests'],
varOpts = options && { fallback: options.env, retainIds: options.retainIds },
id = _.get(collection, 'info._postman_id') || _.get(collection, 'info.id'),
info = collection && collection.info,
allowAuthReset = _.get(options, 'allowFieldResets', []).includes('auth'),
newCollection = {
id: id && options && options.retainIds ? id : util.uid(),
name: info && info.name
};
// ensure that each item has an id
collection = v2Common.populateIds(collection);
try {
// eslint-disable-next-line max-len
newCollection.description = builders.description(info && info.description);
if(allowAuthReset && builders.auth(collection, authOptions) === null){
newCollection.auth = builders.auth(collection, authOptions)
} else (auth = builders.auth(collection, authOptions)) && (newCollection.auth = auth);
(events = builders.events(collection)) && (newCollection.events = events);
(variables = builders.variables(collection, varOpts)) && (newCollection.variables = variables);
util.addProtocolProfileBehavior(collection, newCollection);
units.forEach(function (unit) {
newCollection[unit] = builders[unit](collection);
});
}
catch (e) {
if (callback) { return callback(e, null); }
throw e;
}
if (callback) { return callback(null, newCollection); }
return newCollection;
}
};