-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.js
96 lines (78 loc) · 2.82 KB
/
main.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
'use strict';
import { Promise } from 'es6-promise';
import _ from 'lodash';
import { UTILS } from './utils';
import ENDPOINTS from './endpoints';
if (!global.fetch) {
require('isomorphic-fetch');
}
var HOST = 'https://www.bungie.net/platform/Destiny/'; // the is address to Bungie's API
var API_KEY;
/** FIXME: this could potentially be broken up into smaller blocks
*
* appends a spec to the lirary via iteration.
*
* lib - Object, intially empty.
* item - Object, Destiny::Method.
*/
let createRequest = (lib, method) => {
let template = _.template(method.url); // README: so that we can have parametised URLs
lib[method.name] = function (params, headers) {
return Promise.resolve(params)
.then(params => {
// throw if parameters isn't an object
if (!_.isObject(params)) {
UTILS.error(`Argument must be an Object containing: ${ method.required.join(', ') }.`);
}
// iterate over required fields to aggregate missing ones if not present in current call
var missing = method.required
.filter(function (field) {
return !params.hasOwnProperty(field);
});
// throw for any missing required fields
if (missing.length > 0) {
UTILS.error(`Please provide [${ missing.join(', ') }] to Destiny.${ method.name }()`);
}
return params;
})
.then(params => {
let options = _.merge(
method.options || {},
{
headers: _.merge(headers || {},
{
'x-api-key': API_KEY
}
),
body: JSON.stringify(params)
}
);
if (options.method === UTILS.METHODS.GET) {
delete options.body;
}
return fetch(
`${HOST}${template(params)}`,
options
);
})
.then(UTILS.json)
.then(UTILS.unwrapDestinyResponse);
};
return lib;
};
/**
* preparing library for export
*/
let Destiny = (apiKey=undefined, host='https://www.bungie.net/platform/Destiny/') => {
if (!_.isString(apiKey) || _.isEmpty(apiKey)) {
UTILS.error(`You must provide a valid api key. Expected: String, got: ${ typeof apiKey }. Get a key at: https://www.bungie.net/developer`);
}
if (_.isString(host)) {
HOST = host;
API_KEY = apiKey;
} else {
UTILS.error(`${ host } is not a valid URL.`);
}
return ENDPOINTS.reduce(createRequest, {});
};
export default Destiny;