Skip to content

Commit

Permalink
Merge pull request #31 from Clever/CLOC-216
Browse files Browse the repository at this point in the history
add host() and host_port() to external
  • Loading branch information
tnsardesai authored Nov 13, 2024
2 parents d6ba7ac + 1788873 commit 45cc4f8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions __tests__/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ describe("discovery", () => {
const ex_disc = external("clever.com");
assert.equal(ex_disc.url(), "https://clever.com:443");
assert.equal(ex_disc.proto_host(), "https://clever.com");
assert.equal(ex_disc.host(), "clever.com");
assert.equal(ex_disc.host_port(), "clever.com:443");
});
it("test complex external url", () => {
const ex_disc = external("api.clever.com");
assert.equal(ex_disc.url(), "https://api.clever.com:443");
assert.equal(ex_disc.proto_host(), "https://api.clever.com");
assert.equal(ex_disc.host(), "api.clever.com");
assert.equal(ex_disc.host_port(), "api.clever.com:443");
});
return it("test expect error on missing two vars", () => {
const disc = discovery("missing-proto-and-host", "foobar");
Expand Down
17 changes: 16 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ const discovery = (service_name: string, interface_name: string) => ({
});

const external = (input_url: string) => ({
host() {
return trimSuffix(trimPrefix(this.url(), "https://"), ":443");
},

url() {
return get_var(template_external_url(input_url));
},

host_port() {
return trimPrefix(this.url(), "https://");
},

proto_host() {
return trimSuffix(get_var(template_external_url(input_url)), ":443");
return trimSuffix(this.url(), ":443");
},
});

Expand All @@ -62,3 +70,10 @@ function trimSuffix(str: string, suffix: string) {
}
return str;
}

function trimPrefix(str: string, prefix: string) {
if (str.startsWith(prefix)) {
return str.slice(prefix.length);
}
return str;
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clever-discovery",
"version": "1.2.1",
"version": "1.2.2",
"description": "Node client library for service discovery",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 45cc4f8

Please sign in to comment.