From daecb6b6cfaa22f36e3c81068e4f10e06f9140dc Mon Sep 17 00:00:00 2001 From: Gauthier Date: Sat, 19 Oct 2024 00:19:23 +0200 Subject: [PATCH] feat: add a proxy option into settings --- server/utils/bindHttpMethod.ts | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 server/utils/bindHttpMethod.ts diff --git a/server/utils/bindHttpMethod.ts b/server/utils/bindHttpMethod.ts new file mode 100644 index 00000000..28df1e25 --- /dev/null +++ b/server/utils/bindHttpMethod.ts @@ -0,0 +1,46 @@ +// from https://github.com/gajus/global-agent/blob/master/src/utilities/bindHttpMethod.ts + +import type http from 'http'; +import type https from 'https'; + +type AgentType = http.Agent | https.Agent; + +export default ( + // eslint-disable-next-line @typescript-eslint/ban-types + originalMethod: Function, + agent: AgentType +) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return (...args: any[]) => { + let url; + let options; + let callback; + + if (typeof args[0] === 'string' || args[0] instanceof URL) { + url = args[0]; + + if (typeof args[1] === 'function') { + options = {}; + callback = args[1]; + } else { + options = { + ...args[1], + }; + callback = args[2]; + } + } else { + options = { + ...args[0], + }; + callback = args[1]; + } + + options.agent = agent; + + if (url) { + return originalMethod(url, options, callback); + } else { + return originalMethod(options, callback); + } + }; +};