Repository: sindresorhus/got
Pull request #2258 (2023-04-30 01:42:34)

Allow specifying `undefined` for options
Fix: https://github.com/sindresorhus/got/issues/2257
Does not throw exception when passing undefined agent.


#### Checklist

- [x] I have read the documentation.
- [x] I have included a pull request description of my changes.
- [x] I have included some tests.
- [x] If it's a new feature, I have included documentation updates in both the README and the types.



Patch:
diff --git a/package.json b/package.json
index 5de2646..9754d46 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "got",
-	"version": "12.6.0",
+	"version": "12.6.1",
 	"description": "Human-friendly and powerful HTTP request library for Node.js",
 	"license": "MIT",
 	"repository": "sindresorhus/got",
diff --git a/readme.md b/readme.md
index 3a7e209..b7b2eec 100644
--- a/readme.md
+++ b/readme.md
@@ -203,7 +203,7 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
 | Pagination API        | :heavy_check_mark:  | :x:                  | :x:                      | :x:                | :x:                    |
 | Request cancelation   | :heavy_check_mark:  | :heavy_check_mark:   | :heavy_check_mark:       | :heavy_check_mark: | :heavy_check_mark:     |
 | RFC compliant caching | :heavy_check_mark:  | :x:                  | :x:                      | :x:                | :x:                    |
-| Cookies (out-of-box)  | :heavy_check_mark:  | :x:                  | :x:                      | :x:                | :x:                    |
+| Cookies (out-of-the-box)  | :heavy_check_mark:  | :x:                  | :x:                      | :x:                | :x:                    |
 | Follows redirects     | :heavy_check_mark:  | :heavy_check_mark:   | :heavy_check_mark:       | :heavy_check_mark: | :heavy_check_mark:     |
 | Retries on failure    | :heavy_check_mark:  | :x:                  | :heavy_check_mark:       | :x:                | :heavy_check_mark:     |
 | Progress events       | :heavy_check_mark:  | :x:                  | :heavy_check_mark:\*\*\* | Browser only       | :heavy_check_mark:     |
@@ -436,7 +436,7 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
 		</td>
 		<td align="center">
 			<a href="https://github.com/renovatebot/renovate">
-				<img width="150" valign="middle" src="https://camo.githubusercontent.com/206d470ac709b9a702a97b0c08d6f389a086793d/68747470733a2f2f72656e6f76617465626f742e636f6d2f696d616765732f6c6f676f2e737667">
+				<img width="150" valign="middle" src="https://camo.githubusercontent.com/7c2dc41a8407d4cfa700f762a1abf46b232858ae7e3a2bf5aee7d9f36416127c/68747470733a2f2f6170702e72656e6f76617465626f742e636f6d2f696d616765732f72656e6f766174655f3636305f3232302e6a7067">
 			</a>
 		</td>
 		<td align="center">
diff --git a/source/core/index.ts b/source/core/index.ts
index b97fe8a..6f79377 100644
--- a/source/core/index.ts
+++ b/source/core/index.ts
@@ -14,7 +14,7 @@ import CacheableRequest, {
 } from 'cacheable-request';
 import decompressResponse from 'decompress-response';
 import is from '@sindresorhus/is';
-import {buffer as getBuffer} from 'get-stream';
+import getStream from 'get-stream';
 import {FormDataEncoder, isFormData as isFormDataLike} from 'form-data-encoder';
 import type {ClientRequestWithTimings, Timings, IncomingMessageWithTimings} from '@szmarczak/http-timer';
 import type ResponseLike from 'responselike';
@@ -54,6 +54,8 @@ export type Progress = {
 	total?: number;
 };
 
+const {buffer: getBuffer} = getStream;
+
 const supportsBrotli = is.string(process.versions.brotli);
 
 const methodsWithoutBody: ReadonlySet<string> = new Set(['GET', 'HEAD']);
diff --git a/source/core/options.ts b/source/core/options.ts
index 8eedbe2..fa058a7 100644
--- a/source/core/options.ts
+++ b/source/core/options.ts
@@ -1078,7 +1078,13 @@ export default class Options {
 				}
 
 				// @ts-expect-error Type 'unknown' is not assignable to type 'never'.
-				this[key as keyof Options] = options[key as keyof Options];
+				const value = options[key as keyof Options];
+				if (value === undefined) {
+					continue;
+				}
+
+				// @ts-expect-error Type 'unknown' is not assignable to type 'never'.
+				this[key as keyof Options] = value;
 
 				push = true;
 			}


Test patch:
diff --git a/test/agent.ts b/test/agent.ts
index 7d46eb5..87fbf4f 100644
--- a/test/agent.ts
+++ b/test/agent.ts
@@ -205,3 +205,17 @@ test('no socket hung up regression', withServer, async (t, server, got) => {
 
 	agent.destroy();
 });
+
+test('accept undefined agent', withServer, async (t, server, got) => {
+	server.get('/', (_request, response) => {
+		response.end('ok');
+	});
+
+	const undefinedAgent = undefined;
+	t.truthy((await got({
+		https: {
+			rejectUnauthorized: false,
+		},
+		agent: undefinedAgent,
+	})).body);
+});