-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { APIs } from "constants/urls"; | ||
import { type LoaderFunctionArgs, redirect } from "react-router-dom"; | ||
import { apiEnv } from "services/constants"; | ||
import * as v from "valibot"; | ||
|
||
const schema = v.union([ | ||
v.pipe( | ||
v.string(), | ||
v.transform((x) => +x), | ||
v.number(), | ||
v.integer(), | ||
v.minValue(1) | ||
), | ||
v.pipe( | ||
v.string(), | ||
v.trim(), | ||
v.nonEmpty(), | ||
//must not be id-like | ||
v.regex(/^(?!^\d+$)/, "should not be an id"), | ||
//valid characters | ||
v.regex(/^[a-zA-Z0-9-._~]+$/, "invalid segment"), | ||
v.excludes("..", "invalid segment"), | ||
v.custom((x) => !(x as string).startsWith("."), "invalid segment"), | ||
v.custom((x) => !(x as string).endsWith("."), "invalid segment") | ||
), | ||
]); | ||
|
||
export const profileLoader = async ({ | ||
params, | ||
}: LoaderFunctionArgs): Promise<Response | undefined> => { | ||
const cache = await caches.open("bg"); | ||
const { output: id, issues } = v.safeParse(schema, params.id); | ||
if (issues) return redirect("/marketplace"); | ||
|
||
const url = new URL(APIs.aws); | ||
url.searchParams.set("env", apiEnv); | ||
if (typeof id === "number") { | ||
url.pathname = `v9/endowments/${id}`; | ||
} | ||
if (typeof id === "string") { | ||
url.pathname = `v9/endowments`; | ||
url.searchParams.set("slug", id); | ||
} | ||
const c = await cache.match(url); | ||
if (c) return c.clone(); | ||
|
||
await cache.add(url); | ||
return cache.match(url); | ||
}; |