Skip to content

Commit

Permalink
Fix API array return result refactor (md-y#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
md-y committed Sep 14, 2021
1 parent 0261f74 commit 02d2d99
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mangadex-full-api",
"version": "5.5.0",
"version": "5.5.1",
"description": "A MangaDex api based around the official API.",
"main": "./src/index.js",
"types": "./types/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/internal/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class Tag {
static async getAllTags() {
if (Tag.cache.length === 0) {
let res = await Util.apiRequest('/manga/tag');
if (!(res instanceof Array)) throw new APIRequestError('The API did not respond with an array when it was expected to', APIRequestError.INVALID_RESPONSE);
if (res.length === 0) throw new APIRequestError('The API returned an empty array of tags.', APIRequestError.INVALID_RESPONSE);
Tag.cache = res.map(elem => new Tag(elem.data));
if (!(res.data instanceof Array)) throw new APIRequestError('The API did not respond with an array when it was expected to', APIRequestError.INVALID_RESPONSE);
if (res.data.length === 0) throw new APIRequestError('The API returned an empty array of tags.', APIRequestError.INVALID_RESPONSE);
Tag.cache = res.data.map(elem => new Tag(elem));
}
return Tag.cache;
}
Expand Down
16 changes: 9 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,26 @@ async function apiSearchRequest(baseEndpoint, parameterObject, maxLimit = 100, d

// Need at least one request to find the total items available:
let initialResponse = await apiParameterRequest(baseEndpoint, { ...parameterObject, limit: Math.min(limit, maxLimit) });
if (!(initialResponse.results instanceof Array) || typeof initialResponse.total !== 'number') {
throw new APIRequestError(`The API did not respond the correct structure for a search request:\n${initialResponse}`, APIRequestError.INVALID_RESPONSE);
if (!(initialResponse.data instanceof Array) || typeof initialResponse.total !== 'number') {
throw new APIRequestError(`The API did not respond the correct structure for a search request:\n${JSON.stringify(initialResponse)}`, APIRequestError.INVALID_RESPONSE);
}
// Return if only one request is needed (either the limit is low enough for one request or one request returned all available results)
if (limit <= maxLimit || initialResponse.total <= initialResponse.results.length + initialOffset) return initialResponse.results;
if (limit <= maxLimit || initialResponse.total <= initialResponse.data.length + initialOffset) return initialResponse.data.map(elem => { return { data: elem }; });

// Subsequent concurrent requests for the rest of the results:
limit = Math.min(initialResponse.total, limit);
let promises = [];
for (let offset = initialOffset + maxLimit; offset < limit; offset += maxLimit) {
promises.push(apiParameterRequest(baseEndpoint, { ...parameterObject, limit: Math.min(limit - offset, maxLimit), offset: offset }));
}
let finalArray = initialResponse.results;
let finalArray = initialResponse.data;
for (let elem of await Promise.all(promises)) {
if (!(elem.results instanceof Array)) throw new APIRequestError('The API did not respond with an array when it was expected to', APIRequestError.INVALID_RESPONSE);
finalArray = finalArray.concat(elem.results);
if (!(elem.data instanceof Array)) {
throw new APIRequestError(`The API did not respond the correct structure for a search request:\n${JSON.stringify(elem)}`, APIRequestError.INVALID_RESPONSE);
}
finalArray = finalArray.concat(elem.data);
}
return finalArray;
return finalArray.map(elem => { return { data: elem }; }); // Emulate an array of standard manga objects from the /manga/<id> endpoint
}
exports.apiSearchRequest = apiSearchRequest;

Expand Down

0 comments on commit 02d2d99

Please sign in to comment.