@@ -28,7 +28,7 @@ import { getCampaignHistory } from "./services/eventHistory";
2828import { startEventIndexer } from "./services/eventIndexer" ;
2929import { fetchOpenIssues } from "./services/openIssues" ;
3030import { ensureSorobanRefundConfig , verifyRefundTransaction } from "./services/sorobanRpc" ;
31- import { AppError , ApiErrorResponse } from "./types/errors" ;
31+ import { AppError , ApiErrorResponse , RequestWithId , CampaignListItem } from "./types/errors" ;
3232import {
3333 campaignIdSchema ,
3434 claimCampaignPayloadSchema ,
@@ -78,43 +78,15 @@ app.use(
7878
7979app . use ( express . json ( ) ) ;
8080
81- const rateLimitBuckets = new Map < string , { count : number ; resetAt : number } > ( ) ;
8281
83- function applyRateLimit ( maxRequests : number ) {
84- return ( req : Request , res : Response , next : express . NextFunction ) => {
85- const key = `${ req . ip } :${ req . path } :${ maxRequests } ` ;
86- const now = Date . now ( ) ;
87- const current = rateLimitBuckets . get ( key ) ;
88-
89- if ( ! current || now >= current . resetAt ) {
90- rateLimitBuckets . set ( key , { count : 1 , resetAt : now + RATE_LIMIT_WINDOW_MS } ) ;
91- return next ( ) ;
92- }
93-
94- if ( current . count >= maxRequests ) {
95- const retryAfterSec = Math . max ( 1 , Math . ceil ( ( current . resetAt - now ) / 1000 ) ) ;
96- res . setHeader ( "Retry-After" , String ( retryAfterSec ) ) ;
97- throw new AppError ( "Rate limit exceeded. Please retry shortly." , 429 , "RATE_LIMITED" ) ;
98- }
99-
100- current . count += 1 ;
101- rateLimitBuckets . set ( key , current ) ;
102- return next ( ) ;
103- } ;
104- }
105-
106- app . use ( applyRateLimit ( RATE_LIMIT_MAX_REQUESTS ) ) ;
107-
108- app . use ( ( req : RequestWithId , res : Response , next : express . NextFunction ) => {
109- req . requestId = randomUUID ( ) ;
11082 const startedAt = process . hrtime . bigint ( ) ;
11183
11284 res . on ( "finish" , ( ) => {
11385 const durationMs = Number ( process . hrtime . bigint ( ) - startedAt ) / 1_000_000 ;
11486
11587 logRequest (
11688 {
117- requestId : req . requestId ,
89+ requestId : requestWithId . requestId ,
11890 method : req . method ,
11991 path : req . originalUrl || req . path ,
12092 status : res . statusCode ,
0 commit comments