Skip to content

Commit

Permalink
Set options.url even if some options are invalid
Browse files Browse the repository at this point in the history
Fixes #1753
  • Loading branch information
szmarczak committed Jul 1, 2021
1 parent 891dcbe commit 8d6a680
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
29 changes: 17 additions & 12 deletions source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,20 +776,25 @@ export default class Options {

try {
if (is.plainObject(input)) {
this.merge(input);
this.merge(options);
this.url = input.url;
try {
this.merge(input);
this.merge(options);
} finally {
this.url = input.url;
}
} else {
this.merge(options);

if (options?.url !== undefined) {
if (input === undefined) {
this.url = options.url;
} else {
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
try {
this.merge(options);
} finally {
if (options?.url !== undefined) {
if (input === undefined) {
this.url = options.url;
} else {
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
}
} else if (input !== undefined) {
this.url = input;
}
} else if (input !== undefined) {
this.url = input;
}
}
} catch (error) {
Expand Down
27 changes: 26 additions & 1 deletion test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {parse, URL, URLSearchParams} from 'url';
import test from 'ava';
import {Handler} from 'express';
import pEvent from 'p-event';
import got, {Options, StrictOptions} from '../source/index.js';
import got, {Options, RequestError, StrictOptions} from '../source/index.js';
import withServer, {withBodyParsingServer} from './helpers/with-server.js';
import invalidUrl from './helpers/invalid-url.js';

Expand Down Expand Up @@ -600,3 +600,28 @@ test('throws on too large noise', t => {

/* eslint-enable no-new */
});

test('options have url even if some are invalid', async t => {
const error = await t.throwsAsync<RequestError>(got('https://example.com', {
// @ts-expect-error
invalid: true
}));

t.is((error.options.url as URL).href, 'https://example.com/');
});

test('options have url even if some are invalid - got.extend', async t => {
const instance = got.extend({
handlers: [
(options, next) => {
t.is((options.url as URL).href, 'https://example.com/');
return next(options);
}
]
});

await t.throwsAsync(instance('https://example.com', {
// @ts-expect-error
invalid: true
}));
});

0 comments on commit 8d6a680

Please sign in to comment.