diff --git a/package.json b/package.json index c5e361d..a50749f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apiforgejs", - "version": "2.1.0", + "version": "2.1.1", "description": "API observability & intelligence SDK for Express.js — local-first, privacy-first", "main": "src/index.js", "keywords": [ diff --git a/src/cloud-transport.js b/src/cloud-transport.js index 9c73262..3b607e5 100644 --- a/src/cloud-transport.js +++ b/src/cloud-transport.js @@ -17,6 +17,19 @@ class CloudTransport { this._openUntil = 0; } + writeRoutes(routes) { + if (routes.length === 0) return; + fetch(`${this._url}/routes`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', 'X-API-Key': this._apiKey }, + body: JSON.stringify({ + routes: routes.map(r => ({ route: r.route, method: r.method, service: this._service })), + }), + }).catch(err => { + console.warn(`[apiforgejs] Failed to sync route registry: ${err.message}`); + }); + } + write(rows) { if (rows.length === 0) return; if (Date.now() < this._openUntil) return; diff --git a/src/index.js b/src/index.js index 2f80133..0236f37 100644 --- a/src/index.js +++ b/src/index.js @@ -62,7 +62,11 @@ function apiforge(options = {}) { ? startDashboard(db, config.dashboardPort) : null; - const middleware = createInterceptor(aggregator, db, config); + const storeRoutes = isCloud + ? routes => transport.writeRoutes(routes) + : routes => db.upsertKnownRoutes(routes); + + const middleware = createInterceptor(aggregator, storeRoutes, config); middleware.shutdown = () => { aggregator.stop(); diff --git a/src/interceptor.js b/src/interceptor.js index 311fa31..6e7754e 100644 --- a/src/interceptor.js +++ b/src/interceptor.js @@ -41,7 +41,7 @@ function routerLayerPath(layer) { return '/' + m[1].replace(/\\\//g, '/'); } -function createInterceptor(aggregator, db, config) { +function createInterceptor(aggregator, storeRoutes, config) { const { env, release, service, sampling, ignorePaths } = config; const ignoreSet = new Set(ignorePaths); @@ -50,7 +50,7 @@ function createInterceptor(aggregator, db, config) { function scanRoutes(app) { try { const routes = extractExpressRoutes(app._router); - if (routes.length > 0) db.upsertKnownRoutes(routes); + if (routes.length > 0) storeRoutes(routes); } catch (_) { // Non-critical — never crash the host app }