@repobit/dex-utils 0.6.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,121 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [1.1.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.6.1...@repobit/dex-utils@1.1.0) (2025-04-02)
7
+
8
+
9
+ ### Features
10
+
11
+ * **DEX-22143:** added documentation ([a3e4bf0](https://github.com/bitdefender/dex-core/commit/a3e4bf0cc359f0b4aea939cb386d50230dfd62f6))
12
+ * **DEX-22143:** added unit tests ([977bfbf](https://github.com/bitdefender/dex-core/commit/977bfbfff0b0f3deda9af4bd29aad8e399ce88d3))
13
+
14
+
15
+
16
+ ## [0.6.1](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.6.0...@repobit/dex-utils@0.6.1) (2025-03-26)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * **DEX-21085:** add files to package json ([d1fa71f](https://github.com/bitdefender/dex-core/commit/d1fa71f4b89812fa306b7ec0104af52505e87491))
22
+
23
+
24
+
25
+ ## [0.6.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.5.1...@repobit/dex-utils@0.6.0) (2025-03-26)
26
+
27
+
28
+ ### Features
29
+
30
+ * **DEX-21820:** added syntasa integration ([a2fb875](https://github.com/bitdefender/dex-core/commit/a2fb8756362b6affbf10b0c8affb390749c8a9df))
31
+
32
+
33
+
34
+ ## [0.5.1](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.5.0...@repobit/dex-utils@0.5.1) (2025-03-25)
35
+
36
+
37
+ ### Bug Fixes
38
+
39
+ * **DEX-21811:** apply changes for ts ([974670b](https://github.com/bitdefender/dex-core/commit/974670bd4a36c7dfbf86f09a7b3d8fac7e09f6a7))
40
+
41
+
42
+
43
+ ## [0.5.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.4.0...@repobit/dex-utils@0.5.0) (2025-03-25)
44
+
45
+
46
+ ### Features
47
+
48
+ * **DEX-22225:** added all dataLayer classes ([9afb463](https://github.com/bitdefender/dex-core/commit/9afb4631cba0d3731396182098e07a7df790b4c2))
49
+ * **DEX-22225:** created data layer package ([36d6d05](https://github.com/bitdefender/dex-core/commit/36d6d054998460cca8e2978820874b34cbf7409f))
50
+ * **DEX-22225:** restructured the code and added tests ([e29eb2a](https://github.com/bitdefender/dex-core/commit/e29eb2a48fe8c470fbd6609bca37da1a782e5f5f))
51
+
52
+
53
+
54
+ ## [0.4.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.3.3...@repobit/dex-utils@0.4.0) (2025-03-25)
55
+
56
+
57
+ ### Features
58
+
59
+ * **DEX-21805:** change jest to vitest ([8219cf8](https://github.com/bitdefender/dex-core/commit/8219cf88cef6c4290783bb80a8142be0195de13e))
60
+ * **DEX-21805:** chnage jest to vitest ([a36c9cb](https://github.com/bitdefender/dex-core/commit/a36c9cb6ba0145833713781da5069897a77dde3a))
61
+
62
+
63
+
64
+ ## [0.3.3](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.3.2...@repobit/dex-utils@0.3.3) (2025-03-24)
65
+
66
+
67
+ ### Bug Fixes
68
+
69
+ * **DEX-21805:** add jsdom to jest ([c2d1d0b](https://github.com/bitdefender/dex-core/commit/c2d1d0bb0350df4f09f88cf64d3fec59e6a2815d))
70
+ * **DEX-21805:** move build dependecies to root ([e75bb59](https://github.com/bitdefender/dex-core/commit/e75bb59c6c215b6be8167ca136423e577ef10656))
71
+ * **DEX-21805:** update tsconfig for all packages ([01abd44](https://github.com/bitdefender/dex-core/commit/01abd4449f7c950116c3b5b76c6989299ab99aea))
72
+
73
+
74
+
75
+ ## [0.3.2](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.3.0...@repobit/dex-utils@0.3.2) (2025-03-21)
76
+
77
+
78
+ ### Bug Fixes
79
+
80
+ * **DEX-21805:** fix package.json ([487e824](https://github.com/bitdefender/dex-core/commit/487e82449bd5a0e6cd959a509b4f9820952466b6))
81
+
82
+
83
+
84
+ ## [0.3.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.2.0...@repobit/dex-utils@0.3.0) (2025-03-05)
85
+
86
+
87
+ ### Features
88
+
89
+ * **DEX-22143:** added login functionality to the user ([2d4172e](https://github.com/bitdefender/dex-core/commit/2d4172ef0c04e9e0a9f8f8b736428bbfca91b9ec))
90
+
91
+
92
+
93
+ ## [0.2.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.1.0...@repobit/dex-utils@0.2.0) (2025-03-04)
94
+
95
+
96
+ ### ⚠ BREAKING CHANGES
97
+
98
+ * **DEX-22137:** added launch package
99
+
100
+ ### Features
101
+
102
+ * **DEX-22137:** added launch package ([620e29a](https://github.com/bitdefender/dex-core/commit/620e29a078bddd9acc14e7db094686763a9b9711))
103
+
104
+
105
+
106
+ ## 0.1.0 (2025-03-04)
107
+
108
+
109
+ ### ⚠ BREAKING CHANGES
110
+
111
+ * **DEX-22143:** added utils package
112
+
113
+ ### Features
114
+
115
+ * **DEX-22143:** added all dependencies classes ([77d96ef](https://github.com/bitdefender/dex-core/commit/77d96ef6e8968b499406803f5e1bc33e93ee2221))
116
+ * **DEX-22143:** added Cookie class ([9e944b5](https://github.com/bitdefender/dex-core/commit/9e944b59e6337f1e5848e27802e141717ab49eca))
117
+ * **DEX-22143:** added debounce function ([ab5058b](https://github.com/bitdefender/dex-core/commit/ab5058baafebb02e971e2206b90cfcc8f3556efc))
118
+ * **DEX-22143:** added throttle function ([f11a5b0](https://github.com/bitdefender/dex-core/commit/f11a5b0bec8966d7905d8ee862673b68b27ef2a4))
119
+ * **DEX-22143:** added user agent class ([2d655b4](https://github.com/bitdefender/dex-core/commit/2d655b4febf47d9a710bcf01ea30c1cecc4da09d))
120
+ * **DEX-22143:** added utils package ([4827d20](https://github.com/bitdefender/dex-core/commit/4827d205e74e47d91e29284623f4d70dadd432ac))
121
+ * **DEX-22143:** added utils package ([2922a59](https://github.com/bitdefender/dex-core/commit/2922a5959594672bdb978b123063353941a70622))
package/README.md ADDED
@@ -0,0 +1,253 @@
1
+ # `@repobit/dex-utils`
2
+
3
+ # BD Web Utilities
4
+
5
+ BD Web Utilities is an npm package that provides a comprehensive suite of tools for modern web applications. It simplifies common tasks such as cookie management, page context extraction, user management, and user agent detection, while also offering utility functions for dynamic script loading and performance optimization through throttling and debouncing.
6
+
7
+ ---
8
+
9
+ ## Overview
10
+
11
+ This package is designed to help developers manage essential aspects of web applications with ease. It includes:
12
+
13
+ - **Cookie Management:** A simple wrapper around js-cookie to handle browser cookies.
14
+ - **Page Context Extraction:** A class to manage and extract page information such as locale, country, language, and query parameters.
15
+ - **User Management:** An abstract class for handling user login, fingerprinting, geolocation, and locale determination.
16
+ - **User Agent Detection:** An abstract class for parsing the browser’s user agent string to identify operating system and browser details.
17
+ - **Utility Functions:** Standalone functions for dynamically loading scripts and optimizing function execution using throttling and debouncing.
18
+
19
+ ---
20
+
21
+ ## Key Features
22
+
23
+ - **Modular Design:** Import only the components you need.
24
+ - **Intuitive API:** Simple classes with clear public methods, properties, and getters.
25
+ - **Performance Optimization:** Throttling and debouncing functions improve responsiveness.
26
+ - **Cross-Platform Support:** Robust user agent detection for adapting to different devices and browsers.
27
+ - **User-Centric:** Manage user context with automated locale and geolocation detection.
28
+
29
+ ---
30
+
31
+ ## Installation
32
+
33
+ Install via npm or yarn:
34
+
35
+ ```bash
36
+ npm install @repobit/dex-utils
37
+ ```
38
+
39
+ ---
40
+
41
+ # Classes
42
+
43
+ ## Cookies
44
+
45
+ ### Description:
46
+ A wrapper around the js-cookie library (https://www.npmjs.com/package/js-cookie) for managing browser cookies. Alongside the already existing methods, we have also added the has function.
47
+
48
+ ### Public Methods & Properties:
49
+
50
+ - ## set(name: string, value: string, options: {expires: number, path: string, ...}): void
51
+ Use Case: Set a cookie with the corresponding name, value and options. For expires you pass the number of days.
52
+
53
+ - ## get(name: string, options: { domain: string, ...}): string
54
+ Use Case: Get the cookie value if it exists.
55
+
56
+ - ## has(name: string): boolean
57
+ Use Case: Checks if a cookie with the specified name exists. This is useful before attempting to access or modify cookie data.
58
+
59
+ ### Usage
60
+
61
+ ```typescript
62
+ Cookies.set('key', 'value', { expires: 1 });
63
+
64
+ if (Cookies.has('key')) {
65
+ console.log(Cookies.get('key'));
66
+ }
67
+ ```
68
+
69
+ ---
70
+
71
+ ## Page
72
+
73
+ ### Description:
74
+ Manages page-related context such as locale, country, language, page name, and URL query parameters.
75
+
76
+ ### Public Properties:
77
+
78
+ - ## locale: string
79
+ Locale in the format `${string}-${string}` (e.g., "en-us"). It is passed as a parameter to the constructor so it is up to the developer to set it correctly.
80
+
81
+ - ## country: string
82
+ Derived from the locale (e.g., for "en-us", country is "us").
83
+
84
+ - ## language: string
85
+ Derived from the locale (e.g., for "en-us", language is "en").
86
+
87
+ - ## name: string
88
+ Identifier for the page. It's the last part of the link in most cases (e.g, for "https://www.example.com/page", the name should be "page"). It is passed as a parameter to the constructor so it is up to the developer how they wish to set it.
89
+
90
+ - ## queryParams: URLSearchParams
91
+ URL query parameters extracted from the current page link.
92
+
93
+ - ## environment: 'dev' | 'stage' | 'prod'
94
+ Current deployment environment. It is passed as a parameter to the constructor so it is up to the developer to set it correctly depending on the project.
95
+
96
+ ### Public Methods:
97
+
98
+ - ## getParamValue(key: string): string | null
99
+ Use Case: Retrieves the value of a specific query parameter from the URL.
100
+
101
+ ### Usage
102
+
103
+ ```typescript
104
+ const page = new Page('en-us', 'homepage', 'prod');
105
+ console.log(page.country); // Outputs: "us"
106
+ console.log(page.getParamValue('ref')); // Retrieves the value of 'ref' from the URL query parameters
107
+ ```
108
+
109
+ ---
110
+
111
+ ## User
112
+
113
+ ### Description:
114
+ An abstract class that manages user-related operations such as login, fingerprint generation, and geolocation-based locale determination.
115
+
116
+ ### Public properties:
117
+
118
+ - ## info: Promise<UserInfoResult | null>
119
+ Logged-in user's information (if available) received from Connect.
120
+
121
+ - ## fingerprint: Promise<string | null>
122
+ A unique ID for the user.
123
+
124
+ - ## country: Promise<string>
125
+ The user's country based on geolocation.
126
+
127
+ - ## locale: Promise<string>
128
+ The user's locale (e.g., "en-us") fetched using his country.
129
+
130
+ ### Public Methods:
131
+
132
+ - ## login(): Promise<void>
133
+ Use Case: Initiates the user login process. If the user is not logged in, this method redirects to the login endpoint.
134
+
135
+ ### Usage
136
+
137
+ ```typescript
138
+ User.info.then(info => {
139
+ if (!info) {
140
+ User.login();
141
+ } else {
142
+ console.log('User Info:', info);
143
+ }
144
+ });
145
+ ```
146
+
147
+ ---
148
+
149
+ ## UserAgent
150
+
151
+ ### Description:
152
+ An abstract class that parses the user agent string to identify operating system and browser details. This is built upon the already existing cssua (https://cssuseragent.org/).
153
+
154
+ ### Public Getters (Operating System):
155
+
156
+ - ## windows, macos, ios, android, linux
157
+ Use Case: Identify the user's operating system.
158
+
159
+ ### Public Getters (Browser):
160
+
161
+ - ## edge, chrome, firefox, safari, opera, ie, vivaldi
162
+ Use Case: Determine the user's browser.
163
+
164
+ ### Public Boolean Getters:
165
+
166
+ - ## isWindows, isMacos, isLinux, isUnix, isAndroid, isIos, isDesktop, isMobile
167
+ - ## isEdge, isChrome, isFirefox, isSafari, isOpera, isVivaldi, isIe
168
+ Use Case: Quickly check the device type and browser for conditional logic in the application.
169
+
170
+ ### Computed Getters:
171
+
172
+ - ## os
173
+ Use Case: Returns a string representing the detected operating system.
174
+
175
+ - ## browser
176
+ Use Case: Returns a string representing the detected browser.
177
+
178
+ ### Usage
179
+
180
+ ```typescript
181
+ if (UserAgent.isMobile) {
182
+ console.log('Operating System:', UserAgent.os);
183
+ } else {
184
+ console.log('Browser:', UserAgent.browser);
185
+ }
186
+ ```
187
+
188
+ ---
189
+
190
+ # Public Standalone Functions
191
+
192
+ ## loadScript
193
+
194
+ ### Description
195
+ Dynamically loads an external JavaScript file by appending a script element to the document head, ensuring the same script is not loaded multiple times. The return type of this call is a promise, making it ideal for checks where you need to make sure that the script was loaded successfully or unsuccessfully.
196
+
197
+ ### Parameters:
198
+
199
+ - #### src: string – URL of the script.
200
+ - #### attrs: ScriptParameters – Optional attributes to add to the script element.
201
+
202
+ ### Usage
203
+
204
+ ```typescript
205
+ loadScript('https://example.com/some-library.js', { async: 'true' })
206
+ .then(() => console.log('Script loaded successfully'))
207
+ .catch(error => console.error('Error loading script:', error));
208
+ ```
209
+
210
+ ---
211
+
212
+ ## throttle
213
+
214
+ ### Description
215
+ Creates a throttled version of a function, ensuring it is executed at most once every specified delay period.
216
+
217
+ ### Parameters:
218
+
219
+ - #### callback: Function – The function to throttle.
220
+ - #### delay: number – Delay in milliseconds (default is 250ms).
221
+
222
+ ### Usage
223
+
224
+ ```typescript
225
+ const throttledFunction = throttle(() => {
226
+ console.log('Throttled function executed');
227
+ }, 500);
228
+
229
+ window.addEventListener('resize', throttledFunction);
230
+ ```
231
+
232
+ ---
233
+
234
+ ## debounce
235
+
236
+ ### Description
237
+ Creates a debounced version of a function that delays its execution until a specified time has elapsed since the last invocation. Especially usefull when you know that there will be a lot of clicks on a button for example.
238
+
239
+ ### Parameters:
240
+
241
+ - #### callback: Function – The function to throttle.
242
+ - #### delay: number – Delay in milliseconds (default is 250ms).
243
+ - #### immediate: boolean – If true, triggers the function on the leading edge instead of the trailing edge.
244
+
245
+ ### Usage
246
+
247
+ ```typescript
248
+ const debouncedFunction = debounce(() => {
249
+ console.log('Debounced function executed');
250
+ }, 300);
251
+
252
+ document.querySelector('input')?.addEventListener('input', debouncedFunction);
253
+ ```
@@ -1,4 +1,4 @@
1
- import { UserInfoResult } from "./typeDefinitions.js";
1
+ import { UserInfoResult } from './typeDefinitions.js';
2
2
  export default abstract class User {
3
3
  private static _info;
4
4
  private static _fingerprint;
package/dist/src/user.js CHANGED
@@ -1,6 +1,6 @@
1
- import { Constants } from "./constants.js";
2
- import Cookies from "./cookies.js";
3
- import UserAgent from "./user-agent/index.js";
1
+ import { Constants } from './constants.js';
2
+ import Cookies from './cookies.js';
3
+ import UserAgent from './user-agent/index.js';
4
4
  export default class User {
5
5
  static _info = this.staticInitialise();
6
6
  static _fingerprint = this.getFingerprint();
@@ -35,13 +35,13 @@ export default class User {
35
35
  if (!fingerprintNotExist && UserAgent.isWindows) {
36
36
  try {
37
37
  const fingerprintReq = await fetch(`${Constants.WWW_ONLY_ORIGIN}/site/Main/dummyPost?${Math.random()}`, {
38
- method: "POST",
38
+ method: 'POST',
39
39
  headers: {
40
- "Content-type": "application/x-www-form-urlencoded",
41
- "Pragma": "no-cache",
42
- "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
43
- "Expires": "Tue, 01 Jan 1971 02:00:00 GMT",
44
- "BDUS_A312C09A2666456D9F2B2AA5D6B463D6": "check.bitdefender"
40
+ 'Content-type': 'application/x-www-form-urlencoded',
41
+ 'Pragma': 'no-cache',
42
+ 'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
43
+ 'Expires': 'Tue, 01 Jan 1971 02:00:00 GMT',
44
+ 'BDUS_A312C09A2666456D9F2B2AA5D6B463D6': 'check.bitdefender'
45
45
  }
46
46
  });
47
47
  if (fingerprintReq.ok && fingerprintReq.headers.has(Constants.FINGERPRINT_HEADER)) {
@@ -50,7 +50,7 @@ export default class User {
50
50
  return fingerprint;
51
51
  }
52
52
  else {
53
- Cookies.set(Constants.NO_FINGERPRINT_COOKIE_NAME, "true", { expires: 1 });
53
+ Cookies.set(Constants.NO_FINGERPRINT_COOKIE_NAME, 'true', { expires: 1 });
54
54
  }
55
55
  }
56
56
  catch { /* empty */ }
@@ -65,16 +65,16 @@ export default class User {
65
65
  try {
66
66
  const response = await fetch(`${Constants.PUBLIC_URL_ORIGIN}/geoip`);
67
67
  if (!response.ok) {
68
- return "us";
68
+ return 'us';
69
69
  }
70
70
  const country = await response.json();
71
71
  if (country.error_code) {
72
- return "us";
72
+ return 'us';
73
73
  }
74
- return country["country"].toLowerCase();
74
+ return country['country'].toLowerCase();
75
75
  }
76
76
  catch {
77
- return "us";
77
+ return 'us';
78
78
  }
79
79
  }
80
80
  /** Getting the User's Locale */
@@ -86,7 +86,7 @@ export default class User {
86
86
  return userGeoIpData[0].locale.toLowerCase();
87
87
  }
88
88
  catch {
89
- return "en-us";
89
+ return 'en-us';
90
90
  }
91
91
  }
92
92
  /** attempt to log the user in.
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@repobit/dex-utils",
3
- "version": "0.6.0",
3
+ "version": "1.1.0",
4
4
  "description": "Client for Utils",
5
5
  "author": "Constantin Ioan Mihai <iconstantin@bitdefender.com>",
6
6
  "homepage": "https://github.com/bitdefender/dex-core#readme",
7
7
  "license": "ISC",
8
8
  "main": "dist/src/index.js",
9
9
  "files": [
10
- "dist/src"
10
+ "dist/src",
11
+ "README.md",
12
+ "CHANGELOG.md",
13
+ "LICENSE"
11
14
  ],
12
15
  "publishConfig": {
13
16
  "access": "public"
@@ -35,5 +38,5 @@
35
38
  "devDependencies": {
36
39
  "@types/js-cookie": "^3.0.6"
37
40
  },
38
- "gitHead": "5391b27f1542572bf315961e3aecaa0b45b9b00d"
41
+ "gitHead": "561794201a63fcd69042da59b6bc642f03d0dcd3"
39
42
  }