Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove postman-collection dependency in codegens #515

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions codegens/http/lib/code-http-converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
let utils = require('./util'),
_ = require('./lodash'),
sdk = require('postman-collection');
_ = require('./lodash');

/**
* Used in order to get additional options for generation of C# code snippet (i.e. Include Boilerplate code)
Expand Down Expand Up @@ -33,14 +32,15 @@ function convert (request, options, callback) {
url, host, path, query, body, headers;
options = utils.sanitizeOptions(options, getOptions());

url = sdk.Url.parse(request.url.toString());
host = url.host ? url.host.join('.') : '';
url = request.url;
host = url.host ? utils.getHost(url) : '';
host += url.port ? ':' + url.port : '';
path = url.path ? '/' + url.path.join('/') : '/';
query = url.query ? _.reduce(url.query, (accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
}, []) : [];
path = utils.getPath(url);
query = url.query ? _.filter(url.query, (query) => { return !query.disabled; })
.reduce((accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
}, []) : [];

if (query.length > 0) {
query = '?' + query.join('&');
Expand Down
56 changes: 55 additions & 1 deletion codegens/http/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,63 @@ function addFormParam (array, key, type, val, disabled, contentType) {
}
}

/**
* @param {Object} url - The Postman URL object
* @returns {String}
*
* Remove protocol from url if present
*/
function getHostWithoutProtocol (url) {
return url.host.join('.')
.replace(/^https?:\/\//, '');
}

/**
* @param {Object} url - The Postman URL object
* @returns {String}
*
* Return host from the URL
*/
function getHost (url) {
return getHostWithoutProtocol(url).split('/')[0];
}

/**
* @param {Object} url - The Postman URL object
* @returns {String}
*
* Return the path from a URL
*/
function getPath (url) {
var segments = [];
// Sometimes url.host contains protocol as well as path
// Extract that here
if (getHost(url).length !== url.host.join('.').length) {
url.path = getHostWithoutProtocol(url).split('/').slice(1).concat(url.path);
}
if (url.path) {
segments = _.reduce(url.path, function (res, segment) {
var variable;

// check if the segment has path variable prefix followed by the variable name.
if (segment.length > 1 && segment[0] === ':') {
variable = url.variables.one(segment.slice(1)); // remove path variable prefix.
}

variable = variable && variable.valueOf && variable.valueOf();
res.push(typeof variable === 'string' ? variable : segment);
return res;
}, []);
}

return '/' + segments.join('/'); // add leading slash
}

module.exports = {
getHeaders: getHeaders,
getBody: getBody,
sanitizeOptions: sanitizeOptions,
addFormParam: addFormParam
addFormParam: addFormParam,
getPath: getPath,
getHost: getHost
};
Loading