UNPKG

@easyscrape/core

Version:

EasyScrape is a NodeJS module designed to be integrated into your web scraping project. With it, you can more easily get information from the web from a JSON object to organized data, as a REST API could give you!

58 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const IESQuery_1 = tslib_1.__importDefault(require("../Interfaces/ITypes/IESQuery.type")); const IESObject_1 = tslib_1.__importDefault(require("../Interfaces/ITypes/IESObject.type")); const ESMiddlewareUtilities_1 = tslib_1.__importDefault(require("./ESMiddlewareUtilities.abstract")); class AbstractEasyScrapeMiddleware extends ESMiddlewareUtilities_1.default { collect($, query) { if (!this.canICollect($)) throw new Error(`The Current Node is not compatible with ${this.SupportFor.LibraryName}!`); let response; if (this.isValidQuery(query) || typeof query === 'string') { response = this.collectFromQuery($, typeof query === 'string' ? { _select: query } : query); } else if (typeof query === 'object') { response = {}; Object.keys(query).forEach((key) => { response[key] = this.collect($, query[key]); }); } else { response = query; } return response; } collectFromQuery($, query) { let key; const queryKeys = Object.keys(query); if ($ === undefined || $ === null) return $; $ = this.canICollect($) ? $ : this.QueriesManager._select$($); let response = $; for (let i = 0; i < queryKeys.length; i++) { key = this.getHandleKeyFor(queryKeys[i]); if (key === '_foreach$') { const each = this.exec(key, response, query, queryKeys[i]); response = each.response; if (each.hasIterateAll) break; } else if (key === false) { this.sendError(501, queryKeys[i], this.SupportFor.LibraryName); } else if (key === '_each$') { response = this.exec(key, response, query[queryKeys[i]], query, queryKeys[i]); break; } else { response = this.exec(key, response, query[queryKeys[i]]); } if (response === undefined) break; } return response; } } exports.default = AbstractEasyScrapeMiddleware; //# sourceMappingURL=EasyScrapeMiddleware.abstract.js.map