app structure, url redirects
This commit is contained in:
22
src/lib/server/data.ts
Normal file
22
src/lib/server/data.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export const RegistryItems = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Item One",
|
||||
url: "https://www.amazon.com/dp/B071VG5N9D?lv=shuf&social_share=cm_sw_r_cp_ud_dp_HNQ1V69T73P68F2Z4WZK&channelId=751&plpRedirect=mhFallback&th=1",
|
||||
event: "wedding"
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Another Item",
|
||||
url: "https://www.target.com",
|
||||
event: "wedding"
|
||||
},
|
||||
];
|
||||
|
||||
export const AllowList = [
|
||||
"https://amazon.com/",
|
||||
"https://www.amazon.com/",
|
||||
"https://a.co/",
|
||||
"https://target.com/",
|
||||
"https://www.target.com/"
|
||||
]
|
||||
10
src/routes/(public)/[event]/registry/+page.server.ts
Normal file
10
src/routes/(public)/[event]/registry/+page.server.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { RegistryItems } from '#lib/server/data.ts';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = ({ params }) => {
|
||||
const items = RegistryItems
|
||||
.filter((i) => i.event === params.event)
|
||||
.map(({ id, name }) => ({ id, name }));
|
||||
|
||||
return { event: params.event, items };
|
||||
}
|
||||
16
src/routes/(public)/[event]/registry/+page.svelte
Normal file
16
src/routes/(public)/[event]/registry/+page.svelte
Normal file
@@ -0,0 +1,16 @@
|
||||
<script lang="ts">
|
||||
import type { PageData } from "./$types";
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
</script>
|
||||
|
||||
<h1 class="text-3xl font-bold">Registry</h1>
|
||||
<h2 class="text-lg">Items</h2>
|
||||
|
||||
<ul>
|
||||
{#each data.items as item (item.id)}
|
||||
<li>
|
||||
<a href="/{data.event}/registry/{item.id}" target="_blank" class="underline text-blue-500">{item.name}</a>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
19
src/routes/(public)/[event]/registry/[item]/+server.ts
Normal file
19
src/routes/(public)/[event]/registry/[item]/+server.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { AllowList, RegistryItems } from "#lib/server/data.js";
|
||||
import { error, redirect } from "@sveltejs/kit";
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
|
||||
export const GET: RequestHandler = ({ params }) => {
|
||||
const item = RegistryItems.find((i) => String(i.id) === params.item);
|
||||
|
||||
if (!item) {
|
||||
error(404, "Registry Item not found");
|
||||
};
|
||||
|
||||
const protocol = new URL(item.url).protocol;
|
||||
if (protocol !== 'https:' && protocol !== 'http:') {
|
||||
error(404, "Invalid Link");
|
||||
};
|
||||
|
||||
redirect(302, item.url, { external: AllowList });
|
||||
};
|
||||
Reference in New Issue
Block a user