You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-1Lines changed: 75 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,36 @@ try {
43
43
}
44
44
```
45
45
46
+
You can also use chaining methods if you do not pass the callback function. Check [here](#method-chaining) for details.
47
+
48
+
```js
49
+
// chaining methods
50
+
inject(dispatch)
51
+
.get('/') // set the request method to GET, and request URL to '/'
52
+
.headers({ foo:'bar' }) // set the request headers
53
+
.query({ foo:'bar' }) // set the query parameters
54
+
.end((err, res) => {
55
+
console.log(res.payload)
56
+
})
57
+
58
+
inject(dispatch)
59
+
.post('/') // set the request method to POST, and request URL to '/'
60
+
.payload('request payload') // set the request payload
61
+
.body('request body') // alias for payload
62
+
.end((err, res) => {
63
+
console.log(res.payload)
64
+
})
65
+
66
+
// async-await is also supported
67
+
try {
68
+
constchain=inject(dispatch).get('/')
69
+
constres=awaitchain.end()
70
+
console.log(res.payload)
71
+
} catch (err) {
72
+
console.log(err)
73
+
}
74
+
```
75
+
46
76
File uploads (multipart/form-data) can be achieved by using [form-data](https://github.com/form-data/form-data) package as shown below:
47
77
48
78
```js
@@ -103,7 +133,7 @@ The declaration file exports types for the following parts of the API:
103
133
104
134
## API
105
135
106
-
#### `inject(dispatchFunc, options, callback)`
136
+
#### `inject(dispatchFunc[, options, callback])`
107
137
108
138
Injects a fake request into an HTTP server.
109
139
@@ -148,6 +178,50 @@ Note: You can also pass a string in place of the `options` object as a shorthand
148
178
149
179
Checks if given object `obj` is a *light-my-request*`Request` object.
150
180
181
+
#### Method chaining
182
+
183
+
There are following methods you can used as chaining:
184
+
-`delete`, `get`, `head`, `options`, `patch`, `post`, `put`, `trace`. They will set the HTTP request method and also the request URL.
185
+
-`body`, `headers`, `payload`, `query`. They can be used to set the request options object.
186
+
187
+
And finally you need to call `end`. It has the signature `function (callback)`.
188
+
If you invoke `end` without a callback function, the method will return a promise, thus you can:
189
+
190
+
```js
191
+
constchain=inject(dispatch).get('/')
192
+
193
+
try {
194
+
constres=awaitchain.end()
195
+
console.log(res.payload)
196
+
} catch (err) {
197
+
// handle error
198
+
}
199
+
200
+
// or
201
+
chain.end()
202
+
.then(res=> {
203
+
console.log(res.payload)
204
+
})
205
+
.catch(err=> {
206
+
// handle error
207
+
})
208
+
```
209
+
210
+
By the way, you can also use promises without calling `end`!
211
+
212
+
```js
213
+
inject(dispatch)
214
+
.get('/')
215
+
.then(res=> {
216
+
console.log(res.payload)
217
+
})
218
+
.catch(err=> {
219
+
// handle error
220
+
})
221
+
```
222
+
223
+
Note: The application would not respond multiple times. If you try to invoking any method after the application has responded, the application would throw an error.
224
+
151
225
## Acknowledgements
152
226
This project has been forked from [`hapi/shot`](https://github.com/hapijs/shot) because we wanted to support *Node ≥ v4* and not only *Node ≥ v8*.
153
227
All the credits before the commit [00a2a82](https://github.com/fastify/light-my-request/commit/00a2a82eb773b765003b6085788cc3564cd08326) goes to the `hapi/shot` project [contributors](https://github.com/hapijs/shot/graphs/contributors).
0 commit comments