Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
13 changes: 13 additions & 0 deletions src/cloud-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
}
Expand Down
Loading