Skip to content

Commit

Permalink
🤖 Merge PR #58277 feat(node): declare URL and URLSearchParams as glob…
Browse files Browse the repository at this point in the history
…als by @antongolub

* feat(node): declare URL and URLSearchParams as globals

closes: #34960

* chore(node): simplify URL and URLSearchParams global definitions

* docs(node): add @SInCE comments for URL and URLSearchParam globals

* refactor(node): tweak up URL global type after tsc manual tests

* docs(node): minor jsdoc improvements for global URL and URLSearchParams
  • Loading branch information
antongolub committed Feb 1, 2022
1 parent eb08cf2 commit 781162d
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
6 changes: 6 additions & 0 deletions types/node/test/url.ts
Expand Up @@ -167,3 +167,9 @@ import * as url from 'node:url';
{
const dataUrl: string = url.URL.createObjectURL(new Blob(['']));
}
{
const dataUrl1: URL = new url.URL('file://test');
const dataUrl2: url.URL = new URL('file://test');
const urlSearchParams1: URLSearchParams = new url.URLSearchParams();
const urlSearchParams2: url.URLSearchParams = new URLSearchParams();
}
28 changes: 27 additions & 1 deletion types/node/url.d.ts
Expand Up @@ -778,7 +778,7 @@ declare module 'url' {
* @param fn Invoked for each name-value pair in the query
* @param thisArg To be used as `this` value for when `fn` is called
*/
forEach<TThis = this>(callback: (this: TThis, value: string, name: string, searchParams: this) => void, thisArg?: TThis): void;
forEach<TThis = this>(callback: (this: TThis, value: string, name: string, searchParams: URLSearchParams) => void, thisArg?: TThis): void;
/**
* Returns the value of the first name-value pair whose name is `name`. If there
* are no such pairs, `null` is returned.
Expand Down Expand Up @@ -855,6 +855,32 @@ declare module 'url' {
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}

import { URL as _URL, URLSearchParams as _URLSearchParams } from 'url';
global {
interface URLSearchParams extends _URLSearchParams {}
interface URL extends _URL {}
interface Global {
URL: typeof _URL;
URLSearchParams: typeof _URLSearchParams;
}
/**
* `URL` class is a global reference for `require('url').URL`
* https://nodejs.org/api/url.html#the-whatwg-url-api
* @since v10.0.0
*/
var URL: typeof globalThis extends { webkitURL: infer URL } ? URL : typeof _URL;
/**
* `URLSearchParams` class is a global reference for `require('url').URLSearchParams`
* https://nodejs.org/api/url.html#class-urlsearchparams
* @since v10.0.0
*/
var URLSearchParams: {
prototype: URLSearchParams;
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
toString(): string;
};
}
}
declare module 'node:url' {
export * from 'url';
Expand Down
28 changes: 27 additions & 1 deletion types/node/v12/url.d.ts
Expand Up @@ -103,7 +103,7 @@ declare module 'url' {
append(name: string, value: string): void;
delete(name: string): void;
entries(): IterableIterator<[string, string]>;
forEach(callback: (value: string, name: string, searchParams: this) => void): void;
forEach(callback: (value: string, name: string, searchParams: URLSearchParams) => void): void;
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
Expand All @@ -114,4 +114,30 @@ declare module 'url' {
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}

import { URL as _URL, URLSearchParams as _URLSearchParams } from 'url';
global {
interface URLSearchParams extends _URLSearchParams {}
interface URL extends _URL {}
interface Global {
URL: typeof _URL;
URLSearchParams: typeof _URLSearchParams;
}
/**
* `URL` class is a global reference for `require('url').URL`
* https://nodejs.org/api/url.html#the-whatwg-url-api
* @since v10.0.0
*/
var URL: typeof globalThis extends { webkitURL: infer URL } ? URL : typeof _URL;
/**
* `URLSearchParams` class is a global reference for `require('url').URLSearchParams`.
* https://nodejs.org/api/url.html#class-urlsearchparams
* @since v10.0.0
*/
var URLSearchParams: {
prototype: URLSearchParams;
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
toString(): string;
};
}
}
7 changes: 7 additions & 0 deletions types/node/v14/test/url.ts
Expand Up @@ -154,3 +154,10 @@ import * as url from 'node:url';
{
const path: url.URL = url.pathToFileURL('file://test');
}

{
const dataUrl1: URL = new url.URL('file://test');
const dataUrl2: url.URL = new URL('file://test');
const urlSearchParams1: URLSearchParams = new url.URLSearchParams();
const urlSearchParams2: url.URLSearchParams = new URLSearchParams();
}
28 changes: 27 additions & 1 deletion types/node/v14/url.d.ts
Expand Up @@ -102,7 +102,7 @@ declare module 'url' {
append(name: string, value: string): void;
delete(name: string): void;
entries(): IterableIterator<[string, string]>;
forEach(callback: (value: string, name: string, searchParams: this) => void): void;
forEach(callback: (value: string, name: string, searchParams: URLSearchParams) => void, thisArg?: any): void;
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
Expand All @@ -113,6 +113,32 @@ declare module 'url' {
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}

import { URL as _URL, URLSearchParams as _URLSearchParams } from 'url';
global {
interface URLSearchParams extends _URLSearchParams {}
interface URL extends _URL {}
interface Global {
URL: typeof _URL;
URLSearchParams: typeof _URLSearchParams;
}
/**
* `URL` class is a global reference for `require('url').URL`
* https://nodejs.org/api/url.html#the-whatwg-url-api
* @since v10.0.0
*/
var URL: typeof globalThis extends { webkitURL: infer URL } ? URL : typeof _URL;
/**
* `URLSearchParams` class is a global reference for `require('url').URLSearchParams`.
* https://nodejs.org/api/url.html#class-urlsearchparams
* @since v10.0.0
*/
var URLSearchParams: {
prototype: URLSearchParams;
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
toString(): string;
};
}
}
declare module 'node:url' {
export * from 'url';
Expand Down
6 changes: 6 additions & 0 deletions types/node/v16/test/url.ts
Expand Up @@ -167,3 +167,9 @@ import * as url from 'node:url';
{
const dataUrl: string = url.URL.createObjectURL(new Blob(['']));
}
{
const dataUrl1: URL = new url.URL('file://test');
const dataUrl2: url.URL = new URL('file://test');
const urlSearchParams1: URLSearchParams = new url.URLSearchParams();
const urlSearchParams2: url.URLSearchParams = new URLSearchParams();
}
28 changes: 27 additions & 1 deletion types/node/v16/url.d.ts
Expand Up @@ -738,7 +738,7 @@ declare module 'url' {
* @param fn Invoked for each name-value pair in the query
* @param thisArg To be used as `this` value for when `fn` is called
*/
forEach<TThis = this>(callback: (this: TThis, value: string, name: string, searchParams: this) => void, thisArg?: TThis): void;
forEach<TThis = this>(callback: (this: TThis, value: string, name: string, searchParams: URLSearchParams) => void, thisArg?: TThis): void;
/**
* Returns the value of the first name-value pair whose name is `name`. If there
* are no such pairs, `null` is returned.
Expand Down Expand Up @@ -815,6 +815,32 @@ declare module 'url' {
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}

import { URL as _URL, URLSearchParams as _URLSearchParams } from 'url';
global {
interface URLSearchParams extends _URLSearchParams {}
interface URL extends _URL {}
interface Global {
URL: typeof _URL;
URLSearchParams: typeof _URLSearchParams;
}
/**
* `URL` class is a global reference for `require('url').URL`
* https://nodejs.org/api/url.html#the-whatwg-url-api
* @since v10.0.0
*/
var URL: typeof globalThis extends { webkitURL: infer URL } ? URL : typeof _URL;
/**
* `URLSearchParams` class is a global reference for `require('url').URLSearchParams`.
* https://nodejs.org/api/url.html#class-urlsearchparams
* @since v10.0.0
*/
var URLSearchParams: {
prototype: URLSearchParams;
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
toString(): string;
};
}
}
declare module 'node:url' {
export * from 'url';
Expand Down

0 comments on commit 781162d

Please sign in to comment.