Skip to content

Commit 3f52f2a

Browse files
authored
Merge pull request #44 from ynhhoJ/bugfixes/43
Implement support of OPDF object entries #43
2 parents db232d1 + b811810 commit 3f52f2a

7 files changed

+7054
-4742
lines changed

.yarnrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flibusta",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"author": "ynhhoJ",
55
"description": "Unofficial Flibusta API based on website search engine. If you like to read books - buy",
66
"license": "MIT",
@@ -77,4 +77,4 @@
7777
"unofficial",
7878
"search"
7979
]
80-
}
80+
}

src/flibustaOpdsApiHelper.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,28 @@ class FlibustaOpdsApiHelper {
8282
this.axiosInstance = axiosController;
8383
}
8484

85-
public prepareResponseFromOpdsEntry(entry: Array<OpdsEntry>): Array<SearchBooksByNameOpdsResult> {
86-
return entry.map((item) => {
87-
const {
88-
author, link, title, updated, content, category,
89-
} = item;
90-
91-
return {
92-
author: FlibustaOpdsApiHelper.getAuthorsFromOpdsEntry(author),
93-
title,
94-
updated,
95-
categories: FlibustaOpdsApiHelper.getCategoriesFromOpdsEntry(category),
96-
cover: FlibustaOpdsApiHelper.getCoverFromLink(link),
97-
downloads: FlibustaOpdsApiHelper.getDownloadsItemList(link),
98-
description: content['#text'],
99-
};
100-
});
85+
private prepareResponseFromOpdsObjectEntry(entry: OpdsEntry): SearchBooksByNameOpdsResult {
86+
const {
87+
author, link, title, updated, content, category,
88+
} = entry;
89+
90+
return {
91+
author: FlibustaOpdsApiHelper.getAuthorsFromOpdsEntry(author),
92+
title,
93+
updated,
94+
categories: FlibustaOpdsApiHelper.getCategoriesFromOpdsEntry(category),
95+
cover: FlibustaOpdsApiHelper.getCoverFromLink(link),
96+
downloads: FlibustaOpdsApiHelper.getDownloadsItemList(link),
97+
description: content['#text'],
98+
};
99+
}
100+
101+
public prepareResponseFromOpdsEntry(entry: Array<OpdsEntry> | OpdsEntry): Array<SearchBooksByNameOpdsResult> {
102+
if (!Array.isArray(entry)) {
103+
return [this.prepareResponseFromOpdsObjectEntry(entry)];
104+
}
105+
106+
return entry.map((item) => this.prepareResponseFromOpdsObjectEntry(item));
101107
}
102108

103109
public getTotalPagesCount(totalResults: number, itemsPerPage: number): number {

tests/api/getBooksByNameOpds.ts

+32
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ describe('getBooksByNameOpds', () => {
5454

5555
expect(booksByNameFromOpds).to.be.equal(undefined);
5656
});
57+
58+
it('should be returned correct response when entry is an object', async () => {
59+
const name = 'Почему я отвлекаюсь';
60+
61+
const booksByNameFromOpds = await getBooksByNameOpdsApi.getBooksByNameFromOpds(name);
62+
63+
if (isNil(booksByNameFromOpds)) {
64+
return;
65+
}
66+
67+
booksByNameFromOpds.forEach((booksItem) => {
68+
expect(booksItem.author).to.satisfy(
69+
(item: SearchBooksByNameOpdsResult['author']) => item.every(
70+
(authorItem) => isString(authorItem.name) && isString(authorItem.uri),
71+
),
72+
);
73+
expect(booksItem.title).to.satisfy((item: SearchBooksByNameOpdsResult['title']) => isString(item));
74+
expect(booksItem.updated).to.satisfy((item: SearchBooksByNameOpdsResult['updated']) => isString(item));
75+
expect(booksItem.categories).to.satisfy((item: SearchBooksByNameOpdsResult['categories']) => item.every(
76+
(categoriesItem) => isString(categoriesItem),
77+
));
78+
expect(booksItem.cover).to.satisfy(
79+
(item: SearchBooksByNameOpdsResult['cover']) => isNil(item) || isString(item),
80+
);
81+
expect(booksItem.downloads).to.satisfy(
82+
(item: SearchBooksByNameOpdsResult['downloads']) => item.every(
83+
(downloadItems) => isString(downloadItems.link) && isString(downloadItems.type),
84+
),
85+
);
86+
expect(booksItem.description).to.satisfy((item: SearchBooksByNameOpdsResult['description']) => isString(item));
87+
});
88+
});
5789
});
5890

5991
describe('getBooksByNameFromOpdsPaginated()', () => {

tests/flibustaApiHelper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('FlibustaAPIHelper', () => {
3939
expect(currentPageInformation).to.be.deep.equal({
4040
hasNextPage: true,
4141
hasPreviousPage: false,
42-
totalPages: 5,
42+
totalPages: 6,
4343
});
4444
});
4545
});

types/opdsSearchResult.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type OpdsSearchResult = {
4949
updated: string;
5050
icon: string;
5151
link: Array<OpdsLinkType>;
52-
entry: Nullable<Array<OpdsEntry>>;
52+
entry: Nullable<Array<OpdsEntry>> | Nullable<OpdsEntry>;
5353
'os:totalResults': number;
5454
'os:startIndex': number;
5555
'os:itemsPerPage': number;

0 commit comments

Comments
 (0)