Skip to content

Commit

Permalink
Merge pull request #17 from seamapi/fix/image-url-parser
Browse files Browse the repository at this point in the history
fix: Handle proxied image urls in seed-from-api
  • Loading branch information
louis-pre authored Jul 22, 2024
2 parents 3760594 + 9bb61ba commit 4af79de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib/database/seed-from-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export const seedDatabaseFromApi = async (
}

const getImageIdFromImageUrl = (url: string): string => {
const image_url = new URL(url)
const image_id = image_url.searchParams.get("image_id")
if (image_id == null) {
throw new Error(`No image_id in ${url}`)
}
const searchParams = new URL(url).searchParams
const imageId = searchParams.get("image_id")
if (imageId) return imageId

return image_id
const proxiedUrl = searchParams.get("url")
if (!proxiedUrl) throw new Error(`No image_id in "${url}"`)
return getImageIdFromImageUrl(proxiedUrl)
}
13 changes: 13 additions & 0 deletions test/seed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ test("seed database from api", async (t) => {
await seedDatabaseFromApi(db, client)
})

const client_with_proxied_images = axios.create({
baseURL: serverURL,
headers: {
"x-vercel-protection-bypass": db.vercel_protection_bypass_secret,
"x-forwarded-seam-base-url":
"https://example.com/devicedb?url=https://example.com/devicedb",
},
})

await t.notThrowsAsync(async () => {
await seedDatabaseFromApi(db, client_with_proxied_images)
})

t.deepEqual(db.manufacturers[0], { ...manufacturer, device_model_count: 1 })
t.deepEqual(db.device_models[0], device_model)
})

0 comments on commit 4af79de

Please sign in to comment.