Improve type safety

This commit is contained in:
Max Nuding 2023-04-22 09:28:42 +02:00
parent b62936ed54
commit 1cd9d83910
Signed by: phlaym
GPG Key ID: A06651BAB6777237
6 changed files with 37 additions and 13 deletions

View File

@ -1,4 +1,5 @@
{ {
"apexskier.eslint.config.eslintPath" : "node_modules\/@eslint\/eslintrc\/dist\/eslintrc.cjs",
"apexskier.typescript.config.formatDocumentOnSave" : "true", "apexskier.typescript.config.formatDocumentOnSave" : "true",
"apexskier.typescript.config.isEnabledForJavascript" : "Enable", "apexskier.typescript.config.isEnabledForJavascript" : "Enable",
"apexskier.typescript.config.organizeImportsOnSave" : "true", "apexskier.typescript.config.organizeImportsOnSave" : "true",

View File

@ -6,6 +6,7 @@ node_modules
.env .env
.env.* .env.*
!.env.example !.env.example
/.nova
# Ignore files for PNPM, NPM and YARN # Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml pnpm-lock.yaml

View File

@ -10,8 +10,7 @@ export const handleError = (({ error }) => {
} }
return { return {
message: 'Whoops!', message: `Something went wrong! ${error}`
code: (error as any)?.code ?? 'UNKNOWN'
}; };
}) satisfies HandleServerError; }) satisfies HandleServerError;

View File

@ -45,14 +45,14 @@ db.on('open', () => {
console.log('Opened database'); console.log('Opened database');
db.serialize(); db.serialize();
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))'); db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
db.all('SELECT id FROM migrations', (err, rows) => { db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
if (err !== null) { if (err !== null) {
console.error('Could not fetch existing migrations', err); console.error('Could not fetch existing migrations', err);
databaseReady = true; databaseReady = true;
return; return;
} }
console.debug('Already applied migrations', rows); console.debug('Already applied migrations', rows);
const appliedMigrations: Set<number> = new Set(rows.map((row: any) => row['id'])); const appliedMigrations: Set<number> = new Set(rows.map((row) => row['id']));
const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id)); const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id));
let remaining = toApply.length; let remaining = toApply.length;
if (remaining === 0) { if (remaining === 0) {
@ -291,13 +291,20 @@ export async function savePost(post: Post): Promise<undefined> {
}); });
} }
type FilterParameter = {
$limit: number | undefined | null;
$since?: string | undefined | null;
$before?: string | undefined | null;
[x: string]: string | number | undefined | null;
};
export async function getPosts(since: string | null, before: string | null, limit: number) { export async function getPosts(since: string | null, before: string | null, limit: number) {
if (!databaseReady) { if (!databaseReady) {
await waitReady(); await waitReady();
} }
const promise = await new Promise<Post[]>((resolve, reject) => { const promise = await new Promise<Post[]>((resolve, reject) => {
let filter_query = ''; let filter_query = '';
const params: any = { $limit: limit }; const params: FilterParameter = { $limit: limit };
if (since === null && before === null) { if (since === null && before === null) {
filter_query = ''; filter_query = '';
} else if (since !== null) { } else if (since !== null) {
@ -319,6 +326,25 @@ export async function getPosts(since: string | null, before: string | null, limi
params[usernameParam] = ignoredUser; params[usernameParam] = ignoredUser;
}); });
type PostResult = {
id: string;
content: string;
created_at: string;
url: string;
account_id: string;
acct: string;
username: string;
display_name: string;
account_url: string;
avatar: string;
};
type PostTagResult = {
post_id: string;
tag: string;
url: string;
};
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url, const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name, accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name,
accounts.url AS account_url, accounts.avatar accounts.url AS account_url, accounts.avatar
@ -327,7 +353,7 @@ export async function getPosts(since: string | null, before: string | null, limi
${filter_query} ${filter_query}
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT $limit`; LIMIT $limit`;
db.all(sql, params, (err, rows: any[]) => { db.all(sql, params, (err, rows: PostResult[]) => {
if (err != null) { if (err != null) {
console.error('Error loading posts', err); console.error('Error loading posts', err);
reject(err); reject(err);
@ -344,8 +370,8 @@ export async function getPosts(since: string | null, before: string | null, limi
FROM poststags FROM poststags
JOIN tags ON poststags.tag_url = tags.url JOIN tags ON poststags.tag_url = tags.url
WHERE post_id IN (${postIdsParams});`, WHERE post_id IN (${postIdsParams});`,
rows.map((r: any) => r.url), rows.map((r: PostResult) => r.url),
(tagErr, tagRows: any[]) => { (tagErr, tagRows: PostTagResult[]) => {
if (tagErr != null) { if (tagErr != null) {
console.error('Error loading post tags', tagErr); console.error('Error loading post tags', tagErr);
reject(tagErr); reject(tagErr);

View File

@ -83,10 +83,7 @@ export class TimelineReader {
return null; return null;
} }
private static async getSongInfo( private static async getSongInfo(url: string, remainingTries = 6): Promise<SongInfo | null> {
url: string,
remainingTries: number = 6
): Promise<SongInfo | null> {
if (remainingTries === 0) { if (remainingTries === 0) {
console.error('No tries remaining. Lookup failed!'); console.error('No tries remaining. Lookup failed!');
return null; return null;

View File

@ -25,7 +25,7 @@
} }
const refreshInterval = parseInt(PUBLIC_REFRESH_INTERVAL); const refreshInterval = parseInt(PUBLIC_REFRESH_INTERVAL);
let interval: NodeJS.Timer | null = null; let interval: ReturnType<typeof setTimeout> | null = null;
let moreOlderPostsAvailable = true; let moreOlderPostsAvailable = true;
let loadingOlderPosts = false; let loadingOlderPosts = false;