UNPKG

nhentai-tools

Version:

A toolset to interact with the doujin site nhentai.net

54 lines (53 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.related = exports.random = exports.get = exports.exists = void 0; const client_1 = require("./client"); /** Check if a gallery exists. */ const exists = (id) => { if (!id) throw new Error('id is required'); return client_1.client.head(`/api/gallery/${id}`, { validateStatus: () => true }) .then(({ data, status }) => { if (data.error) throw new Error(data.error); return status === 200; }); }; exports.exists = exists; /** Get a gallery. */ const get = (id) => { if (!id || !Number(id)) throw new Error('ID must be a number'); return client_1.client.get(`/api/gallery/${id}`) .then(({ data }) => { if (data.error) throw new Error(data.error); return data; }); }; exports.get = get; /** Get a random gallery. */ const random = () => { return client_1.client.head('/random/', { validateStatus: status => status === 302 }) .then(({ headers: { location } }) => { if (!location) throw new Error('Location header is missing'); const id = location.split('/')[2]; if (!id || !Number(id)) throw new Error('ID is invalid'); return Number(id); }); }; exports.random = random; /** Get 5 galleries related to another gallery. */ const related = (id) => { if (!id || !Number(id)) throw new Error('ID must be a number'); return client_1.client.get(`/api/gallery/${id}/related`) .then(({ data }) => { if (data.error) throw new Error(data.error); return data.result; }); }; exports.related = related;