chaijs / chai-http

HTTP Response assertions for the Chai Assertion Library.
http://chaijs.com/plugins/chai-http
632 stars 112 forks source link

add ChaiHttp.Request to types #304

Open bgmort opened 1 year ago

bgmort commented 1 year ago

Suppose I assign a type to an unresolved chai http request in Typescript. Chai doesn't export a type, so the type has to be imported from superagent, which breaks encapsulation. This PR exports a wrapper type so that requests can be typed without an explicit dependency on superagent.

Before:

import chai, { request } from 'chai'
import chaiHttp from 'chai-http'
import {SuperAgentRequest} from 'superagent' // breaks encapsulation

chai.use(chaiHttp)

let req: SuperAgentRequest
if (Math.random() < .5) {
    req = request('https://example.com').get('/')
}
else {
    req = request('https://other.com').get('/other')
}

req.set('Cookie', 'cookie=true')

After:

import chai, { request } from 'chai'
import chaiHttp from 'chai-http'

chai.use(chaiHttp)

let req: ChaiHttp.Request
if (Math.random() < .5) {
    req = request('https://example.com').get('/')
}
else {
    req = request('https://other.com').get('/other')
}

req.set('Cookie', 'cookie=true')
FredericFernandes commented 1 month ago

No news from this PR? I've just upgraded to chai 5.x.x

And I need to install "@types/superagent" in devDependencies to be able to use this syntax in TS : chai.request.execute('http://localhost:8080') .get('/')

And I don't understand why I need to install this package.?

43081j commented 1 month ago

upgrading to chai 5 won't have caused that. you needed to install the superagent types with chai 4 too.

technically @types/superagent should be a prod dependency, then you wouldn't need to install it manually. Though if we did that, non-typescript users would unnecessarily be pulling it down