-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add convenience methods to the framework http component and…
… refactor the url lib methods Add convenience methods 'buildQueryString' and 'getClientInfo' to the framework http component.
- Loading branch information
1 parent
22ba496
commit 6fd0a1b
Showing
3 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
buildQueryString, | ||
getClientInfo, | ||
}; | ||
|
||
function buildQueryString(obj) { | ||
const strBuilder = []; | ||
|
||
if(typeof obj !== "object" || !obj) { | ||
return ""; | ||
} | ||
|
||
if(typeof obj === "string" || typeof obj === "number") { | ||
return obj; | ||
} | ||
|
||
for (const [prop, value] of Object.entries(obj)) { | ||
if (Object.prototype.hasOwnProperty.call(obj, prop)) { | ||
strBuilder.push(encodeURIComponent(prop) + "=" + encodeURIComponent(value)); | ||
} | ||
} | ||
|
||
return strBuilder.join("&"); | ||
} | ||
|
||
function getClientInfo(req) { | ||
return { | ||
ipAddress: req.ip, | ||
userAgent: req.headers["user-agent"], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters