chore: update codestyle due to prettier/rule updates

This commit is contained in:
41666 2021-06-30 08:02:25 -04:00
parent f632bfa6e5
commit 10e095656f
16 changed files with 298 additions and 292 deletions

View file

@ -27,17 +27,19 @@ export const addCORS = (init: ResponseInit = {}) => ({
export const respond = (obj: Record<string, any>, init: ResponseInit = {}) =>
new Response(JSON.stringify(obj), addCORS(init));
export const resolveFailures = (
handleWith: () => Response,
handler: (request: Request) => Promise<Response> | Response
) => async (request: Request): Promise<Response> => {
try {
return handler(request);
} catch (e) {
console.error(e);
return handleWith() || respond({ error: 'internal server error' }, { status: 500 });
}
};
export const resolveFailures =
(
handleWith: () => Response,
handler: (request: Request) => Promise<Response> | Response
) =>
async (request: Request): Promise<Response> => {
try {
return handler(request);
} catch (e) {
console.error(e);
return handleWith() || respond({ error: 'internal server error' }, { status: 500 });
}
};
export const parsePermissions = (
permissions: bigint,
@ -106,33 +108,35 @@ export const discordFetch = async <T>(
}
};
export const cacheLayer = <Identity, Data>(
kv: WrappedKVNamespace,
keyFactory: (identity: Identity) => string,
missHandler: (identity: Identity) => Promise<Data | null>,
ttlSeconds?: number
) => async (
identity: Identity,
options: { skipCachePull?: boolean } = {}
): Promise<Data | null> => {
const key = keyFactory(identity);
export const cacheLayer =
<Identity, Data>(
kv: WrappedKVNamespace,
keyFactory: (identity: Identity) => string,
missHandler: (identity: Identity) => Promise<Data | null>,
ttlSeconds?: number
) =>
async (
identity: Identity,
options: { skipCachePull?: boolean } = {}
): Promise<Data | null> => {
const key = keyFactory(identity);
if (!options.skipCachePull) {
const value = await kv.get<Data>(key);
if (value) {
return value;
if (!options.skipCachePull) {
const value = await kv.get<Data>(key);
if (value) {
return value;
}
}
}
const fallbackValue = await missHandler(identity);
if (!fallbackValue) {
return null;
}
const fallbackValue = await missHandler(identity);
if (!fallbackValue) {
return null;
}
await kv.put(key, fallbackValue, ttlSeconds);
await kv.put(key, fallbackValue, ttlSeconds);
return fallbackValue;
};
return fallbackValue;
};
const NotAuthenticated = (extra?: string) =>
respond(
@ -142,21 +146,21 @@ const NotAuthenticated = (extra?: string) =>
{ status: 403 }
);
export const withSession = (
wrappedHandler: (session: SessionData) => Handler
): Handler => async (request: Request): Promise<Response> => {
const sessionID = getSessionID(request);
if (!sessionID) {
return NotAuthenticated('missing authentication');
}
export const withSession =
(wrappedHandler: (session: SessionData) => Handler): Handler =>
async (request: Request): Promise<Response> => {
const sessionID = getSessionID(request);
if (!sessionID) {
return NotAuthenticated('missing authentication');
}
const session = await Sessions.get<SessionData>(sessionID.id);
if (!session) {
return NotAuthenticated('authentication expired or not found');
}
const session = await Sessions.get<SessionData>(sessionID.id);
if (!session) {
return NotAuthenticated('authentication expired or not found');
}
return await wrappedHandler(session)(request);
};
return await wrappedHandler(session)(request);
};
export const setupStateSession = async <T>(data: T): Promise<string> => {
const stateID = (await KSUID.random()).string;

View file

@ -1,4 +1,4 @@
const self = (global as any) as Record<string, string>;
const self = global as any as Record<string, string>;
const env = (key: string) => self[key] ?? '';

View file

@ -53,11 +53,7 @@ class EmulatedKV implements KVNamespace {
this.data.delete(key);
}
async list(options?: {
prefix?: string;
limit?: number;
cursor?: string;
}): Promise<{
async list(options?: { prefix?: string; limit?: number; cursor?: string }): Promise<{
keys: { name: string; expiration?: number; metadata?: unknown }[];
list_complete: boolean;
cursor: string;
@ -83,7 +79,7 @@ class EmulatedKV implements KVNamespace {
const kvOrLocal = (namespace: KVNamespace | null): KVNamespace =>
namespace || new EmulatedKV();
const self = (global as any) as Record<string, any>;
const self = global as any as Record<string, any>;
export const Sessions = new WrappedKVNamespace(kvOrLocal(self.KV_SESSIONS ?? null));
export const GuildData = new WrappedKVNamespace(kvOrLocal(self.KV_GUILD_DATA ?? null));

View file

@ -1,15 +1,13 @@
import { WrappedKVNamespace } from './kv';
export const useRateLimiter = (
kv: WrappedKVNamespace,
key: string,
timeoutSeconds: number
) => async (): Promise<boolean> => {
const value = await kv.get<boolean>(key);
if (value) {
return true;
}
export const useRateLimiter =
(kv: WrappedKVNamespace, key: string, timeoutSeconds: number) =>
async (): Promise<boolean> => {
const value = await kv.get<boolean>(key);
if (value) {
return true;
}
await kv.put(key, true, timeoutSeconds);
return false;
};
await kv.put(key, true, timeoutSeconds);
return false;
};