forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
42 lines (32 loc) · 1.12 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
/* eslint-disable import/no-extraneous-dependencies */
const request = require('request');
function http(url, qs) {
const options = {
url, qs, json: true,
};
return new Promise((resolve, reject) => {
request(options, (err, resp) => {
if (err) {
console.log(err);
return reject({ err });
}
if (resp.body.status !== 'OK') {
console.log(resp.body.status);
return reject({ err: resp.body.status });
}
return resolve(resp.body);
});
});
}
function locationFromAddress(params) {
if (!params.address) return Promise.reject('Missing mandatory property: address');
return http('https://maps.googleapis.com/maps/api/geocode/json', { address: params.address });
}
function sunriseSunset(params) {
if (!params.lat) return Promise.reject('Missing mandatory property: lat');
if (!params.lng) return Promise.reject('Missing mandatory property: lng');
return http('http://api.sunrise-sunset.org/json', { lat: params.lat, lng: params.lng });
}
module.exports.locationFromAddress = locationFromAddress;
module.exports.sunriseSunset = sunriseSunset;