UNPKG

trace.moe.ts

Version:

An API wrapper for https://trace.moe with typings

91 lines (90 loc) 3.75 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.API = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const Endpoints_1 = require("../structures/enums/Endpoints"); const Utils_1 = require("./Utils"); class API { /** * Constructs an instance of the API. */ constructor() { this.uris = { search: Endpoints_1.Endpoints.SEARCH, me: Endpoints_1.Endpoints.ME }; this.utils = new Utils_1.Utils(); } /** * Searches the website for the similar anime. * @param {string} imageURL The URL for the image. * @param {SearchParameters} options Optional parameters for the search. * @returns {SearchResponse} */ fetchAnime(imageURL, options) { return __awaiter(this, void 0, void 0, function* () { if (!imageURL || typeof imageURL !== "string") throw new TypeError(`'imageURL' should be type string. Got type ${typeof imageURL} instead.`); const url = this.utils.handleOptionalParameters(this.uris.search, imageURL, options); return yield (0, node_fetch_1.default)(url) .then((res) => res.json()) .catch((err) => { throw new Error(err); }); }); } /** * Searches the website for the similar anime but this time using an image file. * @param {Buffer} buffer The image buffer. Limited to 25 MB. * @param {SearchParameters} options Optional parameters for the search. * @returns {SearchResponse} */ fetchAnimeFromBuffer(buffer, options) { return __awaiter(this, void 0, void 0, function* () { if (!buffer || !(buffer instanceof Buffer)) throw new TypeError(`'buffer' should be type Buffer. Got type ${typeof buffer} instead.`); const url = this.utils.handleOptionalParameters(this.uris.search, undefined, options); return yield (0, node_fetch_1.default)(url, { method: "POST", body: buffer, headers: { "Content-Type": "application/x-www-form-urlencoded" } }) .then((res) => res.json()) .catch((err) => { throw new Error(err); }); }); } /** * Returns your search quota and limits for your account. * @param {string} key Your API key for trace.moe. * @returns {MeResponse} */ fetchMe(key) { return __awaiter(this, void 0, void 0, function* () { let url = this.uris.me; if (key) url += `?key=${key}`; return yield (0, node_fetch_1.default)(url) .then((res) => res.json()) .catch((err) => { throw new Error(err); }); }); } } exports.API = API;