@@ -24,6 +24,10 @@ import { Sandbox, SandboxSession } from "./sandbox";
2424import { handleResponse } from "./utils/handle-response" ;
2525import { SessionCreateOptions , SessionData } from "./sessions" ;
2626
27+ type SandboxForkResponseWithSession = SandboxForkResponse [ "data" ] & {
28+ start_response : Required < VmStartResponse > [ "data" ] ;
29+ } ;
30+
2731export type SandboxPrivacy = "public" | "unlisted" | "private" ;
2832
2933export type SandboxInfo = {
@@ -247,63 +251,15 @@ export type HandledResponse<D, E> = {
247251 response : Response ;
248252} ;
249253
250- function getDefaultTemplate ( client : Client ) {
251- if ( client . getConfig ( ) . baseUrl ?. includes ( "codesandbox.stream" ) ) {
252- return "7ngcrf" ;
253- }
254-
255- return "pcz35m" ;
256- }
254+ export class SandboxClient {
255+ get defaultTemplate ( ) {
256+ if ( this . apiClient . getConfig ( ) . baseUrl ?. includes ( "codesandbox.stream" ) ) {
257+ return "7ngcrf" ;
258+ }
257259
258- export async function createSandbox (
259- client : Client ,
260- startVM : true ,
261- opts ?: CreateSandboxOpts
262- ) : Promise <
263- SandboxForkResponse [ "data" ] & {
264- start_response : Required < VmStartResponse > [ "data" ] ;
260+ return "pcz35m" ;
265261 }
266- > ;
267- export async function createSandbox (
268- client : Client ,
269- startVM : false ,
270- opts ?: CreateSandboxOpts
271- ) : Promise < SandboxForkResponse [ "data" ] > ;
272- export async function createSandbox (
273- client : Client ,
274- startVM : boolean ,
275- opts ?: CreateSandboxOpts
276- ) : Promise < SandboxForkResponse [ "data" ] > {
277- const templateId = opts ?. template || getDefaultTemplate ( client ) ;
278- const privacy = opts ?. privacy || "public" ;
279- const tags = opts ?. tags || [ "sdk" ] ;
280- const path = opts ?. path || "/SDK" ;
281-
282- // Always add the "sdk" tag to the sandbox, this is used to identify sandboxes created by the SDK.
283- const tagsWithSdk = tags . includes ( "sdk" ) ? tags : [ ...tags , "sdk" ] ;
284- // An empty object will still start the VM, but with server defaults
285- const startOptions = startOptionsFromOpts ( opts ) || { } ;
286-
287- const result = await sandboxFork ( {
288- client,
289- body : {
290- privacy : privacyToNumber ( privacy ) ,
291- title : opts ?. title ,
292- description : opts ?. description ,
293- tags : tagsWithSdk ,
294- path,
295- // We only pass start options if we want to start the VM immediately
296- start_options : startVM ? startOptions : undefined ,
297- } ,
298- path : {
299- id : typeof templateId === "string" ? templateId : templateId . id ,
300- } ,
301- } ) ;
302262
303- return handleResponse ( result , "Failed to create sandbox" ) ;
304- }
305-
306- export class SandboxClient {
307263 constructor ( private readonly apiClient : Client ) { }
308264
309265 /**
@@ -369,9 +325,39 @@ export class SandboxClient {
369325 ) : Promise < Sandbox > ;
370326 async create ( opts ?: CreateSandboxOpts ) : Promise < Sandbox > ;
371327 async create ( opts ?: CreateSandboxOpts ) : Promise < Sandbox | SessionData > {
372- // We always want to start the VM in this context as our intention it to connect immediately,
328+ const templateId = opts ?. template || this . defaultTemplate ;
329+ const privacy = opts ?. privacy || "public" ;
330+ const tags = opts ?. tags || [ "sdk" ] ;
331+ const path = opts ?. path || "/SDK" ;
332+
333+ // Always add the "sdk" tag to the sandbox, this is used to identify sandboxes created by the SDK.
334+ const tagsWithSdk = tags . includes ( "sdk" ) ? tags : [ ...tags , "sdk" ] ;
335+
336+ // We always want to start the VM in this context as our intention is to connect immediately
373337 // or return the session data to manually connect, for example in browser
374- const sandbox = await createSandbox ( this . apiClient , true , opts ) ;
338+ const startOptions = startOptionsFromOpts ( opts ) || { } ;
339+
340+ const result = await sandboxFork ( {
341+ client : this . apiClient ,
342+ body : {
343+ privacy : privacyToNumber ( privacy ) ,
344+ title : opts ?. title ,
345+ description : opts ?. description ,
346+ tags : tagsWithSdk ,
347+ path,
348+ start_options : startOptions ,
349+ } ,
350+ path : {
351+ id : typeof templateId === "string" ? templateId : templateId . id ,
352+ } ,
353+ } ) ;
354+
355+ const sandbox = handleResponse (
356+ result ,
357+ "Failed to create sandbox"
358+ // We currently always pass "start_options" to create a session
359+ ) as SandboxForkResponseWithSession ;
360+
375361 const shouldReturnSessionOnly = opts ?. autoConnect === false ;
376362 const session : SessionData = {
377363 id : sandbox . id ,
0 commit comments