Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Custom fetch function for proxies #23

Closed
OIRNOIR opened this issue Mar 29, 2023 · 2 comments
Closed

Feature: Custom fetch function for proxies #23

OIRNOIR opened this issue Mar 29, 2023 · 2 comments

Comments

@OIRNOIR
Copy link
Collaborator

OIRNOIR commented Mar 29, 2023

I've made a proxy with CloudFlare workers to hide my exact IP address. I want to be able to use it with amaribot.js, but amaribot.js only uses the regular fetch API, which doesn't go through the proxy.

I would like to be able to replace the fetch module within amaribot.js as a config option upon initiating the client with one of my own methods or functions, such as this one:

/**
 * @param {String} endpoint 
 * @param {RequestInit} options 
 * @returns {Promise<Response>}
 */
async function fetch(endpoint, options={}) {
    return new Promise(async (resolve) => {
        const res = await fetch("https://uri.to.my.request.proxy.worker.oirnoir.workers.dev/", {...options, headers: {endpoint, headers: options.headers == null ? "{}" : JSON.stringify(options.headers)}, cache: "no-cache"});
        const receivedHeaders = res.headers.has("incomingHeaders") ? JSON.parse(res.headers.get("incomingHeaders")) : null;
        res.proxyHeaders = res.headers;
        if (receivedHeaders != null) {
            Object.defineProperty(res, "headers", {get: () => new Headers(receivedHeaders)});
        }
        return resolve(res);
    });
}

The function has the exact same syntax as the built-in fetch API, but requests through my cloudflare workers proxy. It returns the data in the exact same format. I want to be able to replace the fetch api within amaribot.js with my own function (it can do anything, it just needs to accept and return similar values as the fetch api would). The function above is just an example input.

@OIRNOIR
Copy link
Collaborator Author

OIRNOIR commented Mar 29, 2023

I would submit a PR but idk how to specify an unknown function as a parameter in ts

@thewilloftheshadow
Copy link
Member

Implemented in v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants