Skip to content

Commit

Permalink
Update: add types reference for client-hints from `user-agent-data-ty…
Browse files Browse the repository at this point in the history
…pes`
  • Loading branch information
faisalman committed Sep 10, 2023
1 parent 06426ae commit 864bf98
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 164 deletions.
37 changes: 21 additions & 16 deletions dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*! UAClientHints.js 0.1.1
/*! UAClientHints.js 0.1.2
Parse & serialize user-agent client hints (UA-CH) HTTP headers
https://github.com/faisalman/ua-client-hints-js
Author: Faisal Salman <f@faisalman.com>
MIT License */
/// <reference types="user-agent-data-types" />
/// <reference types="ua-parser-js" />
declare enum FIELD_TYPE {
export declare enum FIELD_TYPE {
Boolean = "sf-boolean",
List = "sf-list",
String = "sf-string"
}
declare const UACH_MAP: {
export declare const UACH_MAP: {
readonly architecture: {
readonly field: "Sec-CH-UA-Arch";
readonly type: FIELD_TYPE.String;
Expand Down Expand Up @@ -51,23 +52,27 @@ declare const UACH_MAP: {
readonly type: FIELD_TYPE.Boolean;
};
};
type UACHBrowser = {
brand: string | null;
version: string | null;
};
type UACHDataType = boolean | string | string[] | UACHBrowser[] | null | undefined;
type UACHDataField = keyof typeof UACH_MAP;
type UACHHeaderField = Lowercase<typeof UACH_MAP[keyof typeof UACH_MAP]['field']>;
export type UACHDataType = boolean | string | string[] | NavigatorUABrandVersion[] | null | undefined;
export type UACHDataField = keyof typeof UACH_MAP;
export type UACHHeaderType = typeof FIELD_TYPE[keyof typeof FIELD_TYPE];
export type UACHHeaderField = Lowercase<typeof UACH_MAP[keyof typeof UACH_MAP]['field']>;
export declare class UAClientHints {
private data;
constructor();
getValues(fields?: UACHDataField[]): Partial<Record<string, UACHDataType>>;
private architecture?;
private bitness?;
private brands?;
private formFactor?;
private fullVersionList?;
private mobile?;
private model?;
private platform?;
private platformVersion?;
private wow64?;
getValues(fields?: UACHDataField[]): UADataValues;
getValuesAsHeaders(fields?: UACHDataField[]): Partial<Record<UACHHeaderField, string>>;
setValues(values?: Partial<Record<string, UACHDataType>>): UAClientHints;
setValues(values?: UADataValues): UAClientHints;
setValuesFromUAParser(uap: UAParser.IResult): UAClientHints;
setValuesFromHeaders(headers: Partial<Record<UACHHeaderField, string>>): UAClientHints;
setValuesFromHeaders(headers: Record<string, string>): UAClientHints;
private parseHeader;
private serializeHeader;
private isValidType;
}
export {};
81 changes: 39 additions & 42 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"use strict";
/*! UAClientHints.js 0.1.1
/*! UAClientHints.js 0.1.2
Parse & serialize user-agent client hints (UA-CH) HTTP headers
https://github.com/faisalman/ua-client-hints-js
Author: Faisal Salman <f@faisalman.com>
MIT License */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UAClientHints = void 0;
exports.UAClientHints = exports.UACH_MAP = exports.FIELD_TYPE = void 0;
var FIELD_TYPE;
(function (FIELD_TYPE) {
FIELD_TYPE["Boolean"] = "sf-boolean";
FIELD_TYPE["List"] = "sf-list";
FIELD_TYPE["String"] = "sf-string";
})(FIELD_TYPE || (FIELD_TYPE = {}));
})(FIELD_TYPE || (exports.FIELD_TYPE = FIELD_TYPE = {}));
;
const UACH_MAP = {
exports.UACH_MAP = {
architecture: {
field: 'Sec-CH-UA-Arch',
type: FIELD_TYPE.String
Expand Down Expand Up @@ -57,47 +57,44 @@ const UACH_MAP = {
};
class UAClientHints {
constructor() {
this.data = {
architecture: null,
bitness: null,
brands: null,
formFactor: null,
fullVersionList: null,
mobile: null,
model: null,
platform: null,
platformVersion: null,
wow64: null
};
return this;
this.architecture = undefined;
this.bitness = undefined;
this.brands = undefined;
this.formFactor = undefined;
this.fullVersionList = undefined;
this.mobile = undefined;
this.model = undefined;
this.platform = undefined;
this.platformVersion = undefined;
this.wow64 = undefined;
}
getValues(fields) {
let values = {};
let props = fields || Object.keys(UACH_MAP);
let props = fields || Object.keys(exports.UACH_MAP);
for (const prop of props) {
if (this.data.hasOwnProperty(prop)) {
values[prop] = this.data[prop];
if (this.hasOwnProperty(prop)) {
values[prop] = this[prop];
}
}
return values;
}
getValuesAsHeaders(fields) {
let values = {};
let props = fields || Object.keys(UACH_MAP);
let props = fields || Object.keys(exports.UACH_MAP);
for (const prop of props) {
if (this.data.hasOwnProperty(prop)) {
const { field, type } = UACH_MAP[prop];
values[field] = this.serializeHeader(this.data[prop], type);
if (this.hasOwnProperty(prop)) {
const { field, type } = exports.UACH_MAP[prop];
values[field] = this.serializeHeader(this[prop], type);
}
}
return values;
}
setValues(values) {
for (const key in values) {
if (this.data.hasOwnProperty(key)) {
if (this.hasOwnProperty(key)) {
const val = values[key];
if (this.isValidType(val, UACH_MAP[key].type)) {
this.data[key] = val;
if (this.isValidType(val, exports.UACH_MAP[key].type)) {
this[key] = val;
}
}
;
Expand All @@ -107,43 +104,43 @@ class UAClientHints {
setValuesFromUAParser(uap) {
const arch = /(x86|arm).*(64)/.exec(uap.cpu.architecture || '');
if (arch) {
this.data.architecture = arch[1];
this.architecture = arch[1];
if (arch[2] == '64') {
this.data.bitness = '64';
this.bitness = '64';
}
}
switch (uap.device.type) {
case 'mobile':
this.data.formFactor = ['Mobile'];
this.data.mobile = true;
this.formFactor = ['Mobile'];
this.mobile = true;
break;
case 'tablet':
this.data.formFactor = ['Tablet'];
this.formFactor = ['Tablet'];
break;
}
if (uap.device.model) {
this.data.model = uap.device.model;
this.model = uap.device.model;
}
if (uap.os.name) {
this.data.platform = uap.os.name;
this.platform = uap.os.name;
if (uap.os.version) {
this.data.platformVersion = uap.os.version;
this.platformVersion = uap.os.version;
}
}
if (uap.browser.name) {
const brands = [{ brand: uap.browser.name, version: uap.browser.version || '' }];
this.data.brands = brands;
this.data.fullVersionList = brands;
this.brands = brands;
this.fullVersionList = brands;
}
return this;
}
setValuesFromHeaders(headers) {
if (Object.keys(headers).some(prop => prop.startsWith('sec-ch-ua'))) {
for (const key in UACH_MAP) {
const { field, type } = UACH_MAP[key];
for (const key in exports.UACH_MAP) {
const { field, type } = exports.UACH_MAP[key];
const headerField = field.toLowerCase();
if (headers.hasOwnProperty(headerField)) {
this.data[key] = this.parseHeader(headers[headerField], type);
this[key] = this.parseHeader(headers[headerField], type);
}
}
}
Expand All @@ -164,8 +161,8 @@ class UAClientHints {
.map(brands => {
const match = /\\?\"(.+)?\\?\".+\\?\"(.+)?\\?\"/.exec(brands);
return {
brand: match ? match[1] : null,
version: match ? match[2] : null
brand: match ? match[1] : '',
version: match ? match[2] : ''
};
});
case FIELD_TYPE.String:
Expand Down
37 changes: 21 additions & 16 deletions dist/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*! UAClientHints.js 0.1.1
/*! UAClientHints.js 0.1.2
Parse & serialize user-agent client hints (UA-CH) HTTP headers
https://github.com/faisalman/ua-client-hints-js
Author: Faisal Salman <f@faisalman.com>
MIT License */
/// <reference types="user-agent-data-types" />
/// <reference types="ua-parser-js" />
declare enum FIELD_TYPE {
export declare enum FIELD_TYPE {
Boolean = "sf-boolean",
List = "sf-list",
String = "sf-string"
}
declare const UACH_MAP: {
export declare const UACH_MAP: {
readonly architecture: {
readonly field: "Sec-CH-UA-Arch";
readonly type: FIELD_TYPE.String;
Expand Down Expand Up @@ -51,23 +52,27 @@ declare const UACH_MAP: {
readonly type: FIELD_TYPE.Boolean;
};
};
type UACHBrowser = {
brand: string | null;
version: string | null;
};
type UACHDataType = boolean | string | string[] | UACHBrowser[] | null | undefined;
type UACHDataField = keyof typeof UACH_MAP;
type UACHHeaderField = Lowercase<typeof UACH_MAP[keyof typeof UACH_MAP]['field']>;
export type UACHDataType = boolean | string | string[] | NavigatorUABrandVersion[] | null | undefined;
export type UACHDataField = keyof typeof UACH_MAP;
export type UACHHeaderType = typeof FIELD_TYPE[keyof typeof FIELD_TYPE];
export type UACHHeaderField = Lowercase<typeof UACH_MAP[keyof typeof UACH_MAP]['field']>;
export declare class UAClientHints {
private data;
constructor();
getValues(fields?: UACHDataField[]): Partial<Record<string, UACHDataType>>;
private architecture?;
private bitness?;
private brands?;
private formFactor?;
private fullVersionList?;
private mobile?;
private model?;
private platform?;
private platformVersion?;
private wow64?;
getValues(fields?: UACHDataField[]): UADataValues;
getValuesAsHeaders(fields?: UACHDataField[]): Partial<Record<UACHHeaderField, string>>;
setValues(values?: Partial<Record<string, UACHDataType>>): UAClientHints;
setValues(values?: UADataValues): UAClientHints;
setValuesFromUAParser(uap: UAParser.IResult): UAClientHints;
setValuesFromHeaders(headers: Partial<Record<UACHHeaderField, string>>): UAClientHints;
setValuesFromHeaders(headers: Record<string, string>): UAClientHints;
private parseHeader;
private serializeHeader;
private isValidType;
}
export {};
Loading

0 comments on commit 864bf98

Please sign in to comment.