Skip to content

Commit

Permalink
refactor: simplify baseUrl handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uschtwill committed Oct 12, 2023
1 parent 73b4aaf commit a90cea4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const fetch = async (strategy, query, config) => {
const { headers, logResponse } = config.fetchConfig;
const {
url,
baseUrl,
zeroResultsYields404,
getters: { getNextPageHref },
} = strategy;
Expand All @@ -32,7 +31,7 @@ export const fetch = async (strategy, query, config) => {
if (!nextHref) {
href = false;
} else {
href = buildHref(nextHref, baseUrl);
href = buildHref(nextHref, url);
}
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const process = async (htmlDocuments, strategy, query, config) => {
const { database, notifications, debug, alreadySeenHrefs } = config;
const {
getters: { getSingleResult, getResultTitle, getResultHref },
baseUrl,
customDeduplication,
url,
} = strategy;

const fileContent = await loadDatabase(strategy);
Expand All @@ -36,7 +36,7 @@ export const process = async (htmlDocuments, strategy, query, config) => {
strategy,
query,
extractedTitle,
extractedHref: buildHref(extractedHref, baseUrl),
extractedHref: buildHref(extractedHref, url),
};

if (!alreadySeenHrefs.includes(extractedHref)) {
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import fs from "fs";
href.includes("https://") ? href : baseUrl + href;

const _getBaseUrl = (url) => url.match(/^.+?[^\/:](?=[?\/]|$)/)[0];

const buildHref = (href, url) =>
href.includes("https://") ? href : _getBaseUrl(url) + href;

const renderMessage = ({
strategy,
query,
extractedTitle,
extractedHref,
baseUrl,
url,
}) => {
return `${strategy.name} -- ${query} --- ${extractedTitle} --- <${buildHref(
extractedHref,
baseUrl
url
)}>\n`;
};

Expand Down

0 comments on commit a90cea4

Please sign in to comment.