Skip to content

Commit

Permalink
Strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 6, 2024
1 parent 941659e commit 4c6e0b5
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions functions/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo
let newUser: Account;
try {
newUser = await createAccount(body.username, body.email, body.password);
} catch (err) {
} catch (err: any) {

Check warning on line 17 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 17 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 17 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
return parseError(err);
}

return response(StatusCodes.OK, newUser);
} catch (e) {
} catch (e: any) {

Check warning on line 22 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 22 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 22 in functions/account/create.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
if (e instanceof Response) {
return e;
}
Expand Down
2 changes: 1 addition & 1 deletion functions/account/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo
await deleteAccount(target.id, body.reason);

return response(StatusCodes.OK);
} catch (e) {
} catch (e: any) {

Check warning on line 27 in functions/account/delete.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 27 in functions/account/delete.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 27 in functions/account/delete.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
if (e instanceof Response) {
return e;
}
Expand Down
6 changes: 3 additions & 3 deletions functions/account/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo

if (!body.multiple) {
checkParams(body, 'key', 'value');
const target = await getAccount(body.key, body.value);
const target = await getAccount(body.key!, body.value!);

await checkAuth({
auth: request,
Expand All @@ -52,12 +52,12 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo
}

checkParams(body, 'key', 'value');
const accounts = await getAccounts(body.key, body.value);
const accounts = await getAccounts(body.key!, body.value!);
return response(
StatusCodes.OK,
accounts.map(account => stripAccountInfo(account, body.access))
);
} catch (e) {
} catch (e: any) {

Check warning on line 60 in functions/account/info.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 60 in functions/account/info.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 60 in functions/account/info.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
if (e instanceof Response) {
return e;
}
Expand Down
4 changes: 2 additions & 2 deletions functions/account/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo
let token;
try {
token = await login(account.id);
} catch (err) {
} catch (err: any) {

Check warning on line 32 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 32 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 32 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
return parseError(err);
}

return response(StatusCodes.OK, { ...stripAccountInfo(account), token });
} catch (e) {
} catch (e: any) {

Check warning on line 37 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 37 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 37 in functions/account/login.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
if (e instanceof Response) {
return e;
}
Expand Down
4 changes: 2 additions & 2 deletions functions/account/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo

try {
await logout(target.id, body.reason);
} catch (err) {
} catch (err: any) {

Check warning on line 26 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 26 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 26 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
return parseError(err);
}

return response(StatusCodes.OK, true);
} catch (e) {
} catch (e: any) {

Check warning on line 31 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 31 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 31 in functions/account/logout.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
if (e instanceof Response) {
return e;
}
Expand Down
2 changes: 1 addition & 1 deletion functions/account/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function onRequest({ env }: RequestContext) {
try {
setDB(env.DB);
return response(StatusCodes.OK, await getAccountNum(), false);
} catch (e) {
} catch (e: any) {

Check warning on line 12 in functions/account/num.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 12 in functions/account/num.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 12 in functions/account/num.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
console.error(e);
return error(StatusCodes.INTERNAL_SERVER_ERROR, env.DEBUG && e?.message);
}
Expand Down
11 changes: 5 additions & 6 deletions functions/account/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AccountType, accountAttributes, stripAccountInfo, type FullAccount } fr
import { setAccountAttribute, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, error, getAccountFromTokenOrID, parseError, response } from '../../src/backend/utils';
import { Access } from '../../src/generic';

export { onRequestOptions } from '../../src/backend/utils';

Expand Down Expand Up @@ -31,18 +30,18 @@ export async function onRequest({ env, request }: RequestContext): Promise<Respo
await checkAuth({
auth: request,
target,
allowIfSame: ['username', 'email'].includes(body.key),
requiredType: requiredTypeForChange[body.key],
allowIfSame: ['username', 'email'].includes(body.key!),
requiredType: requiredTypeForChange[body.key!],
});

try {
await setAccountAttribute(target.id, body.key, body.value, body.reason);
} catch (err) {
await setAccountAttribute(target.id, body.key!, body.value!, body.reason);
} catch (err: any) {

Check warning on line 39 in functions/account/update.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 39 in functions/account/update.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unexpected any. Specify a different type

Check warning on line 39 in functions/account/update.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unexpected any. Specify a different type
throw parseError(err);
}

return response(StatusCodes.OK, stripAccountInfo(target));
} catch (e) {
} catch (e: any) {
if (e instanceof Response) {
return e;
}
Expand Down
2 changes: 1 addition & 1 deletion functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function onRequest({ env }: RequestContext) {
debug: !!env.DEBUG,
};
return response(StatusCodes.OK, metadata, false);
} catch (e) {
} catch (e: any) {
console.error(e);
return error(StatusCodes.INTERNAL_SERVER_ERROR, env.DEBUG && e?.message);
}
Expand Down
2 changes: 1 addition & 1 deletion functions/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function onRequest({ env }: RequestContext) {
debug: !!env.DEBUG,
};
return response(StatusCodes.OK, metadata, false);
} catch (e) {
} catch (e: any) {
console.error(e);
return error(StatusCodes.INTERNAL_SERVER_ERROR, env.DEBUG && e?.message);
}
Expand Down
12 changes: 6 additions & 6 deletions src/backend/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function sendMailToUser({ username, email }: { username: string; email?:
}

export async function getAccountNum(): Promise<number> {
return getDB().prepare('select count(1) as num from accounts').first<number>('num');
return (await getDB().prepare('select count(1) as num from accounts').first<number>('num'))!;
}

export async function getAccount(attr: string, value: string): Promise<FullAccount> {
Expand Down Expand Up @@ -170,8 +170,8 @@ export async function createAccount(username: string, email: string, rawPassword
}

export async function accountExists(id: string): Promise<boolean> {
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<FullAccount> {
Expand All @@ -188,7 +188,7 @@ export async function deleteAccount(id: string, reason?: string): Promise<FullAc
If you have any concerns please reach out to [email protected].`
);

return getDB().prepare('delete from accounts where id=?').bind(id).first();
return (await getDB().prepare('delete from accounts where id=?').bind(id).first())!;
}

export async function login(id: string): Promise<string> {
Expand All @@ -197,8 +197,8 @@ export async function login(id: string): Promise<string> {
return token;
}

export function logout(id: string, reason?: string): Promise<boolean> {
return getDB().prepare('update accounts set token="" where id=?').bind(id).first();
export async function logout(id: string, reason?: string): Promise<boolean> {
return (await getDB().prepare('update accounts set token="" where id=?').bind(id).first())!;
}

export async function generateSession(id: string): Promise<string> {
Expand Down
14 changes: 7 additions & 7 deletions src/backend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function parseBody<V extends Record<string, FormDataEntryValue>>(re
export function response<R>(status: StatusCodes = StatusCodes.OK, result?: R, error = false): Response {
const statusText: ReasonPhrases = ReasonPhrases[StatusCodes[status] as keyof typeof ReasonPhrases];

const body: APIResponse<R> = { status, statusText, result, error };
const body: APIResponse<R | undefined> = { status, statusText, result, error };
return new Response(JSON.stringify(body), {
status,
statusText,
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function auth({
allowIfSame = false,
access = Access.PROTECTED,
debug = false,
}: AuthorizationOptions): Promise<Response> {
}: AuthorizationOptions): Promise<Response | void> {
try {
if (access == Access.PUBLIC) {
return;
Expand All @@ -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);
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -131,11 +131,11 @@ export async function checkBody<const B>(request: CFRequest, ...params: (keyof B
}

export async function getAccountFromTokenOrID<const B extends { id?: string; token?: string }>(body: B): Promise<Account> {
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');
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/endpoints/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function parseAccount<A extends Account>(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];
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*/
export type KeyValue<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T];
}[keyof T] &
[unknown, unknown];
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"noEmit": true,
"esModuleInterop": true,
"allowJs": true,
"strictFunctionTypes": true,
"strict": true,
},
}

0 comments on commit 4c6e0b5

Please sign in to comment.