Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLIBZ-2332: Add support for Instance ID #278

Merged
merged 2 commits into from
Mar 29, 2018
Merged
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
36 changes: 24 additions & 12 deletions src/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export class Client {
* Creates a new instance of the Client class.
*
* @param {Object} options Options
* @param {string} [options.apiHostname='https://baas.kinvey.com'] Host name used for Kinvey API requests
* @param {string} [options.micHostname='https://auth.kinvey.com'] Host name used for Kinvey MIC requests
* @param {string} [options.instanceId='<my-subdomain>'] Custom subdomain for Kinvey API and MIC requests.
* @param {string} [options.apiHostname='https://baas.kinvey.com'] Deprecated: Use the instanceID property instead. Host name used for Kinvey API requests
* @param {string} [options.micHostname='https://auth.kinvey.com'] Deprecated: Use the instanceID property instead. Host name used for Kinvey MIC requests
* @param {string} [options.appKey] App Key
* @param {string} [options.appSecret] App Secret
* @param {string} [options.masterSecret] App Master Secret
Expand All @@ -70,12 +71,30 @@ export class Client {
*/

constructor(config = {}) {
let apiHostname = isString(config.apiHostname) ? config.apiHostname : 'https://baas.kinvey.com';
if (/^https?:\/\//i.test(apiHostname) === false) {
apiHostname = `https://${apiHostname}`;
let apiHostname = 'https://baas.kinvey.com';
let micHostname = 'https://auth.kinvey.com';

if (config.instanceId) {
const { instanceId } = config;

if (!isString(instanceId)) {
throw new KinveyError('Instance ID must be a string.');
}

apiHostname = `https://${instanceId}-baas.kinvey.com`;
micHostname = `https://${instanceId}-auth.kinvey.com`;
} else {
if (isString(config.apiHostname)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this ticket, the micHostname and apiHostname config options should be marked deprecated in the API reference. Also, the instanceId option should be documented in the API reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already did this. Take a look at the documentation comments above the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I see it now. Can you make the following change?
"Deprecated: Host name used for Kinvey API requests": add the following - "Use the instanceID property instead"

LGTM overall.

apiHostname = /^https?:\/\//i.test(config.apiHostname) ? config.apiHostname : `https://${config.apiHostname}`;
}

if (isString(config.micHostname)) {
micHostname = /^https?:\/\//i.test(config.micHostname) ? config.micHostname : `https://${config.micHostname}`;
}
}

const apiHostnameParsed = url.parse(apiHostname);
const micHostnameParsed = url.parse(micHostname);

/**
* @type {string}
Expand All @@ -92,13 +111,6 @@ export class Client {
*/
this.apiHost = apiHostnameParsed.host;

let micHostname = isString(config.micHostname) ? config.micHostname : 'https://auth.kinvey.com';
if (/^https?:\/\//i.test(micHostname) === false) {
micHostname = `https://${micHostname}`;
}

const micHostnameParsed = url.parse(micHostname);

/**
* @type {string}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/core/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ describe('Client', () => {
expect(client.micHostname).toEqual('https://auth.kinvey.com');
});

it('should throw an error if instance id is not a string', () => {
expect(() => {
const instanceId = {};
const client = new Client({ instanceId });
return client;
}).toThrow(/Instance ID must be a string./);
});

it('should be able to provide an instance id', () => {
const instanceId = randomString().toLowerCase();
const client = new Client({ instanceId });
expect(client.apiHostname).toEqual(`https://${instanceId}-baas.kinvey.com`);
expect(client.micHostname).toEqual(`https://${instanceId}-auth.kinvey.com`);
});

it('should be able to provide custom apiHostname with protocol https:', () => {
const apiHostname = 'https://mybaas.kinvey.com';
const client = new Client({ apiHostname: apiHostname });
Expand Down