mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 16:08:47 +03:00
Fix #655 - Provide in-package type definitions
This commit is contained in:
parent
f6fbf170e3
commit
5a0d9cc3d0
@ -167,6 +167,7 @@
|
|||||||
"Ziding Zhang <zidingz@gmail.com>"
|
"Ziding Zhang <zidingz@gmail.com>"
|
||||||
],
|
],
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
|
"types": "src/main/ua-parser.d.ts",
|
||||||
"main": "src/main/ua-parser.js",
|
"main": "src/main/ua-parser.js",
|
||||||
"module": "src/main/ua-parser.mjs",
|
"module": "src/main/ua-parser.mjs",
|
||||||
"browser": "dist/ua-parser.pack.js",
|
"browser": "dist/ua-parser.pack.js",
|
||||||
@ -193,6 +194,7 @@
|
|||||||
"build+test": "npm run build && npm run test",
|
"build+test": "npm run build && npm run test",
|
||||||
"fuzz": "jazzer ./test/jazzer-fuzz-test.js --sync",
|
"fuzz": "jazzer ./test/jazzer-fuzz-test.js --sync",
|
||||||
"test": "./script/test-all.sh",
|
"test": "./script/test-all.sh",
|
||||||
|
"test:dts": "tsd --typings src/main/ua-parser.d.ts --files test/dts-test.ts",
|
||||||
"test:eslint": "eslint src && eslint script",
|
"test:eslint": "eslint src && eslint script",
|
||||||
"test:jshint": "jshint src/main",
|
"test:jshint": "jshint src/main",
|
||||||
"test:lockfile-lint": "npx lockfile-lint -p package-lock.json",
|
"test:lockfile-lint": "npx lockfile-lint -p package-lock.json",
|
||||||
@ -208,6 +210,7 @@
|
|||||||
"mocha": "~8.2.0",
|
"mocha": "~8.2.0",
|
||||||
"requirejs": "2.3.2",
|
"requirejs": "2.3.2",
|
||||||
"safe-regex": "^2.1.1",
|
"safe-regex": "^2.1.1",
|
||||||
|
"tsd": "^0.29.0",
|
||||||
"uglify-js": "~3.12.0"
|
"uglify-js": "~3.12.0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -25,3 +25,8 @@ echo '
|
|||||||
- lint lockfile
|
- lint lockfile
|
||||||
'
|
'
|
||||||
npm run test:lockfile-lint || exit 1
|
npm run test:lockfile-lint || exit 1
|
||||||
|
|
||||||
|
echo '
|
||||||
|
- lint d.ts files
|
||||||
|
'
|
||||||
|
npm run test:dts || exit 1
|
104
src/main/ua-parser.d.ts
vendored
Normal file
104
src/main/ua-parser.d.ts
vendored
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
// Type definitions for UAParser.js v2.0.0-beta
|
||||||
|
// Project: https://github.com/faisalman/ua-parser-js
|
||||||
|
// Definitions by: Faisal Salman <https://github.com/faisalman>
|
||||||
|
|
||||||
|
declare namespace UAParser {
|
||||||
|
|
||||||
|
interface IData<T> {
|
||||||
|
is(val: string): boolean;
|
||||||
|
toString(): string;
|
||||||
|
withClientHints(): PromiseLike<T> | T;
|
||||||
|
withFeatureCheck(): T;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IBrowser extends IData<IBrowser> {
|
||||||
|
name?: string;
|
||||||
|
version?: string;
|
||||||
|
major?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ICPU extends IData<ICPU> {
|
||||||
|
architecture?: 'ia32' | 'ia64' | 'amd64' | 'arm' | 'arm64' | 'armhf' | 'avr' | 'irix' | 'irix64' | 'mips' | 'mips64' | '68k' | 'ppc' | 'sparc' | 'sparc64';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IDevice extends IData<IDevice> {
|
||||||
|
type?: 'mobile' | 'tablet' | 'console' | 'smarttv' | 'wearable';
|
||||||
|
vendor?: string;
|
||||||
|
model?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IEngine extends IData<IEngine> {
|
||||||
|
name?: 'Amaya' | 'Blink' | 'EdgeHTML' | 'Flow' | 'Gecko' | 'Goanna' | 'iCab' | 'KHTML' | 'LibWeb' | 'Links' | 'Lynx' | 'NetFront' | 'NetSurf' | 'Presto' | 'Tasman' | 'Trident' | 'w3m' | 'WebKit';
|
||||||
|
version?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IOS extends IData<IOS> {
|
||||||
|
name?: string;
|
||||||
|
version?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IResult extends IData<IResult> {
|
||||||
|
ua: string;
|
||||||
|
browser: IBrowser;
|
||||||
|
cpu: ICPU;
|
||||||
|
device: IDevice;
|
||||||
|
engine: IEngine;
|
||||||
|
os: IOS;
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegexMap = ((RegExp | string | (string | RegExp | Function)[])[])[];
|
||||||
|
|
||||||
|
export function UAParser(uastring?: string, extensions?: Record<string, RegexMap>, headers?: Record<string, string>): IResult;
|
||||||
|
export function UAParser(uastring?: string, headers?: Record<string, string>): IResult;
|
||||||
|
export function UAParser(extensions?: Record<string, RegexMap>, headers?: Record<string, string>): IResult;
|
||||||
|
export function UAParser(headers?: Record<string, string>): IResult;
|
||||||
|
|
||||||
|
export class UAParser {
|
||||||
|
|
||||||
|
static readonly BROWSER: {
|
||||||
|
NAME: 'name';
|
||||||
|
VERSION: 'version';
|
||||||
|
MAJOR: 'major';
|
||||||
|
};
|
||||||
|
static readonly CPU: {
|
||||||
|
ARCHITECTURE: 'architecture';
|
||||||
|
};
|
||||||
|
static readonly DEVICE: {
|
||||||
|
TYPE: 'type';
|
||||||
|
VENDOR: 'vendor';
|
||||||
|
MODEL: 'model';
|
||||||
|
CONSOLE: 'console';
|
||||||
|
MOBILE: 'mobile';
|
||||||
|
SMARTTV: 'smarttv';
|
||||||
|
TABLET: 'tablet';
|
||||||
|
WEARABLE: 'wearable';
|
||||||
|
EMBEDDED: 'embedded';
|
||||||
|
};
|
||||||
|
static readonly ENGINE: {
|
||||||
|
NAME: 'name';
|
||||||
|
VERSION: 'version';
|
||||||
|
};
|
||||||
|
static readonly OS: {
|
||||||
|
NAME: 'name';
|
||||||
|
VERSION: 'version';
|
||||||
|
};
|
||||||
|
static readonly VERSION: string;
|
||||||
|
|
||||||
|
constructor(uastring?: string, extensions?: Record<string, RegexMap>, headers?: Record<string, string>);
|
||||||
|
constructor(uastring?: string, headers?: Record<string, string>);
|
||||||
|
constructor(extensions?: Record<string, RegexMap>, headers?: Record<string, string>);
|
||||||
|
constructor(headers?: Record<string, string>);
|
||||||
|
|
||||||
|
getUA(): string;
|
||||||
|
getBrowser(): IBrowser;
|
||||||
|
getCPU(): ICPU;
|
||||||
|
getDevice(): IDevice;
|
||||||
|
getEngine(): IEngine;
|
||||||
|
getOS(): IOS;
|
||||||
|
getResult(): IResult;
|
||||||
|
setUA(uastring: string): UAParser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export as namespace UAParser;
|
||||||
|
export = UAParser;
|
42
test/dts-test.ts
Normal file
42
test/dts-test.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { expectType } from 'tsd';
|
||||||
|
import { UAParser, IResult, IBrowser, ICPU, IEngine, IDevice, IOS } from "../src/main/ua-parser";
|
||||||
|
|
||||||
|
const uastring = 'Mozilla/5.0 (X11; MyCustomOS; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0';
|
||||||
|
const extensions = {
|
||||||
|
os : [
|
||||||
|
[/(mycustomos)/], [UAParser.OS.NAME, [UAParser.OS.VERSION, '10']]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
const headers = {
|
||||||
|
'sec-ch-ua-mobile' : '?1'
|
||||||
|
};
|
||||||
|
|
||||||
|
expectType<IResult>(UAParser());
|
||||||
|
expectType<IResult>(UAParser(uastring));
|
||||||
|
expectType<IResult>(UAParser(uastring, extensions));
|
||||||
|
expectType<IResult>(UAParser(uastring, headers));
|
||||||
|
expectType<IResult>(UAParser(extensions, headers));
|
||||||
|
expectType<IResult>(UAParser(extensions));
|
||||||
|
expectType<IResult>(UAParser(headers));
|
||||||
|
expectType<UAParser>(new UAParser());
|
||||||
|
|
||||||
|
const parser = new UAParser(uastring);
|
||||||
|
const browser = parser.getBrowser();
|
||||||
|
|
||||||
|
expectType<IBrowser>(browser);
|
||||||
|
expectType<string | undefined>(browser.name);
|
||||||
|
expectType<string | undefined>(browser.version);
|
||||||
|
expectType<string | undefined>(browser.major);
|
||||||
|
expectType<boolean>(browser.is(''));
|
||||||
|
expectType<string>(browser.toString());
|
||||||
|
expectType<IBrowser | PromiseLike<IBrowser>>(browser.withClientHints());
|
||||||
|
expectType<IBrowser>((<IBrowser>browser.withClientHints()).withFeatureCheck());
|
||||||
|
expectType<boolean>((<IBrowser>browser.withClientHints()).withFeatureCheck().is(''));
|
||||||
|
|
||||||
|
expectType<ICPU>(parser.getCPU());
|
||||||
|
expectType<IDevice>(parser.getDevice());
|
||||||
|
expectType<IEngine>(parser.getEngine());
|
||||||
|
expectType<IOS>(parser.getOS());
|
||||||
|
expectType<IResult>(parser.getResult());
|
||||||
|
expectType<string>(parser.getUA());
|
||||||
|
expectType<UAParser>(parser.setUA(uastring));
|
Loading…
x
Reference in New Issue
Block a user