Skip to content

Commit

Permalink
Fix type conflicts for URL and URLSearchParams types (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh authored and sindresorhus committed Dec 12, 2019
1 parent 4e9f188 commit cd4226d
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 83 deletions.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -125,7 +125,9 @@
"documentation/examples/*"
],
"rules": {
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/no-empty-function": "off",
"node/prefer-global/url": "off",
"node/prefer-global/url-search-params": "off"
}
}
}
2 changes: 2 additions & 0 deletions source/normalize-arguments.ts
@@ -1,3 +1,4 @@
import {URL, URLSearchParams} from 'url';
import {promisify} from 'util';
import CacheableRequest = require('cacheable-request');
import http = require('http');
Expand Down Expand Up @@ -234,6 +235,7 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default
}

if (is.urlInstance(url) || is.string(url)) {
// @ts-ignore URL is not URL
options.url = url;

options = mergeOptions(defaults?.options ?? {}, options);
Expand Down
3 changes: 2 additions & 1 deletion source/request-as-event-emitter.ts
Expand Up @@ -2,6 +2,7 @@ import CacheableRequest = require('cacheable-request');
import EventEmitter = require('events');
import http = require('http');
import stream = require('stream');
import {URL} from 'url';
import {promisify} from 'util';
import is from '@sindresorhus/is';
import timer from '@szmarczak/http-timer';
Expand Down Expand Up @@ -239,7 +240,7 @@ export default (options: NormalizedOptions): RequestAsEventEmitter => {
} else {
// Catches errors thrown by calling `requestFn(…)`
try {
// @ts-ignore URLSearchParams does not equal URLSearchParams
// @ts-ignore ResponseObject does not equal IncomingMessage
handleRequest(httpOptions.request(options.url, httpOptions, handleResponse));
} catch (error) {
emitError(new RequestError(error, options));
Expand Down
38 changes: 0 additions & 38 deletions source/types/url/index.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions source/utils/merge.ts
@@ -1,3 +1,4 @@
import {URL} from 'url';
import is from '@sindresorhus/is';
import {Merge} from 'type-fest';

Expand Down
2 changes: 2 additions & 0 deletions source/utils/options-to-url.ts
@@ -1,3 +1,5 @@
import {URL, URLSearchParams} from 'url';

function validateSearchParams(searchParams: Record<string, unknown>): asserts searchParams is Record<string, string | number | boolean | null> {
for (const value of Object.values(searchParams)) {
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean' && value !== null) {
Expand Down
42 changes: 1 addition & 41 deletions source/utils/types.ts
Expand Up @@ -4,6 +4,7 @@ import Keyv = require('keyv');
import CacheableRequest = require('cacheable-request');
import PCancelable = require('p-cancelable');
import ResponseLike = require('responselike');
import {URL} from 'url';
import {Readable as ReadableStream} from 'stream';
import {Timings} from '@szmarczak/http-timer';
import CacheableLookup from 'cacheable-lookup';
Expand Down Expand Up @@ -247,44 +248,3 @@ export interface CancelableRequest<T extends Response | Response['body']> extend
buffer(): CancelableRequest<Buffer>;
text(): CancelableRequest<string>;
}

// TODO: Remove this when https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34960 is resolved
declare global {
// @ts-ignore TypeScript complains about duplicated identifier.
class URL {
readonly origin: string;
readonly searchParams: URLSearchParams;
hash: string;
host: string;
hostname: string;
href: string;
password: string;
pathname: string;
port: string;
protocol: string;
search: string;
username: string;

constructor(input: string, base?: string | URL);
toString(): string;
toJSON(): string;
}

// @ts-ignore TypeScript complains about duplicated identifier.
class URLSearchParams implements Iterable<[string, string]> {
constructor(init?: URLSearchParams | string | {[key: string]: string | string[] | undefined} | Iterable<[string, string]> | Array<[string, string]>);
append(name: string, value: string): void;
delete(name: string): void;
entries(): IterableIterator<[string, string]>;
forEach(callback: (value: string, name: string, searchParams: this) => void): void;
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
keys(): IterableIterator<string>;
set(name: string, value: string): void;
sort(): void;
toString(): string;
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}
}
2 changes: 1 addition & 1 deletion source/utils/url-to-options.ts
@@ -1,4 +1,4 @@
import {UrlWithStringQuery} from 'url';
import {URL, UrlWithStringQuery} from 'url';
import is from '@sindresorhus/is';

// TODO: Deprecate legacy URL at some point
Expand Down
2 changes: 1 addition & 1 deletion test/arguments.ts
@@ -1,5 +1,5 @@
/* eslint-disable node/no-deprecated-api */
import {parse} from 'url';
import {parse, URL, URLSearchParams} from 'url';
import test from 'ava';
import {Handler} from 'express';
import pEvent = require('p-event');
Expand Down
1 change: 1 addition & 0 deletions test/create.ts
@@ -1,4 +1,5 @@
import {Agent as HttpAgent, IncomingMessage, request as httpRequest, RequestOptions} from 'http';
import {URL} from 'url';
import test from 'ava';
import is from '@sindresorhus/is';
import {Handler} from 'express';
Expand Down
1 change: 1 addition & 0 deletions test/url-to-options.ts
@@ -1,4 +1,5 @@
import url = require('url');
import {URL} from 'url';
import test from 'ava';
import urlToOptions from '../source/utils/url-to-options';

Expand Down

0 comments on commit cd4226d

Please sign in to comment.