diff --git a/.eslintrc.json b/.eslintrc.json index 7a6bb76..f120e56 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -32,6 +32,7 @@ "@typescript-eslint/adjacent-overload-signatures": "warn", "@typescript-eslint/no-inferrable-types": "warn", "@typescript-eslint/no-namespace": "off", - "@typescript-eslint/no-duplicate-enum-values": "off" + "@typescript-eslint/no-duplicate-enum-values": "off", + "@typescript-eslint/no-explicit-any": "warn" } } diff --git a/functions/account/create.ts b/functions/account/create.ts index d9dc618..8979fdd 100644 --- a/functions/account/create.ts +++ b/functions/account/create.ts @@ -14,12 +14,12 @@ export async function onRequest({ env, request }: RequestContext): Promise stripAccountInfo(account, body.access)) ); - } catch (e) { + } catch (e: any) { if (e instanceof Response) { return e; } diff --git a/functions/account/login.ts b/functions/account/login.ts index 31a7e0a..7eec3bc 100644 --- a/functions/account/login.ts +++ b/functions/account/login.ts @@ -29,12 +29,12 @@ export async function onRequest({ env, request }: RequestContext): Promise { - return getDB().prepare('select count(1) as num from accounts').first('num'); + return (await getDB().prepare('select count(1) as num from accounts').first('num'))!; } export async function getAccount(attr: string, value: string): Promise { @@ -170,8 +170,8 @@ export async function createAccount(username: string, email: string, rawPassword } export async function accountExists(id: string): Promise { - const result = await getDB().prepare('select count(1) as num from accounts where id=?').bind(id).all(); - return !!result[0].num; + const { results } = await getDB().prepare('select count(1) as num from accounts where id=?').bind(id).all(); + return !!results[0].num; } export async function deleteAccount(id: string, reason?: string): Promise { @@ -188,7 +188,7 @@ export async function deleteAccount(id: string, reason?: string): Promise { @@ -197,8 +197,8 @@ export async function login(id: string): Promise { return token; } -export function logout(id: string, reason?: string): Promise { - return getDB().prepare('update accounts set token="" where id=?').bind(id).first(); +export async function logout(id: string, reason?: string): Promise { + return (await getDB().prepare('update accounts set token="" where id=?').bind(id).first())!; } export async function generateSession(id: string): Promise { diff --git a/src/backend/utils.ts b/src/backend/utils.ts index 47d0a1e..5e6e6e1 100644 --- a/src/backend/utils.ts +++ b/src/backend/utils.ts @@ -20,7 +20,7 @@ export async function parseBody>(re export function response(status: StatusCodes = StatusCodes.OK, result?: R, error = false): Response { const statusText: ReasonPhrases = ReasonPhrases[StatusCodes[status] as keyof typeof ReasonPhrases]; - const body: APIResponse = { status, statusText, result, error }; + const body: APIResponse = { status, statusText, result, error }; return new Response(JSON.stringify(body), { status, statusText, @@ -55,7 +55,7 @@ export async function auth({ allowIfSame = false, access = Access.PROTECTED, debug = false, -}: AuthorizationOptions): Promise { +}: AuthorizationOptions): Promise { try { if (access == Access.PUBLIC) { return; @@ -67,7 +67,7 @@ export async function auth({ if (!auth.headers.has('Authorization')) { return error(StatusCodes.UNAUTHORIZED, 'Missing authorization header'); } - auth = auth.headers.get('Authorization'); + auth = auth.headers.get('Authorization')!; } if (auth.startsWith('Bearer ')) { auth = auth.substring(7); @@ -82,10 +82,10 @@ export async function auth({ return error(StatusCodes.UNAUTHORIZED, 'Invalid auth token'); } - if (authUser.type < Math.max(requiredType, +target?.type + 1) && (target?.id != authUser.id || !allowIfSame) && access < Access.PUBLIC) { + if (authUser.type < Math.max(requiredType, (target?.type || 0) + 1) && (target?.id != authUser.id || !allowIfSame) && access < Access.PUBLIC) { return error(StatusCodes.FORBIDDEN, 'Permission denied'); } - } catch (e) { + } catch (e: any) { return error(StatusCodes.INTERNAL_SERVER_ERROR, 'Authorization failed' + (debug && ': ' + e.message)); } } @@ -131,11 +131,11 @@ export async function checkBody(request: CFRequest, ...params: (keyof B } export async function getAccountFromTokenOrID(body: B): Promise { - if (!(body.id || body.token)) { + if (!(body.token || body.id)) { throw error(StatusCodes.BAD_REQUEST, 'Missing id or token'); } - const targetUser = await getAccount(body.token ? 'token' : 'id', body.token || body.id); + const targetUser = await getAccount(body.token ? 'token' : 'id', (body.token || body.id)!); if (!targetUser) { throw error(StatusCodes.NOT_FOUND, 'Target user does not exist'); diff --git a/src/frontend/endpoints/account.ts b/src/frontend/endpoints/account.ts index 5afec00..254d391 100644 --- a/src/frontend/endpoints/account.ts +++ b/src/frontend/endpoints/account.ts @@ -20,7 +20,7 @@ function parseAccount(result: AccountResult): A { created: new Date(result?.created), is_disabled: result?.is_disabled, }; - for (const maybe of ['token', 'session', 'email']) { + for (const maybe of ['token', 'session', 'email'] as const) { if (maybe in result) { parsed[maybe] = result[maybe]; } diff --git a/src/utils.ts b/src/utils.ts index 1e0900b..0d7dc12 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,4 +3,5 @@ */ export type KeyValue = { [K in keyof T]: [K, T[K]]; -}[keyof T]; +}[keyof T] & + [unknown, unknown]; diff --git a/tsconfig.json b/tsconfig.json index 931a006..ee5bbe8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,6 @@ "noEmit": true, "esModuleInterop": true, "allowJs": true, - "strictFunctionTypes": true, + "strict": true, }, }