11import { NextRequest , NextResponse } from 'next/server'
2- import { createServerClient } from '@/lib/supabase-server '
2+ import { Sandbox } from '@e2b/code-interpreter '
33
4- // Mock file content storage - in a real implementation this would integrate with project storage
5- const mockFileContents : Record < string , string > = {
6- '/src/App.tsx' : `import React from 'react'
7- import { Button } from './components/Button'
8- import { Card } from './components/Card'
9-
10- function App() {
11- return (
12- <div className="app">
13- <h1>Welcome to CodingIT</h1>
14- <Card>
15- <Button onClick={() => console.log('Hello!')}>
16- Click me
17- </Button>
18- </Card>
19- </div>
20- )
21- }
22-
23- export default App` ,
24-
25- '/src/components/Button.tsx' : `import React from 'react'
26-
27- interface ButtonProps {
28- children: React.ReactNode
29- onClick?: () => void
30- variant?: 'primary' | 'secondary'
31- }
32-
33- export function Button({ children, onClick, variant = 'primary' }: ButtonProps) {
34- return (
35- <button
36- className={\`btn btn--\${variant}\`}
37- onClick={onClick}
38- >
39- {children}
40- </button>
41- )
42- }` ,
43-
44- '/src/components/Card.tsx' : `import React from 'react'
45-
46- interface CardProps {
47- children: React.ReactNode
48- className?: string
49- }
50-
51- export function Card({ children, className = '' }: CardProps) {
52- return (
53- <div className={\`card \${className}\`}>
54- {children}
55- </div>
56- )
57- }` ,
58-
59- '/src/utils/helpers.ts' : `export function formatDate(date: Date): string {
60- return date.toLocaleDateString('en-US', {
61- year: 'numeric',
62- month: 'long',
63- day: 'numeric'
64- })
65- }
66-
67- export function debounce<T extends (...args: any[]) => void>(
68- func: T,
69- wait: number
70- ): (...args: Parameters<T>) => void {
71- let timeout: NodeJS.Timeout
72- return (...args: Parameters<T>) => {
73- clearTimeout(timeout)
74- timeout = setTimeout(() => func(...args), wait)
75- }
76- }` ,
77-
78- '/package.json' : `{
79- "name": "my-project",
80- "version": "1.0.0",
81- "scripts": {
82- "dev": "vite",
83- "build": "vite build",
84- "preview": "vite preview"
85- },
86- "dependencies": {
87- "react": "^18.0.0",
88- "react-dom": "^18.0.0"
89- },
90- "devDependencies": {
91- "@types/react": "^18.0.0",
92- "typescript": "^5.0.0",
93- "vite": "^4.0.0"
4+ export async function GET ( request : NextRequest ) {
5+ const searchParams = request . nextUrl . searchParams
6+ const sandboxId = searchParams . get ( 'sandboxId' )
7+ const path = searchParams . get ( 'path' )
8+
9+ if ( ! sandboxId || ! path ) {
10+ return NextResponse . json (
11+ { error : 'sandboxId and path are required' } ,
12+ { status : 400 } ,
13+ )
9414 }
95- }` ,
96-
97- '/README.md' : `# My Project
98-
99- This is a sample project created with CodingIT.
100-
101- ## Getting Started
102-
103- 1. Install dependencies: \`npm install\`
104- 2. Start development server: \`npm run dev\`
105- 3. Build for production: \`npm run build\`
10615
107- ## Features
108-
109- - React with TypeScript
110- - Component-based architecture
111- - Utility functions
112- - Modern development setup
113-
114- Happy coding! 🚀`
115- }
116-
117- export async function GET ( request : NextRequest ) {
11816 try {
119- // Skip authentication in development for now
120- if ( process . env . NODE_ENV === 'development' ) {
121- const { searchParams } = new URL ( request . url )
122- const path = searchParams . get ( 'path' )
123-
124- if ( ! path ) {
125- return NextResponse . json ( { error : 'Path parameter is required' } , { status : 400 } )
126- }
127-
128- const content = mockFileContents [ path ] || `// File: ${ path } \n// Content not found or empty file\n`
129-
130- return new NextResponse ( content , {
131- headers : {
132- 'Content-Type' : 'text/plain' ,
133- } ,
134- } )
135- }
136-
137- const supabase = createServerClient ( )
138- const { data : { session } } = await supabase . auth . getSession ( )
139-
140- if ( ! session ) {
141- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
142- }
143-
144- const { searchParams } = new URL ( request . url )
145- const path = searchParams . get ( 'path' )
146-
147- if ( ! path ) {
148- return NextResponse . json ( { error : 'Path parameter is required' } , { status : 400 } )
149- }
150-
151- // TODO: In a real implementation, fetch the actual file content from project storage
152- const content = mockFileContents [ path ] || `// File: ${ path } \n// Content not found or empty file\n`
153-
17+ const sandbox = await Sandbox . connect ( sandboxId )
18+ const content = await sandbox . files . read ( path )
15419 return new NextResponse ( content , {
15520 headers : {
15621 'Content-Type' : 'text/plain' ,
15722 } ,
15823 } )
15924 } catch ( error ) {
16025 console . error ( 'Error fetching file content:' , error )
161- return NextResponse . json ( { error : 'Failed to fetch file content' } , { status : 500 } )
26+ return NextResponse . json (
27+ { error : 'Failed to fetch file content' } ,
28+ { status : 500 } ,
29+ )
16230 }
16331}
16432
16533export async function POST ( request : NextRequest ) {
166- try {
167- // Skip authentication in development for now
168- if ( process . env . NODE_ENV === 'development' ) {
169- const { path, content } = await request . json ( )
170-
171- if ( ! path || content === undefined ) {
172- return NextResponse . json (
173- { error : 'Path and content are required' } ,
174- { status : 400 }
175- )
176- }
177-
178- mockFileContents [ path ] = content
179-
180- return NextResponse . json ( { success : true , message : 'File saved successfully' } )
181- }
182-
183- const supabase = createServerClient ( )
184- const { data : { session } } = await supabase . auth . getSession ( )
185-
186- if ( ! session ) {
187- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
188- }
189-
190- const { path, content } = await request . json ( )
191-
192- if ( ! path || content === undefined ) {
193- return NextResponse . json (
194- { error : 'Path and content are required' } ,
195- { status : 400 }
196- )
197- }
34+ const {
35+ sandboxId,
36+ path,
37+ content,
38+ } : { sandboxId : string ; path : string ; content : string } = await request . json ( )
39+
40+ if ( ! sandboxId || ! path || content === undefined ) {
41+ return NextResponse . json (
42+ { error : 'sandboxId, path, and content are required' } ,
43+ { status : 400 } ,
44+ )
45+ }
19846
199- // TODO: In a real implementation, save the file content to project storage
200- mockFileContents [ path ] = content
201-
202- return NextResponse . json ( { success : true , message : 'File saved successfully' } )
47+ try {
48+ const sandbox = await Sandbox . connect ( sandboxId )
49+ await sandbox . files . write ( path , content )
50+ return NextResponse . json ( { success : true } )
20351 } catch ( error ) {
20452 console . error ( 'Error saving file content:' , error )
205- return NextResponse . json ( { error : 'Failed to save file content' } , { status : 500 } )
53+ return NextResponse . json (
54+ { error : 'Failed to save file content' } ,
55+ { status : 500 } ,
56+ )
20657 }
20758}
20859
20960export async function DELETE ( request : NextRequest ) {
210- try {
211- // Skip authentication in development for now
212- if ( process . env . NODE_ENV === 'development' ) {
213- const { searchParams } = new URL ( request . url )
214- const path = searchParams . get ( 'path' )
215-
216- if ( ! path ) {
217- return NextResponse . json ( { error : 'Path parameter is required' } , { status : 400 } )
218- }
219-
220- delete mockFileContents [ path ]
221-
222- return NextResponse . json ( { success : true , message : 'File deleted successfully' } )
223- }
224-
225- const supabase = createServerClient ( )
226- const { data : { session } } = await supabase . auth . getSession ( )
227-
228- if ( ! session ) {
229- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
230- }
231-
232- const { searchParams } = new URL ( request . url )
233- const path = searchParams . get ( 'path' )
234-
235- if ( ! path ) {
236- return NextResponse . json ( { error : 'Path parameter is required' } , { status : 400 } )
237- }
61+ const searchParams = request . nextUrl . searchParams
62+ const sandboxId = searchParams . get ( 'sandboxId' )
63+ const path = searchParams . get ( 'path' )
64+
65+ if ( ! sandboxId || ! path ) {
66+ return NextResponse . json (
67+ { error : 'sandboxId and path are required' } ,
68+ { status : 400 } ,
69+ )
70+ }
23871
239- // TODO: In a real implementation, delete the file from project storage
240- delete mockFileContents [ path ]
241-
242- return NextResponse . json ( { success : true , message : 'File deleted successfully' } )
72+ try {
73+ const sandbox = await Sandbox . connect ( sandboxId )
74+ await sandbox . files . remove ( path )
75+ return NextResponse . json ( { success : true } )
24376 } catch ( error ) {
24477 console . error ( 'Error deleting file:' , error )
245- return NextResponse . json ( { error : 'Failed to delete file' } , { status : 500 } )
78+ return NextResponse . json (
79+ { error : 'Failed to delete file' } ,
80+ { status : 500 } ,
81+ )
24682 }
247- }
83+ }
0 commit comments