Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 5822426

Browse files
committed
Expose res and use common options
Fixes #3
1 parent 3320935 commit 5822426

File tree

14 files changed

+306
-229
lines changed

14 files changed

+306
-229
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ See the official [HTTP API][consul-docs-api] docs for more information.
2222
* [Session](#session)
2323
* [Status](#status)
2424

25+
<a name="callback"/>
26+
### Callback
27+
28+
All callbacks having the following signature `function(err, data, res)`.
29+
30+
* err (Error, optional): set if there was an error, otherwise falsy
31+
* data (Object, optional): response data if any, otherwise `undefined`
32+
* res (http.IncomingMessage, optional): HTTP response object with additional `body` property. This might not exist when `err` is set. The `body` property can be a decoded object, string, or Buffer.
33+
2534
<a name="init"/>
2635
### consul([options])
2736

lib/agent.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Agent.prototype.services = function(callback) {
4343
*/
4444

4545
Agent.prototype.members = function(opts, callback) {
46-
if (typeof opts === 'function') {
46+
if (!callback) {
4747
callback = opts;
4848
opts = {};
4949
}
@@ -53,30 +53,36 @@ Agent.prototype.members = function(opts, callback) {
5353
opts = utils.normalizeKeys(opts);
5454

5555
var req = {
56+
name: 'agent.members',
57+
path: '/agent/members',
5658
query: {},
5759
};
5860

59-
if (opts.wan) req.query.wan = '1';
61+
utils.options(req, opts);
6062

61-
this.consul._get('/agent/members', req, function(err, res) {
62-
if (err) return callback(err);
63-
64-
callback(null, res.body);
65-
});
63+
this.consul._get(req, utils.body, callback);
6664
};
6765

6866
/**
6967
* Returns the local node configuration
7068
*/
7169

72-
Agent.prototype.self = function(callback) {
70+
Agent.prototype.self = function(opts, callback) {
71+
if (!callback) {
72+
callback = opts;
73+
opts = {};
74+
}
75+
7376
this.consul._log(['debug', 'agent', 'self']);
7477

75-
this.consul._get('/agent/self', function(err, res) {
76-
if (err) return callback(err);
78+
var req = {
79+
name: 'agent.self',
80+
path: '/agent/self',
81+
};
7782

78-
callback(null, res.body);
79-
});
83+
utils.options(req, opts);
84+
85+
this.consul._get(req, utils.body, callback);
8086
};
8187

8288
/**
@@ -92,20 +98,20 @@ Agent.prototype.join = function(opts, callback) {
9298

9399
opts = utils.normalizeKeys(opts);
94100

95-
if (!opts.address) return callback(new Error('address required'));
96-
97101
var req = {
98-
path: { address: opts.address },
102+
name: 'agent.join',
103+
path: '/agent/join/{address}',
104+
params: { address: opts.address },
99105
query: {},
100106
};
101107

102-
if (opts.wan) req.query.wan = '1';
108+
if (!opts.address) {
109+
return callback(this.consul._err('address required', req));
110+
}
103111

104-
this.consul._get('/agent/join/{address}', req, function(err) {
105-
if (err) return callback(err);
112+
utils.options(req, opts);
106113

107-
callback();
108-
});
114+
this.consul._get(req, utils.empty, callback);
109115
};
110116

111117
/**
@@ -121,18 +127,19 @@ Agent.prototype.forceLeave = function(opts, callback) {
121127

122128
opts = utils.normalizeKeys(opts);
123129

124-
if (!opts.node) return callback(new Error('node required'));
125-
126130
var req = {
127-
method: 'GET',
128-
path: { node: opts.node },
131+
name: 'agent.forceLeave',
132+
path: '/agent/force-leave/{node}',
133+
params: { node: opts.node },
129134
};
130135

131-
this.consul._get('/agent/force-leave/{node}', req, function(err) {
132-
if (err) return callback(err);
136+
if (!opts.node) {
137+
return callback(this.consul._err('node required', req));
138+
}
139+
140+
utils.options(req, opts);
133141

134-
callback();
135-
});
142+
this.consul._get(req, utils.empty, callback);
136143
};
137144

138145
/**

lib/agent/check.js

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ function AgentCheck(consul) {
2222
* Returns the checks the local agent is managing
2323
*/
2424

25-
AgentCheck.prototype.list = function(callback) {
25+
AgentCheck.prototype.list = function(opts, callback) {
26+
if (!callback) {
27+
callback = opts;
28+
opts = {};
29+
}
30+
2631
this.consul._log(['debug', 'agentcheck', 'list']);
2732

28-
this.consul._get('/agent/checks', function(err, res) {
29-
if (err) return callback(err);
33+
var req = {
34+
name: 'agent.check.list',
35+
path: '/agent/checks',
36+
};
37+
38+
utils.options(req, opts);
3039

31-
callback(null, res.body);
32-
});
40+
this.consul._get(req, utils.body, callback);
3341
};
3442

3543
/**
@@ -41,13 +49,15 @@ AgentCheck.prototype.register = function(opts, callback) {
4149

4250
opts = utils.normalizeKeys(opts);
4351

44-
if (!opts.name) return callback(new Error('name required'));
45-
4652
var req = {
53+
name: 'agent.check.register',
54+
path: '/agent/check/register',
4755
type: 'json',
4856
body: {},
4957
};
5058

59+
if (!opts.name) return callback(this.consul._err('name required', req));
60+
5161
if (opts.hasOwnProperty('id')) req.body.Id = opts.id;
5262
if (opts.hasOwnProperty('name')) req.body.Name = opts.name;
5363
if (opts.script && opts.interval) {
@@ -56,15 +66,13 @@ AgentCheck.prototype.register = function(opts, callback) {
5666
} else if (opts.ttl) {
5767
req.body.TTL = opts.ttl;
5868
} else {
59-
return callback(new Error('script and interval, or ttl required'));
69+
return callback(this.consul._err('script and interval, or ttl required', req));
6070
}
6171
if (opts.hasOwnProperty('notes')) req.body.Notes = opts.notes;
6272

63-
this.consul._put('/agent/check/register', req, function(err) {
64-
if (err) return callback(err);
73+
utils.options(req, opts);
6574

66-
callback();
67-
});
75+
this.consul._put(req, utils.empty, callback);
6876
};
6977

7078
/**
@@ -80,18 +88,17 @@ AgentCheck.prototype.deregister = function(opts, callback) {
8088

8189
opts = utils.normalizeKeys(opts);
8290

83-
if (!opts.id) return callback(new Error('id required'));
84-
8591
var req = {
86-
method: 'GET',
87-
path: { id: opts.id },
92+
name: 'agent.check.deregister',
93+
path: '/agent/check/deregister/{id}',
94+
params: { id: opts.id },
8895
};
8996

90-
this.consul._get('/agent/check/deregister/{id}', req, function(err) {
91-
if (err) return callback(err);
97+
if (!opts.id) return callback(this.consul._err('id required', req));
9298

93-
callback();
94-
});
99+
utils.options(req, opts);
100+
101+
this.consul._get(req, utils.empty, callback);
95102
};
96103

97104
/**
@@ -107,20 +114,20 @@ AgentCheck.prototype.pass = function(opts, callback) {
107114

108115
opts = utils.normalizeKeys(opts);
109116

110-
if (!opts.id) return callback(new Error('id required'));
111-
112117
var req = {
113-
path: { id: opts.id },
118+
name: 'agent.check.pass',
119+
path: '/agent/check/pass/{id}',
120+
params: { id: opts.id },
114121
query: {},
115122
};
116123

124+
if (!opts.id) return callback(this.consul._err('id required', req));
125+
117126
if (opts.note) req.query.note = opts.note;
118127

119-
this.consul._get('/agent/check/pass/{id}', req, function(err) {
120-
if (err) return callback(err);
128+
utils.options(req, opts);
121129

122-
callback();
123-
});
130+
this.consul._get(req, utils.empty, callback);
124131
};
125132

126133
/**
@@ -136,20 +143,20 @@ AgentCheck.prototype.warn = function(opts, callback) {
136143

137144
opts = utils.normalizeKeys(opts);
138145

139-
if (!opts.id) return callback(new Error('id required'));
140-
141146
var req = {
142-
path: { id: opts.id },
147+
name: 'agent.check.warn',
148+
path: '/agent/check/warn/{id}',
149+
params: { id: opts.id },
143150
query: {},
144151
};
145152

153+
if (!opts.id) return callback(this.consul._err('id required', req));
154+
146155
if (opts.note) req.query.note = opts.note;
147156

148-
this.consul._get('/agent/check/warn/{id}', req, function(err) {
149-
if (err) return callback(err);
157+
utils.options(req, opts);
150158

151-
callback();
152-
});
159+
this.consul._get(req, utils.empty, callback);
153160
};
154161

155162
/**
@@ -165,20 +172,20 @@ AgentCheck.prototype.fail = function(opts, callback) {
165172

166173
opts = utils.normalizeKeys(opts);
167174

168-
if (!opts.id) return callback(new Error('id required'));
169-
170175
var req = {
171-
path: { id: opts.id },
176+
name: 'agent.check.fail',
177+
path: '/agent/check/fail/{id}',
178+
params: { id: opts.id },
172179
query: {},
173180
};
174181

182+
if (!opts.id) return callback(this.consul._err('id required', req));
183+
175184
if (opts.note) req.query.note = opts.note;
176185

177-
this.consul._get('/agent/check/fail/{id}', req, function(err) {
178-
if (err) return callback(err);
186+
utils.options(req, opts);
179187

180-
callback();
181-
});
188+
this.consul._get(req, utils.empty, callback);
182189
};
183190

184191
/**

lib/agent/service.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ function AgentService(consul) {
2222
* Returns the services local agent is managing
2323
*/
2424

25-
AgentService.prototype.list = function(callback) {
25+
AgentService.prototype.list = function(opts, callback) {
26+
if (!callback) {
27+
callback = opts;
28+
opts = {};
29+
}
30+
2631
this.consul._log(['debug', 'agentservice', 'list']);
2732

28-
this.consul._get('/agent/services', function(err, res) {
29-
if (err) return callback(err);
33+
var req = {
34+
name: 'agent.service.list',
35+
path: '/agent/services',
36+
};
37+
38+
utils.options(req, opts);
3039

31-
callback(null, res.body);
32-
});
40+
this.consul._get(req, utils.body, callback);
3341
};
3442

3543
/**
@@ -45,13 +53,15 @@ AgentService.prototype.register = function(opts, callback) {
4553

4654
opts = utils.normalizeKeys(opts);
4755

48-
if (!opts.name) return callback(new Error('name required'));
49-
5056
var req = {
57+
name: 'agent.service.register',
58+
path: '/agent/service/register',
5159
type: 'json',
5260
body: {},
5361
};
5462

63+
if (!opts.name) return callback(this.consul._err('name required', req));
64+
5565
req.body.Name = opts.name;
5666
if (opts.id) req.body.ID = opts.id;
5767
if (opts.tags) req.body.Tags = opts.tags;
@@ -68,16 +78,14 @@ AgentService.prototype.register = function(opts, callback) {
6878
} else if (check.ttl) {
6979
req.body.Check.TTL = check.ttl;
7080
} else {
71-
return callback(new Error('script and interval, or ttl required'));
81+
return callback(this.consul._err('script and interval, or ttl required', req));
7282
}
7383
if (check.hasOwnProperty('notes')) req.body.Check.Notes = check.notes;
7484
}
7585

76-
this.consul._put('/agent/service/register', req, function(err) {
77-
if (err) return callback(err);
86+
utils.options(req, opts);
7887

79-
callback();
80-
});
88+
this.consul._put(req, utils.empty, callback);
8189
};
8290

8391
/**
@@ -93,18 +101,17 @@ AgentService.prototype.deregister = function(opts, callback) {
93101

94102
opts = utils.normalizeKeys(opts);
95103

96-
if (!opts.id) return callback(new Error('id required'));
97-
98104
var req = {
99-
method: 'GET',
100-
path: { id: opts.id },
105+
name: 'agent.service.deregister',
106+
path: '/agent/service/deregister/{id}',
107+
params: { id: opts.id },
101108
};
102109

103-
this.consul._get('/agent/service/deregister/{id}', req, function(err) {
104-
if (err) return callback(err);
110+
if (!opts.id) return callback(this.consul._err('id required', req));
111+
112+
utils.options(req, opts);
105113

106-
callback();
107-
});
114+
this.consul._get(req, utils.empty, callback);
108115
};
109116

110117
/**

0 commit comments

Comments
 (0)