From 4d6867e6904c77a9b9d838e77aebb5735ef6fb75 Mon Sep 17 00:00:00 2001 From: waltem01 Date: Thu, 22 Feb 2024 10:41:00 +0100 Subject: [PATCH] endpoint for reddit 'redirect' --- Webserver/src/routes/api/reddit/+server.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Webserver/src/routes/api/reddit/+server.ts diff --git a/Webserver/src/routes/api/reddit/+server.ts b/Webserver/src/routes/api/reddit/+server.ts new file mode 100644 index 0000000..6e58e77 --- /dev/null +++ b/Webserver/src/routes/api/reddit/+server.ts @@ -0,0 +1,22 @@ +import type { APIResponse } from "$lib/interfaces"; +import { json, type RequestHandler } from "@sveltejs/kit"; + +export const GET: RequestHandler = async ({ url }) => { + // Get endpoint on main API from searchparams + const params = url.searchParams; + const subreddit = params.get('subreddit'); + // Return if param not found + if (subreddit === null) return json({ success: false } as APIResponse); + + // Get subreddit page data + const response = await fetch(`https://www.reddit.com/r/${subreddit}/`, { + headers: new Headers({ + "User-Agent": "MatrixRedditMemes/0.0.1" + }) + }); + // Read data as string + const text = await response.text(); + + // Return source data + return json({ success: true, results: text.split('\n') } as APIResponse); +};