|
| 1 | +import { Hono } from 'hono'; |
| 2 | +import type { ContentfulStatusCode } from 'hono/utils/http-status'; |
| 3 | +import type { Env } from '../index'; |
| 4 | + |
| 5 | +export const metaPublicRoutes = new Hono<{ Bindings: Env }>(); |
| 6 | +export const metaRoutes = new Hono<{ Bindings: Env }>(); |
| 7 | + |
| 8 | +// Public: Canon info and lightweight schema refs |
| 9 | +metaPublicRoutes.get('/canon', async (c) => { |
| 10 | + const env = c.env; |
| 11 | + const canonicalUri = 'chittycanon://core/services/chittycommand'; |
| 12 | + const serviceId = await env.COMMAND_KV.get('register:service_id'); |
| 13 | + const lastBeaconAt = await env.COMMAND_KV.get('register:last_beacon_at'); |
| 14 | + const lastBeaconStatus = await env.COMMAND_KV.get('register:last_beacon_status'); |
| 15 | + return c.json({ |
| 16 | + name: 'ChittyCommand', |
| 17 | + version: '0.1.0', |
| 18 | + environment: env.ENVIRONMENT || 'production', |
| 19 | + canonicalUri, |
| 20 | + namespace: 'chittycanon://core/services', |
| 21 | + tier: 5, |
| 22 | + registered_with: env.CHITTYREGISTER_URL || null, |
| 23 | + registration: { service_id: serviceId || null, last_beacon_at: lastBeaconAt || null, last_status: lastBeaconStatus || null }, |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +metaPublicRoutes.get('/schema', (c) => { |
| 28 | + // Lightweight schema refs + canonical links (ChittySchema) |
| 29 | + return c.json({ |
| 30 | + schemaVersion: '0.1.0', |
| 31 | + generatedAt: new Date().toISOString(), |
| 32 | + endpoints: [ |
| 33 | + '/api/dashboard', |
| 34 | + '/api/accounts', |
| 35 | + '/api/obligations', |
| 36 | + '/api/disputes', |
| 37 | + '/api/recommendations', |
| 38 | + '/api/cashflow', |
| 39 | + ], |
| 40 | + db_tables: [ |
| 41 | + 'cc_accounts', |
| 42 | + 'cc_obligations', |
| 43 | + 'cc_transactions', |
| 44 | + 'cc_recommendations', |
| 45 | + 'cc_cashflow_projections', |
| 46 | + 'cc_disputes', |
| 47 | + 'cc_dispute_correspondence', |
| 48 | + 'cc_legal_deadlines', |
| 49 | + 'cc_documents', |
| 50 | + 'cc_actions_log', |
| 51 | + 'cc_sync_log', |
| 52 | + 'cc_properties', |
| 53 | + ], |
| 54 | + canonicalRefs: { |
| 55 | + chittyschema_base: 'https://schema.chitty.cc/api/v1', |
| 56 | + list_schemas: 'https://schema.chitty.cc/api/v1/schemas', |
| 57 | + get_schema: 'https://schema.chitty.cc/api/v1/schemas/{type}', |
| 58 | + validate: 'https://schema.chitty.cc/api/v1/validate', |
| 59 | + drift: 'https://schema.chitty.cc/api/v1/drift', |
| 60 | + catalog_repo: 'https://github.com/chittyfoundation/chittyschema', |
| 61 | + }, |
| 62 | + notes: 'Zod schemas are used server-side; canonical JSON Schemas are governed by chittyfoundation/chittyschema.', |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +metaPublicRoutes.get('/beacon', async (c) => { |
| 67 | + const regAt = await c.env.COMMAND_KV.get('register:last_beacon_at'); |
| 68 | + const regSt = await c.env.COMMAND_KV.get('register:last_beacon_status'); |
| 69 | + const conAt = await c.env.COMMAND_KV.get('connect:last_beacon_at'); |
| 70 | + const conSt = await c.env.COMMAND_KV.get('connect:last_beacon_status'); |
| 71 | + return c.json({ |
| 72 | + register: { last_beacon_at: regAt || null, last_status: regSt || null }, |
| 73 | + connect: { last_beacon_at: conAt || null, last_status: conSt || null }, |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +// Certificate verification passthrough (public) |
| 78 | +metaPublicRoutes.post('/cert/verify', async (c) => { |
| 79 | + try { |
| 80 | + const body = await c.req.json().catch(() => ({})); |
| 81 | + const certificateId = String(body?.certificate_id || '').trim(); |
| 82 | + if (!certificateId) return c.json({ error: 'Missing field: certificate_id' }, 400); |
| 83 | + const base = c.env.CHITTYCERT_URL || 'https://cert.chitty.cc'; |
| 84 | + const res = await fetch(`${base}/api/v1/verify`, { |
| 85 | + method: 'POST', |
| 86 | + headers: { 'Content-Type': 'application/json', 'X-Source-Service': 'chittycommand' }, |
| 87 | + body: JSON.stringify({ certificate_id: certificateId }), |
| 88 | + }); |
| 89 | + const out = await res.json().catch(() => ({})); |
| 90 | + if (!res.ok) return c.json({ valid: false, result: out, code: res.status }, res.status as ContentfulStatusCode); |
| 91 | + return c.json(out); |
| 92 | + } catch (err) { |
| 93 | + return c.json({ error: String(err) }, 500); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +// Certificate fetch (public) |
| 98 | +metaPublicRoutes.get('/cert/:id', async (c) => { |
| 99 | + const id = c.req.param('id'); |
| 100 | + if (!id) return c.json({ error: 'Missing certificate id' }, 400); |
| 101 | + try { |
| 102 | + const base = c.env.CHITTYCERT_URL || 'https://cert.chitty.cc'; |
| 103 | + const res = await fetch(`${base}/api/v1/certificate/${encodeURIComponent(id)}`); |
| 104 | + const out = await res.json().catch(() => ({})); |
| 105 | + if (!res.ok) return c.json({ error: 'Not found', code: res.status, result: out }, res.status as ContentfulStatusCode); |
| 106 | + return c.json(out); |
| 107 | + } catch (err) { |
| 108 | + return c.json({ error: String(err) }, 500); |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +// Authenticated: identity resolution |
| 113 | +metaRoutes.get('/whoami', (c) => { |
| 114 | + // authMiddleware populates userId/scopes into variables |
| 115 | + // @ts-expect-error hono types for Variables are on app-level |
| 116 | + const userId = c.get('userId') as string | undefined; |
| 117 | + // @ts-expect-error see above |
| 118 | + const scopes = (c.get('scopes') as string[] | undefined) || []; |
| 119 | + if (!userId) return c.json({ error: 'Unauthorized' }, 401); |
| 120 | + return c.json({ |
| 121 | + userId, |
| 122 | + subjectUri: `chittyid://users/${userId}`, |
| 123 | + scopes, |
| 124 | + environment: c.env.ENVIRONMENT || 'production', |
| 125 | + }); |
| 126 | +}); |
0 commit comments