import express from 'express';
import { createGrantex } from '@grantex/express';
import type { GrantexRequest } from '@grantex/express';
const app = express();
app.use(express.json());
const grantex = createGrantex({
jwksUri: 'https://grantex-auth-dd4mtrt2gq-uc.a.run.app/.well-known/jwks.json',
});
// Public health check — no auth required
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
// Protected routes
const api = express.Router();
api.use(grantex.requireToken());
api.get('/me', (req, res) => {
const { principalId, agentDid, scopes } = (req as GrantexRequest).grant;
res.json({ principalId, agentDid, scopes });
});
api.get('/calendar', grantex.requireScopes('calendar:read'), (req, res) => {
const { principalId } = (req as GrantexRequest).grant;
res.json({ events: getCalendarEvents(principalId) });
});
api.post('/calendar/events', grantex.requireScopes('calendar:write'), (req, res) => {
const { principalId } = (req as GrantexRequest).grant;
const event = createEvent(principalId, req.body);
res.status(201).json(event);
});
api.post('/email/send',
grantex.requireScopes('email:read', 'email:send'),
(req, res) => {
res.json({ sent: true });
},
);
app.use('/api', api);
app.listen(3000, () => {
console.log('API server running on http://localhost:3000');
});
function getCalendarEvents(principalId: string) {
return [{ id: '1', title: 'Team standup', principalId }];
}
function createEvent(principalId: string, body: unknown) {
return { id: '2', principalId, ...(body as Record<string, unknown>) };
}