From 0d8d2965baddf47ab779cd503920165ed540dc97 Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:15:00 +0800 Subject: [PATCH] Refactor check function in hosting-node.js to use userAgentIdentifier instead of optionsOrAgentId and check for option or db --- src/hosting-node.js | 17 ++++++++--------- src/hosting.test.js | 8 +++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/hosting-node.js b/src/hosting-node.js index 9ab4d40..c1baa88 100644 --- a/src/hosting-node.js +++ b/src/hosting-node.js @@ -53,7 +53,7 @@ async function getBody(url, userAgentIdentifier) { * Check if a domain is hosted by a green web host. * @param {string|array} domain - The domain to check, or an array of domains to be checked. * @param {string[] | DomainCheckOptions} optionsOrDb - Optional. An object of domain check options, or a database list to use for lookups. - * @param {string | DomainCheckOptions} optionsOrAgentId - Optional. An object of domain check options, or a string + * @param {string | DomainCheckOptions} userAgentIdentifier - Optional. An object of domain check options, or a string * representing the app, site, or organisation that is making the request. * @returns - A boolean if a string was provided, or an array of booleans if an array of domains was provided. * if a string was provided for `domain`: a boolean indicating whether the domain is hosted by a green web host if `options.verbose` is false, @@ -62,19 +62,18 @@ async function getBody(url, userAgentIdentifier) { * otherwise a dictionary of domain to host information. */ -function check(domain, optionsOrDb, optionsOrAgentId) { - let db, options; - if (!db || Array.isArray(optionsOrDb)) { +function check(domain, optionsOrDb, userAgentIdentifier) { + let db, + options = {}; + if (!db && Array.isArray(optionsOrDb)) { db = optionsOrDb; - options = - typeof optionsOrAgentId === "string" - ? { userAgentIdentifier: optionsOrAgentId } - : optionsOrAgentId; } else { options = optionsOrDb; - db = optionsOrDb.db; + db = optionsOrDb?.db; } + console.log({ db, options }); + if (db && options?.verbose) { throw new Error("verbose mode cannot be used with a local lookup database"); } diff --git a/src/hosting.test.js b/src/hosting.test.js index 9bb2b67..9ec326b 100644 --- a/src/hosting.test.js +++ b/src/hosting.test.js @@ -66,7 +66,7 @@ describe("hosting", () => { expect(res).toEqual(true); }); it("use the API instead with verbose=true", async () => { - const res = await hosting.check("google.com", null, { + const res = await hosting.check("google.com", { verbose: true, }); expect(res).toMatchObject({ @@ -77,7 +77,9 @@ describe("hosting", () => { }); }); it("sets the correct user agent header", async () => { - await hosting.check("google.com", null, requestHeaderComment); + await hosting.check("google.com", { + userAgentIdentifier: requestHeaderComment, + }); expect(httpsGetSpy).toHaveBeenCalledTimes(1); expect(httpsGetSpy).toHaveBeenLastCalledWith( expect.any(String), @@ -95,7 +97,7 @@ describe("hosting", () => { }); it("use the API with verbose=true", async () => { - const res = await hosting.check(["google.com", "pchome.com"], null, { + const res = await hosting.check(["google.com", "pchome.com"], { verbose: true, }); expect(res).toEqual({