@@ -3,14 +3,13 @@ import type { RemoteOptions } from '../interfaces/RemoteOptions';
33import type { TsConfigJson } from '../interfaces/TsConfigJson' ;
44
55import AdmZip from 'adm-zip' ;
6- import axios , { AxiosResponse , InternalAxiosRequestConfig } from 'axios' ;
76import {
87 existsSync ,
98 mkdirSync ,
109 mkdtempSync ,
11- readFileSync ,
1210 rmSync ,
1311 writeFileSync ,
12+ readFileSync ,
1413} from 'fs' ;
1514import os from 'os' ;
1615import { join } from 'path' ;
@@ -31,6 +30,7 @@ import {
3130 retrieveTypesZipPath ,
3231} from './archiveHandler' ;
3332import { fileLog } from '../../server' ;
33+ import * as utils from './utils' ;
3434
3535describe ( 'archiveHandler' , ( ) => {
3636 const tmpDir = mkdtempSync ( join ( os . tmpdir ( ) , 'archive-handler' ) ) ;
@@ -283,15 +283,13 @@ describe('archiveHandler', () => {
283283 Buffer . from ( 'export declare const bar: number;' ) ,
284284 ) ;
285285
286- const mockResponse : AxiosResponse < Buffer > = {
287- data : zip . toBuffer ( ) ,
288- status : 200 ,
289- statusText : 'OK' ,
290- headers : { } ,
291- config : { } as InternalAxiosRequestConfig ,
292- request : { } as XMLHttpRequest ,
293- } ;
294- vi . spyOn ( axios , 'get' ) . mockResolvedValueOnce ( mockResponse ) ;
286+ const nativeFetchMock = vi
287+ . spyOn ( utils , 'nativeFetch' )
288+ . mockResolvedValueOnce ( {
289+ data : zip . toBuffer ( ) ,
290+ status : 200 ,
291+ headers : { } ,
292+ } as any ) ;
295293
296294 const result = await downloadTypesArchive ( hostOptions ) ( [
297295 destinationFolder ,
@@ -300,38 +298,35 @@ describe('archiveHandler', () => {
300298
301299 expect ( result ) . toEqual ( [ destinationFolder , archivePath ] ) ;
302300 expect ( existsSync ( join ( archivePath , 'sample.d.ts' ) ) ) . toBeTruthy ( ) ;
303- expect ( axios . get ) . toHaveBeenCalledTimes ( 1 ) ;
304- // Only verify the URL and responseType
305- const axiosGetMock = vi . mocked ( axios . get ) ;
306- const [ [ url , options ] ] = axiosGetMock . mock . calls ;
301+ expect ( nativeFetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
302+ const [ [ url , options ] ] = nativeFetchMock . mock . calls ;
307303 expect ( url ) . toBe ( new URL ( fileToDownload ) . href ) ;
308304 expect ( options . responseType ) . toBe ( 'arraybuffer' ) ;
309305 } ) ;
310306
311307 it ( 'should retry on network failure up to maxRetries' , async ( ) => {
312308 const error = new Error ( 'Network error' ) ;
313- vi . spyOn ( axios , 'get' ) . mockRejectedValue ( error ) ;
309+ const nativeFetchMock = vi
310+ . spyOn ( utils , 'nativeFetch' )
311+ . mockRejectedValue ( error ) ;
314312
315313 await expect ( ( ) =>
316314 downloadTypesArchive ( hostOptions ) ( [ destinationFolder , fileToDownload ] ) ,
317315 ) . rejects . toThrowError ( / N e t w o r k e r r o r / ) ;
318316
319- expect ( axios . get ) . toHaveBeenCalledTimes ( hostOptions . maxRetries ) ;
317+ expect ( nativeFetchMock ) . toHaveBeenCalledTimes ( hostOptions . maxRetries ) ;
320318 } ) ;
321319
322320 it ( 'should handle empty archives gracefully' , async ( ) => {
323321 const archivePath = join ( tmpDir , destinationFolder ) ;
324322 const zip = new AdmZip ( ) ;
325323 // Add an empty directory to the archive
326324 zip . addFile ( '.keep' , Buffer . from ( '' ) ) ;
327- vi . spyOn ( axios , 'get ' ) . mockResolvedValueOnce ( {
325+ vi . spyOn ( utils , 'nativeFetch ' ) . mockResolvedValueOnce ( {
328326 data : zip . toBuffer ( ) ,
329327 status : 200 ,
330- statusText : 'OK' ,
331328 headers : { } ,
332- config : { } as InternalAxiosRequestConfig ,
333- request : { } as XMLHttpRequest ,
334- } as AxiosResponse < Buffer > ) ;
329+ } as any ) ;
335330
336331 const result = await downloadTypesArchive ( hostOptions ) ( [
337332 destinationFolder ,
@@ -349,14 +344,11 @@ describe('archiveHandler', () => {
349344
350345 const zip = new AdmZip ( ) ;
351346 zip . addFile ( 'new.d.ts' , Buffer . from ( 'new content' ) ) ;
352- vi . spyOn ( axios , 'get ' ) . mockResolvedValueOnce ( {
347+ vi . spyOn ( utils , 'nativeFetch ' ) . mockResolvedValueOnce ( {
353348 data : zip . toBuffer ( ) ,
354349 status : 200 ,
355- statusText : 'OK' ,
356350 headers : { } ,
357- config : { } as InternalAxiosRequestConfig ,
358- request : { } as XMLHttpRequest ,
359- } as AxiosResponse < Buffer > ) ;
351+ } as any ) ;
360352
361353 await downloadTypesArchive ( hostOptions ) ( [
362354 destinationFolder ,
@@ -377,14 +369,11 @@ describe('archiveHandler', () => {
377369
378370 const zip = new AdmZip ( ) ;
379371 zip . addFile ( 'new.d.ts' , Buffer . from ( 'new content' ) ) ;
380- vi . spyOn ( axios , 'get ' ) . mockResolvedValueOnce ( {
372+ vi . spyOn ( utils , 'nativeFetch ' ) . mockResolvedValueOnce ( {
381373 data : zip . toBuffer ( ) ,
382374 status : 200 ,
383- statusText : 'OK' ,
384375 headers : { } ,
385- config : { } as InternalAxiosRequestConfig ,
386- request : { } as XMLHttpRequest ,
387- } as AxiosResponse < Buffer > ) ;
376+ } as any ) ;
388377
389378 await downloadTypesArchive ( options ) ( [ destinationFolder , fileToDownload ] ) ;
390379
@@ -397,26 +386,25 @@ describe('archiveHandler', () => {
397386 ...hostOptions ,
398387 abortOnError : false ,
399388 } ;
400- vi . spyOn ( axios , 'get' ) . mockRejectedValue ( new Error ( 'Network error' ) ) ;
389+ const nativeFetchMock = vi
390+ . spyOn ( utils , 'nativeFetch' )
391+ . mockRejectedValue ( new Error ( 'Network error' ) ) ;
401392
402393 const result = await downloadTypesArchive ( options ) ( [
403394 destinationFolder ,
404395 fileToDownload ,
405396 ] ) ;
406397
407398 expect ( result ) . toBeUndefined ( ) ;
408- expect ( axios . get ) . toHaveBeenCalledTimes ( options . maxRetries ) ;
399+ expect ( nativeFetchMock ) . toHaveBeenCalledTimes ( options . maxRetries ) ;
409400 } ) ;
410401
411402 it ( 'should handle malformed zip data' , async ( ) => {
412- vi . spyOn ( axios , 'get ' ) . mockResolvedValueOnce ( {
403+ vi . spyOn ( utils , 'nativeFetch ' ) . mockResolvedValueOnce ( {
413404 data : Buffer . from ( 'not a valid zip file' ) ,
414405 status : 200 ,
415- statusText : 'OK' ,
416406 headers : { } ,
417- config : { } as InternalAxiosRequestConfig ,
418- request : { } as XMLHttpRequest ,
419- } as AxiosResponse < Buffer > ) ;
407+ } as any ) ;
420408
421409 await expect ( ( ) =>
422410 downloadTypesArchive ( hostOptions ) ( [ destinationFolder , fileToDownload ] ) ,
0 commit comments