Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix handling of goodreads api when currently reading contains a single item #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
fix - handle single review result
When getting reviews from goodreads api, if a single result was
returned, it was not returned as an array. I fixed this by casting the result to an array.
  • Loading branch information
AntoineDrouhin committed Nov 17, 2020
commit e18e7ee8ed8c4bc0a88d9c0b77afe15df5bb450b
5 changes: 4 additions & 1 deletion src/goodreads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import xml from 'fast-xml-parser'
import he from 'he'
import got from 'got'
import { ensureArray } from './type-utils'

export default class Goodreads {

Expand Down Expand Up @@ -56,8 +57,10 @@ export default class Goodreads {
},
})
.text()

const reviewList: Goodreads.ReviewList = xml.parse(res)
const reviews = reviewList.GoodreadsResponse.reviews.review
// reviews.review can be an array or an item
const reviews = ensureArray(reviewList.GoodreadsResponse.reviews.review)
return reviews
}

Expand Down
7 changes: 7 additions & 0 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export function ensureArray<T>(v: T | T[]): T[] {
if (Array.isArray(v)) {
return v
}
return [v]
}
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare namespace Goodreads {
interface ReviewList {
GoodreadsResponse: {
reviews: {
review: Review[];
review: Review[] | Review;
};
};
}
Expand Down