-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
171 lines (143 loc) · 3.74 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
/* eslint-env node, browser */
const http = require('https');
const queryString = require('querystring');
const API_URL = 'https://api.apixu.com/';
const API_VERSION = 'v1';
const FORMAT = 'json';
const HTTP_TIMEOUT = 20000;
const DOC_WEATHER_CONDITIONS_URL =
'https://www.apixu.com/doc/Apixu_weather_conditions.';
const HTTP_STATUS_OK = 200;
const HTTP_STATUS_NOT_FOUND = 404;
const HTTP_STATUS_INTERNAL_SERVER_ERROR = 500;
const config = {
apikey: null,
};
class Apixu {
constructor(config) {
this.config = config;
}
conditions() {
const url = DOC_WEATHER_CONDITIONS_URL + FORMAT;
return request(url);
}
current(query) {
const params = {
key: this.config.apikey,
q: query,
};
const url = getUrl('current', params);
return request(url);
}
forecast(query, days, hour) {
let params = {
key: this.config.apikey,
q: query,
days: days,
};
if (hour !== undefined) {
params['hour'] = hour;
}
const url = getUrl('forecast', params);
return request(url);
}
history(query, since, until) {
if (!(since instanceof Date)) {
return new Promise((resolve, reject) => {
const error = new Error('Param \'since\' must be of Date type.');
reject(error);
});
}
if (until !== undefined && !(until instanceof Date)) {
return new Promise((resolve, reject) => {
const error = new Error('Param \'until\' must be of Date type.');
reject(error);
});
}
const params = {
key: this.config.apikey,
q: query,
dt: since.getFullYear() + '-' +
(since.getMonth() + 1) + '-'
+ since.getDate(),
};
if (until instanceof Date) {
params['end_dt'] = until.getFullYear() + '-' +
(until.getMonth() + 1) + '-'
+ until.getDate();
}
const url = getUrl('history', params);
return request(url);
}
search(query) {
const params = {
key: this.config.apikey,
q: query,
};
const url = getUrl('search', params);
return request(url);
}
}
module.exports = {
config: config,
Apixu: Apixu,
};
const getUrl = (method, params) => {
return API_URL +
API_VERSION + '/' +
method + '.' + FORMAT +
'?' + queryString.stringify(params);
};
const request = (url) => {
let response = '';
return new Promise((resolve, reject) => {
const request = http.get(url, (res) => {
res.setEncoding('utf8');
const { statusCode } = res;
if (statusCode !== HTTP_STATUS_OK) {
if (statusCode === HTTP_STATUS_NOT_FOUND) {
const error = new Error('Not found');
error.code = statusCode;
return reject(error);
}
if (statusCode >= HTTP_STATUS_INTERNAL_SERVER_ERROR) {
const error = new Error('Interval server error');
error.code = statusCode;
return reject(error);
}
}
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', () => {
let r;
try {
r = JSON.parse(response);
} catch (e) {
return reject(e);
}
if (r.error !== undefined) {
const error = new Error(r.error.message);
error.code = r.error.code;
return reject(error);
}
resolve(r);
});
});
request.on('error', (err) => {
reject(err);
}).end();
const timeout = () => {
request.abort();
const error = new Error('The request took too long.');
reject(error);
};
if (typeof window === 'undefined') {
request.setTimeout(HTTP_TIMEOUT, timeout);
} else {
request.setTimeout = window.setTimeout.bind(window);
request.setTimeout(timeout, HTTP_TIMEOUT);
}
});
};