Skip to content

Commit

Permalink
feat: add settings.domain argument (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherwinski authored Jun 5, 2019
1 parent 950c7ba commit af40091
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/imgix-core-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
var DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i;
var DEFAULTS = {
host: null,
domain: null,
domains: [],
useHTTPS: true,
includeLibraryParam: true,
Expand All @@ -43,14 +44,24 @@
this.settings[key] = val;
}

if (!Array.isArray(this.settings.domains)) {
this.settings.domains = [this.settings.domains];
if (Array.isArray(this.settings.domains)) {
if (this.settings.domains.length > 1) {
console.warn("Warning: Domain sharding has been deprecated and will be removed in the next major version.\nAs a result, the 'domains' argument will be deprecated in favor of 'domain' instead.");
}
else if (this.settings.domains.length == 0) {
if (typeof(this.settings.domain) != "string" && this.settings.domain != null) {
throw new Error('ImgixClient.settings.domain only accepts a string argument');
}
else {
this.settings.domains = [this.settings.domain];
}
}
}
else {
console.warn("Warning: Domain sharding has been deprecated and will be removed in the next major version.\nAs a result, the 'domains' argument will be deprecated in favor of 'domain' instead.");
this.settings.domains = [this.settings.domains];
}

if (!this.settings.host && this.settings.domains.length === 0) {
if (!this.settings.host && this.settings.domains == 0) {
throw new Error('ImgixClient must be passed valid domain(s)');
}

Expand All @@ -61,7 +72,7 @@
}

if (this.settings.host) {
console.warn("'host' argument is deprecated; use 'domains' instead.");
console.warn("'host' argument is deprecated; either use 'domain' or 'domains' instead.");
if (this.settings.domains.length == 0)
this.settings.domains[0] = this.settings.host;
}
Expand Down
27 changes: 27 additions & 0 deletions test/test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ describe('Imgix client:', function describeSuite() {
}, Error);
stub.restore();
});

it('accepts argument "domain" as a single domain name', function testSpec() {
var expectedUrl = 'https://my-host.imgix.net/image.jpg?ixlib=js-'+ImgixClient.VERSION;
var client = new ImgixClient({ domain: 'my-host.imgix.net' });
assert.equal("my-host.imgix.net", client.settings.domain);
assert.equal(expectedUrl, client.buildURL('image.jpg'));
});

it('accepts both arguments "domains" and "domain", giving priority to "domains"', function testSpec() {
var expectedUrl = 'https://my-host.imgix.net/image.jpg?ixlib=js-'+ImgixClient.VERSION;
var client = new ImgixClient({ domains: 'my-host.imgix.net', domain: 'other-domain.imgix.net' });
assert.equal("my-host.imgix.net", client.settings.domains);
assert.equal(expectedUrl, client.buildURL('image.jpg'));
});

it('errors when "domain" is passed multiple domains', function testSpec() {
assert.throws(function() {
new ImgixClient({ domain: ['my-host.imgix.net', 'another-domain.imgix.net'] });
}, Error);
});

it('errors when neither "domains" nor "domain" is passed', function testSpec() {
assert.throws(function() {
new ImgixClient({});
}, Error);
});

});

describe('Calling _sanitizePath()', function describeSuite() {
Expand Down

0 comments on commit af40091

Please sign in to comment.