endpoint for reddit 'redirect'

This commit is contained in:
waltem01 2024-02-22 10:41:00 +01:00
parent a92193f33b
commit 4d6867e690

View File

@ -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);
};