Skip to content

Commit

Permalink
Merge pull request #44 from ynhhoJ/bugfixes/43
Browse files Browse the repository at this point in the history
Implement support of OPDF object entries #43
  • Loading branch information
ynhhoJ authored Jul 7, 2024
2 parents db232d1 + b811810 commit 3f52f2a
Show file tree
Hide file tree
Showing 7 changed files with 7,054 additions and 4,742 deletions.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flibusta",
"version": "0.4.4",
"version": "0.4.5",
"author": "ynhhoJ",
"description": "Unofficial Flibusta API based on website search engine. If you like to read books - buy",
"license": "MIT",
Expand Down Expand Up @@ -77,4 +77,4 @@
"unofficial",
"search"
]
}
}
38 changes: 22 additions & 16 deletions src/flibustaOpdsApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,28 @@ class FlibustaOpdsApiHelper {
this.axiosInstance = axiosController;
}

public prepareResponseFromOpdsEntry(entry: Array<OpdsEntry>): Array<SearchBooksByNameOpdsResult> {
return entry.map((item) => {
const {
author, link, title, updated, content, category,
} = item;

return {
author: FlibustaOpdsApiHelper.getAuthorsFromOpdsEntry(author),
title,
updated,
categories: FlibustaOpdsApiHelper.getCategoriesFromOpdsEntry(category),
cover: FlibustaOpdsApiHelper.getCoverFromLink(link),
downloads: FlibustaOpdsApiHelper.getDownloadsItemList(link),
description: content['#text'],
};
});
private prepareResponseFromOpdsObjectEntry(entry: OpdsEntry): SearchBooksByNameOpdsResult {
const {
author, link, title, updated, content, category,
} = entry;

return {
author: FlibustaOpdsApiHelper.getAuthorsFromOpdsEntry(author),
title,
updated,
categories: FlibustaOpdsApiHelper.getCategoriesFromOpdsEntry(category),
cover: FlibustaOpdsApiHelper.getCoverFromLink(link),
downloads: FlibustaOpdsApiHelper.getDownloadsItemList(link),
description: content['#text'],
};
}

public prepareResponseFromOpdsEntry(entry: Array<OpdsEntry> | OpdsEntry): Array<SearchBooksByNameOpdsResult> {
if (!Array.isArray(entry)) {
return [this.prepareResponseFromOpdsObjectEntry(entry)];
}

return entry.map((item) => this.prepareResponseFromOpdsObjectEntry(item));
}

public getTotalPagesCount(totalResults: number, itemsPerPage: number): number {
Expand Down
32 changes: 32 additions & 0 deletions tests/api/getBooksByNameOpds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ describe('getBooksByNameOpds', () => {

expect(booksByNameFromOpds).to.be.equal(undefined);
});

it('should be returned correct response when entry is an object', async () => {
const name = 'Почему я отвлекаюсь';

const booksByNameFromOpds = await getBooksByNameOpdsApi.getBooksByNameFromOpds(name);

if (isNil(booksByNameFromOpds)) {
return;
}

booksByNameFromOpds.forEach((booksItem) => {
expect(booksItem.author).to.satisfy(
(item: SearchBooksByNameOpdsResult['author']) => item.every(
(authorItem) => isString(authorItem.name) && isString(authorItem.uri),
),
);
expect(booksItem.title).to.satisfy((item: SearchBooksByNameOpdsResult['title']) => isString(item));
expect(booksItem.updated).to.satisfy((item: SearchBooksByNameOpdsResult['updated']) => isString(item));
expect(booksItem.categories).to.satisfy((item: SearchBooksByNameOpdsResult['categories']) => item.every(
(categoriesItem) => isString(categoriesItem),
));
expect(booksItem.cover).to.satisfy(
(item: SearchBooksByNameOpdsResult['cover']) => isNil(item) || isString(item),
);
expect(booksItem.downloads).to.satisfy(
(item: SearchBooksByNameOpdsResult['downloads']) => item.every(
(downloadItems) => isString(downloadItems.link) && isString(downloadItems.type),
),
);
expect(booksItem.description).to.satisfy((item: SearchBooksByNameOpdsResult['description']) => isString(item));
});
});
});

describe('getBooksByNameFromOpdsPaginated()', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/flibustaApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('FlibustaAPIHelper', () => {
expect(currentPageInformation).to.be.deep.equal({
hasNextPage: true,
hasPreviousPage: false,
totalPages: 5,
totalPages: 6,
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion types/opdsSearchResult.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type OpdsSearchResult = {
updated: string;
icon: string;
link: Array<OpdsLinkType>;
entry: Nullable<Array<OpdsEntry>>;
entry: Nullable<Array<OpdsEntry>> | Nullable<OpdsEntry>;
'os:totalResults': number;
'os:startIndex': number;
'os:itemsPerPage': number;
Expand Down
Loading

0 comments on commit 3f52f2a

Please sign in to comment.