feat(api): links routes
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import { router as agentsRouter, tokensRouter as agentTokensRouter } from './rou
|
||||
import { router as conversationsRouter } from './routes/conversations.js';
|
||||
import { conversationsScopedRouter as messagesByConvRouter } from './routes/messages.js';
|
||||
import { router as tagsRouter, entityScopedRouter as tagsByEntityRouter } from './routes/tags.js';
|
||||
import { router as linksRouter } from './routes/links.js';
|
||||
|
||||
export function mountApi(app) {
|
||||
const api = Router();
|
||||
@@ -39,6 +40,7 @@ export function mountApi(app) {
|
||||
api.use('/conversations', conversationsRouter);
|
||||
api.use('/conversations/:conversation_id/messages', messagesByConvRouter);
|
||||
api.use('/tags', tagsRouter);
|
||||
api.use('/links', linksRouter);
|
||||
api.use('/:entity_type/:entity_id/tags', tagsByEntityRouter);
|
||||
|
||||
api.use((_req, _res, next) => next(new NotFoundError('route not found')));
|
||||
|
||||
58
lib/api/routes/links.js
Normal file
58
lib/api/routes/links.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import * as repo from '../../db/repos/links.js';
|
||||
import { validate } from '../validate.js';
|
||||
import { requireWrite } from '../cap.js';
|
||||
import { asyncWrap } from '../errors.js';
|
||||
|
||||
const ENTITY_TYPES = ['space','project','task','page','ref','resource','source_doc','conversation'];
|
||||
|
||||
const createSchema = z.object({
|
||||
from_type: z.enum(ENTITY_TYPES),
|
||||
from_id: z.string().uuid(),
|
||||
to_type: z.enum(ENTITY_TYPES),
|
||||
to_id: z.string().uuid(),
|
||||
relation: z.string().min(1).max(64).optional()
|
||||
});
|
||||
|
||||
const endpointParams = z.object({
|
||||
type: z.enum(ENTITY_TYPES),
|
||||
id: z.string().uuid()
|
||||
});
|
||||
|
||||
const idParams = z.object({ id: z.string().uuid() });
|
||||
|
||||
export const router = Router();
|
||||
|
||||
router.post('/',
|
||||
requireWrite('link'),
|
||||
validate({ body: createSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const { from_type, from_id, to_type, to_id, relation } = req.body;
|
||||
const row = await repo.create(from_type, from_id, to_type, to_id, relation);
|
||||
res.status(201).json(row);
|
||||
})
|
||||
);
|
||||
|
||||
router.get('/from/:type/:id',
|
||||
validate({ params: endpointParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
res.json(await repo.listFrom(req.params.type, req.params.id));
|
||||
})
|
||||
);
|
||||
|
||||
router.get('/to/:type/:id',
|
||||
validate({ params: endpointParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
res.json(await repo.listTo(req.params.type, req.params.id));
|
||||
})
|
||||
);
|
||||
|
||||
router.delete('/:id',
|
||||
requireWrite('link'),
|
||||
validate({ params: idParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
await repo.remove(req.params.id);
|
||||
res.status(204).end();
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user