Skip to content

Commit 98cee1a

Browse files
committed
fix: use stackblitz http json api
1 parent 5e7383d commit 98cee1a

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/download.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ interface StackBlitzProject {
1818
}>
1919
}
2020

21+
interface StackBlitzProjectResponse {
22+
project: StackBlitzProject
23+
}
24+
2125
async function createZip(options: Omit<DownloadOptions, 'outputPath'>): Promise<JSZip> {
2226
const {
2327
projectId,
@@ -31,7 +35,7 @@ async function createZip(options: Omit<DownloadOptions, 'outputPath'>): Promise<
3135
throw new Error('Invalid project ID: must contain only alphanumeric characters, hyphens, and underscores')
3236
}
3337

34-
const url = `https://stackblitz.com/edit/${projectId}`
38+
const url = `https://stackblitz.com/api/projects/${projectId}?include_files=true`
3539
if (verbose) {
3640
// eslint-disable-next-line no-console
3741
console.log(`Fetching project: ${url}`)
@@ -48,18 +52,7 @@ async function createZip(options: Omit<DownloadOptions, 'outputPath'>): Promise<
4852
throw new Error(`Failed to fetch project: ${response.statusText}`)
4953
}
5054

51-
const html = await response.text()
52-
53-
// Extract project data from the Redux store
54-
const scriptMatch = html.match(/<script type="application\/json" data-redux-store="">([\s\S]*?)<\/script>/)
55-
56-
if (!scriptMatch || !scriptMatch[1]) {
57-
throw new Error('Could not find project data in page')
58-
}
59-
60-
// Parse the JSON data
61-
const reduxStore = JSON.parse(scriptMatch[1])
62-
const projectData = reduxStore?.project as StackBlitzProject | undefined
55+
const { project: projectData } = await response.json() as StackBlitzProjectResponse
6356

6457
if (!projectData || !projectData.appFiles) {
6558
throw new Error('No files found in project data')

0 commit comments

Comments
 (0)