import { serve } from '@hono/node-server'
import { HookfishServer } from '@hookfish/api'
import { Hookfish } from '@hookfish/sdk'
import { Hono } from 'hono'
import config from '../hookfish.config'
import { requireUser } from './auth'
import { scopedBrokerTokenFor } from './broker-tokens'
const hookfishServer = await HookfishServer.init(config)
const app = new Hono()
const applicationUrl =
process.env.APPLICATION_URL ?? 'http://127.0.0.1:3000'
app.route('/', hookfishServer)
const applicationRoutes = new Hono()
.get('/rpc/integrations/connections', requireUser, async (context) => {
const user = context.get('user')
const brokerToken = await scopedBrokerTokenFor(user.organizationId)
const hookfish = new Hookfish({
apiKey: brokerToken,
baseUrl: `${applicationUrl}/api`,
})
const connections = await hookfish.connections.list({
namespace: `organizations/${user.organizationId}`,
})
return context.json(connections)
})
app.route('/', applicationRoutes)
serve({
fetch: (request) => app.fetch(request, process.env),
hostname: process.env.HOST ?? '127.0.0.1',
port: Number(process.env.PORT ?? 3000),
})
export type ApplicationApi = typeof applicationRoutes