-
Notifications
You must be signed in to change notification settings - Fork 1
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Hello! Please, add proxy support. I think is not currently supported by native fetch
, so I'm using it with axios, like:
import {NekosAPI} from "nekosapi/v3/index.js";
import { SocksProxyAgent } from "socks-proxy-agent";
import axios, { AxiosResponse } from "axios";
type ProxyAuth = {
username: string;
password: string;
}
type CustomProxy = {
protocol: string;
host: string;
port: number;
auth: ProxyAuth;
}
export const Socks5Proxy: CustomProxy = {
protocol: 'socks5',
host: process.env.PROXY_HOST!,
port: parseFloat(process.env.PROXY_PORT!),
auth: {
username: process.env.PROXY_USER != undefined ? process.env.PROXY_USER : "",
password: process.env.PROXY_PASS != undefined ? process.env.PROXY_PASS : ""
}
}
function checkResponseCode(response: AxiosResponse<any, any>) {
if ((response.status > 200 && response.status <= 300)) {
throw new Error(`An error occurred while fetching data from the server. ${response.statusText}. Status: ${response.status}. ${response.request?.url}`)
}
}
async function fetchResponse<T>(url: URL): Promise<T> {
const proxy = process.env.PROXY_HOST != undefined ? Socks5Proxy : false;
let agent;
if (proxy) {
const proxyURL = `${proxy.protocol}://${proxy.auth.username}:${proxy.auth.password}@${proxy.host}:${proxy.port}`;
agent = new SocksProxyAgent(proxyURL);
}
const response = await axios.get(
url.toString(),
{
httpsAgent: agent,
responseType: "json"
}
)
checkResponseCode(response);
return <T> await response.data;
}
export class NekosAPIAxiosProxy extends NekosAPI {
constructor() {
super();
(this as any).checkResponseCode = checkResponseCode;
(this as any).fetchResponse = fetchResponse;
}
}
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request