allow AsyncHttpRequest to parse j...
son on it's own
This commit is contained in:
parent
8dc931eede
commit
07dcc8f825
3 changed files with 16 additions and 4 deletions
|
@ -2,6 +2,7 @@ const osu = require("osu-packet"),
|
||||||
User = require("./User.js"),
|
User = require("./User.js"),
|
||||||
{ v4: uuid } = require('uuid'),
|
{ v4: uuid } = require('uuid'),
|
||||||
ahttp = require("./util/AsyncHttpRequest.js"),
|
ahttp = require("./util/AsyncHttpRequest.js"),
|
||||||
|
RequestType = require("./util/RequestType.json"),
|
||||||
|
|
||||||
getUserByUsername = require("./util/getUserByUsername.js"),
|
getUserByUsername = require("./util/getUserByUsername.js"),
|
||||||
getUserByToken = require("./util/getUserByToken.js"),
|
getUserByToken = require("./util/getUserByToken.js"),
|
||||||
|
@ -41,7 +42,7 @@ module.exports = async function(req, res, loginInfo) {
|
||||||
userLocation = [0, 0];
|
userLocation = [0, 0];
|
||||||
} else {
|
} else {
|
||||||
// Get user's location using zxq
|
// Get user's location using zxq
|
||||||
userLocationData = JSON.parse(await ahttp(`http://ip.zxq.co/${requestIP}`));
|
userLocationData = await ahttp(`http://ip.zxq.co/${requestIP}`, RequestType.JSON);
|
||||||
userLocation = userLocationData.loc.split(",");
|
userLocation = userLocationData.loc.split(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
const request = require("request");
|
const request = require("request"), RequestType = require("./RequestType.json");
|
||||||
|
|
||||||
module.exports = async function(url) {
|
const functionMap = {
|
||||||
|
0: (resolve, body) => resolve(body),
|
||||||
|
1: (resolve, body) => resolve(JSON.parse(body)),
|
||||||
|
2: null
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = async function(url, reqType = RequestType.Text) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request(url, (err, res, body) => {
|
request(url, (err, res, body) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
else resolve(body);
|
else functionMap[reqType](resolve, body);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
5
server/util/RequestType.json
Normal file
5
server/util/RequestType.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"Text": 0,
|
||||||
|
"JSON": 1,
|
||||||
|
"XML": 2
|
||||||
|
}
|
Loading…
Reference in a new issue