mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-28 04:41:18 +00:00
feat(backend): add PUT /api/sources/:sourceId (#87)
Add a PUT endpoint that inserts or fully replaces a user's source config. Unlike PATCH (which deep-merges and requires an existing row), PUT requires both `enabled` and `config`, performs an upsert via INSERT ... ON CONFLICT DO UPDATE, and replaces config entirely. - Add `upsertConfig` to user-sources data layer - Add `upsertSourceConfig` to UserSessionManager - Add `addSource` to UserSession for new source registration - 12 new tests covering insert, replace, validation, and session refresh Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -24,6 +24,11 @@ const UpdateSourceConfigRequestBody = type({
|
||||
"config?": "unknown",
|
||||
})
|
||||
|
||||
const ReplaceSourceConfigRequestBody = type({
|
||||
enabled: "boolean",
|
||||
config: "unknown",
|
||||
})
|
||||
|
||||
export function registerSourcesHttpHandlers(
|
||||
app: Hono,
|
||||
{ sessionManager, authSessionMiddleware }: SourcesHttpHandlersDeps,
|
||||
@@ -34,6 +39,7 @@ export function registerSourcesHttpHandlers(
|
||||
})
|
||||
|
||||
app.patch("/api/sources/:sourceId", inject, authSessionMiddleware, handleUpdateSource)
|
||||
app.put("/api/sources/:sourceId", inject, authSessionMiddleware, handleReplaceSource)
|
||||
}
|
||||
|
||||
async function handleUpdateSource(c: Context<Env>) {
|
||||
@@ -83,3 +89,49 @@ async function handleUpdateSource(c: Context<Env>) {
|
||||
|
||||
return c.body(null, 204)
|
||||
}
|
||||
|
||||
async function handleReplaceSource(c: Context<Env>) {
|
||||
const sourceId = c.req.param("sourceId")
|
||||
if (!sourceId) {
|
||||
return c.body(null, 404)
|
||||
}
|
||||
|
||||
const sessionManager = c.get("sessionManager")
|
||||
|
||||
const provider = sessionManager.getProvider(sourceId)
|
||||
if (!provider) {
|
||||
return c.json({ error: `Source "${sourceId}" not found` }, 404)
|
||||
}
|
||||
|
||||
let body: unknown
|
||||
try {
|
||||
body = await c.req.json()
|
||||
} catch {
|
||||
return c.json({ error: "Invalid JSON" }, 400)
|
||||
}
|
||||
|
||||
const parsed = ReplaceSourceConfigRequestBody(body)
|
||||
if (parsed instanceof type.errors) {
|
||||
return c.json({ error: parsed.summary }, 400)
|
||||
}
|
||||
|
||||
const { enabled, config } = parsed
|
||||
const user = c.get("user")!
|
||||
|
||||
try {
|
||||
await sessionManager.upsertSourceConfig(user.id, sourceId, {
|
||||
enabled,
|
||||
config,
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof SourceNotFoundError) {
|
||||
return c.json({ error: err.message }, 404)
|
||||
}
|
||||
if (err instanceof InvalidSourceConfigError) {
|
||||
return c.json({ error: err.message }, 400)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
return c.body(null, 204)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user