1- import { NextResponse } from 'next/server' ;
1+ import { NextRequest , NextResponse } from 'next/server' ;
22import axios from 'axios' ;
33import { telemetry } from '@/lib/telemetry' ;
44import { env } from '@/lib/env' ;
5+ import { banksQuerySchema } from '@/lib/apiSchemas' ;
6+ import { applyRateLimit , getClientIp } from '@/lib/rateLimit' ;
57
68const PAYSTACK_SECRET_KEY = env . PAYSTACK_SECRET_KEY ;
9+ const RATE_LIMIT = { maxRequests : 30 , windowMs : 60_000 } ;
10+
11+ export async function GET ( request : NextRequest ) {
12+ const limited = applyRateLimit ( getClientIp ( request ) , '/api/banks' , RATE_LIMIT ) ;
13+ if ( limited ) return limited ;
714
8- export async function GET ( request : Request ) {
915 const traceContext = telemetry . extractTraceFromHeaders (
1016 request . headers as Headers ,
1117 ) ;
@@ -20,6 +26,30 @@ export async function GET(request: Request) {
2026 endpoint : '/api/banks' ,
2127 } ) ;
2228
29+ const query = Object . fromEntries ( request . nextUrl . searchParams . entries ( ) ) ;
30+ const validationResult = banksQuerySchema . safeParse ( query ) ;
31+ if ( ! validationResult . success ) {
32+ telemetry . addLog ( span . spanId , 'warn' , 'Banks query validation failed' , {
33+ errors : validationResult . error . issues ,
34+ } ) ;
35+ telemetry . finishSpan ( span . spanId , { success : false , error : 'Invalid request' } ) ;
36+
37+ const response = NextResponse . json (
38+ {
39+ success : false ,
40+ error : {
41+ code : 'INVALID_REQUEST' ,
42+ issues : validationResult . error . issues ,
43+ } ,
44+ } ,
45+ { status : 400 } ,
46+ ) ;
47+ telemetry . setTraceHeaders ( response . headers as Headers , traceContext ) ;
48+ return response ;
49+ }
50+
51+ const { country } = validationResult . data ;
52+
2353 if ( ! PAYSTACK_SECRET_KEY ) {
2454 telemetry . addLog (
2555 span . spanId ,
@@ -104,11 +134,11 @@ export async function GET(request: Request) {
104134 // Call real Paystack API to get Nigerian banks
105135 telemetry . addLog ( span . spanId , 'info' , 'Calling Paystack API' , {
106136 endpoint : 'https://api.paystack.co/bank' ,
107- country : 'nigeria' ,
137+ country,
108138 } ) ;
109139
110140 const response = await axios . get (
111- ' https://api.paystack.co/bank?country=nigeria' ,
141+ ` https://api.paystack.co/bank?country=${ country } ` ,
112142 {
113143 headers : {
114144 Authorization : `Bearer ${ PAYSTACK_SECRET_KEY } ` ,
0 commit comments