Skip to content

Commit

Permalink
Pass URL search parameters through to sources
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorsharp committed Feb 19, 2024
1 parent 45f5481 commit fcc2147
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/pages/api/[feedId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import type { NextApiRequest, NextApiResponse } from 'next';

const getFeed = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const { host } = req.headers;
const { feedId } = req.query;
if (typeof feedId !== 'string') return res.status(400).send('Missing Feed Id');

const { host } = req.headers;
const searchParams =
host && req.url ? new URL(`http://${host}${req.url}`).searchParams : undefined;

const modConfig = await decompressModConfig(feedId);
const feedData = await fetchFeedData(modConfig.sources);
const feedData = await fetchFeedData(modConfig.sources, searchParams);
const moddedFeedData = applyMods(feedData, modConfig);
const feed = buildFeed(moddedFeedData, feedId, host);

Expand Down
13 changes: 8 additions & 5 deletions src/services/feedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ const buildFeed = (feed: FeedData, feedId: string, host?: string) => {
return builder.build(feed) as string;
};

const fetchFeedData = async (urls: string[]) => {
const fetchFeedData = async (urls: string[], searchParams?: URLSearchParams) => {
const [firstFeed, ...otherFeeds] = await Promise.all(
urls.map((url) =>
fetch(url, {
urls.map((urlString) => {
const url = new URL(urlString);
if (searchParams) searchParams.forEach((value, key) => url.searchParams.set(key, value));

return fetch(url, {
headers: {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
Expand All @@ -40,8 +43,8 @@ const fetchFeedData = async (urls: string[]) => {
.catch((error) => {
console.log(error);
throw 'Error pulling source feed data';
})
)
});
})
);

if (!firstFeed) throw 'Error pulling source feed data';
Expand Down

0 comments on commit fcc2147

Please sign in to comment.