add turns and update token logic.
This commit is contained in:
46
src/lib/server/requestTools.ts
Normal file
46
src/lib/server/requestTools.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import type { LocalCredentials } from "./auth";
|
||||
|
||||
export enum ResourceId {
|
||||
Game = "gameid",
|
||||
}
|
||||
|
||||
export function getUser(locals: { user: LocalCredentials }) {
|
||||
// SvelteKit screws up this type somehow, and it's important to explicitely set it
|
||||
// here.
|
||||
const user: LocalCredentials = locals.user;
|
||||
|
||||
if (user.kind !== "Bearer") {
|
||||
throw new Error("bad user information");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
export function getParam(params: Partial<Record<string, string>>, resource: ResourceId) {
|
||||
const id = params[resource];
|
||||
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
export async function getBody<T>(
|
||||
request: Request,
|
||||
dataGuard: (body: unknown) => body is T,
|
||||
) {
|
||||
let body: unknown;
|
||||
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!dataGuard(body)) {
|
||||
throw new Error("data guard failed");
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
Reference in New Issue
Block a user