Skip to content

Commit

Permalink
Refactor check function in hosting-node.js to use userAgentIdentifier…
Browse files Browse the repository at this point in the history
… instead of optionsOrAgentId and check for option or db
  • Loading branch information
fershad committed Apr 6, 2024
1 parent ccd5bc1 commit 0d8d296
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 8 additions & 9 deletions src/hosting-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
}
Expand Down
8 changes: 5 additions & 3 deletions src/hosting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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),
Expand All @@ -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({
Expand Down

0 comments on commit 0d8d296

Please sign in to comment.