woah man
This commit is contained in:
@@ -54,15 +54,11 @@ export async function authenticate(c: Context<HonoEnv>, nxt: Next) {
|
||||
} else return c.json(genericResponse(false, "Authorization required"), 401);
|
||||
}
|
||||
|
||||
export enum GalvanicErrors {
|
||||
jex = "jex", // Error in account creation, check platform
|
||||
sploot = "sploot", // Error in account creation, steamid was not valid or profile could not be created
|
||||
export function recNetResult(success: boolean, error?: string) {
|
||||
return {success, error};
|
||||
}
|
||||
export function galvanicError(code: GalvanicErrors) {
|
||||
return {success: false, error:`Galvanic Error (code: ${code})`};
|
||||
}
|
||||
export function recNetError(err: string) {
|
||||
return {success:false, error: err};
|
||||
export function recNetResultResponse(c: Context<HonoEnv>, status: HTTPStatus, success: boolean, error?: string) {
|
||||
return c.json(recNetResult(success, error), status as ContentfulStatusCode);
|
||||
}
|
||||
|
||||
export function generateRandomString(length: number) {
|
||||
@@ -126,7 +122,7 @@ export class RateLimiter {
|
||||
this.#addressIncrement(address);
|
||||
|
||||
const hits = this.#getAddressHits(address);
|
||||
if (hits && hits > this.#hitLimit) return c.json(recNetError("Rate Limited. Please try again later."), 429);
|
||||
if (hits && hits > this.#hitLimit) return c.json(recNetResult(false, "Rate Limited. Please try again later."), 429);
|
||||
else return next();
|
||||
};
|
||||
}
|
||||
|
||||
46
src/util/flags.ts
Normal file
46
src/util/flags.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
ChatGPT GPT-5 prompt:
|
||||
|
||||
Create a TypeScript class that can handle operations on a number
|
||||
similar to how the [Flags] attribute changes enums in C#.
|
||||
Include methods for checking the presence of a certain bit, adding a bit if it is not included, and removing bits.
|
||||
|
||||
Make the class generic.
|
||||
*/
|
||||
export class BitFlags<T extends number> {
|
||||
private value: T;
|
||||
|
||||
constructor(initialValue: T | number = 0 as T) {
|
||||
this.value = initialValue as T;
|
||||
}
|
||||
|
||||
/** Get current numeric value */
|
||||
public getValue(): T {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/** Check if a specific flag is set */
|
||||
public has(flag: T): boolean {
|
||||
return (this.value & flag) === flag;
|
||||
}
|
||||
|
||||
/** Add a flag */
|
||||
public add(flag: T): void {
|
||||
this.value = (this.value | flag) as T;
|
||||
}
|
||||
|
||||
/** Remove a flag */
|
||||
public remove(flag: T): void {
|
||||
this.value = (this.value & ~flag) as T;
|
||||
}
|
||||
|
||||
/** Toggle a flag */
|
||||
public toggle(flag: T): void {
|
||||
this.value = (this.value ^ flag) as T;
|
||||
}
|
||||
|
||||
/** Reset all flags */
|
||||
public clear(): void {
|
||||
this.value = 0 as T;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import type { MiddlewareHandler } from "@hono/hono";
|
||||
import { z } from "zod";
|
||||
import type { HonoEnv } from "./types.ts";
|
||||
import { ZodSchema } from "zod/v4";
|
||||
import { HTTPStatus } from "@oneday/http-status";
|
||||
|
||||
// thanks claude, this hurt my brain!
|
||||
|
||||
@@ -11,9 +12,16 @@ export const typedZValidator = <
|
||||
S extends ZodSchema
|
||||
>(
|
||||
target: T,
|
||||
schema: S
|
||||
schema: S,
|
||||
httpOk?: boolean,
|
||||
recNetError?: string
|
||||
) => {
|
||||
return zValidator(target, schema) as MiddlewareHandler<
|
||||
return zValidator(target, schema, (result, c) => {
|
||||
if (!result.success) {
|
||||
if (recNetError) return c.json({ success: false, error: recNetError }, httpOk == true ? HTTPStatus.OK : HTTPStatus.BadRequest);
|
||||
else return c.json({ success: false, error: "Request validation failed" }, httpOk == true ? HTTPStatus.OK : HTTPStatus.BadRequest);
|
||||
}
|
||||
}) as MiddlewareHandler<
|
||||
HonoEnv,
|
||||
string,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user