diff --git a/doc/GETTING_STARTED.md b/doc/GETTING_STARTED.md index 36d87b1c..be1967ff 100644 --- a/doc/GETTING_STARTED.md +++ b/doc/GETTING_STARTED.md @@ -36,6 +36,16 @@ const myApiKey = '' const client = new recurly.Client(myApiKey) ``` +To access Recurly API in Europe, you will need to specify the EU Region in the `options`. + +```js +const recurly = require('recurly') +// You should store your API key somewhere safe +// and not in plain text if possible +const myApiKey = '' +const client = new recurly.Client(myApiKey, { 'region': 'eu' }) +``` + **Note**: to import using typescript: ```ts import * as recurly from "recurly"; diff --git a/lib/recurly/BaseClient.js b/lib/recurly/BaseClient.js index a5827a73..fae6b38f 100644 --- a/lib/recurly/BaseClient.js +++ b/lib/recurly/BaseClient.js @@ -15,20 +15,34 @@ const BINARY_TYPES = [ ] const JSON_CONTENT_TYPE = 'application/json' const ALLOWED_OPTIONS = ['body', 'params', 'headers'] +const API_HOSTS = { + us: 'v3.recurly.com', + eu: 'v3.eu.recurly.com' +} /** * This is the base functionality for the Recurly.Client. * @private * @param {string} apiKey - The private api key for the site. + * @param {string} region - The private region for the site. */ class BaseClient { - constructor (apiKey) { + constructor (apiKey, options = {}) { // API key should not be instance variable. // This way it's not accidentally logged. this._getApiKey = () => apiKey + let apiHost = API_HOSTS['us'] + + if (options.hasOwnProperty('region')) { + if (!(options.region in API_HOSTS)) { + throw new ApiError(`Invalid region type. Expected one of: ${Object.keys(API_HOSTS).join(', ')}`) + } + apiHost = API_HOSTS[options.region] + } + this._httpOptions = { - host: 'v3.recurly.com', + host: apiHost, port: 443, agent: new https.Agent({ keepAlive: true, diff --git a/test/mock_client.js b/test/mock_client.js index e47aec93..89497503 100644 --- a/test/mock_client.js +++ b/test/mock_client.js @@ -4,8 +4,8 @@ const Pager = require('../lib/recurly/Pager') const { Request, Response } = require('../lib/recurly/Http') class MockClient extends BaseClient { - constructor (apiKey) { - super(apiKey) + constructor (apiKey, options = {}) { + super(apiKey, options) this._sandbox = sinon.createSandbox() } diff --git a/test/recurly/BaseClient.test.js b/test/recurly/BaseClient.test.js index 6964f820..cfc72fbc 100644 --- a/test/recurly/BaseClient.test.js +++ b/test/recurly/BaseClient.test.js @@ -22,6 +22,22 @@ describe('BaseClient', () => { assert.ok(userAgentRegex.test(client._getRequestOptions('GET', '/resources', {}).headers['User-Agent'])) assert.equal(client._getRequestOptions('GET', '/resources', {}).headers['Accept'], 'application/vnd.recurly.v2022-01-01') }) + + it('should set host to EU DataCenter', () => { + const clientEU = new MockClient('myapikey', { 'region': 'eu' }) + assert.equal(clientEU._httpOptions.host, 'v3.eu.recurly.com') + }) + + it('should set host to US DataCenter', () => { + assert.equal(client._httpOptions.host, 'v3.recurly.com') + }) + + it('should validate that region is an invalid value', () => { + assert.throws(() => { + const invalidClient = new MockClient('myapikey', { 'region': 'none' }) + assert.equal(invalidClient._httpOptions.host, '') + }, recurly.ApiError) + }) }) describe('#_interpolatePath', () => {