UNPKG

@xcrap/core

Version:

Xcrap Core is the core package of the Xcrap framework for web scraping, offering tools such as HttpClient, BaseClient, Randomizer, Rotator, and support for proxies and pagination.

75 lines (74 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpResponse = void 0; const parser_1 = require("@xcrap/parser"); const errors_1 = require("./errors"); class HttpResponse { constructor({ body, status, statusText, headers = {}, attempts = 1, failedAttempts = [] }) { this.status = status; this.statusText = statusText; this.body = body; this.attempts = attempts; this.failedAttempts = failedAttempts; this.headers = Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), String(value)])); } isSuccess() { return this.status >= 200 && this.status < 300; } getHeader(name) { return this.headers[name.toLowerCase()]; } get text() { if (this.body === null || this.body === undefined) { return ""; } if (typeof this.body === "string") { return this.body; } if (typeof this.body === "object") { return JSON.stringify(this.body); } return String(this.body); } asJsonParser(ignoreHeader = false) { const contentType = this.getHeader("content-type") || ""; const isJsonHeader = contentType.includes("application/json"); if (!ignoreHeader && !isJsonHeader && typeof this.body !== "object") { try { const validJson = JSON.parse(this.text); const validJsonString = JSON.stringify(validJson); return new parser_1.JsonParser(validJsonString); } catch (e) { throw new errors_1.InvalidJsonBody("Response body is not valid JSON"); } } return new parser_1.JsonParser(this.body); } asMarkdownParser(ignoreHeader = false) { const contentType = this.getHeader("content-type") || ""; const isMarkdownHeader = contentType.includes("text/markdown") || contentType.includes("text/x-markdown"); if (!ignoreHeader && !isMarkdownHeader && typeof this.body !== "string") { throw new errors_1.InvalidMarkdownBody("Response body is not valid Markdown"); } return new parser_1.MarkdownParser(this.body); } asHtmlParser(ignoreHeader = false) { const contentType = this.getHeader("content-type") || ""; const isHtmlHeader = !contentType.includes("text/html"); if (!ignoreHeader && !isHtmlHeader && typeof this.body !== "string") { throw new errors_1.InvalidHtmlBody("Response body is not valid HTML"); } return new parser_1.HtmlParser(this.body); } asParser(constructor) { return new constructor(this.body); } getFailedAttemptsCount() { return this.failedAttempts.length; } hadRetries() { return this.failedAttempts.length > 0; } } exports.HttpResponse = HttpResponse;