Skip to content

Commit

Permalink
Upgrade Axios
Browse files Browse the repository at this point in the history
  • Loading branch information
brahma-dev committed Mar 1, 2024
1 parent 2b9713d commit 1cefb6e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "metafetch",
"description": "Metafetch fetches a given URL's title, description, images, links etc.",
"version": "3.1.2",
"version": "3.1.3",
"homepage": "https://github.com/brahma-dev/metafetch",
"repository": {
"type": "git",
Expand All @@ -21,7 +21,7 @@
],
"main": "dist/index",
"dependencies": {
"axios": "^0.28.0",
"axios": "^1.6.7",
"cheerio": "^1.0.0-rc.12",
"franc": "^6.2.0",
"iconv-lite": "^0.6.3",
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Expand Up @@ -2,6 +2,7 @@ import _ from "lodash";
import iconv from 'iconv-lite';
import parser, { MetafetchResponse } from "./parser";
import axios, { AxiosRequestHeaders, AxiosBasicCredentials, AxiosProxyConfig } from "axios";
import { AxiosHeaders } from "axios";

axios.interceptors.response.use(response => {
let enc = (response.headers['content-type']?.match(/charset=(.+)/) || []).pop();
Expand Down Expand Up @@ -84,10 +85,10 @@ class Metafetch {
}
const http_options: FetchOptions["http"] = {
timeout: 20000,
headers: {
headers: new AxiosHeaders({
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'User-Agent': this._userAgent
},
}),
maxRedirects: 5
};
const _options = {
Expand All @@ -106,11 +107,11 @@ class Metafetch {
};
let userAgent = this._userAgent;
if (typeof options === 'object') {
_.merge(http_options.headers, options.http?.headers || {});
http_options.timeout = options.http?.timeout || http_options.timeout;
http_options.maxRedirects = options.http?.maxRedirects || http_options.maxRedirects;
_.merge(_options, options.flags || {});
userAgent = options.userAgent || userAgent;
http_options.headers?.set( options.http?.headers || {});
}
axios({
method: 'get',
Expand Down
10 changes: 5 additions & 5 deletions src/parser.ts
@@ -1,7 +1,7 @@
import cheerio, { CheerioAPI } from "cheerio";
import { URL } from 'url';
import langs, { Language } from "langs";
import type { AxiosResponseHeaders } from "axios";
import { RawAxiosRequestHeaders } from "axios";
export interface MetafetchResponse {
title?: string,
charset?: string,
Expand All @@ -16,9 +16,9 @@ export interface MetafetchResponse {
siteName?: string,
image?: string,
meta?: { [x: string]: string },
headers?: AxiosResponseHeaders,
headers?: RawAxiosRequestHeaders,
}
export default function (url: string, options: any, body: string, headers: AxiosResponseHeaders, franc: ((value?: string | undefined) => string) | ((arg0: string) => string)): MetafetchResponse {
export default function (url: string, options: any, body: string, headers: RawAxiosRequestHeaders, franc: ((value?: string | undefined) => string) | ((arg0: string) => string)): MetafetchResponse {
if (!body.includes("html"))
throw new Error("Invalid HTML");
let $: CheerioAPI;
Expand All @@ -35,7 +35,7 @@ export default function (url: string, options: any, body: string, headers: Axios
title = $('title').text();
}
if (options.charset) {
response.charset = $("meta[charset]").attr("charset") || (headers['content-type']?.match(/charset=(.+)/) || []).pop();
response.charset = $("meta[charset]").attr("charset") || (headers['content-type']?.toString().match(/charset=(.+)/) || []).pop();
}
if (options.images) {
var imagehash: { [x: string]: boolean } = {};
Expand Down Expand Up @@ -106,7 +106,7 @@ export default function (url: string, options: any, body: string, headers: Axios
}
});
if (options.language) {
response.language = $("html").attr("lang") || $("html").attr("xml:lang") || headers["Content-Language"] || headers["content-language"];
response.language = $("html").attr("lang") || $("html").attr("xml:lang") || headers["Content-Language"]?.toString() || headers["content-language"]?.toString();
if (typeof response.language == "string") {
response.language = response.language.split("-")[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Expand Up @@ -3,7 +3,6 @@ var should = require('should'),
path = require('path'),
fetchog = require(path.join(__dirname, '../dist/index.js')).default,
classog = require(path.join(__dirname, '../dist/index.js')).Metafetch;
console.log(fetchog)
//Server for redirects
var http = require('http');
var server1;
Expand Down Expand Up @@ -233,7 +232,8 @@ describe('server', function () {
it('should redirect too many times.', function (done) {
fetchog.fetch('http://127.0.0.1:2444/', {
http: {
timeout: 1500
timeout: 1500,
headers: { "X-Hello": 1 }
}
}).then((res) => {
done(res);
Expand Down

0 comments on commit 1cefb6e

Please sign in to comment.