Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getCache(req) {
const key = cacheKey(req);
const data = cache.get(key);
debug("getting cache key: " + key);
debug(data);
return data;
}
async function getWargamingData({ url, fields, cacheKey, parseDataCb }) {
let data = cache.get(cacheKey)
if(data === null){
data = await requestWargamingData(url, fields, parseDataCb);
cache.put(cacheKey, data, CACHE_EXPIRE)
}
return data;
}
router.get('/api/machines', function(req, res) {
var machines = cache.get('machines') || [];
res.json(machines);
});
router.get('/api/keys', function(req, res) {
var keys = cache.get('keys') || [];
res.json(keys);
});
handler: function(req, res) {
var cachedResult = cache.get('nest');
if (cachedResult) return res.json(cachedResult);
var url = NEST_API_URL + '/devices/thermostats/' +
this.secrets.device_id + '?auth=' +
this.secrets.access_token;
var requestOpts = { url: url, headers: { 'Accept': 'application/json' } };
request(requestOpts, function(err, response, body) {
if (err || response.statusCode != 200) return handleError(err, res);
var data = JSON.parse(body);
cache.put('nest', data, DEFAULT_CACHE_MSEC);
return res.json(data);
});
}
}
handler: function(req, res) {
var cachedResult = cache.get('mixcloud');
if (cachedResult) return res.json(cachedResult);
var url = 'http://api.mixcloud.com/' + this.me + '/feed/';
request({ url: url, headers: { 'User-Agent': this.me }}, function(err, response, body) {
if (err || response.statusCode != 200) return handleError(err, res);
var data = JSON.parse(body);
cache.put('mixcloud', data, DEFAULT_CACHE_MSEC);
return res.json(data);
});
}
}
handler: function(req, res) {
var cachedResult = cache.get('foursquare');
if (cachedResult) return res.json(cachedResult);
var accessToken = this.accessToken;
if (!accessToken) return handleError('You have not specified an access Token yet', res);
var fs = getFoursquare(req, this);
fs.Users.getCheckins('self', {}, accessToken, function(err, checkins) {
if (err) return handleError(err, res);
cache.put('foursquare', checkins, DEFAULT_CACHE_MSEC);
res.json(checkins);
});
}
},
handler: function(req, res) {
var cachedResult = cache.get('keybase');
if (cachedResult) return res.json(cachedResult);
var url = 'https://keybase.io/_/api/1.0/user/lookup.json?usernames=' + this.me;
request({ url: url, headers: { 'User-Agent': this.me }}, function(err, response, body) {
if (err || response.statusCode != 200) return handleError(err, res);
var data = JSON.parse(body);
cache.put('keybase', data.them, DEFAULT_CACHE_MSEC);
return res.json(data.them);
});
}
},
handler: function(req, res) {
var cachedResult = cache.get('github');
if (cachedResult) return res.json(cachedResult);
var url = 'https://api.github.com/users/' + this.me + '/events/public';
request({ url: url, headers: { 'User-Agent': this.me }}, function(err, response, body) {
if (err || response.statusCode != 200) return handleError(err, res);
var data = JSON.parse(body);
cache.put('github', data, DEFAULT_CACHE_MSEC);
return res.json(data);
});
}
}
return (request, response, next) => {
var key = '__express__' + request.originalUrl || request.url
var cached_response = mcache.get(key)
if (cached_response) {
response.send(cached_response)
return
} else {
response.sendResponse = response.send
response.send = (body) => {
mcache.put(key, body, duration);
response.sendResponse(body)
}
next()
}
}
}