-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (52 loc) · 1.39 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var request = require('request-promise');
var APOD_ENDPOINT = 'https://api.nasa.gov/planetary/apod';
var DEFAULT_PARAMS = {
api_key: 'DEMO_KEY',
concept_tags: true
};
/**
*
* @param date
*/
var apod = function (date) {
return _request(DEFAULT_PARAMS.api_key, DEFAULT_PARAMS.concept_tags, date);
};
/**
*
* @param args
* @returns {Function}
* @constructor
*/
apod.Client = function (args) {
var client = function(date) {
return _request(that.apiKey, that.conceptTags, date);
};
args = args || {};
client.apiKey = args.apiKey || DEFAULT_PARAMS.api_key;
client.conceptTags = args.conceptTags != undefined && args.conceptTags != null ?
args.conceptTags :
DEFAULT_PARAMS.concept_tags;
client.__proto__ = this.__proto__;
var that = client;
return client;
};
_request = function (apiKey, conceptTags, date) {
var qs = {
api_key: apiKey,
concept_tags: conceptTags
};
if (date) {
var isoDate = date.toISOString();
qs.date = isoDate.substring(0, isoDate.indexOf('T'));
}
return request({url: APOD_ENDPOINT, qs: qs}).then(function (body) {
// Handle 200 responses that are actually errors.
var parsed = JSON.parse(body);
if (parsed.error) {
throw new Error(parsed.error);
} else {
return parsed;
}
});
};
module.exports = apod;