diff --git a/package.json b/package.json index 5a539782..aa887d2b 100755 --- a/package.json +++ b/package.json @@ -221,8 +221,11 @@ "@babel/traverse": "7.23.2", "@jazzer.js/core": "^1.4.0", "@playwright/test": "~1.32.2", + "@types/node": "^22.9.1", + "@types/node-fetch": "^2.6.12", "jshint": "~2.13.6", "mocha": "~8.2.0", + "node-fetch": "^2.7.0", "requirejs": "2.3.2", "safe-regex": "^2.1.1", "tsd": "^0.29.0", diff --git a/src/main/ua-parser.d.ts b/src/main/ua-parser.d.ts index 150d8672..03be66ee 100644 --- a/src/main/ua-parser.d.ts +++ b/src/main/ua-parser.d.ts @@ -2,6 +2,9 @@ // Project: https://github.com/faisalman/ua-parser-js // Definitions by: Faisal Salman +import type { IncomingHttpHeaders } from 'http'; +import type { Headers as FetchAPIHeaders } from 'node-fetch'; + declare namespace UAParser { interface IData { @@ -50,11 +53,12 @@ declare namespace UAParser { type RegexMap = ((RegExp | string | (string | RegExp | Function)[])[])[]; type UAParserProps = 'browser' | 'cpu' | 'device' | 'engine' | 'os'; type UAParserExt = Partial> | Partial>[]; + type UAParserHeaders = Record | IncomingHttpHeaders | FetchAPIHeaders; - export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: Record): IResult; - export function UAParser(uastring?: string, headers?: Record): IResult; - export function UAParser(extensions?: UAParserExt, headers?: Record): IResult; - export function UAParser(headers?: Record): IResult; + export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: UAParserHeaders): IResult; + export function UAParser(uastring?: string, headers?: UAParserHeaders): IResult; + export function UAParser(extensions?: UAParserExt, headers?: UAParserHeaders): IResult; + export function UAParser(headers?: UAParserHeaders): IResult; export class UAParser { diff --git a/test/mocha-test.js b/test/mocha-test.js index 9b35330e..ecd7fe0f 100644 --- a/test/mocha-test.js +++ b/test/mocha-test.js @@ -10,6 +10,7 @@ var cpus = require('./specs/cpu-all.json'); var devices = readJsonFiles('test/specs/devices'); var engines = require('./specs/engine-all.json'); var os = require('./specs/os-all.json'); +var { Headers } = require('node-fetch'); function readJsonFiles(dir) { var list = []; @@ -354,6 +355,13 @@ describe('Read user-agent data from req.headers', function () { let engine = UAParser(req.headers).engine; assert.strictEqual(engine.name, "EdgeHTML"); }); + + it('Fetch API\'s Header can be passed directly into headers', () => { + const reqHeaders = new Headers(); + reqHeaders.append('User-Agent', 'Midori/0.2.2 (X11; Linux i686; U; en-us) WebKit/531.2+'); + const { browser } = UAParser(reqHeaders); + assert.strictEqual(browser.is('Midori'), true); + }); }); describe('Map UA-CH headers', function () {