Skip to content

Commit b66a0d4

Browse files
committed
fix: update docs and types to expose request
Updates the types to export a `request` object, and also updates the readme to prefer `request.*` rather than `chai.request.*`.
1 parent 1e1abbe commit b66a0d4

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

README.md

+46-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ chai.use(chaiHttp);
2828
// if you need to access `request`
2929
import {default as chaiHttp, request} from "chai-http";
3030
chai.use(chaiHttp);
31+
3132
request.get(...).send(...);
33+
34+
// or setting up an app
35+
request.execute(app);
3236
```
3337

3438
To use Chai HTTP on a web page, please use the latest v4 version for now.
@@ -54,7 +58,9 @@ port to listen on for a given test.
5458
__Note:__ This feature is only supported on Node.js, not in web browsers.
5559

5660
```js
57-
chai.request.execute(app)
61+
import {request} from 'chai-http';
62+
63+
request.execute(app)
5864
.get('/')
5965
```
6066

@@ -65,7 +71,9 @@ keep the server open, perhaps if you're making multiple requests, you must call
6571
`.keepOpen()` after `.request()`, and manually close the server down:
6672

6773
```js
68-
const requester = chai.request.Request(app).keepOpen()
74+
import {request} from 'chai-http';
75+
76+
const requester = request.Request(app).keepOpen()
6977

7078
Promise.all([
7179
requester.get('/a'),
@@ -81,7 +89,9 @@ Promise.all([
8189
You may also use a base url as the foundation of your request.
8290

8391
```js
84-
chai.request.execute('http://localhost:8080')
92+
import {request} from 'chai-http';
93+
94+
request.execute('http://localhost:8080')
8595
.get('/')
8696
```
8797

@@ -102,25 +112,31 @@ Examples:
102112

103113
`.set()`
104114
```js
115+
import {request} from 'chai-http';
116+
105117
// Set a request header
106-
chai.request.execute(app)
118+
request.execute(app)
107119
.put('/user/me')
108120
.set('Content-Type', 'application/json')
109121
.send({ password: '123', confirmPassword: '123' })
110122
```
111123

112124
`.send()`
113125
```js
126+
import {request} from 'chai-http';
127+
114128
// Send some JSON
115-
chai.request.execute(app)
129+
request.execute(app)
116130
.put('/user/me')
117131
.send({ password: '123', confirmPassword: '123' })
118132
```
119133

120134
`.type()`
121135
```js
136+
import {request} from 'chai-http';
137+
122138
// Send some Form Data
123-
chai.request.execute(app)
139+
request.execute(app)
124140
.post('/user/me')
125141
.type('form')
126142
.send({
@@ -132,30 +148,36 @@ chai.request.execute(app)
132148

133149
`.attach()`
134150
```js
151+
import {request} from 'chai-http';
152+
135153
// Attach a file
136-
chai.request.execute(app)
154+
request.execute(app)
137155
.post('/user/avatar')
138156
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png')
139157
```
140158

141159
`.auth()`
142160
```js
161+
import {request} from 'chai-http';
162+
143163
// Authenticate with Basic authentication
144-
chai.request.execute(app)
164+
request.execute(app)
145165
.get('/protected')
146166
.auth('user', 'pass')
147167

148168
// Authenticate with Bearer Token
149-
chai.request.execute(app)
169+
request.execute(app)
150170
.get('/protected')
151171
.auth(accessToken, { type: 'bearer' })
152172

153173
```
154174

155175
`.query()`
156176
```js
177+
import {request} from 'chai-http';
178+
157179
// Chain some GET query parameters
158-
chai.request.execute(app)
180+
request.execute(app)
159181
.get('/search')
160182
.query({name: 'foo', limit: 10}) // /search?name=foo&limit=10
161183
```
@@ -171,7 +193,9 @@ const { expect } = chai;
171193
To make the request and assert on its response, the `end` method can be used:
172194

173195
```js
174-
chai.request.execute(app)
196+
import {request} from 'chai-http';
197+
198+
request.execute(app)
175199
.put('/user/me')
176200
.send({ password: '123', confirmPassword: '123' })
177201
.end((err, res) => {
@@ -193,8 +217,10 @@ accomplished using the
193217
callback has completed, and the assertions can be verified:
194218

195219
```js
220+
import {request} from 'chai-http';
221+
196222
it('fails, as expected', function(done) { // <= Pass in done callback
197-
chai.request.execute('http://localhost:8080')
223+
request.execute('http://localhost:8080')
198224
.get('/')
199225
.end((err, res) => {
200226
expect(res).to.have.status(123);
@@ -203,7 +229,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
203229
});
204230

205231
it('succeeds silently!', () => { // <= No done callback
206-
chai.request.execute('http://localhost:8080')
232+
request.execute('http://localhost:8080')
207233
.get('/')
208234
.end((err, res) => {
209235
expect(res).to.have.status(123); // <= Test completes before this runs
@@ -221,7 +247,9 @@ If `Promise` is available, `request()` becomes a Promise capable library -
221247
and chaining of `then`s becomes possible:
222248

223249
```js
224-
chai.request.execute(app)
250+
import {request} from 'chai-http';
251+
252+
request.execute(app)
225253
.put('/user/me')
226254
.send({ password: '123', confirmPassword: '123' })
227255
.then((res) => {
@@ -238,8 +266,10 @@ Sometimes you need to keep cookies from one request, and send them with the
238266
next (for example, when you want to login with the first request, then access an authenticated-only resource later). For this, `.request.agent()` is available:
239267

240268
```js
269+
import {request} from 'chai-http';
270+
241271
// Log in
242-
const agent = chai.request.agent(app)
272+
const agent = request.agent(app)
243273
agent
244274
.post('/session')
245275
.send({ username: 'me', password: '123' })
@@ -254,7 +284,7 @@ agent
254284
});
255285
```
256286

257-
Note: The server started by `chai.request.agent(app)` will not automatically close following the test(s). You should call `agent.close()` after your tests to ensure your program exits.
287+
Note: The server started by `request.agent(app)` will not automatically close following the test(s). You should call `agent.close()` after your tests to ensure your program exits.
258288

259289
## Assertions
260290

types/index.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Austin Cawley-Edwards <https://github.com/austince>
66
// TypeScript Version: 3.0
77
/// <reference types="chai" />
8-
import * as request from 'superagent';
8+
import * as superAgentRequest from 'superagent';
99

1010
// Merge namespace with global chai
1111
declare global {
@@ -48,8 +48,8 @@ declare global {
4848
}
4949

5050
namespace ChaiHttp {
51-
interface Response extends request.Response {}
52-
interface Agent extends request.SuperAgentStatic {
51+
interface Response extends superAgentRequest.Response {}
52+
interface Agent extends superAgentRequest.SuperAgentStatic {
5353
keepOpen(): Agent;
5454
close(callback?: (err: any) => void): Agent;
5555
}
@@ -59,3 +59,7 @@ declare global {
5959
declare function chaiHttp(chai: any, utils: any): void;
6060

6161
export default chaiHttp;
62+
63+
declare const request: Chai.ChaiHttpRequest;
64+
65+
export {request};

0 commit comments

Comments
 (0)