-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
49 lines (44 loc) · 1.27 KB
/
Copy pathapi.js
File metadata and controls
49 lines (44 loc) · 1.27 KB
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
// @ts-check
import xhr from 'xhr';
import * as qs from 'querystring';
export const API_BASE = '/api';
export const API_RATES_SCHEDULES = '/schedules';
export const API_PATH_RATES_CSV = '/rates/csv';
export const API_PATH_RATES = '/rates';
export const API_PATH_SEARCH = '/search';
/**
* @typedef {Object} ScheduleMetadata
* @property {string} schedule - The schedule identifier
* @property {string} sin - The SIN number (can be an empty string)
* @property {string} full_name - The user-facing name for the schedule
*/
export default class API {
constructor(basePath = API_BASE) {
this.basePath = basePath;
}
get({ uri, data }, callback) {
let path = this.basePath + uri;
if (data) {
path += `?${qs.stringify(data)}`;
}
return xhr(path, {
json: true,
}, (err, res) => {
if (err) {
callback(`Error: Internal XMLHttpRequest Error: ${err.toString()}`);
} else if (res.statusCode >= 400) {
callback(res.rawRequest.statusText);
} else {
callback(null, res.body);
}
});
}
/**
* Get schedule metadata from the API.
*
* @param {function(string|null, ScheduleMetadata[]?)} callback
*/
getSchedules(callback) {
this.get({ uri: API_RATES_SCHEDULES, data: null }, callback);
}
}