Skip to content

Commit

Permalink
fix: openlibrary cover
Browse files Browse the repository at this point in the history
  • Loading branch information
benjifs committed Sep 12, 2024
1 parent e7755a3 commit 3603a63
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/functions/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,18 @@ const types = {
limit: 10,
q: query,
page: page,
fields: 'key,title,cover_i,cover_edition_key,edition_key,author_name,author_key'
fields: 'key,title,editions,author_name,author_key,cover_i'
}),
buildError: ({ status, response }) => Response.error({ statusCode: status }, response.error),
parseResponse: res => ({
totalResults: res?.num_found || 0,
results: res?.docs.map(b => {
const key = b.key.replace('/works/', '')
let coverKey
if (b.cover_i && b.cover_edition_key) {
coverKey = `id/${b.cover_i}`
} else if (b.edition_key && b.edition_key.length > 0) {
// Temporary address the fact that sometimes `cover_i` doesn't have an updated image
coverKey = `olid/${b.edition_key[b.edition_key.length - 1]}`
}
const coverKey = b?.editions?.docs[0]?.cover_i
return {
id: `olid:${key}`,
id: `olid:${b.key.replace('/works/', '')}`,
title: b.title,
author: b.author_name ? b.author_name.join(', ') : '',
...(coverKey && { image: `https://covers.openlibrary.org/b/${coverKey}-M.jpg` }),
...(coverKey && { image: `https://covers.openlibrary.org/b/id/${coverKey}-M.jpg` }),
year: b.first_publish_year,
url: `https://openlibrary.org${b.key}`
}
Expand Down Expand Up @@ -113,7 +106,7 @@ export const handler = async (e) => {
if (res.status !== 200) {
return opts.buildError({ status: res.status, response })
} else if (type == 'game' && response.error != 'OK') {
return opts.buildError({ status: 400, response})
return opts.buildError({ status: 400, response })
}

return Response.success(opts.parseResponse(response, type))
Expand Down

0 comments on commit 3603a63

Please sign in to comment.