From 1c877850534682505c6e1c8b1372275c91777e80 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 24 Dec 2018 12:04:43 +0100 Subject: [PATCH] [psl] Add types --- types/psl/index.d.ts | 39 +++++++++++++++++++++++++++++++++++++++ types/psl/psl-tests.ts | 26 ++++++++++++++++++++++++++ types/psl/tsconfig.json | 23 +++++++++++++++++++++++ types/psl/tslint.json | 1 + 4 files changed, 89 insertions(+) create mode 100644 types/psl/index.d.ts create mode 100644 types/psl/psl-tests.ts create mode 100644 types/psl/tsconfig.json create mode 100644 types/psl/tslint.json diff --git a/types/psl/index.d.ts b/types/psl/index.d.ts new file mode 100644 index 0000000000..0898ed95e0 --- /dev/null +++ b/types/psl/index.d.ts @@ -0,0 +1,39 @@ +// Type definitions for psl 1.1 +// Project: https://github.com/wrangr/psl#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export as namespace psl; + +export function parse(domain: string): ParsedDomain | ParseError; + +export function get(domain: null | undefined): null; +export function get(domain: string): string | null; + +export function isValid(domain: string): boolean; + +export interface ParsedDomain { + tld: string | null; + sld: string | null; + domain: string | null; + subdomain: string | null; + listed: boolean; + input: string; + error: undefined; +} + +export interface ParseError { + input: string; + error: { + code: + | 'DOMAIN_TOO_SHORT' + | 'DOMAIN_TOO_LONG' + | 'LABEL_STARTS_WITH_DASH' + | 'LABEL_ENDS_WITH_DASH' + | 'LABEL_TOO_LONG' + | 'LABEL_TOO_SHORT' + | 'LABEL_INVALID_CHARS'; + message: string; + }; +} diff --git a/types/psl/psl-tests.ts b/types/psl/psl-tests.ts new file mode 100644 index 0000000000..77769a64ae --- /dev/null +++ b/types/psl/psl-tests.ts @@ -0,0 +1,26 @@ +import * as psl from 'psl'; + +const parsed = psl.parse('google.com'); + +if (parsed.error != null) { + const parseError = parsed as psl.ParseError; // tslint:disable-line:no-unnecessary-type-assertion + + // $ExpectType "DOMAIN_TOO_SHORT" | "DOMAIN_TOO_LONG" | "LABEL_STARTS_WITH_DASH" | "LABEL_ENDS_WITH_DASH" | "LABEL_TOO_LONG" | "LABEL_TOO_SHORT" | "LABEL_INVALID_CHARS" + parseError.error.code; + parseError.error.message; // $ExpectType string +} else { + const parsedDomain = parsed as psl.ParsedDomain; // tslint:disable-line:no-unnecessary-type-assertion + + parsedDomain.sld; // $ExpectType string | null + parsedDomain.tld; // $ExpectType string | null + parsedDomain.domain; // $ExpectType string | null + parsedDomain.subdomain; // $ExpectType string | null + parsedDomain.listed; // $ExpectType boolean + parsedDomain.input; // $ExpectType string +} + +psl.get(null); // $ExpectType null +psl.get(undefined); // $ExpectType null +psl.get('example.COM'); // $ExpectType string | null + +psl.isValid('COM'); // $ExpectType boolean diff --git a/types/psl/tsconfig.json b/types/psl/tsconfig.json new file mode 100644 index 0000000000..dd3cadf53f --- /dev/null +++ b/types/psl/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", + "psl-tests.ts" + ] +} diff --git a/types/psl/tslint.json b/types/psl/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/psl/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }