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.isEnabledForJavascript" : "Enable",
"apexskier.typescript.config.organizeImportsOnSave" : "true",

View File

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

View File

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

View File

@ -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<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));
let remaining = toApply.length;
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) {
if (!databaseReady) {
await waitReady();
}
const promise = await new Promise<Post[]>((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);

View File

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

View File

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