Avatar

yieldray

Joined January 16, 2023
Public vals
19
yieldray avatar
yieldray
decorator_router
Script
Decorator Router Fair simple decorator based router, with GET/POST and middleware support. demo live demo: https://yieldray-decorator_router_demo.web.val.run/ import { get, post, all, use, registered, handler, type Context } from "https://esm.town/v/yieldray/decorator_router"; import { parseBearerAuth, transformResponse } from "https://esm.sh/serve-router@1.1.0/utils"; interface User { id: number; name: string; } const users: User[] = [ { id: 0, name: "Alice" }, { id: 1, name: "Ray" }, ]; class _Server { /** * Decorator @get: Parses URLSearchParams into an object as the first parameter. */ @get("/users") getAllUsers() { return users; // Automatically wrapped in a Response.json } @get("/getUserByName") // GET /getUserByName?name=Alice getUserByName({ name }: Record<string, string>) { const user = users.find((u) => u.name === name); if (user) { return user; // Automatically wrapped as Response.json(user) } // Optionally, manually return a Response object return Response.json({ error: "not found" }, { status: 404 }); } @get("/user/:id") // GET /user/123 user(_: unknown, { params: { id } }: Context) { return users.find((u) => u.id === Number(id)); } /** * Decorator @post: Parses the request body into an object as the first parameter. */ @post("/user") // POST /user async createUser(user: User) { if (users.find((u) => u.id === user.id)) { return { error: "already exists!" }; } await users.push(user); // Assume insertion into a database return { ok: true, users }; } @post("/user/:id") // POST /user/123 async updateUser(user: User, { params: { id }, request }: Context) { const token = parseBearerAuth(request.headers.get("Authorization")!); // Additional logic here... } @all("/") home({ request }: { request: Request }) { return { registered, method: request.method, url: request.url, headers: Object.fromEntries(request.headers.entries()), }; } @use("/*") async corsMiddleware({ next, request }: Context) { const resp = await next(); return transformResponse(resp, { headers: { "Access-Control-Allow-Origin": request.headers.get("origin") || "*", }, }); } } // For Deno: Deno.serve(handler); // For val.town: export default handler;
0
yieldray avatar
yieldray
minhtml
HTTP
An interactive, runnable TypeScript val by yieldray
0
yieldray avatar
yieldray
fav
HTTP
An https://fav.farm alternative built on val.town Fork this val to host your own!
1
yieldray avatar
yieldray
crux_add
Script
An interactive, runnable TypeScript val by yieldray
1
yieldray avatar
yieldray
draw
HTTP
Draw
0
yieldray avatar
yieldray
gists
HTTP
Get A Website for Your Gists Example: https://yieldray-gists.web.val.run Usage: fork this val and replace with your github usename
0
yieldray avatar
yieldray
translate
HTTP
Translator using a public deepl api
2
yieldray avatar
yieldray
notebook
HTTP
https://yieldray-notebook.web.val.run/
0
yieldray avatar
yieldray
notems
Script
An interactive, runnable TypeScript val by yieldray
0
yieldray avatar
yieldray
serve_router
HTTP
serve-router Example https://yieldray-serve_router.web.val.run https://yieldray-serve_router.web.val.run/user/ray https://yieldray-serve_router.web.val.run/headers https://yieldray-serve_router.web.val.run/api https://yieldray-serve_router.web.val.run/api/one
0
yieldray avatar
yieldray
web
HTTP
Debug ValTown's Web API
0
yieldray avatar
yieldray
pushplus
Script
get token from: https://www.pushplus.plus/push1.html
2
yieldray avatar
yieldray
anime
Script
An interactive, runnable TypeScript val by yieldray
0
yieldray avatar
yieldray
minifyHTML
Script
Usage: import { minifyHTML } from "https://esm.town/v/yieldray/minifyHTML"; const minified = await minifyHTML("<html>...</html>", { ...options }); As an API: import { runVal } from "https://esm.town/v/std/runVal"; const minified = awaitrunVal("yieldray.minifyHTML", "<html>...</html>", { ...options });
1
yieldray avatar
yieldray
uuid
Express (deprecated)
UUID Get an uuid via http GET call
2
yieldray avatar
yieldray
kv
Script
An interactive, runnable TypeScript val by yieldray
2