11import { NextResponse } from 'next/server'
2- import type { ProfileName } from '@sandchest/contract'
32import { createClient } from '@/lib/simulate-sdk'
43
5- function sdkErrorStatus ( err : unknown ) : number {
6- if ( typeof err === 'object' && err !== null && 'status' in err ) {
7- const s = ( err as { status : number } ) . status
8- if ( s >= 400 && s < 600 ) return s
9- }
10- return 500
11- }
12-
13- function sdkErrorMessage ( err : unknown ) : string {
14- if ( err instanceof Error ) return err . message
15- return 'Unknown error'
16- }
17-
184export async function POST ( request : Request ) {
195 try {
206 const body = ( await request . json ( ) ) as {
@@ -25,24 +11,49 @@ export async function POST(request: Request) {
2511 ttlSeconds ?: number | undefined
2612 }
2713
28- const client = createClient ( body . apiKey , body . baseUrl )
29- const sandbox = await client . create ( {
30- image : body . image || undefined ,
31- profile : ( body . profile as ProfileName ) || undefined ,
32- ttlSeconds : body . ttlSeconds || undefined ,
33- waitReady : false ,
14+ const baseUrl = body . baseUrl . replace ( / \/ $ / , '' )
15+ const apiBody : Record < string , unknown > = {
16+ profile : body . profile || 'small' ,
17+ ttl_seconds : body . ttlSeconds || 3600 ,
18+ }
19+ if ( body . image ) {
20+ apiBody . image = body . image
21+ }
22+
23+ const res = await fetch ( `${ baseUrl } /v1/sandboxes` , {
24+ method : 'POST' ,
25+ headers : {
26+ 'Authorization' : `Bearer ${ body . apiKey } ` ,
27+ 'Content-Type' : 'application/json' ,
28+ 'Accept' : 'application/json' ,
29+ } ,
30+ body : JSON . stringify ( apiBody ) ,
3431 } )
3532
33+ const text = await res . text ( )
34+ let data : Record < string , unknown >
35+ try {
36+ data = JSON . parse ( text ) as Record < string , unknown >
37+ } catch {
38+ return NextResponse . json (
39+ { error : `API returned ${ res . status } : ${ text . slice ( 0 , 500 ) } ` } ,
40+ { status : res . status || 500 } ,
41+ )
42+ }
43+
44+ if ( ! res . ok ) {
45+ const msg = ( data . message as string ) || ( data . error as string ) || `API ${ res . status } `
46+ return NextResponse . json ( { error : msg } , { status : res . status } )
47+ }
48+
3649 return NextResponse . json ( {
37- id : sandbox . id ,
38- status : sandbox . status ,
39- replayUrl : sandbox . replayUrl ,
50+ id : data . sandbox_id ,
51+ status : data . status ,
52+ replayUrl : data . replay_url ,
4053 } )
4154 } catch ( err ) {
42- return NextResponse . json (
43- { error : sdkErrorMessage ( err ) } ,
44- { status : sdkErrorStatus ( err ) } ,
45- )
55+ const message = err instanceof Error ? err . message : 'Unknown error'
56+ return NextResponse . json ( { error : message } , { status : 500 } )
4657 }
4758}
4859
@@ -60,9 +71,7 @@ export async function DELETE(request: Request) {
6071
6172 return NextResponse . json ( { ok : true } )
6273 } catch ( err ) {
63- return NextResponse . json (
64- { error : sdkErrorMessage ( err ) } ,
65- { status : sdkErrorStatus ( err ) } ,
66- )
74+ const message = err instanceof Error ? err . message : 'Unknown error'
75+ return NextResponse . json ( { error : message } , { status : 500 } )
6776 }
6877}
0 commit comments