@orxataguy/tyr 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,62 +1,62 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2
- import * as cheerio from 'cheerio';
3
-
4
- import { Logger } from '../core/Logger.js';
5
- import { TyrError } from '../core/TyrError.js';
6
-
7
- /**
8
- * @class WebManager
9
- * @description Utilities for accessing APIs and the Internet.
10
- */
11
- export class WebManager {
12
- private logger: Logger;
13
-
14
- constructor(logger: Logger) {
15
- this.logger = logger;
16
- }
17
-
18
- private async fetch(url: string, config?: AxiosRequestConfig): Promise<cheerio.CheerioAPI> {
19
- try {
20
- const response: AxiosResponse<string> = await axios.get(url, config);
21
- return cheerio.load(response.data);
22
- } catch (e) {
23
- throw new TyrError(`Could not fetch URL: ${url}`, e, 'Check your internet connection and that the URL is valid.');
24
- }
25
- }
26
-
27
- /**
28
- * @method selectFromWeb
29
- * @description Selects elements from a web page using a CSS selector.
30
- * @param {string} url - URL of the page to scrape.
31
- * @param {Function} selector - CSS selector function.
32
- * @returns {Promise<string[]>} List of extracted text values.
33
- * @example
34
- * const titles = await web.selectFromWeb('https://example.com', ($) => $('h1'));
35
- */
36
- public async selectFromWeb(url: string, selector: ($: cheerio.CheerioAPI) => cheerio.Cheerio<any>): Promise<string[]> {
37
- try {
38
- const $ = await this.fetch(url);
39
- const results: string[] = [];
40
- const selection = selector($);
41
-
42
- if (typeof selection === 'string') return [selection];
43
-
44
- selection.each((_, element) => {
45
- const text = $(element).text().trim();
46
- if (text) results.push(text);
47
- });
48
-
49
- return results;
50
- } catch (e) {
51
- if (e instanceof TyrError) throw e;
52
- throw new TyrError(`Could not select content from: ${url}`, e);
53
- }
54
- }
55
- }
56
-
57
- export const WebManagerTests = {
58
- selectFromWeb: {
59
- url: 'https://www.w3schools.com/',
60
- selector: ($: any) => $('title'),
61
- },
62
- };
1
+ import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import * as cheerio from 'cheerio';
3
+
4
+ import { Logger } from '../core/Logger.js';
5
+ import { TyrError } from '../core/TyrError.js';
6
+
7
+ /**
8
+ * @class WebManager
9
+ * @description Utilities for accessing APIs and the Internet.
10
+ */
11
+ export class WebManager {
12
+ private logger: Logger;
13
+
14
+ constructor(logger: Logger) {
15
+ this.logger = logger;
16
+ }
17
+
18
+ private async fetch(url: string, config?: AxiosRequestConfig): Promise<cheerio.CheerioAPI> {
19
+ try {
20
+ const response: AxiosResponse<string> = await axios.get(url, config);
21
+ return cheerio.load(response.data);
22
+ } catch (e) {
23
+ throw new TyrError(`Could not fetch URL: ${url}`, e, 'Check your internet connection and that the URL is valid.');
24
+ }
25
+ }
26
+
27
+ /**
28
+ * @method selectFromWeb
29
+ * @description Selects elements from a web page using a CSS selector.
30
+ * @param {string} url - URL of the page to scrape.
31
+ * @param {Function} selector - CSS selector function.
32
+ * @returns {Promise<string[]>} List of extracted text values.
33
+ * @example
34
+ * const titles = await web.selectFromWeb('https://example.com', ($) => $('h1'));
35
+ */
36
+ public async selectFromWeb(url: string, selector: ($: cheerio.CheerioAPI) => cheerio.Cheerio<any>): Promise<string[]> {
37
+ try {
38
+ const $ = await this.fetch(url);
39
+ const results: string[] = [];
40
+ const selection = selector($);
41
+
42
+ if (typeof selection === 'string') return [selection];
43
+
44
+ selection.each((_, element) => {
45
+ const text = $(element).text().trim();
46
+ if (text) results.push(text);
47
+ });
48
+
49
+ return results;
50
+ } catch (e) {
51
+ if (e instanceof TyrError) throw e;
52
+ throw new TyrError(`Could not select content from: ${url}`, e);
53
+ }
54
+ }
55
+ }
56
+
57
+ export const WebManagerTests = {
58
+ selectFromWeb: {
59
+ url: 'https://www.w3schools.com/',
60
+ selector: ($: any) => $('title'),
61
+ },
62
+ };