1- module . exports = {
1+ const _ = require ( './lodash' ) ;
2+
3+ const self = module . exports = {
24 /**
3- * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
4- * and trim input if required
5- *
6- * @param {String } inputString
7- * @param {Boolean } [trim] - indicates whether to trim string or not
8- * @returns {String }
9- */
5+ * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
6+ * and trim input if required
7+ *
8+ * @param {String } inputString
9+ * @param {Boolean } [trim] - indicates whether to trim string or not
10+ * @returns {String }
11+ */
1012 sanitize : function ( inputString , trim ) {
1113 if ( typeof inputString !== 'string' ) {
1214 return '' ;
@@ -20,13 +22,13 @@ module.exports = {
2022 } ,
2123
2224 /**
23- * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
24- * and trim input if required
25- *
26- * @param {String } inputString
27- * @param {Boolean } [trim] - indicates whether to trim string or not
28- * @returns {String }
29- */
25+ * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
26+ * and trim input if required
27+ *
28+ * @param {String } inputString
29+ * @param {Boolean } [trim] - indicates whether to trim string or not
30+ * @returns {String }
31+ */
3032 sanitizeMultiline : function ( inputString , trim ) {
3133 if ( typeof inputString !== 'string' ) {
3234 return '' ;
@@ -38,6 +40,90 @@ module.exports = {
3840
3941 } ,
4042
43+ /**
44+ *
45+ * @param {Object } urlObject The request sdk request.url object
46+ * @returns {String } The final string after parsing all the parameters of the url including
47+ * protocol, auth, host, port, path, query, hash
48+ * This will be used because the url.toString() method returned the URL with non encoded query string
49+ * and hence a manual call is made to getQueryString() method with encode option set as true.
50+ */
51+ getUrlStringfromUrlObject : function ( urlObject ) {
52+ var url = '' ;
53+ if ( ! urlObject ) {
54+ return url ;
55+ }
56+ if ( urlObject . protocol ) {
57+ url += ( urlObject . protocol . endsWith ( '://' ) ? urlObject . protocol : urlObject . protocol + '://' ) ;
58+ }
59+ if ( urlObject . auth && urlObject . auth . user ) {
60+ url = url + ( ( urlObject . auth . password ) ?
61+ urlObject . auth . user + ':' + urlObject . auth . password : urlObject . auth . user ) + '@' ;
62+ }
63+ if ( urlObject . host ) {
64+ url += urlObject . getHost ( ) ;
65+ }
66+ if ( urlObject . port ) {
67+ url += ':' + urlObject . port . toString ( ) ;
68+ }
69+ if ( urlObject . path ) {
70+ url += urlObject . getPath ( ) ;
71+ }
72+ if ( urlObject . query && urlObject . query . count ( ) ) {
73+ let queryString = self . getQueryString ( urlObject ) ;
74+ queryString && ( url += '?' + queryString ) ;
75+ }
76+ if ( urlObject . hash ) {
77+ url += '#' + urlObject . hash ;
78+ }
79+
80+ return self . sanitize ( url , false ) ;
81+ } ,
82+
83+ /**
84+ * @param {Object } urlObject
85+ * @returns {String }
86+ */
87+ getQueryString : function ( urlObject ) {
88+ let isFirstParam = true ,
89+ params = _ . get ( urlObject , 'query.members' ) ,
90+ result = '' ;
91+ if ( Array . isArray ( params ) ) {
92+ result = _ . reduce ( params , function ( result , param ) {
93+ if ( param . disabled === true ) {
94+ return result ;
95+ }
96+
97+ if ( isFirstParam ) {
98+ isFirstParam = false ;
99+ }
100+ else {
101+ result += '&' ;
102+ }
103+
104+ return result + self . encodeParam ( param . key ) + '=' + self . encodeParam ( param . value ) ;
105+ } , result ) ;
106+ }
107+
108+ return result ;
109+ } ,
110+
111+ /**
112+ * Encode param except the following characters- [,{,},],%
113+ *
114+ * @param {String } param
115+ * @returns {String }
116+ */
117+ encodeParam : function ( param ) {
118+ return encodeURIComponent ( param )
119+ . replace ( / % 5 B / g, '[' )
120+ . replace ( / % 7 B / g, '{' )
121+ . replace ( / % 5 D / g, ']' )
122+ . replace ( / % 7 D / g, '}' )
123+ . replace ( / % 2 5 / g, '%' )
124+ . replace ( / ' / g, '%27' ) ;
125+ } ,
126+
41127 /**
42128 * sanitizes input options
43129 *
0 commit comments