From 9f06b22b1f897ae39161c128a0c5b76e89eed8f3 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Thu, 29 Mar 2018 03:22:37 -0700 Subject: [PATCH] Add types for whatwg-url --- types/whatwg-url/index.d.ts | 105 +++++++++++++++++++++++++++ types/whatwg-url/tsconfig.json | 23 ++++++ types/whatwg-url/tslint.json | 1 + types/whatwg-url/whatwg-url-tests.ts | 45 ++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 types/whatwg-url/index.d.ts create mode 100644 types/whatwg-url/tsconfig.json create mode 100644 types/whatwg-url/tslint.json create mode 100644 types/whatwg-url/whatwg-url-tests.ts diff --git a/types/whatwg-url/index.d.ts b/types/whatwg-url/index.d.ts new file mode 100644 index 0000000000..8bbc271b9a --- /dev/null +++ b/types/whatwg-url/index.d.ts @@ -0,0 +1,105 @@ +// Type definitions for whatwg-url 6.4 +// Project: https://github.com/jsdom/whatwg-url#readme +// Definitions by: Alexander Marks +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +/** https://url.spec.whatwg.org/#url-representation */ +export interface URLRecord { + scheme: string; + username: string; + password: string; + host: string|number|IPv6Address|null; + port: number|null; + path: string[]; + query: string|null; + fragment: string|null; + cannotBeABaseURL?: boolean; +} + +/** https://url.spec.whatwg.org/#concept-ipv6 */ +export type IPv6Address = + [number, number, number, number, number, number, number, number]; + +/** https://url.spec.whatwg.org/#url-class */ +export class URL { + constructor(url: string, base?: string); + + href: string; + readonly origin: string; + protocol: string; + username: string; + password: string; + host: string; + hostname: string; + port: string; + pathname: string; + search: string; + readonly searchParams: URLSearchParams; + hash: string; + + toJSON(): string; +} + +/** https://url.spec.whatwg.org/#interface-urlsearchparams */ +export class URLSearchParams { + constructor(init: (Array<[string, string]>|Iterable<[string, string]>| + {[name: string]: string}|string)); + + append(name: string, value: string): void; + delete(name: string): void; + get(name: string): string|null; + getAll(name: string): string[]; + has(name: string): boolean; + set(name: string, value: string): void; + sort(): void; + + [Symbol.iterator](): IterableIterator<[string, string]>; +} + +/** https://url.spec.whatwg.org/#concept-url-parser */ +export function parseURL( + input: string, + options?: {baseURL?: string, encodingOverride?: string}): URLRecord|null; + +/** https://url.spec.whatwg.org/#concept-basic-url-parser */ +export function basicURLParse(input: string, options?: { + baseURL?: string, + encodingOverride?: string, + url?: URLRecord, + stateOverride?: StateOverride +}): URLRecord|null; + +/** https://url.spec.whatwg.org/#scheme-start-state */ +export type StateOverride = + 'scheme start'|'scheme'|'no scheme'|'special relative or authority'| + 'path or authority'|'relative'|'relative slash'|'special authority slashes'| + 'special authority ignore slashes'|'authority'|'host'|'hostname'|'port'| + 'file'|'file slash'|'file host'|'path start'|'path'| + 'cannot-be-a-base-URL path'|'query'|'fragment'; + +/** https://url.spec.whatwg.org/#concept-url-serializer */ +export function serializeURL( + urlRecord: URLRecord, excludeFragment?: boolean): string; + +/** https://url.spec.whatwg.org/#concept-host-serializer */ +export function serializeHost(host: string|number|IPv6Address): string; + +/** https://url.spec.whatwg.org/#serialize-an-integer */ +export function serializeInteger(number: number): string; + +/** https://html.spec.whatwg.org#ascii-serialisation-of-an-origin */ +export function serializeURLOrigin(urlRecord: URLRecord): string; + +/** https://url.spec.whatwg.org/#set-the-username */ +export function setTheUsername(urlRecord: URLRecord, username: string): void; + +/** https://url.spec.whatwg.org/#set-the-password */ +export function setThePassword(urlRecord: URLRecord, password: string): void; + +/** https://url.spec.whatwg.org/#cannot-have-a-username-password-port */ +export function cannotHaveAUsernamePasswordPort(urlRecord: URLRecord): boolean; + +/** https://url.spec.whatwg.org/#percent-decode */ +export function percentDecode(buffer: Buffer): Buffer; diff --git a/types/whatwg-url/tsconfig.json b/types/whatwg-url/tsconfig.json new file mode 100644 index 0000000000..9b32093c23 --- /dev/null +++ b/types/whatwg-url/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "whatwg-url-tests.ts" + ] +} diff --git a/types/whatwg-url/tslint.json b/types/whatwg-url/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/whatwg-url/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/whatwg-url/whatwg-url-tests.ts b/types/whatwg-url/whatwg-url-tests.ts new file mode 100644 index 0000000000..6aae6315eb --- /dev/null +++ b/types/whatwg-url/whatwg-url-tests.ts @@ -0,0 +1,45 @@ +import * as whatwgUrl from 'whatwg-url'; + +// $ExpectType URL +new whatwgUrl.URL('http://example.com'); + +// $ExpectType URL +new whatwgUrl.URL('foo', 'http://example.com'); + +// $ExpectType URLRecord | null +const urlRecord = whatwgUrl.parseURL('http://example.com'); + +if (urlRecord !== null) { + // $ExpectType URLRecord | null + whatwgUrl.basicURLParse('http://example.com', {url: urlRecord}); + + // $ExpectType string + whatwgUrl.serializeURL(urlRecord); + // + // $ExpectType string + whatwgUrl.serializeURLOrigin(urlRecord); + // + // $ExpectType void + whatwgUrl.setTheUsername(urlRecord, 'user'); + // + // $ExpectType void + whatwgUrl.setThePassword(urlRecord, 'secret'); + // + // $ExpectType boolean + whatwgUrl.cannotHaveAUsernamePasswordPort(urlRecord); +} + +// $ExpectType string +whatwgUrl.serializeHost('http://example.com'); + +// $ExpectType string +whatwgUrl.serializeHost(42); + +// $ExpectType string +whatwgUrl.serializeHost([0, 0, 0, 0, 0, 0, 0, 0]); + +// $ExpectType string +whatwgUrl.serializeInteger(42); + +// $ExpectType Buffer +whatwgUrl.percentDecode(Buffer.from('foo'));