From 1cd9d8391079e81f724b3db2d2803dd303ffbcb7 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Sat, 22 Apr 2023 09:28:42 +0200 Subject: [PATCH] Improve type safety --- .nova/Configuration.json | 1 + .prettierignore | 1 + src/hooks.server.ts | 3 +-- src/lib/server/db.ts | 38 ++++++++++++++++++++++++++++++++------ src/lib/server/timeline.ts | 5 +---- src/routes/+page.svelte | 2 +- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/.nova/Configuration.json b/.nova/Configuration.json index 51b43ff..1cc2406 100644 --- a/.nova/Configuration.json +++ b/.nova/Configuration.json @@ -1,4 +1,5 @@ { + "apexskier.eslint.config.eslintPath" : "node_modules\/@eslint\/eslintrc\/dist\/eslintrc.cjs", "apexskier.typescript.config.formatDocumentOnSave" : "true", "apexskier.typescript.config.isEnabledForJavascript" : "Enable", "apexskier.typescript.config.organizeImportsOnSave" : "true", diff --git a/.prettierignore b/.prettierignore index 3897265..6eaade7 100644 --- a/.prettierignore +++ b/.prettierignore @@ -6,6 +6,7 @@ node_modules .env .env.* !.env.example +/.nova # Ignore files for PNPM, NPM and YARN pnpm-lock.yaml diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 99616f5..ca9e60f 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -10,8 +10,7 @@ export const handleError = (({ error }) => { } return { - message: 'Whoops!', - code: (error as any)?.code ?? 'UNKNOWN' + message: `Something went wrong! ${error}` }; }) satisfies HandleServerError; diff --git a/src/lib/server/db.ts b/src/lib/server/db.ts index 0a4e0c2..b159973 100644 --- a/src/lib/server/db.ts +++ b/src/lib/server/db.ts @@ -45,14 +45,14 @@ db.on('open', () => { console.log('Opened database'); db.serialize(); 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) { console.error('Could not fetch existing migrations', err); databaseReady = true; return; } console.debug('Already applied migrations', rows); - const appliedMigrations: Set = new Set(rows.map((row: any) => row['id'])); + const appliedMigrations: Set = new Set(rows.map((row) => row['id'])); const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id)); let remaining = toApply.length; if (remaining === 0) { @@ -291,13 +291,20 @@ export async function savePost(post: Post): Promise { }); } +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) { if (!databaseReady) { await waitReady(); } const promise = await new Promise((resolve, reject) => { let filter_query = ''; - const params: any = { $limit: limit }; + const params: FilterParameter = { $limit: limit }; if (since === null && before === null) { filter_query = ''; } else if (since !== null) { @@ -319,6 +326,25 @@ export async function getPosts(since: string | null, before: string | null, limi 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, accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name, accounts.url AS account_url, accounts.avatar @@ -327,7 +353,7 @@ export async function getPosts(since: string | null, before: string | null, limi ${filter_query} ORDER BY created_at DESC LIMIT $limit`; - db.all(sql, params, (err, rows: any[]) => { + db.all(sql, params, (err, rows: PostResult[]) => { if (err != null) { console.error('Error loading posts', err); reject(err); @@ -344,8 +370,8 @@ export async function getPosts(since: string | null, before: string | null, limi FROM poststags JOIN tags ON poststags.tag_url = tags.url WHERE post_id IN (${postIdsParams});`, - rows.map((r: any) => r.url), - (tagErr, tagRows: any[]) => { + rows.map((r: PostResult) => r.url), + (tagErr, tagRows: PostTagResult[]) => { if (tagErr != null) { console.error('Error loading post tags', tagErr); reject(tagErr); diff --git a/src/lib/server/timeline.ts b/src/lib/server/timeline.ts index 7f78fe5..7489e7c 100644 --- a/src/lib/server/timeline.ts +++ b/src/lib/server/timeline.ts @@ -83,10 +83,7 @@ export class TimelineReader { return null; } - private static async getSongInfo( - url: string, - remainingTries: number = 6 - ): Promise { + private static async getSongInfo(url: string, remainingTries = 6): Promise { if (remainingTries === 0) { console.error('No tries remaining. Lookup failed!'); return null; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 55b8f7b..88e8f82 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -25,7 +25,7 @@ } const refreshInterval = parseInt(PUBLIC_REFRESH_INTERVAL); - let interval: NodeJS.Timer | null = null; + let interval: ReturnType | null = null; let moreOlderPostsAvailable = true; let loadingOlderPosts = false;