Skip to content

Commit

Permalink
Fix #767 #763 - Improve type definition for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Nov 22, 2024
1 parent b4bc86a commit bcf249d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions src/main/ua-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Project: https://github.com/faisalman/ua-parser-js
// Definitions by: Faisal Salman <https://github.com/faisalman>

import type { IncomingHttpHeaders } from 'http';
import type { Headers as FetchAPIHeaders } from 'node-fetch';

declare namespace UAParser {

interface IData<T> {
Expand Down Expand Up @@ -50,11 +53,12 @@ declare namespace UAParser {
type RegexMap = ((RegExp | string | (string | RegExp | Function)[])[])[];
type UAParserProps = 'browser' | 'cpu' | 'device' | 'engine' | 'os';
type UAParserExt = Partial<Record<UAParserProps, RegexMap>> | Partial<Record<UAParserProps, RegexMap>>[];
type UAParserHeaders = Record<string, string> | IncomingHttpHeaders | FetchAPIHeaders;

export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: Record<string, string>): IResult;
export function UAParser(uastring?: string, headers?: Record<string, string>): IResult;
export function UAParser(extensions?: UAParserExt, headers?: Record<string, string>): IResult;
export function UAParser(headers?: Record<string, string>): 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 {

Expand Down
8 changes: 8 additions & 0 deletions test/mocha-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit bcf249d

Please sign in to comment.