Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const util = require('../util');
* @class Client
* @param {object} options configuration options for this instance
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] - Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.subdomain] Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @param {string} [options.apiHostname] If you need to change the domain that the library makes requests to, pass the string domain. This gets passed as the hostname to the config for the https library (so you cant add a path fragment on the end, just the domain). Defaults to 'api.challonge.com'
* @description
* Constructor function for the Client base responsible for communicating with Challonge API
* createClient takes one argument for configuration and returns an instance of the api client.
Expand All @@ -34,6 +35,9 @@ const Client = exports.Client = function(options) {
if (!this.options.format) {
this.options.format = 'json';
}
if (!this.options.apiHostname) {
this.options.apiHostname = 'api.challonge.com';
}

this.setSubdomain(this.options.subdomain);

Expand Down Expand Up @@ -95,7 +99,7 @@ Client.prototype.makeRequest = function(obj) {

// create options for the https call
const options = {
hostname: 'api.challonge.com',
hostname: this.options.get('apiHostname'),
path: path,
method: method,
headers: {
Expand Down
7 changes: 7 additions & 0 deletions lib/api/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ describe('Client Class', () => {
}).options.massageProperties).toBe(false);
});

it('should set the apiHostname by default, or override if passed', () => {
expect(new Client().options.apiHostname).toBe('api.challonge.com');
expect(new Client({
apiHostname: 'something.else.com'
}).options.apiHostname).toBe('something.else.com');
});

it('should set the format to json by default, or override if passed', () => {
expect(new Client().options.format).toBe('json');
expect(new Client({
Expand Down
19 changes: 19 additions & 0 deletions lib/challonge.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const https = require('https');
const Challonge = require('./challonge');

describe('Challonge object', () => {
Expand Down Expand Up @@ -48,4 +49,22 @@ describe('Challonge object', () => {
expect(client.matches.setSubdomain).toHaveBeenCalled();
});
});

it('should allow you to proxy to another domain if needed', () => {
const client = Challonge.createClient({
apiHostname: 'myother.domain.com'
});

client.setSubdomain('somedomain');

spyOn(https, 'request').and.returnValue({
end: () => {}
});

client.tournaments.index({
id: 25
});

expect(https.request.calls.allArgs()[0][0].hostname).toEqual('myother.domain.com');
});
});