Skip to content

Commit 34adfd9

Browse files
authored
revert: "fix: support URL object as config.url input (#10866)" (#10874)
This reverts commit 847d89b.
1 parent 847d89b commit 34adfd9

9 files changed

Lines changed: 29 additions & 272 deletions

File tree

README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,20 +592,11 @@ response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'));
592592

593593
##### axios(url[, config])
594594

595-
`url` accepts either a string or a [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance. The `URL` is coerced to a string before the request is dispatched.
596-
597595
```js
598596
// Send a GET request (default method)
599597
axios('/user/12345');
600598
```
601599

602-
```js
603-
// `url` may also be a URL object
604-
axios.get(new URL('https://api.example.com/foo'), {
605-
params: { a: 1 },
606-
});
607-
```
608-
609600
### Request method aliases
610601

611602
For convenience, aliases have been provided for all common request methods.

index.d.cts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,53 +176,53 @@ declare class Axios {
176176
config: axios.AxiosRequestConfig<D>
177177
): Promise<R>;
178178
get<T = any, R = axios.AxiosResponse<T>, D = any>(
179-
url: string | URL,
179+
url: string,
180180
config?: axios.AxiosRequestConfig<D>
181181
): Promise<R>;
182182
delete<T = any, R = axios.AxiosResponse<T>, D = any>(
183-
url: string | URL,
183+
url: string,
184184
config?: axios.AxiosRequestConfig<D>
185185
): Promise<R>;
186186
head<T = any, R = axios.AxiosResponse<T>, D = any>(
187-
url: string | URL,
187+
url: string,
188188
config?: axios.AxiosRequestConfig<D>
189189
): Promise<R>;
190190
options<T = any, R = axios.AxiosResponse<T>, D = any>(
191-
url: string | URL,
191+
url: string,
192192
config?: axios.AxiosRequestConfig<D>
193193
): Promise<R>;
194194
post<T = any, R = axios.AxiosResponse<T>, D = any>(
195-
url: string | URL,
195+
url: string,
196196
data?: D,
197197
config?: axios.AxiosRequestConfig<D>
198198
): Promise<R>;
199199
put<T = any, R = axios.AxiosResponse<T>, D = any>(
200-
url: string | URL,
200+
url: string,
201201
data?: D,
202202
config?: axios.AxiosRequestConfig<D>
203203
): Promise<R>;
204204
patch<T = any, R = axios.AxiosResponse<T>, D = any>(
205-
url: string | URL,
205+
url: string,
206206
data?: D,
207207
config?: axios.AxiosRequestConfig<D>
208208
): Promise<R>;
209209
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(
210-
url: string | URL,
210+
url: string,
211211
data?: D,
212212
config?: axios.AxiosRequestConfig<D>
213213
): Promise<R>;
214214
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(
215-
url: string | URL,
215+
url: string,
216216
data?: D,
217217
config?: axios.AxiosRequestConfig<D>
218218
): Promise<R>;
219219
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(
220-
url: string | URL,
220+
url: string,
221221
data?: D,
222222
config?: axios.AxiosRequestConfig<D>
223223
): Promise<R>;
224224
query<T = any, R = axios.AxiosResponse<T>, D = any>(
225-
url: string | URL,
225+
url: string,
226226
data?: D,
227227
config?: axios.AxiosRequestConfig<D>
228228
): Promise<R>;
@@ -473,7 +473,7 @@ declare namespace axios {
473473
type LookupAddress = string | LookupAddressEntry;
474474

475475
interface AxiosRequestConfig<D = any> {
476-
url?: string | URL;
476+
url?: string;
477477
method?: Method | string;
478478
baseURL?: string;
479479
allowAbsoluteUrls?: boolean;
@@ -661,7 +661,7 @@ declare namespace axios {
661661
interface AxiosInstance extends Axios {
662662
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
663663
<T = any, R = AxiosResponse<T>, D = any>(
664-
url: string | URL,
664+
url: string,
665665
config?: AxiosRequestConfig<D>
666666
): Promise<R>;
667667

index.d.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export interface LookupAddressEntry {
364364
export type LookupAddress = string | LookupAddressEntry;
365365

366366
export interface AxiosRequestConfig<D = any> {
367-
url?: string | URL;
367+
url?: string;
368368
method?: StringLiteralsOrString<Method>;
369369
baseURL?: string;
370370
allowAbsoluteUrls?: boolean;
@@ -609,61 +609,61 @@ export class Axios {
609609
getUri(config?: AxiosRequestConfig): string;
610610
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
611611
get<T = any, R = AxiosResponse<T>, D = any>(
612-
url: string | URL,
612+
url: string,
613613
config?: AxiosRequestConfig<D>
614614
): Promise<R>;
615615
delete<T = any, R = AxiosResponse<T>, D = any>(
616-
url: string | URL,
616+
url: string,
617617
config?: AxiosRequestConfig<D>
618618
): Promise<R>;
619619
head<T = any, R = AxiosResponse<T>, D = any>(
620-
url: string | URL,
620+
url: string,
621621
config?: AxiosRequestConfig<D>
622622
): Promise<R>;
623623
options<T = any, R = AxiosResponse<T>, D = any>(
624-
url: string | URL,
624+
url: string,
625625
config?: AxiosRequestConfig<D>
626626
): Promise<R>;
627627
post<T = any, R = AxiosResponse<T>, D = any>(
628-
url: string | URL,
628+
url: string,
629629
data?: D,
630630
config?: AxiosRequestConfig<D>
631631
): Promise<R>;
632632
put<T = any, R = AxiosResponse<T>, D = any>(
633-
url: string | URL,
633+
url: string,
634634
data?: D,
635635
config?: AxiosRequestConfig<D>
636636
): Promise<R>;
637637
patch<T = any, R = AxiosResponse<T>, D = any>(
638-
url: string | URL,
638+
url: string,
639639
data?: D,
640640
config?: AxiosRequestConfig<D>
641641
): Promise<R>;
642642
postForm<T = any, R = AxiosResponse<T>, D = any>(
643-
url: string | URL,
643+
url: string,
644644
data?: D,
645645
config?: AxiosRequestConfig<D>
646646
): Promise<R>;
647647
putForm<T = any, R = AxiosResponse<T>, D = any>(
648-
url: string | URL,
648+
url: string,
649649
data?: D,
650650
config?: AxiosRequestConfig<D>
651651
): Promise<R>;
652652
patchForm<T = any, R = AxiosResponse<T>, D = any>(
653-
url: string | URL,
653+
url: string,
654654
data?: D,
655655
config?: AxiosRequestConfig<D>
656656
): Promise<R>;
657657
query<T = any, R = AxiosResponse<T>, D = any>(
658-
url: string | URL,
658+
url: string,
659659
data?: D,
660660
config?: AxiosRequestConfig<D>
661661
): Promise<R>;
662662
}
663663

664664
export interface AxiosInstance extends Axios {
665665
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
666-
<T = any, R = AxiosResponse<T>, D = any>(url: string | URL, config?: AxiosRequestConfig<D>): Promise<R>;
666+
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
667667

668668
create(config?: CreateAxiosDefaults): AxiosInstance;
669669
defaults: Omit<AxiosDefaults, 'headers'> & {

lib/core/Axios.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ import transitionalDefaults from '../defaults/transitional.js';
1212

1313
const validators = validator.validators;
1414

15-
const { toString } = Object.prototype;
16-
17-
const isURL = (url) => {
18-
if (url === null || typeof url !== 'object') {
19-
return false;
20-
}
21-
22-
const prototype = Object.getPrototypeOf(url);
23-
return prototype !== null && prototype !== Object.prototype && toString.call(url) === '[object URL]';
24-
};
25-
26-
const normalizeURL = (url) => (isURL(url) ? url.toString() : url);
27-
2815
/**
2916
* Create a new instance of Axios
3017
*
@@ -44,7 +31,7 @@ class Axios {
4431
/**
4532
* Dispatch a request
4633
*
47-
* @param {String|URL|Object} configOrUrl The config specific for this request (merged with this.defaults)
34+
* @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
4835
* @param {?Object} config
4936
*
5037
* @returns {Promise} The Promise to be fulfilled
@@ -95,19 +82,13 @@ class Axios {
9582
_request(configOrUrl, config) {
9683
/*eslint no-param-reassign:0*/
9784
// Allow for axios('example/url'[, config]) a la fetch API
98-
if (typeof configOrUrl === 'string' || isURL(configOrUrl)) {
85+
if (typeof configOrUrl === 'string') {
9986
config = config || {};
100-
config.url = normalizeURL(configOrUrl);
87+
config.url = configOrUrl;
10188
} else {
10289
config = configOrUrl || {};
10390
}
10491

105-
// Coerce config.url to a string if it's a URL object, so that all downstream
106-
// consumers (buildFullPath, combineURLs, adapters) see a string.
107-
if (isURL(config.url)) {
108-
config.url = normalizeURL(config.url);
109-
}
110-
11192
config = mergeConfig(this.defaults, config);
11293

11394
const { transitional, paramsSerializer, headers } = config;
@@ -251,9 +232,6 @@ class Axios {
251232

252233
getUri(config) {
253234
config = mergeConfig(this.defaults, config);
254-
if (isURL(config.url)) {
255-
config.url = normalizeURL(config.url);
256-
}
257235
const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
258236
return buildURL(fullPath, config.params, config.paramsSerializer);
259237
}

lib/core/dispatchRequest.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,6 @@ import CanceledError from '../cancel/CanceledError.js';
77
import AxiosHeaders from '../core/AxiosHeaders.js';
88
import adapters from '../adapters/adapters.js';
99

10-
const { toString } = Object.prototype;
11-
12-
const isURL = (url) => {
13-
if (url === null || typeof url !== 'object') {
14-
return false;
15-
}
16-
17-
const prototype = Object.getPrototypeOf(url);
18-
return prototype !== null && prototype !== Object.prototype && toString.call(url) === '[object URL]';
19-
};
20-
21-
const normalizeURL = (url) => (isURL(url) ? url.toString() : url);
22-
23-
function normalizeConfigURL(config) {
24-
if (isURL(config.url)) {
25-
config.url = normalizeURL(config.url);
26-
}
27-
}
28-
2910
/**
3011
* Throws a `CanceledError` if cancellation has been requested.
3112
*
@@ -51,8 +32,6 @@ function throwIfCancellationRequested(config) {
5132
* @returns {Promise} The Promise to be fulfilled
5233
*/
5334
export default function dispatchRequest(config) {
54-
normalizeConfigURL(config);
55-
5635
throwIfCancellationRequested(config);
5736

5837
config.headers = AxiosHeaders.from(config.headers);
@@ -64,8 +43,6 @@ export default function dispatchRequest(config) {
6443
config.headers.setContentType('application/x-www-form-urlencoded', false);
6544
}
6645

67-
normalizeConfigURL(config);
68-
6946
const adapter = adapters.getAdapter(config.adapter || defaults.adapter, config);
7047

7148
return adapter(config).then(

lib/helpers/buildURL.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ export function encode(val) {
2929
* @returns {string} The formatted url
3030
*/
3131
export default function buildURL(url, params, options) {
32-
// Safeguard for direct callers (e.g. via ./unsafe/helpers/buildURL.js): coerce URL-like
33-
// objects to strings before using string methods below.
34-
if (url !== null && typeof url === 'object' && typeof url.toString === 'function') {
35-
url = url.toString();
36-
}
37-
3832
if (!params) {
3933
return url;
4034
}

tests/unit/adapters/http.test.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,46 +3116,6 @@ describe('supports http with nodejs', () => {
31163116
}
31173117
});
31183118

3119-
it('should support URL object with params', async () => {
3120-
const server = await startHTTPServer(
3121-
(req, res) => {
3122-
res.setHeader('Content-Type', 'application/json');
3123-
res.end(JSON.stringify({ url: req.url }));
3124-
},
3125-
{ port: SERVER_PORT }
3126-
);
3127-
3128-
try {
3129-
const url = new URL(`http://localhost:${server.address().port}/foo?a=1`);
3130-
const response = await axios.get(url, { params: { b: '2' } });
3131-
3132-
assert.strictEqual(response.status, 200);
3133-
assert.strictEqual(response.data.url, '/foo?a=1&b=2');
3134-
} finally {
3135-
await stopHTTPServer(server);
3136-
}
3137-
});
3138-
3139-
it('should support URL object without params (no crash)', async () => {
3140-
const server = await startHTTPServer(
3141-
(req, res) => {
3142-
res.setHeader('Content-Type', 'application/json');
3143-
res.end(JSON.stringify({ url: req.url }));
3144-
},
3145-
{ port: SERVER_PORT }
3146-
);
3147-
3148-
try {
3149-
const url = new URL(`http://localhost:${server.address().port}/foo`);
3150-
const response = await axios.get(url);
3151-
3152-
assert.strictEqual(response.status, 200);
3153-
assert.strictEqual(response.data.url, '/foo');
3154-
} finally {
3155-
await stopHTTPServer(server);
3156-
}
3157-
});
3158-
31593119
it('should support HTTP protocol', async () => {
31603120
const server = await startHTTPServer(
31613121
(req, res) => {

0 commit comments

Comments
 (0)