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

Add a typescript definition file #27

Closed
abidon opened this issue Apr 7, 2017 · 5 comments
Closed

Add a typescript definition file #27

abidon opened this issue Apr 7, 2017 · 5 comments

Comments

@abidon
Copy link

abidon commented Apr 7, 2017

Using this library in typescript is impossible (with noImplicitAny option enabled) as there is no definition file.

Would you mind adding one ?

@yonilerner
Copy link

yonilerner commented May 15, 2018

FWIW, I just created an https-proxy-agent.d.ts file in my project with:

declare module 'https-proxy-agent' {
    import * as https from 'https'

    namespace HttpsProxyAgent {
        interface HttpsProxyAgentOptions {
            host: string
            port: number
            secureProxy?: boolean
            headers?: {
                [key: string]: string
            }
            [key: string]: any
        }
    }
    
    // HttpsProxyAgent doesnt *actually* extend https.Agent, but for my purposes I want it to pretend that it does
    class HttpsProxyAgent extends https.Agent {
        constructor(opts: HttpsProxyAgent.HttpsProxyAgentOptions)
    }

    export = HttpsProxyAgent
}

And I use

import * as HttpsProxyAgent from 'https-proxy-agent'
import {HttpsProxyAgentOptions} from 'https-proxy-agent'

// HttpsProxyAgent.HttpsProxyAgentOptions would also work
const opts: HttpsProxyAgentOptions = {...}

const agent = new HttpsProxyAgent(opts)

@yesworld
Copy link

@yonilerner Thank you man!

TooTallNate pushed a commit that referenced this issue May 21, 2019
* Added index.d.ts

Based on: #27 (comment)
(thank you @yonilerner)

* Update package.json
@TooTallNate
Copy link
Owner

Fixed by #66.

@MichaelHindley
Copy link

@TooTallNate
Just a heads up, using the index.d.ts from master to work with the currently published 2.2.1 package doesn't work with the signature defined in the constructor, which no longer accepts proxy as a string.

Might just be a intermediate change for upcoming bc breaks, but figured it might be good to share this finding just in case :)

// index.d.ts - master
class HttpsProxyAgent extends https.Agent {
    constructor(opts: HttpsProxyAgent.HttpsProxyAgentOptions)
  }

 const agent = new HttpsProxyAgent({
   host,
   port,
 }); // fails silently when supplied to ws
// index.d.ts - modified
class HttpsProxyAgent extends https.Agent {
  constructor(proxy: string)
}

const agent = new HttpsProxyAgent(proxyString); // works

@nickelswitte
Copy link

I am not sure if this is the right place to talk about this, but using the workaround provided by @MichaelHindley, I get an error when passing the https-proxy-agent to node-fetch.

When trying to do

fetch(url, { 
        method: 'POST', 
        headers: headers, 
        agent: agent, 
        body: data
})

I will get an error, that the agent is not of the correct type. The full error is below.

(property) RequestInit.agent?: Agent | ((parsedUrl: URL) => Agent)

Type 'HttpsProxyAgent' is not assignable to type 'Agent | ((parsedUrl: URL) => Agent)'.
Property 'freeSockets' is missing in type 'HttpsProxyAgent' but required in type 'Agent'.ts(2322)

http.d.ts(361, 18): 'freeSockets' is declared here.
index.d.ts(55, 5): The expected type comes from property 'agent' which is declared here on type 'RequestInit'

This can be fixed using agent: agent as any, but does not seem like a good solution.

How to properly deal with this issue?

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

6 participants