Skip to content

Commit

Permalink
fix: keep statusMessage alias to statusText on response.res object
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 29, 2024
1 parent de110fa commit 3db8b4b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export class HttpClient extends EventEmitter {
status: -1,
statusCode: -1,
statusText: '',
statusMessage: '',
headers: resHeaders,
size: 0,
aborted: false,
Expand Down Expand Up @@ -589,7 +590,7 @@ export class HttpClient extends EventEmitter {

res.headers = response.headers;
res.status = res.statusCode = response.statusCode;
res.statusText = STATUS_CODES[res.status] || '';
res.statusMessage = res.statusText = STATUS_CODES[res.status] || '';
if (res.headers['content-length']) {
res.size = parseInt(res.headers['content-length']);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type RawResponseWithMeta = Readable & {
status: number;
statusCode: number;
statusText: string;
/**
* @alias statusText
* @deprecated use `statusText` instead
**/
statusMessage: string;
headers: IncomingHttpHeaders;
timing: Timing;
// SocketInfo
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ describe('index.test.ts', () => {
it('should work', async () => {
const response = await getDefaultHttpClient().request(`${_url}html`);
assert.equal(response.status, 200);
assert.equal(response.statusText, 'OK');
assert.equal(response.res.statusMessage, 'OK');
assert.equal(response.res.statusText, 'OK');
assert.equal(response.headers['content-type'], 'text/html');
assert(response.headers.date);
assert.equal(response.url, `${_url}html`);
Expand Down Expand Up @@ -338,6 +341,8 @@ describe('index.test.ts', () => {
dataType: 'json',
});
assert.equal(response.status, 400);
assert.equal(response.res.statusMessage, 'Bad Request');
assert.equal(response.res.statusText, 'Bad Request');
assert.deepEqual(response.data, { message: 'mock 400 bad request' });

response = await httpClient.request(`${_url}bar?q=1`, {
Expand Down

0 comments on commit 3db8b4b

Please sign in to comment.