nhentai-tools
Version:
A toolset to interact with the doujin site nhentai.net
71 lines (67 loc) • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.popular = exports.recent = exports.homepage = exports.autocomplete = exports.tagged = exports.query = void 0;
const client_1 = require("./client");
const error_1 = require("./error");
const utils_1 = require("./utils");
/* this is a test to try making nh's shit search a bit easier
const MAX_ITEMS_PER_PAGE = 25
const aggregateSearchResults = (type: 'search' | 'tagged', query: string | number, { start, count, sort }: {start: number, count: number, sort: string}) => {
const promises: Promise<unknown>[] = []
let countRetrieved = 0
do {
const page = (start / MAX_ITEMS_PER_PAGE) + (countRetrieved / MAX_ITEMS_PER_PAGE) + 1
promises.push(client.get(`/api/galleries/${type}`, { params: { query, page, sort } }))
countRetrieved += MAX_ITEMS_PER_PAGE
} while (countRetrieved < count)
return Promise.all(promises).then(results => results.flat())
} */
/** Search with a string query. */
const query = (query = '&', options) => {
const page = options === null || options === void 0 ? void 0 : options.page;
const sort = options === null || options === void 0 ? void 0 : options.sort;
if (typeof page !== 'undefined' && (!Number(page) || page < 1))
(0, error_1.createError)('Invalid page number');
// conditionally add the query params
const params = Object.assign(Object.assign(Object.assign({}, query && { query }), page && { page }), sort && { sort });
return client_1.client.get('/api/galleries/search', { params }).then(utils_1.extractData).catch(error_1.createError);
};
exports.query = query;
/** Search for a specific tag. This endpoint is inferior to using a namespaced query as it seems to update less frequently. */
const tagged = (tag_id, options) => {
const page = options === null || options === void 0 ? void 0 : options.page;
const sort = options === null || options === void 0 ? void 0 : options.sort;
if (!tag_id || !Number(tag_id))
(0, error_1.createError)('Invalid tag id');
if (typeof page !== 'undefined' && (!Number(page) || page < 1))
(0, error_1.createError)('Invalid page number');
// conditionally add the query params
const params = Object.assign(Object.assign({ tag_id }, page && { page }), sort && { sort });
return client_1.client.get('/api/galleries/tagged', { params }).then(utils_1.extractData).catch(error_1.createError);
};
exports.tagged = tagged;
/** Autocomplete tags, sorted by count. */
const autocomplete = (type, name = '') => {
return client_1.client.post('/api/autocomplete', new URLSearchParams({ name, type }).toString())
.then(({ data: { result } }) => {
// filter all repeated tags
// if we have the exact name, the api returns it twice
if (result.length === 2 && result[0].id === result[1].id)
result.pop();
return result;
}).catch(error_1.createError);
};
exports.autocomplete = autocomplete;
/** Get galleries, the same way the homepage would. */
const homepage = (page = 1) => {
if (!page || page < 1)
(0, error_1.createError)('Invalid page number');
return client_1.client.get('/api/galleries/all', { params: { page } }).then(utils_1.extractData).catch(error_1.createError);
};
exports.homepage = homepage;
/** Search with a string query, sorted by recency. */
const recent = (_query = '&', page = 1) => (0, exports.query)(_query, { page, sort: 'recent' });
exports.recent = recent;
/** Search with a string query, sorted by popularity. */
const popular = (_query = '&', page = 1) => (0, exports.query)(_query, { page, sort: 'popular' });
exports.popular = popular;