Skip to content

Commit 4af79de

Browse files
authored
Merge pull request #17 from seamapi/fix/image-url-parser
fix: Handle proxied image urls in seed-from-api
2 parents 3760594 + 9bb61ba commit 4af79de

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/lib/database/seed-from-api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export const seedDatabaseFromApi = async (
9898
}
9999

100100
const getImageIdFromImageUrl = (url: string): string => {
101-
const image_url = new URL(url)
102-
const image_id = image_url.searchParams.get("image_id")
103-
if (image_id == null) {
104-
throw new Error(`No image_id in ${url}`)
105-
}
101+
const searchParams = new URL(url).searchParams
102+
const imageId = searchParams.get("image_id")
103+
if (imageId) return imageId
106104

107-
return image_id
105+
const proxiedUrl = searchParams.get("url")
106+
if (!proxiedUrl) throw new Error(`No image_id in "${url}"`)
107+
return getImageIdFromImageUrl(proxiedUrl)
108108
}

test/seed.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ test("seed database from api", async (t) => {
9292
await seedDatabaseFromApi(db, client)
9393
})
9494

95+
const client_with_proxied_images = axios.create({
96+
baseURL: serverURL,
97+
headers: {
98+
"x-vercel-protection-bypass": db.vercel_protection_bypass_secret,
99+
"x-forwarded-seam-base-url":
100+
"https://example.com/devicedb?url=https://example.com/devicedb",
101+
},
102+
})
103+
104+
await t.notThrowsAsync(async () => {
105+
await seedDatabaseFromApi(db, client_with_proxied_images)
106+
})
107+
95108
t.deepEqual(db.manufacturers[0], { ...manufacturer, device_model_count: 1 })
96109
t.deepEqual(db.device_models[0], device_model)
97110
})

0 commit comments

Comments
 (0)