From 532091339cb9c34fd1d32e92b5c0a2d6c9195600 Mon Sep 17 00:00:00 2001 From: Aaron Tidwell Date: Sat, 23 Jun 2018 22:26:36 -0400 Subject: [PATCH] add apiHostname property to config --- lib/api/client.js | 8 ++++++-- lib/api/client.spec.js | 7 +++++++ lib/challonge.spec.js | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/api/client.js b/lib/api/client.js index 447bd3a..9948935 100644 --- a/lib/api/client.js +++ b/lib/api/client.js @@ -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. @@ -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); @@ -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: { diff --git a/lib/api/client.spec.js b/lib/api/client.spec.js index 4e20b6b..e421fb4 100644 --- a/lib/api/client.spec.js +++ b/lib/api/client.spec.js @@ -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({ diff --git a/lib/challonge.spec.js b/lib/challonge.spec.js index 50f927e..0c5c908 100644 --- a/lib/challonge.spec.js +++ b/lib/challonge.spec.js @@ -1,3 +1,4 @@ +const https = require('https'); const Challonge = require('./challonge'); describe('Challonge object', () => { @@ -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'); + }); });