11const DEFAULT_API_URL = process . env . TESTUDO_API_URL ;
2+ const DEFAULT_API_KEY = process . env . TESTUDO_API_KEY ;
23const DEFAULT_TIMEOUT = 800 ; // ms
34const MAX_RETRIES = 1 ;
45
@@ -32,6 +33,7 @@ export interface ThreatResponse {
3233export interface ApiClientOptions {
3334 baseUrl ?: string ;
3435 timeout ?: number ;
36+ apiKey ?: string ;
3537}
3638
3739export type ApiErrorCategory =
@@ -55,22 +57,29 @@ export interface ApiClientResult {
5557async function fetchWithTimeout (
5658 url : string ,
5759 timeout : number ,
60+ apiKey ?: string ,
5861 signal ?: AbortSignal ,
5962) : Promise < Response > {
6063 const controller = new AbortController ( ) ;
6164 const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeout ) ;
6265
63- // Combine with external signal if provided
6466 if ( signal ) {
6567 signal . addEventListener ( 'abort' , ( ) => controller . abort ( ) , { once : true } ) ;
6668 }
6769
70+ const headers : Record < string , string > = {
71+ 'Content-Type' : 'application/json' ,
72+ } ;
73+
74+ const key = apiKey || DEFAULT_API_KEY ;
75+ if ( key ) {
76+ headers [ 'X-API-Key' ] = key ;
77+ }
78+
6879 try {
6980 const response = await fetch ( url , {
7081 signal : controller . signal ,
71- headers : {
72- 'Content-Type' : 'application/json' ,
73- } ,
82+ headers,
7483 } ) ;
7584 clearTimeout ( timeoutId ) ;
7685 return response ;
@@ -98,6 +107,7 @@ export async function checkAddressThreat(
98107 } ;
99108 }
100109
110+ const apiKey = options ?. apiKey ;
101111 const url = `${ baseUrl } /api/v1/threats/address/${ address . toLowerCase ( ) } ` ;
102112 let lastError : string = '' ;
103113 let lastCategory : ApiErrorCategory = 'unknown' ;
@@ -108,7 +118,7 @@ export async function checkAddressThreat(
108118 console . log ( `[API Client] Retry attempt ${ attempt } for ${ address } ` ) ;
109119 }
110120
111- const response = await fetchWithTimeout ( url , timeout ) ;
121+ const response = await fetchWithTimeout ( url , timeout , apiKey ) ;
112122
113123 // Handle rate limiting
114124 if ( response . status === 429 ) {
@@ -186,6 +196,7 @@ export async function checkDomainThreat(
186196 } ;
187197 }
188198
199+ const apiKey = options ?. apiKey ;
189200 const url = `${ baseUrl } /api/v1/threats/domain/${ encodeURIComponent ( domain ) } ` ;
190201 let lastError : string = '' ;
191202 let lastCategory : ApiErrorCategory = 'unknown' ;
@@ -196,7 +207,7 @@ export async function checkDomainThreat(
196207 console . log ( `[API Client] Retry attempt ${ attempt } for ${ domain } ` ) ;
197208 }
198209
199- const response = await fetchWithTimeout ( url , timeout ) ;
210+ const response = await fetchWithTimeout ( url , timeout , apiKey ) ;
200211
201212 if ( response . status === 429 ) {
202213 console . warn ( '[API Client] Rate limited' ) ;
0 commit comments