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 ChaiHttp.Request to types #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

add ChaiHttp.Request to types #304

wants to merge 1 commit into from

Conversation

bgmort
Copy link

@bgmort bgmort commented Jan 20, 2023

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
Copy link

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
Copy link
Contributor

43081j commented Jul 27, 2024

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

@@ -50,6 +50,7 @@ declare global {
}

namespace ChaiHttp {
interface Request extends request.SuperAgentRequest {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any particular reason you know of why this isn't request.Request?

SuperAgentRequest is an alias to it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have been able to answer that question eighteen months ago. I'll take a look when I have some time to clone this repo and check it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries. if you can catch up from main and double check that, we can probably merge this

if you don't have time, just let me know and ill do a new branch 👍

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

Successfully merging this pull request may close these issues.

3 participants