11import type { Client , Config } from "@hey-api/client-fetch" ;
2- import { createClient , createConfig } from "@hey-api/client-fetch" ;
3- import { handleResponse , retryWithDelay } from "./utils/api" ;
2+ import { handleResponse , retryWithDelay , createApiClient } from "./utils/api" ;
43import { getInferredBaseUrl } from "./utils/constants" ;
54import {
65 metaInfo ,
@@ -56,54 +55,6 @@ import type {
5655} from "./api-clients/client" ;
5756import { PitcherManagerResponse } from "./types" ;
5857
59- async function enhanceFetch (
60- request : Request ,
61- instrumentation ?: ( request : Request ) => Promise < Response >
62- ) {
63- // Clone the request to modify headers
64- const headers = new Headers ( request . headers ) ;
65- const existingUserAgent = headers . get ( "User-Agent" ) || "" ;
66-
67- // Extend User-Agent with SDK version
68- headers . set (
69- "User-Agent" ,
70- `${ existingUserAgent ? `${ existingUserAgent } ` : "" } codesandbox-sdk/${
71- // @ts -expect-error - Replaced at build time
72- CSB_SDK_VERSION
73- } `. trim ( )
74- ) ;
75-
76- // Create new request with updated headers and optionally add instrumentation
77- return instrumentation
78- ? instrumentation (
79- new Request ( request , {
80- headers,
81- } )
82- )
83- : fetch (
84- new Request ( request , {
85- headers,
86- } )
87- ) ;
88- }
89-
90- function createApiClient (
91- apiKey : string ,
92- config : Config = { } ,
93- instrumentation ?: ( request : Request ) => Promise < Response >
94- ) {
95- return createClient (
96- createConfig ( {
97- baseUrl : config . baseUrl || getInferredBaseUrl ( apiKey ) ,
98- fetch : ( request ) => enhanceFetch ( request , instrumentation ) ,
99- ...config ,
100- headers : {
101- Authorization : `Bearer ${ apiKey } ` ,
102- ...config . headers ,
103- } ,
104- } )
105- ) ;
106- }
10758
10859export interface APIOptions {
10960 apiKey : string ;
@@ -297,17 +248,18 @@ export class API {
297248 }
298249
299250 async hibernate ( id : string , data ?: VmHibernateData [ "body" ] ) {
300- const response = await retryWithDelay (
301- ( ) =>
302- vmHibernate ( {
251+ return retryWithDelay (
252+ async ( ) => {
253+ const response = await vmHibernate ( {
303254 client : this . client ,
304255 path : { id } ,
305256 body : data ,
306- } ) ,
257+ } ) ;
258+ return handleResponse ( response , `Failed to hibernate VM ${ id } ` ) ;
259+ } ,
307260 3 ,
308261 200
309262 ) ;
310- return handleResponse ( response , `Failed to hibernate VM ${ id } ` ) ;
311263 }
312264
313265 async updateHibernationTimeout (
@@ -335,17 +287,18 @@ export class API {
335287 }
336288
337289 async shutdown ( id : string , data ?: VmShutdownData [ "body" ] ) {
338- const response = await retryWithDelay (
339- ( ) =>
340- vmShutdown ( {
290+ return retryWithDelay (
291+ async ( ) => {
292+ const response = await vmShutdown ( {
341293 client : this . client ,
342294 path : { id } ,
343295 body : data ,
344- } ) ,
296+ } ) ;
297+ return handleResponse ( response , `Failed to shutdown VM ${ id } ` ) ;
298+ } ,
345299 3 ,
346300 200
347301 ) ;
348- return handleResponse ( response , `Failed to shutdown VM ${ id } ` ) ;
349302 }
350303
351304 async updateSpecs ( id : string , data : VmUpdateSpecsData [ "body" ] ) {
@@ -359,20 +312,18 @@ export class API {
359312
360313 async startVm ( id : string , options ?: StartVmOptions ) {
361314 const { retryDelay = 200 , ...data } = options || { } ;
362- const response = await retryWithDelay (
363- ( ) =>
364- vmStart ( {
315+ const handledResponse = await retryWithDelay (
316+ async ( ) => {
317+ const response = await vmStart ( {
365318 client : this . client ,
366319 path : { id } ,
367320 body : data ,
368- } ) ,
321+ } ) ;
322+ return handleResponse ( response , `Failed to start VM ${ id } ` ) ;
323+ } ,
369324 3 ,
370325 retryDelay
371326 ) ;
372- const handledResponse = handleResponse (
373- response ,
374- `Failed to start VM ${ id } `
375- ) ;
376327
377328 return {
378329 bootupType :
0 commit comments