react-user-agent-client-hints
Advanced tools
Comparing version 0.1.2 to 0.1.3
import { UADataValues, UALowEntropyJSON } from "../types"; | ||
export declare type Hint = "architecture" | "model" | "bitness" | "platformVersion" | "fullVersionList"; | ||
declare type Hint = "architecture" | "model" | "bitness" | "platformVersion" | "fullVersionList"; | ||
declare type HighEntropy = "high"; | ||
declare type LowEntropy = "low"; | ||
export declare function useUserAgentClientHints(params: { | ||
declare const notAllowedErrorName = "NotAllowedError"; | ||
declare const errors: { | ||
highEntropyAndNoHints: string; | ||
unexpectedErrorOccurred: string; | ||
userAgentApiUndefined: string; | ||
permissionDenied: string; | ||
}; | ||
/** | ||
* Overloads to help infer the correct return type. | ||
*/ | ||
declare function useUserAgentClientHints(params: { | ||
entropy: HighEntropy; | ||
hints: Hint[]; | ||
}): UADataValues; | ||
export declare function useUserAgentClientHints(params: { | ||
}): UADataValues | Error; | ||
declare function useUserAgentClientHints(params: { | ||
entropy: LowEntropy; | ||
}): UALowEntropyJSON; | ||
export declare function useUserAgentClientHints(): UALowEntropyJSON; | ||
export {}; | ||
}): UALowEntropyJSON | Error; | ||
declare function useUserAgentClientHints(): UALowEntropyJSON | Error; | ||
export { useUserAgentClientHints, errors, notAllowedErrorName }; | ||
//# sourceMappingURL=useUserAgentClientHints.d.ts.map |
@@ -12,6 +12,16 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useUserAgentClientHints = void 0; | ||
exports.notAllowedErrorName = exports.errors = exports.useUserAgentClientHints = void 0; | ||
const react_1 = require("react"); | ||
const types_1 = require("../types"); | ||
const HIGH_ENTROPY = "high"; | ||
const LOW_ENTROPY = "low"; | ||
const notAllowedErrorName = "NotAllowedError"; | ||
exports.notAllowedErrorName = notAllowedErrorName; | ||
const errors = { | ||
highEntropyAndNoHints: "Cannot have high entropy and no hints.", | ||
unexpectedErrorOccurred: "An unexpected error has been occurred.", | ||
userAgentApiUndefined: "User-agent client hints API is undefined.", | ||
permissionDenied: "Permission denied accessing user-agent data." | ||
}; | ||
exports.errors = errors; | ||
/** | ||
@@ -22,3 +32,2 @@ * Type-safe hook for accessing the current browser and operating system information. | ||
* @returns User agent data mapped from the hints argument or an error. | ||
* @throws | ||
*/ | ||
@@ -34,11 +43,8 @@ function useUserAgentClientHints(params) { | ||
if (params.hints === undefined) { | ||
setError(new Error("Cannot have high entropy and no hints.")); | ||
return; | ||
throw new Error(errors.highEntropyAndNoHints); | ||
} | ||
const data = yield getHighEntropyUserAgentData(params.hints); | ||
if (data instanceof Error) { | ||
setError(data); | ||
break; | ||
if ((0, types_1.isUADataValues)(data)) { | ||
dispatch({ type: HIGH_ENTROPY, payload: data }); | ||
} | ||
dispatch({ type: HIGH_ENTROPY, payload: data }); | ||
break; | ||
@@ -48,3 +54,5 @@ } | ||
const data = getLowEntropyUserAgentData(); | ||
dispatch({ type: LOW_ENTROPY, payload: data }); | ||
if ((0, types_1.isUALowEntropyJSON)(data)) { | ||
dispatch({ type: LOW_ENTROPY, payload: data }); | ||
} | ||
break; | ||
@@ -54,8 +62,9 @@ } | ||
const data = getLowEntropyUserAgentData(); | ||
dispatch({ type: LOW_ENTROPY, payload: data }); | ||
if ((0, types_1.isUALowEntropyJSON)(data)) { | ||
dispatch({ type: LOW_ENTROPY, payload: data }); | ||
} | ||
break; | ||
} | ||
default: { | ||
setError(new Error("An unexpected case has been encountered.")); | ||
break; | ||
throw new Error(errors.unexpectedErrorOccurred); | ||
} | ||
@@ -65,6 +74,9 @@ } | ||
} | ||
void getUserAgentData(); | ||
getUserAgentData().catch(err => { | ||
if (err instanceof Error) | ||
setError(err); | ||
}); | ||
}, [params === null || params === void 0 ? void 0 : params.entropy, params === null || params === void 0 ? void 0 : params.hints]); | ||
if (error instanceof Error) | ||
throw error; | ||
return error; | ||
return state; | ||
@@ -79,3 +91,3 @@ } | ||
if (agentData === undefined) { | ||
return new Error("Could not return user-agent data."); | ||
throw new Error(errors.userAgentApiUndefined); | ||
} | ||
@@ -86,7 +98,7 @@ return agentData; | ||
if (!(err instanceof Error)) | ||
throw new Error("An unexpected error has occurred."); | ||
if (err.name === "NotAllowedError") { | ||
return new Error("Permission denied accessing user-agent data."); | ||
throw new Error(errors.unexpectedErrorOccurred); | ||
if (err.name === notAllowedErrorName) { | ||
throw new Error(errors.permissionDenied); | ||
} | ||
return err; | ||
throw err; | ||
} | ||
@@ -97,3 +109,3 @@ }); | ||
if (navigator.userAgentData === undefined) | ||
throw new Error("Client does not have user-agent data."); | ||
throw new Error(errors.userAgentApiUndefined); | ||
return navigator.userAgentData.toJSON(); | ||
@@ -110,5 +122,3 @@ } | ||
} | ||
default: | ||
throw new Error("An unexpected case has been reached."); | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
interface NavigatorUABrandVersion { | ||
export interface NavigatorUABrandVersion { | ||
readonly brand: string; | ||
@@ -20,3 +20,4 @@ readonly version: string; | ||
} | ||
export {}; | ||
export declare function isUADataValues(params: unknown): params is UADataValues; | ||
export declare function isUALowEntropyJSON(params: unknown): params is UALowEntropyJSON; | ||
//# sourceMappingURL=types.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isUALowEntropyJSON = exports.isUADataValues = void 0; | ||
function hasKey(k, o) { | ||
return k in o; | ||
} | ||
function isUADataValues(params) { | ||
return (typeof params === "object" && | ||
params !== null && | ||
hasKey("architecture", params) && | ||
typeof params.architecture === "string" && | ||
hasKey("mobile", params) && | ||
typeof params.mobile === "boolean" && | ||
hasKey("platform", params) && | ||
typeof params.platform === "string" && | ||
hasKey("bitness", params) && | ||
typeof params.bitness === "string" && | ||
hasKey("brands", params) && | ||
Array.isArray(params.brands) && | ||
params.brands.every((brand) => brand.brand && brand.version) && | ||
hasKey("model", params) && | ||
typeof params.model === "string" && | ||
hasKey("platformVersion", params) && | ||
typeof params.platformVersion === "string"); | ||
} | ||
exports.isUADataValues = isUADataValues; | ||
function isUALowEntropyJSON(params) { | ||
return (typeof params === "object" && | ||
params !== null && | ||
hasKey("brands", params) && | ||
Array.isArray(params.brands) && | ||
params.brands.every((brand) => brand.brand && brand.version) && | ||
hasKey("mobile", params) && | ||
typeof params.mobile === "boolean" && | ||
hasKey("platform", params) && | ||
typeof params.platform === "string"); | ||
} | ||
exports.isUALowEntropyJSON = isUALowEntropyJSON; |
{ | ||
"name": "react-user-agent-client-hints", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -57,6 +57,3 @@ "main": "./dist/index.js", | ||
"dist" | ||
], | ||
"browsersList": [ | ||
"last 2 Chrome versions" | ||
] | ||
} |
@@ -7,3 +7,3 @@ # React User Agent Client Hints | ||
Hook for getting information about the browser and operating system of a user. | ||
React hook implementing the User-Agent Client Hints API to get information about the browser and operating system of a user. | ||
@@ -51,3 +51,3 @@ ### Potential Use Cases | ||
*/ | ||
const highEntropy = useUserAgentClientHints({ entropy: "high", hints }) | ||
const highEntropyUAData = useUserAgentClientHints({ entropy: "high", hints }) | ||
@@ -58,3 +58,8 @@ /* | ||
*/ | ||
const lowEntropy = useUserAgentClientHints({ entropy: "low" }) | ||
const lowEntropyUAData = useUserAgentClientHints({ entropy: "low" }) | ||
/* | ||
* Can call without parameters too. | ||
*/ | ||
const UADataNoParams = useUserAgentClientHint() | ||
``` | ||
@@ -61,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28748
17
483
68