diff --git a/__tests__/discovery.test.ts b/__tests__/discovery.test.ts index 56a19f8..a3e0495 100644 --- a/__tests__/discovery.test.ts +++ b/__tests__/discovery.test.ts @@ -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"); diff --git a/lib/index.ts b/lib/index.ts index d67d9a8..6ee3715 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"); }, }); @@ -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; +} diff --git a/package-lock.json b/package-lock.json index 11db346..71f9ef7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "clever-discovery", - "version": "1.2.1", + "version": "1.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "clever-discovery", - "version": "1.2.1", + "version": "1.2.2", "license": "ISC", "devDependencies": { "@eslint/js": "^9.9.1", diff --git a/package.json b/package.json index 88ecccb..bc6d2db 100644 --- a/package.json +++ b/package.json @@ -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",