forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestor.js
83 lines (75 loc) · 2.21 KB
/
requestor.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
var requestify = require('requestify');
/**
* Creates a new Requestor object, which handles remote requests to fetch feature flags for LaunchDarkly.
* This is never called synchronously when requesting a feature flag for a user (e.g. via the toggle) call.
*
* It will be called once per second in polling mode (i.e. when streaming is disabled), or for extremely large
* feature flag representations if streaming is enabled (the stream may contain a pointer to a large representation,
* which will be polled by the requestor)
*
* @param {String} the SDK key
* @param {Object} the LaunchDarkly client configuration object
**/
function Requestor(sdk_key, config) {
var requestor = {};
requestify.cacheTransporter({
cache: {},
get: function(url, fn) {
fn(null, this.cache[url]);
},
set: function(url, response, fn) {
this.cache[url] = response;
fn();
},
purge: function(url, fn) {
delete this.cache[url];
fn();
}
});
function make_request(resource) {
var request_params = {
method: "GET",
headers: {
'Authorization': sdk_key,
'User-Agent': 'NodeJSClient/' + config.version
},
timeout: config.timeout * 1000,
agent: config.proxy_agent
}
return function(cb, err_cb) {
requestify.request(config.base_uri + resource, request_params).then(cb, err_cb);
};
}
requestor.request_flag = function(key, cb) {
var req = make_request('/sdk/latest-flags/' + key);
req(
function(response) {
if (response.code !== 200) {
cb(new Error('[LaunchDarkly] Unexpected status code: ' + response.code), null);
} else {
cb(null, response.getBody());
}
},
function(err) {
cb(err, null);
}
);
}
requestor.request_all_flags = function(cb) {
var req = make_request('/sdk/latest-flags');
req(
function(response) {
if (response.code !== 200) {
cb(new Error('[LaunchDarkly] Unexpected status code: ' + response.code), null);
} else {
cb(null, response.getBody());
}
},
function(err) {
cb(err, null);
}
);
}
return requestor;
}
module.exports = Requestor;