GithubHelp home page GithubHelp logo

cataclym / nekosapi Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 1.23 MB

Async API wrapper for Nekos API

Home Page: https://www.npmjs.com/package/nekosapi

TypeScript 95.27% JavaScript 4.73%
anime image library typescript wrapper

nekosapi's Introduction

nekosapi's People

Contributors

cataclym avatar kamuridesu avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

kamuridesu

nekosapi's Issues

Add Proxy Support

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;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.