From f20fb8052bff2b6e71e54531ff3c086dad2fc00f Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 24 Feb 2023 10:19:25 +0000 Subject: [PATCH] improve typing around getDataBase --- backend/src/database/d1.ts | 4 ++-- backend/src/database/index.ts | 2 +- consumer/src/deliver.ts | 4 ++-- consumer/src/inbox.ts | 2 +- consumer/src/index.ts | 2 +- frontend/src/routes/(admin)/oauth/authorize/index.tsx | 6 +++--- .../routes/(frontend)/[accountId]/[statusId]/index.tsx | 8 ++++---- frontend/src/routes/(frontend)/[accountId]/index.tsx | 2 +- frontend/src/routes/(frontend)/[accountId]/layout.tsx | 4 ++-- .../routes/(frontend)/[accountId]/with_replies/index.tsx | 2 +- frontend/src/routes/(frontend)/explore/index.tsx | 2 +- frontend/src/routes/(frontend)/public/index.tsx | 2 +- frontend/src/routes/(frontend)/public/local/index.tsx | 2 +- 13 files changed, 21 insertions(+), 21 deletions(-) diff --git a/backend/src/database/d1.ts b/backend/src/database/d1.ts index 576bdce..99359e8 100644 --- a/backend/src/database/d1.ts +++ b/backend/src/database/d1.ts @@ -1,6 +1,6 @@ import { type Database } from 'wildebeest/backend/src/database' import type { Env } from 'wildebeest/backend/src/types/env' -export default function make(env: Env): Database { - return env.DATABASE +export default function make({ DATABASE }: Pick): Database { + return DATABASE } diff --git a/backend/src/database/index.ts b/backend/src/database/index.ts index ca8fbe4..260409e 100644 --- a/backend/src/database/index.ts +++ b/backend/src/database/index.ts @@ -23,6 +23,6 @@ export interface PreparedStatement { raw(): Promise } -export function getDatabase(env: Env): Database { +export function getDatabase(env: Pick): Database { return d1(env) } diff --git a/consumer/src/deliver.ts b/consumer/src/deliver.ts index 0e4021b..c033825 100644 --- a/consumer/src/deliver.ts +++ b/consumer/src/deliver.ts @@ -8,12 +8,12 @@ import { deliverToActor } from 'wildebeest/backend/src/activitypub/deliver' export async function handleDeliverMessage(env: Env, actor: Actor, message: DeliverMessageBody) { const toActorId = new URL(message.toActorId) - const targetActor = await actors.getAndCache(toActorId, getDatabase(env as any)) + const targetActor = await actors.getAndCache(toActorId, getDatabase(env)) if (targetActor === null) { console.warn(`actor ${toActorId} not found`) return } - const signingKey = await getSigningKey(message.userKEK, getDatabase(env as any), actor) + const signingKey = await getSigningKey(message.userKEK, getDatabase(env), actor) await deliverToActor(signingKey, actor, targetActor, message.activity, env.DOMAIN) } diff --git a/consumer/src/inbox.ts b/consumer/src/inbox.ts index 8c54d5b..35687c4 100644 --- a/consumer/src/inbox.ts +++ b/consumer/src/inbox.ts @@ -9,7 +9,7 @@ import type { Env } from './' export async function handleInboxMessage(env: Env, actor: Actor, message: InboxMessageBody) { const domain = env.DOMAIN - const db = getDatabase(env as any) + const db = getDatabase(env) const adminEmail = env.ADMIN_EMAIL const cache = cacheFromEnv(env) const activity = message.activity diff --git a/consumer/src/index.ts b/consumer/src/index.ts index 0881094..bf070b1 100644 --- a/consumer/src/index.ts +++ b/consumer/src/index.ts @@ -20,7 +20,7 @@ export type Env = { export default { async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext) { const sentry = initSentryQueue(env, ctx) - const db = getDatabase(env as any) + const db = getDatabase(env) try { for (const message of batch.messages) { diff --git a/frontend/src/routes/(admin)/oauth/authorize/index.tsx b/frontend/src/routes/(admin)/oauth/authorize/index.tsx index 657bc4e..0085774 100644 --- a/frontend/src/routes/(admin)/oauth/authorize/index.tsx +++ b/frontend/src/routes/(admin)/oauth/authorize/index.tsx @@ -14,7 +14,7 @@ export const clientLoader = loader$, { DATABASE: D1Database }>(a const client_id = query.get('client_id') || '' let client: Client | null = null try { - client = await getClientById(getDatabase(platform as any), client_id) + client = await getClientById(getDatabase(platform), client_id) } catch (e: unknown) { const error = e as { stack: string; cause: string } console.warn(error.stack, error.cause) @@ -49,10 +49,10 @@ export const userLoader = loader$< throw html(500, getErrorHtml("The Access JWT doesn't contain an email")) } - const person = await getPersonByEmail(getDatabase(platform as any), payload.email) + const person = await getPersonByEmail(getDatabase(platform), payload.email) if (person === null) { const isFirstLogin = true - const res = await buildRedirect(getDatabase(platform as any), request as Request, isFirstLogin, jwt.value) + const res = await buildRedirect(getDatabase(platform), request as Request, isFirstLogin, jwt.value) if (res.status === 302) { throw redirect(302, res.headers.get('location') || '') } else { diff --git a/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx b/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx index c0e2e86..06951cd 100644 --- a/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx @@ -1,5 +1,5 @@ import { component$ } from '@builder.io/qwik' -import { getDatabase } from 'wildebeest/backend/src/database' +import { Database, getDatabase } from 'wildebeest/backend/src/database' import { MastodonStatus, StatusContext } from '~/types' import Status from '~/components/Status' import * as statusAPI from 'wildebeest/functions/api/v1/statuses/[id]' @@ -13,13 +13,13 @@ import { Person } from 'wildebeest/backend/src/activitypub/actors' export const statusLoader = loader$< Promise<{ status: MastodonStatus; statusTextContent: string; context: StatusContext }>, - { DATABASE: D1Database } + { DATABASE: Database } >(async ({ request, html, platform, params }) => { const domain = new URL(request.url).hostname let statusText = '' try { const statusResponse = await statusAPI.handleRequestGet( - getDatabase(platform as any), + getDatabase(platform), params.statusId, domain, {} as Person @@ -37,7 +37,7 @@ export const statusLoader = loader$< const statusTextContent = await getTextContent(status.content) try { - const contextResponse = await contextAPI.handleRequest(domain, getDatabase(platform as any), params.statusId) + const contextResponse = await contextAPI.handleRequest(domain, getDatabase(platform), params.statusId) const contextText = await contextResponse.text() const context = JSON.parse(contextText ?? null) as StatusContext | null if (!context) { diff --git a/frontend/src/routes/(frontend)/[accountId]/index.tsx b/frontend/src/routes/(frontend)/[accountId]/index.tsx index 0a551ed..8cf8de8 100644 --- a/frontend/src/routes/(frontend)/[accountId]/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/index.tsx @@ -22,7 +22,7 @@ export const statusesLoader = loader$< const handle = parseHandle(accountId) accountId = handle.localPart - const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, false) + const response = await getLocalStatuses(request as Request, getDatabase(platform), handle, 0, false) statuses = await response.json>() } catch { throw html( diff --git a/frontend/src/routes/(frontend)/[accountId]/layout.tsx b/frontend/src/routes/(frontend)/[accountId]/layout.tsx index 8a7891e..7985839 100644 --- a/frontend/src/routes/(frontend)/[accountId]/layout.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/layout.tsx @@ -25,14 +25,14 @@ export const accountPageLoader = loader$< const accountId = url.pathname.split('/')[1] try { - const statusResponse = await statusAPI.handleRequestGet(getDatabase(platform as any), params.statusId, domain) + const statusResponse = await statusAPI.handleRequestGet(getDatabase(platform), params.statusId, domain) const statusText = await statusResponse.text() isValidStatus = !!statusText } catch { isValidStatus = false } - account = await getAccount(domain, accountId, getDatabase(platform as any)) + account = await getAccount(domain, accountId, getDatabase(platform)) } catch { throw html( 500, diff --git a/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx b/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx index 2436092..8009950 100644 --- a/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx @@ -23,7 +23,7 @@ export const statusesLoader = loader$< const handle = parseHandle(accountId) accountId = handle.localPart - const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, true) + const response = await getLocalStatuses(request as Request, getDatabase(platform), handle, 0, true) statuses = await response.json>() } catch { throw html( diff --git a/frontend/src/routes/(frontend)/explore/index.tsx b/frontend/src/routes/(frontend)/explore/index.tsx index 14bb9a0..932f4ff 100644 --- a/frontend/src/routes/(frontend)/explore/index.tsx +++ b/frontend/src/routes/(frontend)/explore/index.tsx @@ -11,7 +11,7 @@ export const statusesLoader = loader$, { DATABASE: D1D async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any)) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform)) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[] diff --git a/frontend/src/routes/(frontend)/public/index.tsx b/frontend/src/routes/(frontend)/public/index.tsx index ffa4fdd..f32a349 100644 --- a/frontend/src/routes/(frontend)/public/index.tsx +++ b/frontend/src/routes/(frontend)/public/index.tsx @@ -12,7 +12,7 @@ export const statusesLoader = loader$, { DATABASE: D1D async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any)) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform)) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[] diff --git a/frontend/src/routes/(frontend)/public/local/index.tsx b/frontend/src/routes/(frontend)/public/local/index.tsx index 70d9a53..f756133 100644 --- a/frontend/src/routes/(frontend)/public/local/index.tsx +++ b/frontend/src/routes/(frontend)/public/local/index.tsx @@ -12,7 +12,7 @@ export const statusesLoader = loader$, { DATABASE: D1D async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any), { local: true }) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform), { local: true }) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[]