Skip to content

Commit 352c691

Browse files
authored
feat: expose statusText in request() ResponseData (#4784)
1 parent fecd473 commit 352c691

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

docs/docs/api/Dispatcher.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ The `RequestOptions.method` property should not be value `'CONNECT'`.
476476
#### Parameter: `ResponseData`
477477

478478
* **statusCode** `number`
479+
* **statusText** `string` - The status message from the response (e.g., "OK", "Not Found").
479480
* **headers** `Record<string, string | string[]>` - Note that all header keys are lower-cased, e.g. `content-type`.
480481
* **body** `stream.Readable` which also implements [the body mixin from the Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin).
481482
* **trailers** `Record<string, string>` - This object starts out
@@ -517,7 +518,7 @@ await once(server, 'listening')
517518
const client = new Client(`http://localhost:${server.address().port}`)
518519

519520
try {
520-
const { body, headers, statusCode, trailers } = await client.request({
521+
const { body, headers, statusCode, statusText, trailers } = await client.request({
521522
path: '/',
522523
method: 'GET'
523524
})

lib/api/api-request.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class RequestHandler extends AsyncResource {
121121
try {
122122
this.runInAsyncScope(callback, null, null, {
123123
statusCode,
124+
statusText: statusMessage,
124125
headers,
125126
trailers: this.trailers,
126127
opaque,

test/request.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,29 @@ describe('Should include headers from iterable objects', scope => {
468468
})
469469
})
470470
})
471+
472+
test('request should include statusText in response', async t => {
473+
t = tspl(t, { plan: 2 })
474+
475+
const server = createServer((req, res) => {
476+
res.writeHead(200, 'Custom Status Text', { 'content-type': 'text/plain' })
477+
res.end('hello')
478+
})
479+
480+
after(() => {
481+
server.closeAllConnections?.()
482+
server.close()
483+
})
484+
485+
await new Promise((resolve) => server.listen(0, resolve))
486+
487+
const { statusText, body } = await request({
488+
method: 'GET',
489+
origin: `http://localhost:${server.address().port}`,
490+
path: '/'
491+
})
492+
493+
t.strictEqual(statusText, 'Custom Status Text')
494+
await body.dump()
495+
t.ok('request completed')
496+
})

types/dispatcher.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ declare namespace Dispatcher {
181181
}
182182
export interface ResponseData<TOpaque = null> {
183183
statusCode: number;
184+
statusText: string;
184185
headers: IncomingHttpHeaders;
185186
body: BodyReadable & BodyMixin;
186187
trailers: Record<string, string>;

0 commit comments

Comments
 (0)