Skip to content

Commit 1fa976a

Browse files
author
Will Myers
authored
Add apiVersion to client config (#18)
* Add apiVersion to client config * Add YYYY-MM-DD format to docstring
1 parent 3d10227 commit 1fa976a

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

CHANGELOG.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
Current Version
2+
- Add apiVersion to client config
3+
14
2.4.0 June 12, 2017
2-
- Request errors now include the `response` object.
5+
- Request errors now include the `response` object.
36

47
2.3.0 January 6, 2017
5-
- Add `merchants` resource
6-
+ `merchants#all`
8+
- Add `merchants` resource
9+
+ `merchants#all`
710

811
2.2.0 January 4, 2017
9-
- Add `client.utils.isWebhookAuthentic` function
12+
- Add `client.utils.isWebhookAuthentic` function
1013

1114
2.1.1 October 12, 2016
12-
- Renamed package to `@button/button-client-node`
15+
- Renamed package to `@button/button-client-node`
1316

1417
2.1.0 September 12, 2016
15-
- Added config.hostname, config.port, and config.secure
18+
- Added config.hostname, config.port, and config.secure
1619

1720
2.0.0 August 24, 2016
1821
- Added `accounts` resource

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ var configDefaults = {
1313
timeout: false
1414
};
1515

16+
function headers(apiVersion) {
17+
const nodeVersion = process.versions.node;
18+
const userAgent = 'button-client-node/' + version + ' node/' + nodeVersion;
19+
20+
return merge(compact({ 'X-Button-API-Version': apiVersion }), {
21+
'Content-Type': 'application/json',
22+
'User-Agent': userAgent
23+
});
24+
}
25+
1626
function client(apiKey, config) {
1727
//
1828
// #client provides the top-level interface to making API requests to Button.
@@ -26,6 +36,8 @@ function client(apiKey, config) {
2636
// @param {bool=} config.secure will use HTTPS if true and HTTP if false
2737
// @param {number=} config.timeout a timeout in ms to abort API calls
2838
// @param {Func=} config.promise a function which should return a promise
39+
// @param {string=} config.apiVersion a string pinning your API version for
40+
// the request (YYYY-MM-DD)
2941
// @returns {Object} a client
3042
//
3143
if (!apiKey) {
@@ -44,10 +56,7 @@ function client(apiKey, config) {
4456

4557
var requestOptions = merge(requestConfig, {
4658
auth: apiKey + ':',
47-
headers: {
48-
'Content-Type': 'application/json',
49-
'User-Agent': 'button-client-node/' + version + ' node/' + process.versions.node
50-
}
59+
headers: headers(config.apiVersion)
5160
});
5261

5362
var maybePromiseRequest = maybePromise(

test/index-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ describe('client', function() {
3737
it('defaults config options', function(done) {
3838
var c = client('sk-XXX').orders;
3939
var orderId = 'btnorder-XXX';
40-
var scope = nock('https://api.usebutton.com:443')
41-
.get('/v1/order/' + orderId)
40+
var scope = nock('https://api.usebutton.com:443', {
41+
badheaders: ['x-button-api-version']
42+
}).get('/v1/order/' + orderId)
4243
.reply(200, { meta: { status: 'ok' }, 'object': {} });
4344

4445
c.get(orderId, function(err) {
@@ -90,6 +91,23 @@ describe('client', function() {
9091
}.bind(this));
9192
});
9293

94+
it('sets the API Version', function(done) {
95+
var c = client('sk-XXX', { apiVersion: '2017-01-01' }).orders;
96+
var orderId = 'btnorder-XXX';
97+
var scope = nock('https://api.usebutton.com', {
98+
reqheaders: {
99+
'x-button-api-version': '2017-01-01'
100+
}
101+
}).get('/v1/order/' + orderId)
102+
.reply(200, { meta: { status: 'ok' }, 'object': {} });
103+
104+
c.get(orderId, function(err) {
105+
expect(err).to.be(null);
106+
scope.done();
107+
done();
108+
}.bind(this));
109+
});
110+
93111
});
94112

95113
});

0 commit comments

Comments
 (0)