Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend superagent with proxy method?

I haven't found any TypeScript definitions for superagent-proxy. So when I try to compile my TypeScript application to JavaScript, I get the error message:

ts: Property 'proxy' does not exist on type 'SuperAgentRequest'.

import * as request from 'superagent';
import * as withProxy from 'superagent-proxy';

withProxy(request);

request
    .get(...)
    .proxy(proxy)

I have created a file with declarations, but I don't know what more to do.

declare module 'superagent-proxy';

I guess I want to define some higher order function that takes a superagent and returns a superagent with proxy somehow.

This is my best attempt this far:

import * as request from 'superagent';

declare module 'superagent-proxy' {
    interface SuperAgentRequestWithProxy extends request.SuperAgentStatic {
        proxy(url: string): SuperAgentRequestWithProxy;
    }
}

But it doesn't work and I'm not even sure its close to correct.

Invalid module name in augmentation. Module 'superagent-proxy' resolves to an untyped module at '.../node_modules/superagent-proxy/index.js', which cannot be augmented

like image 911
user1283776 Avatar asked Jan 02 '26 03:01

user1283776


2 Answers

you need to add the type dependency as well, importing only the super agent proxy dependency is not sufficient, you need to have two imports : "superagent-proxy": "^2.1.0", under dependencies
"@types/superagent-proxy": "^2.0.0", under dev dependencies

like image 55
Majd Mahajenah Avatar answered Jan 06 '26 10:01

Majd Mahajenah


To install

npm i superagent
npm i superagent-proxy
npm i --save-dev suuperagent-proxy

To use

import * as superagent from 'superagent';
import enableProxy from 'superagent-proxy';
enableProxy(superagent);

Then:

await superagent.get(...).proxy(proxyUrl)
like image 40
TeYoU Avatar answered Jan 06 '26 09:01

TeYoU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!