Skip to content

Commit

Permalink
Merge pull request #5 from hapinessjs/master
Browse files Browse the repository at this point in the history
master in next
  • Loading branch information
akanass committed Apr 10, 2018
2 parents 5050957 + 620e61f commit 0d6885f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 78 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -106,8 +106,16 @@ export interface HapinessConsulClientOptions {
scheme?: string;
host?: string;
port?: string;
username?: string;
password?: string;
defaults?: {
consistent?: boolean;
dc?: string;
stale?: boolean;
token?: string;
wait?: string;
wan?: boolean;
ctx?: NodeJS.EventEmitter;
timeout?: number;
};
ca?: string;
baseUrl?: string;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@hapiness/consul",
"version": "1.0.0",
"version": "1.0.1",
"description": "Hapiness module for consul",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/module/interfaces/hapiness.client.options.interface.ts
@@ -1,9 +1,10 @@
import { CommonOptions } from 'consul';

export interface HapinessConsulClientOptions {
scheme?: string;
host?: string;
port?: number;
username?: string;
password?: string;
defaults?: CommonOptions;
ca?: string;
baseUrl?: string;
}
24 changes: 6 additions & 18 deletions src/module/managers/consul.manager.ts
@@ -1,6 +1,6 @@
import * as consul from 'consul';

import { bindObservable, appendAuthStringToUrl } from '../utils';
import { bindObservable } from '../utils';
import { HapinessConsulClientOptions } from '../interfaces';

export class ConsulClientManager {
Expand All @@ -18,27 +18,15 @@ export class ConsulClientManager {
scheme: config.scheme || 'http',
host: config.host || '127.0.0.1',
port: config.port || 8500,
username: config.username,
password: config.password,
defaults: config.defaults,
ca: config.ca,
baseUrl: config.baseUrl
};

const authString = _config.username && _config.password ?
`${_config.username}:${_config.password}` : '';

if (!!authString.length) {
if (_config.baseUrl) {
_config.baseUrl = appendAuthStringToUrl(authString, _config.baseUrl);
} else {
// If there is no baseUrl provided, make it point to the /v1 (as it is done in consul module)
const baseUrl = `${_config.scheme}://${_config.host}:${_config.port}/v1`;
_config.baseUrl = appendAuthStringToUrl(authString, baseUrl);
}

_config.scheme = undefined;
_config.host = undefined;
_config.port = undefined;
if (_config.baseUrl) {
delete _config.scheme;
delete _config.host;
delete _config.port;
}

return Object.keys(_config).reduce((finalConf, prop) => {
Expand Down
17 changes: 0 additions & 17 deletions src/module/utils/append.authstring.to.url.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/module/utils/index.ts
@@ -1,2 +1 @@
export * from './append.authstring.to.url';
export * from './bind.observable';
22 changes: 0 additions & 22 deletions test/append.authstring.to.url.test.ts

This file was deleted.

21 changes: 6 additions & 15 deletions test/consul.client.manager.test.ts
Expand Up @@ -18,25 +18,16 @@ export class ConsulClientManagerTest {
}
);

const managerWithAuth = new ConsulClientManager({ username: 'user1', password: 'passwd' });
unit.object(managerWithAuth.config).is(
const managerWithAuth = new ConsulClientManager(
{
username: 'user1',
password: 'passwd',
baseUrl: 'http://user1:passwd@127.0.0.1:8500/v1'
defaults: { token: 'user1-token' },
baseUrl: 'http://my_base_url:8500/v1'
}
);

const managerWithAuthAndBaseUrl = new ConsulClientManager({
baseUrl: 'https://thisisatest.com:4444',
username: 'user1',
password: 'passwd'
});
unit.object(managerWithAuthAndBaseUrl.config).is(
unit.object(managerWithAuth.config).is(
{
username: 'user1',
password: 'passwd',
baseUrl: 'https://user1:passwd@thisisatest.com:4444'
defaults: { token: 'user1-token' },
baseUrl: 'http://my_base_url:8500/v1'
}
);
}
Expand Down

0 comments on commit 0d6885f

Please sign in to comment.