From e3ab2444ba936879d54e037d0b75f7e5387af94d Mon Sep 17 00:00:00 2001 From: Iswarya Sankaran Date: Wed, 18 May 2022 09:53:24 +0530 Subject: [PATCH] Addressed all comments --- documentation/3-streams.md | 9 +++++++++ source/core/index.ts | 3 +-- source/core/response.ts | 5 +++-- test/http.ts | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/documentation/3-streams.md b/documentation/3-streams.md index 854185189..27b56b406 100644 --- a/documentation/3-streams.md +++ b/documentation/3-streams.md @@ -354,6 +354,15 @@ The server's IP address. Whether the response comes from cache or not. +### `ok` + +**Type: `boolean`** + +**Note:** +> - This property need not be checked if throwHttpErrors is true. + +Whether the response was successful (status code in the range 200-299). + ### `statusCode` **Type: `number`** diff --git a/source/core/index.ts b/source/core/index.ts index 2fe465dcd..63e2c5e7b 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -147,7 +147,6 @@ export default class Request extends Duplex implements RequestEvents { requestUrl?: URL; redirectUrls: URL[]; retryCount: number; - ok?: boolean; declare private _requestOptions: NativeRequestOptions; @@ -633,7 +632,7 @@ export default class Request extends Duplex implements RequestEvents { typedResponse.isFromCache = (this._nativeResponse as any).fromCache ?? false; typedResponse.ip = this.ip; typedResponse.retryCount = this.retryCount; - typedResponse.ok = (statusCode >= 200 && statusCode <= 299); + typedResponse.ok = isResponseOk(typedResponse); this._isFromCache = typedResponse.isFromCache; diff --git a/source/core/response.ts b/source/core/response.ts index 60580d62f..eb89d0207 100644 --- a/source/core/response.ts +++ b/source/core/response.ts @@ -93,9 +93,10 @@ export interface PlainResponse extends IncomingMessageWithTimings { body?: unknown; /** - Whether the response status code is 2xx. + Whether the response was successful (status code in the range 200-299). + This property need not be checked if throwHttpErrors is true. */ - ok?: boolean; + ok: boolean; } // For Promise support diff --git a/test/http.ts b/test/http.ts index f18388d1a..662129461 100644 --- a/test/http.ts +++ b/test/http.ts @@ -385,7 +385,7 @@ test('status code 200 has response ok is true', withServer, async (t, server, go const promise = got(''); await t.notThrowsAsync(promise); const {statusCode, body, ok} = await promise; - t.is(ok, true); + t.true(ok); t.is(statusCode, 200); t.is(body, ''); });