feat(api): error + validate + pagination plumbing

Add lib/api/{errors,validate,pagination,index}.js: typed ApiError
subclasses, errorMiddleware, zod-backed validate(), parsePagination
with caps, and a mountApi() that owns /api routing + 404 + error tail.
server.js delegates /api to mountApi and drops the inline /api/spaces
smoke (returns in Task 2).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
root
2026-05-31 16:37:06 +10:00
parent 42d7f568a2
commit 75afedaef0
9 changed files with 254 additions and 15 deletions

View File

@@ -25,16 +25,14 @@ describe('server', () => {
expect(res.status).toBe(401);
});
it('GET /api/spaces with token returns 200 and empty array', async () => {
const res = await request(app)
.get('/api/spaces')
.set('Authorization', 'Bearer test-token');
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
it('unknown /api route returns 404 with structured error', async () => {
const res = await request(app).get('/api/nope').set('Authorization', 'Bearer test-token');
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: { code: 'not_found', message: 'route not found' } });
});
it('unknown route returns 404', async () => {
const res = await request(app).get('/api/nope').set('Authorization', 'Bearer test-token');
it('unknown non-api route returns 404', async () => {
const res = await request(app).get('/missing');
expect(res.status).toBe(404);
});
});