Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): declare URL and URLSearchParams as globals #58277

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions types/node/test/url.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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