Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove dependency on postman-collection in nodejs-native codegen
  • Loading branch information
webholik committed Aug 3, 2021
commit 44dafbab5e69d6949066e52b308e1fde5b9a4ed9
19 changes: 10 additions & 9 deletions codegens/nodejs-native/lib/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const _ = require('./lodash'),
sdk = require('postman-collection'),
sanitizeOptions = require('./util').sanitizeOptions,
sanitize = require('./util').sanitize,
addFormParam = require('./util').addFormParam,

getHost = require('./util').getHost,
getPath = require('./util').getPath,
parseRequest = require('./parseRequest');
var self;

Expand Down Expand Up @@ -132,13 +132,14 @@ function makeSnippet (request, indentString, options) {
}


url = sdk.Url.parse(request.url.toString());
host = url.host ? url.host.join('.') : '';
path = url.path ? '/' + url.path.join('/') : '/';
query = url.query ? _.reduce(url.query, (accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
}, []) : [];
url = request.url;
host = url.host ? getHost(url) : '';
path = 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
264 changes: 162 additions & 102 deletions codegens/nodejs-native/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,177 @@
module.exports = {
/**
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
* and trim input if required
*
* @param {String} inputString
* @param {Boolean} [trim] - indicates whether to trim string or not
* @returns {String}
*/
sanitize: function (inputString, trim) {
if (typeof inputString !== 'string') {
return '';
const _ = require('./lodash');

/**
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
* and trim input if required
*
* @param {String} inputString
* @param {Boolean} [trim] - indicates whether to trim string or not
* @returns {String}
*/
function sanitize (inputString, trim) {
if (typeof inputString !== 'string') {
return '';
}
(trim) && (inputString = inputString.trim());
return inputString.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\n/g, '\\n');
}

/**
* sanitizes input options
*
* @param {Object} options - Options provided by the user
* @param {Array} optionsArray - options array received from getOptions function
*
* @returns {Object} - Sanitized options object
*/
function sanitizeOptions (options, optionsArray) {
var result = {},
defaultOptions = {},
id;
optionsArray.forEach((option) => {
defaultOptions[option.id] = {
default: option.default,
type: option.type
};
if (option.type === 'enum') {
defaultOptions[option.id].availableOptions = option.availableOptions;
}
(trim) && (inputString = inputString.trim());
return inputString.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\n/g, '\\n');
},
});

/**
* sanitizes input options
*
* @param {Object} options - Options provided by the user
* @param {Array} optionsArray - options array received from getOptions function
*
* @returns {Object} - Sanitized options object
*/
sanitizeOptions: function (options, optionsArray) {
var result = {},
defaultOptions = {},
id;
optionsArray.forEach((option) => {
defaultOptions[option.id] = {
default: option.default,
type: option.type
};
if (option.type === 'enum') {
defaultOptions[option.id].availableOptions = option.availableOptions;
for (id in options) {
if (options.hasOwnProperty(id)) {
if (defaultOptions[id] === undefined) {
continue;
}
});

for (id in options) {
if (options.hasOwnProperty(id)) {
if (defaultOptions[id] === undefined) {
continue;
}
switch (defaultOptions[id].type) {
case 'boolean':
if (typeof options[id] !== 'boolean') {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
case 'positiveInteger':
if (typeof options[id] !== 'number' || options[id] < 0) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
case 'enum':
if (!defaultOptions[id].availableOptions.includes(options[id])) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
default:
switch (defaultOptions[id].type) {
case 'boolean':
if (typeof options[id] !== 'boolean') {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
case 'positiveInteger':
if (typeof options[id] !== 'number' || options[id] < 0) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
}
break;
case 'enum':
if (!defaultOptions[id].availableOptions.includes(options[id])) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
default:
result[id] = options[id];
}
}
}

for (id in defaultOptions) {
if (defaultOptions.hasOwnProperty(id)) {
if (result[id] === undefined) {
result[id] = defaultOptions[id].default;
}
for (id in defaultOptions) {
if (defaultOptions.hasOwnProperty(id)) {
if (result[id] === undefined) {
result[id] = defaultOptions[id].default;
}
}
return result;
},
}
return result;
}

/**
/**
*
* @param {Array} array - form data array
* @param {String} key - key of form data param
* @param {String} type - type of form data param(file/text)
* @param {String} val - value/src property of form data param
* @param {String} disabled - Boolean denoting whether the param is disabled or not
* @param {String} contentType - content type header of the param
*
* Appends a single param to form data array
*/
function addFormParam (array, key, type, val, disabled, contentType) {
if (type === 'file') {
array.push({
key: key,
type: type,
src: val,
disabled: disabled,
contentType: contentType
});
}
else {
array.push({
key: key,
type: type,
value: val,
disabled: disabled,
contentType: 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}
*
* @param {Array} array - form data array
* @param {String} key - key of form data param
* @param {String} type - type of form data param(file/text)
* @param {String} val - value/src property of form data param
* @param {String} disabled - Boolean denoting whether the param is disabled or not
* @param {String} contentType - content type header of the param
* Return host from the URL
*/
function getHost (url) {
return getHostWithoutProtocol(url).split('/')[0];
}

/**
* @param {Object} url - The Postman URL object
* @returns {String}
*
* Appends a single param to form data array
* Return the path from a URL
*/
addFormParam: function (array, key, type, val, disabled, contentType) {
if (type === 'file') {
array.push({
key: key,
type: type,
src: val,
disabled: disabled,
contentType: contentType
});
}
else {
array.push({
key: key,
type: type,
value: val,
disabled: disabled,
contentType: contentType
});
}
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 = {
sanitize,
sanitizeOptions,
addFormParam,
getHost,
getPath
};
3 changes: 0 additions & 3 deletions codegens/nodejs-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
"author": "Postman Labs <[email protected]>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/nodejs-native",
"dependencies": {
"postman-collection": "3.6.11"
},
"devDependencies": {
"follow-redirects": "1.14.1"
},
Expand Down