Skip to content
Open
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
26 changes: 13 additions & 13 deletions apps/public-api/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const dotenv = require('dotenv');
const path = require('path');
dotenv.config({ path: path.join(__dirname, '../../../.env') });

const {validateEnv} = require('@urbackend/common');
const { validateEnv } = require('@urbackend/common');

if (process.env.NODE_ENV !== 'test') {
validateEnv();
Expand All @@ -19,13 +19,13 @@ const { capture } = require('@kiroo/sdk');


// Initialize Queue Workers
const {emailQueue} = require('@urbackend/common');
const {authEmailQueue} = require('@urbackend/common');
const {initWebhookWorker} = require('@urbackend/common');
const {initAuthEmailWorker, initPublicEmailWorker} = require('@urbackend/common');
const {initActivityRollupWorker, scheduleActivityRollup} = require('@urbackend/common');
const {initReliabilityAlertWorker, scheduleReliabilityAlert} = require('@urbackend/common');
const {initTrashCleanupWorker} = require('@urbackend/common');
const { emailQueue } = require('@urbackend/common');
const { authEmailQueue } = require('@urbackend/common');
const { initWebhookWorker } = require('@urbackend/common');
const { initAuthEmailWorker, initPublicEmailWorker } = require('@urbackend/common');
const { initActivityRollupWorker, scheduleActivityRollup } = require('@urbackend/common');
const { initReliabilityAlertWorker, scheduleReliabilityAlert } = require('@urbackend/common');
const { initTrashCleanupWorker } = require('@urbackend/common');

app.use('/api/mail/webhook', express.raw({ type: 'application/json' }));
app.use(express.json());
Expand All @@ -41,10 +41,10 @@ if (process.env.NODE_ENV !== 'test') {
}

app.use(capture({
supabaseUrl: process.env.SUPABASE_URL,
supabaseKey: process.env.SUPABASE_KEY,
bucket: process.env.SUPABASE_BUCKET,
sampleRate: 0
supabaseUrl: process.env.SUPABASE_URL,
supabaseKey: process.env.SUPABASE_KEY,
bucket: process.env.SUPABASE_BUCKET,
sampleRate: 0
}));


Expand Down Expand Up @@ -117,7 +117,7 @@ app.use((err, req, res, next) => {

app.use((req, res) => {
const id = res.get("X-Kiroo-Replay-ID");
res.json({error: "Not Found", replayId: id})
res.json({ error: "Not Found", replayId: id })
})
Comment on lines 118 to 121

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Return 404 in the Not Found handler.

This handler currently responds with 200 OK, which breaks HTTP semantics and client error handling for unknown routes.

Suggested fix
 app.use((req, res) => {
     const id = res.get("X-Kiroo-Replay-ID");
-    res.json({ error: "Not Found", replayId: id })
+    res.status(404).json({ error: "Not Found", replayId: id })
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
app.use((req, res) => {
const id = res.get("X-Kiroo-Replay-ID");
res.json({error: "Not Found", replayId: id})
res.json({ error: "Not Found", replayId: id })
})
app.use((req, res) => {
const id = res.get("X-Kiroo-Replay-ID");
res.status(404).json({ error: "Not Found", replayId: id })
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/public-api/src/app.js` around lines 118 - 121, The Not Found handler
currently sends a 200 response; update the middleware passed to app.use (the
anonymous function that reads X-Kiroo-Replay-ID) to set the HTTP status to 404
before sending JSON (e.g., call res.status(404) or res.statusCode = 404) so
unknown routes return a proper 404 Not Found response while still including the
replayId in the response body.

// INITIALIZATION
if (process.env.NODE_ENV !== 'test') {
Expand Down
14 changes: 5 additions & 9 deletions apps/public-api/src/middlewares/api_usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ const limiter = rateLimit({
message: { error: "Too many requests, please try again later." },
standardHeaders: true,
legacyHeaders: false,
validate: {
xForwardedForHeader: false,
trustProxy: false
}
});

// Logger with API analytics
const logger = (req, res, next) => {
// Capture start time for response time measurement
const startHr = process.hrtime();

// Check for routes included in platform analytics
if (
req.originalUrl.startsWith('/api/data') ||
Expand All @@ -44,20 +40,20 @@ const logger = (req, res, next) => {
if (!req._dailyCountIncremented) {
const day = getDayKey();
const reqCountKey = `project:usage:req:count:${req.project._id}:${day}`;
incrWithTtlAtomic(redis, reqCountKey, DEFAULT_DAILY_TTL_SECONDS).catch(() => {});
incrWithTtlAtomic(redis, reqCountKey, DEFAULT_DAILY_TTL_SECONDS).catch(() => { });
}

console.log(`📝 Logged: ${req.method} ${req.originalUrl} (${res.statusCode})`);
} catch (e) {
console.error("Logging failed:", e.message);
}
}

// --- API performance analytics ---
if (req.project) {
const diff = process.hrtime(startHr);
const responseTimeMs = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(2);

setImmediate(async () => {
try {
await ApiAnalytics.create({
Expand Down Expand Up @@ -111,7 +107,7 @@ const logger = (req, res, next) => {
}
});
}

next();
};

Expand Down
26 changes: 0 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading