@oxyhq/core 1.0.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.
Files changed (277) hide show
  1. package/README.md +50 -0
  2. package/dist/cjs/AuthManager.js +361 -0
  3. package/dist/cjs/CrossDomainAuth.js +258 -0
  4. package/dist/cjs/HttpService.js +618 -0
  5. package/dist/cjs/OxyServices.base.js +263 -0
  6. package/dist/cjs/OxyServices.errors.js +22 -0
  7. package/dist/cjs/OxyServices.js +63 -0
  8. package/dist/cjs/constants/version.js +16 -0
  9. package/dist/cjs/crypto/index.js +20 -0
  10. package/dist/cjs/crypto/keyManager.js +887 -0
  11. package/dist/cjs/crypto/polyfill.js +64 -0
  12. package/dist/cjs/crypto/recoveryPhrase.js +169 -0
  13. package/dist/cjs/crypto/signatureService.js +296 -0
  14. package/dist/cjs/i18n/index.js +73 -0
  15. package/dist/cjs/i18n/locales/ar-SA.json +120 -0
  16. package/dist/cjs/i18n/locales/ca-ES.json +120 -0
  17. package/dist/cjs/i18n/locales/de-DE.json +120 -0
  18. package/dist/cjs/i18n/locales/en-US.json +956 -0
  19. package/dist/cjs/i18n/locales/es-ES.json +944 -0
  20. package/dist/cjs/i18n/locales/fr-FR.json +120 -0
  21. package/dist/cjs/i18n/locales/it-IT.json +120 -0
  22. package/dist/cjs/i18n/locales/ja-JP.json +119 -0
  23. package/dist/cjs/i18n/locales/ko-KR.json +120 -0
  24. package/dist/cjs/i18n/locales/locales/ar-SA.json +120 -0
  25. package/dist/cjs/i18n/locales/locales/ca-ES.json +120 -0
  26. package/dist/cjs/i18n/locales/locales/de-DE.json +120 -0
  27. package/dist/cjs/i18n/locales/locales/en-US.json +956 -0
  28. package/dist/cjs/i18n/locales/locales/es-ES.json +944 -0
  29. package/dist/cjs/i18n/locales/locales/fr-FR.json +120 -0
  30. package/dist/cjs/i18n/locales/locales/it-IT.json +120 -0
  31. package/dist/cjs/i18n/locales/locales/ja-JP.json +119 -0
  32. package/dist/cjs/i18n/locales/locales/ko-KR.json +120 -0
  33. package/dist/cjs/i18n/locales/locales/pt-PT.json +120 -0
  34. package/dist/cjs/i18n/locales/locales/zh-CN.json +120 -0
  35. package/dist/cjs/i18n/locales/pt-PT.json +120 -0
  36. package/dist/cjs/i18n/locales/zh-CN.json +120 -0
  37. package/dist/cjs/index.js +153 -0
  38. package/dist/cjs/mixins/OxyServices.analytics.js +49 -0
  39. package/dist/cjs/mixins/OxyServices.assets.js +380 -0
  40. package/dist/cjs/mixins/OxyServices.auth.js +259 -0
  41. package/dist/cjs/mixins/OxyServices.developer.js +97 -0
  42. package/dist/cjs/mixins/OxyServices.devices.js +116 -0
  43. package/dist/cjs/mixins/OxyServices.features.js +309 -0
  44. package/dist/cjs/mixins/OxyServices.fedcm.js +435 -0
  45. package/dist/cjs/mixins/OxyServices.karma.js +108 -0
  46. package/dist/cjs/mixins/OxyServices.language.js +154 -0
  47. package/dist/cjs/mixins/OxyServices.location.js +43 -0
  48. package/dist/cjs/mixins/OxyServices.payment.js +158 -0
  49. package/dist/cjs/mixins/OxyServices.popup.js +371 -0
  50. package/dist/cjs/mixins/OxyServices.privacy.js +162 -0
  51. package/dist/cjs/mixins/OxyServices.redirect.js +345 -0
  52. package/dist/cjs/mixins/OxyServices.security.js +81 -0
  53. package/dist/cjs/mixins/OxyServices.user.js +355 -0
  54. package/dist/cjs/mixins/OxyServices.utility.js +156 -0
  55. package/dist/cjs/mixins/index.js +79 -0
  56. package/dist/cjs/mixins/mixinHelpers.js +53 -0
  57. package/dist/cjs/models/interfaces.js +20 -0
  58. package/dist/cjs/models/session.js +2 -0
  59. package/dist/cjs/shared/index.js +70 -0
  60. package/dist/cjs/shared/utils/colorUtils.js +153 -0
  61. package/dist/cjs/shared/utils/debugUtils.js +73 -0
  62. package/dist/cjs/shared/utils/errorUtils.js +183 -0
  63. package/dist/cjs/shared/utils/index.js +49 -0
  64. package/dist/cjs/shared/utils/networkUtils.js +183 -0
  65. package/dist/cjs/shared/utils/themeUtils.js +106 -0
  66. package/dist/cjs/utils/apiUtils.js +61 -0
  67. package/dist/cjs/utils/asyncUtils.js +194 -0
  68. package/dist/cjs/utils/cache.js +226 -0
  69. package/dist/cjs/utils/deviceManager.js +205 -0
  70. package/dist/cjs/utils/errorUtils.js +154 -0
  71. package/dist/cjs/utils/index.js +26 -0
  72. package/dist/cjs/utils/languageUtils.js +165 -0
  73. package/dist/cjs/utils/loggerUtils.js +126 -0
  74. package/dist/cjs/utils/platform.js +144 -0
  75. package/dist/cjs/utils/requestUtils.js +209 -0
  76. package/dist/cjs/utils/sessionUtils.js +181 -0
  77. package/dist/cjs/utils/validationUtils.js +173 -0
  78. package/dist/esm/AuthManager.js +356 -0
  79. package/dist/esm/CrossDomainAuth.js +253 -0
  80. package/dist/esm/HttpService.js +614 -0
  81. package/dist/esm/OxyServices.base.js +259 -0
  82. package/dist/esm/OxyServices.errors.js +17 -0
  83. package/dist/esm/OxyServices.js +59 -0
  84. package/dist/esm/constants/version.js +13 -0
  85. package/dist/esm/crypto/index.js +13 -0
  86. package/dist/esm/crypto/keyManager.js +850 -0
  87. package/dist/esm/crypto/polyfill.js +61 -0
  88. package/dist/esm/crypto/recoveryPhrase.js +132 -0
  89. package/dist/esm/crypto/signatureService.js +259 -0
  90. package/dist/esm/i18n/index.js +69 -0
  91. package/dist/esm/i18n/locales/ar-SA.json +120 -0
  92. package/dist/esm/i18n/locales/ca-ES.json +120 -0
  93. package/dist/esm/i18n/locales/de-DE.json +120 -0
  94. package/dist/esm/i18n/locales/en-US.json +956 -0
  95. package/dist/esm/i18n/locales/es-ES.json +944 -0
  96. package/dist/esm/i18n/locales/fr-FR.json +120 -0
  97. package/dist/esm/i18n/locales/it-IT.json +120 -0
  98. package/dist/esm/i18n/locales/ja-JP.json +119 -0
  99. package/dist/esm/i18n/locales/ko-KR.json +120 -0
  100. package/dist/esm/i18n/locales/locales/ar-SA.json +120 -0
  101. package/dist/esm/i18n/locales/locales/ca-ES.json +120 -0
  102. package/dist/esm/i18n/locales/locales/de-DE.json +120 -0
  103. package/dist/esm/i18n/locales/locales/en-US.json +956 -0
  104. package/dist/esm/i18n/locales/locales/es-ES.json +944 -0
  105. package/dist/esm/i18n/locales/locales/fr-FR.json +120 -0
  106. package/dist/esm/i18n/locales/locales/it-IT.json +120 -0
  107. package/dist/esm/i18n/locales/locales/ja-JP.json +119 -0
  108. package/dist/esm/i18n/locales/locales/ko-KR.json +120 -0
  109. package/dist/esm/i18n/locales/locales/pt-PT.json +120 -0
  110. package/dist/esm/i18n/locales/locales/zh-CN.json +120 -0
  111. package/dist/esm/i18n/locales/pt-PT.json +120 -0
  112. package/dist/esm/i18n/locales/zh-CN.json +120 -0
  113. package/dist/esm/index.js +55 -0
  114. package/dist/esm/mixins/OxyServices.analytics.js +46 -0
  115. package/dist/esm/mixins/OxyServices.assets.js +377 -0
  116. package/dist/esm/mixins/OxyServices.auth.js +256 -0
  117. package/dist/esm/mixins/OxyServices.developer.js +94 -0
  118. package/dist/esm/mixins/OxyServices.devices.js +113 -0
  119. package/dist/esm/mixins/OxyServices.features.js +306 -0
  120. package/dist/esm/mixins/OxyServices.fedcm.js +433 -0
  121. package/dist/esm/mixins/OxyServices.karma.js +105 -0
  122. package/dist/esm/mixins/OxyServices.language.js +118 -0
  123. package/dist/esm/mixins/OxyServices.location.js +40 -0
  124. package/dist/esm/mixins/OxyServices.payment.js +155 -0
  125. package/dist/esm/mixins/OxyServices.popup.js +369 -0
  126. package/dist/esm/mixins/OxyServices.privacy.js +159 -0
  127. package/dist/esm/mixins/OxyServices.redirect.js +343 -0
  128. package/dist/esm/mixins/OxyServices.security.js +78 -0
  129. package/dist/esm/mixins/OxyServices.user.js +352 -0
  130. package/dist/esm/mixins/OxyServices.utility.js +153 -0
  131. package/dist/esm/mixins/index.js +76 -0
  132. package/dist/esm/mixins/mixinHelpers.js +48 -0
  133. package/dist/esm/models/interfaces.js +17 -0
  134. package/dist/esm/models/session.js +1 -0
  135. package/dist/esm/shared/index.js +31 -0
  136. package/dist/esm/shared/utils/colorUtils.js +143 -0
  137. package/dist/esm/shared/utils/debugUtils.js +65 -0
  138. package/dist/esm/shared/utils/errorUtils.js +170 -0
  139. package/dist/esm/shared/utils/index.js +15 -0
  140. package/dist/esm/shared/utils/networkUtils.js +173 -0
  141. package/dist/esm/shared/utils/themeUtils.js +98 -0
  142. package/dist/esm/utils/apiUtils.js +55 -0
  143. package/dist/esm/utils/asyncUtils.js +179 -0
  144. package/dist/esm/utils/cache.js +218 -0
  145. package/dist/esm/utils/deviceManager.js +168 -0
  146. package/dist/esm/utils/errorUtils.js +146 -0
  147. package/dist/esm/utils/index.js +7 -0
  148. package/dist/esm/utils/languageUtils.js +158 -0
  149. package/dist/esm/utils/loggerUtils.js +115 -0
  150. package/dist/esm/utils/platform.js +102 -0
  151. package/dist/esm/utils/requestUtils.js +203 -0
  152. package/dist/esm/utils/sessionUtils.js +171 -0
  153. package/dist/esm/utils/validationUtils.js +153 -0
  154. package/dist/types/AuthManager.d.ts +143 -0
  155. package/dist/types/CrossDomainAuth.d.ts +160 -0
  156. package/dist/types/HttpService.d.ts +163 -0
  157. package/dist/types/OxyServices.base.d.ts +126 -0
  158. package/dist/types/OxyServices.d.ts +81 -0
  159. package/dist/types/OxyServices.errors.d.ts +11 -0
  160. package/dist/types/constants/version.d.ts +13 -0
  161. package/dist/types/crypto/index.d.ts +11 -0
  162. package/dist/types/crypto/keyManager.d.ts +189 -0
  163. package/dist/types/crypto/polyfill.d.ts +11 -0
  164. package/dist/types/crypto/recoveryPhrase.d.ts +58 -0
  165. package/dist/types/crypto/signatureService.d.ts +86 -0
  166. package/dist/types/i18n/index.d.ts +3 -0
  167. package/dist/types/index.d.ts +50 -0
  168. package/dist/types/mixins/OxyServices.analytics.d.ts +66 -0
  169. package/dist/types/mixins/OxyServices.assets.d.ts +135 -0
  170. package/dist/types/mixins/OxyServices.auth.d.ts +186 -0
  171. package/dist/types/mixins/OxyServices.developer.d.ts +99 -0
  172. package/dist/types/mixins/OxyServices.devices.d.ts +96 -0
  173. package/dist/types/mixins/OxyServices.features.d.ts +228 -0
  174. package/dist/types/mixins/OxyServices.fedcm.d.ts +200 -0
  175. package/dist/types/mixins/OxyServices.karma.d.ts +85 -0
  176. package/dist/types/mixins/OxyServices.language.d.ts +81 -0
  177. package/dist/types/mixins/OxyServices.location.d.ts +64 -0
  178. package/dist/types/mixins/OxyServices.payment.d.ts +111 -0
  179. package/dist/types/mixins/OxyServices.popup.d.ts +205 -0
  180. package/dist/types/mixins/OxyServices.privacy.d.ts +122 -0
  181. package/dist/types/mixins/OxyServices.redirect.d.ts +245 -0
  182. package/dist/types/mixins/OxyServices.security.d.ts +78 -0
  183. package/dist/types/mixins/OxyServices.user.d.ts +182 -0
  184. package/dist/types/mixins/OxyServices.utility.d.ts +93 -0
  185. package/dist/types/mixins/index.d.ts +30 -0
  186. package/dist/types/mixins/mixinHelpers.d.ts +31 -0
  187. package/dist/types/models/interfaces.d.ts +415 -0
  188. package/dist/types/models/session.d.ts +27 -0
  189. package/dist/types/shared/index.d.ts +28 -0
  190. package/dist/types/shared/utils/colorUtils.d.ts +104 -0
  191. package/dist/types/shared/utils/debugUtils.d.ts +48 -0
  192. package/dist/types/shared/utils/errorUtils.d.ts +97 -0
  193. package/dist/types/shared/utils/index.d.ts +13 -0
  194. package/dist/types/shared/utils/networkUtils.d.ts +139 -0
  195. package/dist/types/shared/utils/themeUtils.d.ts +90 -0
  196. package/dist/types/utils/apiUtils.d.ts +53 -0
  197. package/dist/types/utils/asyncUtils.d.ts +58 -0
  198. package/dist/types/utils/cache.d.ts +127 -0
  199. package/dist/types/utils/deviceManager.d.ts +65 -0
  200. package/dist/types/utils/errorUtils.d.ts +46 -0
  201. package/dist/types/utils/index.d.ts +6 -0
  202. package/dist/types/utils/languageUtils.d.ts +37 -0
  203. package/dist/types/utils/loggerUtils.d.ts +48 -0
  204. package/dist/types/utils/platform.d.ts +40 -0
  205. package/dist/types/utils/requestUtils.d.ts +123 -0
  206. package/dist/types/utils/sessionUtils.d.ts +54 -0
  207. package/dist/types/utils/validationUtils.d.ts +85 -0
  208. package/package.json +84 -0
  209. package/src/AuthManager.ts +436 -0
  210. package/src/CrossDomainAuth.ts +307 -0
  211. package/src/HttpService.ts +752 -0
  212. package/src/OxyServices.base.ts +334 -0
  213. package/src/OxyServices.errors.ts +26 -0
  214. package/src/OxyServices.ts +129 -0
  215. package/src/constants/version.ts +15 -0
  216. package/src/crypto/index.ts +25 -0
  217. package/src/crypto/keyManager.ts +962 -0
  218. package/src/crypto/polyfill.ts +70 -0
  219. package/src/crypto/recoveryPhrase.ts +166 -0
  220. package/src/crypto/signatureService.ts +323 -0
  221. package/src/i18n/index.ts +75 -0
  222. package/src/i18n/locales/ar-SA.json +120 -0
  223. package/src/i18n/locales/ca-ES.json +120 -0
  224. package/src/i18n/locales/de-DE.json +120 -0
  225. package/src/i18n/locales/en-US.json +956 -0
  226. package/src/i18n/locales/es-ES.json +944 -0
  227. package/src/i18n/locales/fr-FR.json +120 -0
  228. package/src/i18n/locales/it-IT.json +120 -0
  229. package/src/i18n/locales/ja-JP.json +119 -0
  230. package/src/i18n/locales/ko-KR.json +120 -0
  231. package/src/i18n/locales/pt-PT.json +120 -0
  232. package/src/i18n/locales/zh-CN.json +120 -0
  233. package/src/index.ts +153 -0
  234. package/src/mixins/OxyServices.analytics.ts +53 -0
  235. package/src/mixins/OxyServices.assets.ts +412 -0
  236. package/src/mixins/OxyServices.auth.ts +358 -0
  237. package/src/mixins/OxyServices.developer.ts +114 -0
  238. package/src/mixins/OxyServices.devices.ts +119 -0
  239. package/src/mixins/OxyServices.features.ts +428 -0
  240. package/src/mixins/OxyServices.fedcm.ts +494 -0
  241. package/src/mixins/OxyServices.karma.ts +111 -0
  242. package/src/mixins/OxyServices.language.ts +127 -0
  243. package/src/mixins/OxyServices.location.ts +46 -0
  244. package/src/mixins/OxyServices.payment.ts +163 -0
  245. package/src/mixins/OxyServices.popup.ts +443 -0
  246. package/src/mixins/OxyServices.privacy.ts +182 -0
  247. package/src/mixins/OxyServices.redirect.ts +397 -0
  248. package/src/mixins/OxyServices.security.ts +103 -0
  249. package/src/mixins/OxyServices.user.ts +392 -0
  250. package/src/mixins/OxyServices.utility.ts +191 -0
  251. package/src/mixins/index.ts +91 -0
  252. package/src/mixins/mixinHelpers.ts +69 -0
  253. package/src/models/interfaces.ts +511 -0
  254. package/src/models/session.ts +30 -0
  255. package/src/shared/index.ts +82 -0
  256. package/src/shared/utils/colorUtils.ts +155 -0
  257. package/src/shared/utils/debugUtils.ts +73 -0
  258. package/src/shared/utils/errorUtils.ts +181 -0
  259. package/src/shared/utils/index.ts +59 -0
  260. package/src/shared/utils/networkUtils.ts +248 -0
  261. package/src/shared/utils/themeUtils.ts +115 -0
  262. package/src/types/bip39.d.ts +32 -0
  263. package/src/types/buffer.d.ts +97 -0
  264. package/src/types/color.d.ts +20 -0
  265. package/src/types/elliptic.d.ts +62 -0
  266. package/src/utils/apiUtils.ts +88 -0
  267. package/src/utils/asyncUtils.ts +252 -0
  268. package/src/utils/cache.ts +264 -0
  269. package/src/utils/deviceManager.ts +198 -0
  270. package/src/utils/errorUtils.ts +216 -0
  271. package/src/utils/index.ts +21 -0
  272. package/src/utils/languageUtils.ts +174 -0
  273. package/src/utils/loggerUtils.ts +153 -0
  274. package/src/utils/platform.ts +117 -0
  275. package/src/utils/requestUtils.ts +237 -0
  276. package/src/utils/sessionUtils.ts +206 -0
  277. package/src/utils/validationUtils.ts +174 -0
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Color Utility Functions
3
+ *
4
+ * Consolidated color manipulation utilities used across the OxyServices ecosystem.
5
+ * These functions work in any JavaScript environment (browser, Node.js, React Native).
6
+ *
7
+ * @module shared/utils/colorUtils
8
+ */
9
+ /**
10
+ * Darkens a hex color by a specified factor.
11
+ *
12
+ * @param color - Hex color string (with or without #)
13
+ * @param factor - Amount to darken (0-1). Default: 0.6
14
+ * @returns Darkened hex color string with # prefix
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * darkenColor('#FF0000', 0.5); // Returns a darker red
19
+ * darkenColor('FF0000', 0.3); // Also works without #
20
+ * ```
21
+ */
22
+ export const darkenColor = (color, factor = 0.6) => {
23
+ const hex = color.replace('#', '');
24
+ const r = parseInt(hex.substring(0, 2), 16);
25
+ const g = parseInt(hex.substring(2, 4), 16);
26
+ const b = parseInt(hex.substring(4, 6), 16);
27
+ const newR = Math.max(0, Math.round(r * (1 - factor)));
28
+ const newG = Math.max(0, Math.round(g * (1 - factor)));
29
+ const newB = Math.max(0, Math.round(b * (1 - factor)));
30
+ return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
31
+ };
32
+ /**
33
+ * Lightens a hex color by a specified factor.
34
+ *
35
+ * @param color - Hex color string (with or without #)
36
+ * @param factor - Amount to lighten (0-1). Default: 0.3
37
+ * @returns Lightened hex color string with # prefix
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * lightenColor('#0000FF', 0.5); // Returns a lighter blue
42
+ * ```
43
+ */
44
+ export const lightenColor = (color, factor = 0.3) => {
45
+ const hex = color.replace('#', '');
46
+ const r = parseInt(hex.substring(0, 2), 16);
47
+ const g = parseInt(hex.substring(2, 4), 16);
48
+ const b = parseInt(hex.substring(4, 6), 16);
49
+ const newR = Math.min(255, Math.round(r + (255 - r) * factor));
50
+ const newG = Math.min(255, Math.round(g + (255 - g) * factor));
51
+ const newB = Math.min(255, Math.round(b + (255 - b) * factor));
52
+ return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
53
+ };
54
+ /**
55
+ * Converts a hex color to RGB values.
56
+ *
57
+ * @param hex - Hex color string (with or without #)
58
+ * @returns Object with r, g, b values (0-255)
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * hexToRgb('#FF5733'); // { r: 255, g: 87, b: 51 }
63
+ * ```
64
+ */
65
+ export const hexToRgb = (hex) => {
66
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
67
+ return result
68
+ ? {
69
+ r: parseInt(result[1], 16),
70
+ g: parseInt(result[2], 16),
71
+ b: parseInt(result[3], 16),
72
+ }
73
+ : null;
74
+ };
75
+ /**
76
+ * Converts RGB values to a hex color string.
77
+ *
78
+ * @param r - Red value (0-255)
79
+ * @param g - Green value (0-255)
80
+ * @param b - Blue value (0-255)
81
+ * @returns Hex color string with # prefix
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * rgbToHex(255, 87, 51); // '#ff5733'
86
+ * ```
87
+ */
88
+ export const rgbToHex = (r, g, b) => {
89
+ return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
90
+ };
91
+ /**
92
+ * Adjusts the opacity of a hex color, returning an rgba string.
93
+ *
94
+ * @param hex - Hex color string
95
+ * @param opacity - Opacity value (0-1)
96
+ * @returns RGBA color string
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * withOpacity('#FF0000', 0.5); // 'rgba(255, 0, 0, 0.5)'
101
+ * ```
102
+ */
103
+ export const withOpacity = (hex, opacity) => {
104
+ const rgb = hexToRgb(hex);
105
+ if (!rgb)
106
+ return hex;
107
+ return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${opacity})`;
108
+ };
109
+ /**
110
+ * Checks if a color is considered "light" (for determining text contrast).
111
+ *
112
+ * @param hex - Hex color string
113
+ * @returns true if the color is light, false if dark
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * isLightColor('#FFFFFF'); // true
118
+ * isLightColor('#000000'); // false
119
+ * ```
120
+ */
121
+ export const isLightColor = (hex) => {
122
+ const rgb = hexToRgb(hex);
123
+ if (!rgb)
124
+ return true;
125
+ // Using relative luminance formula
126
+ const luminance = (0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b) / 255;
127
+ return luminance > 0.5;
128
+ };
129
+ /**
130
+ * Gets a contrasting text color (black or white) for a given background color.
131
+ *
132
+ * @param backgroundColor - Hex color string of the background
133
+ * @returns '#000000' for light backgrounds, '#ffffff' for dark backgrounds
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * getContrastTextColor('#FFFF00'); // '#000000' (black text on yellow)
138
+ * getContrastTextColor('#000080'); // '#ffffff' (white text on navy)
139
+ * ```
140
+ */
141
+ export const getContrastTextColor = (backgroundColor) => {
142
+ return isLightColor(backgroundColor) ? '#000000' : '#ffffff';
143
+ };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Debug Utilities
3
+ *
4
+ * Provides safe logging functions that only output in development mode.
5
+ * All logs are stripped in production builds.
6
+ *
7
+ * @module shared/utils/debugUtils
8
+ */
9
+ /**
10
+ * Check if running in development mode
11
+ */
12
+ export const isDev = () => {
13
+ return typeof __DEV__ !== 'undefined' && __DEV__;
14
+ };
15
+ /**
16
+ * Log a debug message (only in development)
17
+ * @param prefix - Log prefix (e.g., '[FedCM]')
18
+ * @param args - Arguments to log
19
+ */
20
+ export const debugLog = (prefix, ...args) => {
21
+ if (isDev()) {
22
+ console.log(prefix, ...args);
23
+ }
24
+ };
25
+ /**
26
+ * Log a debug warning (only in development)
27
+ * @param prefix - Log prefix
28
+ * @param args - Arguments to log
29
+ */
30
+ export const debugWarn = (prefix, ...args) => {
31
+ if (isDev()) {
32
+ console.warn(prefix, ...args);
33
+ }
34
+ };
35
+ /**
36
+ * Log a debug error (only in development)
37
+ * @param prefix - Log prefix
38
+ * @param args - Arguments to log
39
+ */
40
+ export const debugError = (prefix, ...args) => {
41
+ if (isDev()) {
42
+ console.error(prefix, ...args);
43
+ }
44
+ };
45
+ /**
46
+ * Create a namespaced debug logger
47
+ * @param namespace - Logger namespace (e.g., 'FedCM', 'PopupAuth')
48
+ * @returns Object with log, warn, error methods
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const debug = createDebugLogger('FedCM');
53
+ * debug.log('Starting authentication');
54
+ * debug.warn('Token expires soon');
55
+ * debug.error('Authentication failed', error);
56
+ * ```
57
+ */
58
+ export const createDebugLogger = (namespace) => {
59
+ const prefix = `[${namespace}]`;
60
+ return {
61
+ log: (...args) => debugLog(prefix, ...args),
62
+ warn: (...args) => debugWarn(prefix, ...args),
63
+ error: (...args) => debugError(prefix, ...args),
64
+ };
65
+ };
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Error Utility Functions
3
+ *
4
+ * Consolidated error handling utilities for the OxyServices ecosystem.
5
+ * These functions help with common error patterns like checking HTTP status codes.
6
+ *
7
+ * @module shared/utils/errorUtils
8
+ */
9
+ /**
10
+ * Common HTTP status codes used in error checking.
11
+ */
12
+ export const HttpStatus = {
13
+ BAD_REQUEST: 400,
14
+ UNAUTHORIZED: 401,
15
+ FORBIDDEN: 403,
16
+ NOT_FOUND: 404,
17
+ CONFLICT: 409,
18
+ UNPROCESSABLE_ENTITY: 422,
19
+ TOO_MANY_REQUESTS: 429,
20
+ INTERNAL_SERVER_ERROR: 500,
21
+ BAD_GATEWAY: 502,
22
+ SERVICE_UNAVAILABLE: 503,
23
+ GATEWAY_TIMEOUT: 504,
24
+ };
25
+ /**
26
+ * Extracts the HTTP status code from an error object.
27
+ *
28
+ * @param error - Any error object
29
+ * @returns The status code if found, undefined otherwise
30
+ */
31
+ export const getErrorStatus = (error) => {
32
+ if (!error || typeof error !== 'object')
33
+ return undefined;
34
+ const err = error;
35
+ // Direct status property
36
+ if (typeof err.status === 'number')
37
+ return err.status;
38
+ // Axios-style response.status
39
+ if (err.response && typeof err.response === 'object') {
40
+ const response = err.response;
41
+ if (typeof response.status === 'number')
42
+ return response.status;
43
+ }
44
+ // statusCode property (some libraries use this)
45
+ if (typeof err.statusCode === 'number')
46
+ return err.statusCode;
47
+ return undefined;
48
+ };
49
+ /**
50
+ * Extracts the error message from an error object.
51
+ *
52
+ * @param error - Any error object
53
+ * @param fallback - Fallback message if none found
54
+ * @returns The error message
55
+ */
56
+ export const getErrorMessage = (error, fallback = 'An unknown error occurred') => {
57
+ if (!error)
58
+ return fallback;
59
+ if (typeof error === 'string')
60
+ return error;
61
+ if (error instanceof Error)
62
+ return error.message;
63
+ if (typeof error === 'object') {
64
+ const err = error;
65
+ if (typeof err.message === 'string')
66
+ return err.message;
67
+ if (typeof err.error === 'string')
68
+ return err.error;
69
+ // Axios-style response.data.message
70
+ if (err.response && typeof err.response === 'object') {
71
+ const response = err.response;
72
+ if (response.data && typeof response.data === 'object') {
73
+ const data = response.data;
74
+ if (typeof data.message === 'string')
75
+ return data.message;
76
+ if (typeof data.error === 'string')
77
+ return data.error;
78
+ }
79
+ }
80
+ }
81
+ return fallback;
82
+ };
83
+ /**
84
+ * Check if an error indicates the user is already registered (HTTP 409 Conflict).
85
+ *
86
+ * The backend should always return HTTP 409 for duplicate registrations.
87
+ *
88
+ * @param error - Any error object
89
+ * @returns true if the error is a 409 Conflict
90
+ */
91
+ export const isAlreadyRegisteredError = (error) => {
92
+ return getErrorStatus(error) === HttpStatus.CONFLICT;
93
+ };
94
+ /**
95
+ * Check if an error is an authentication error (HTTP 401).
96
+ *
97
+ * @param error - Any error object
98
+ * @returns true if the error is a 401 Unauthorized
99
+ */
100
+ export const isUnauthorizedError = (error) => {
101
+ return getErrorStatus(error) === HttpStatus.UNAUTHORIZED;
102
+ };
103
+ /**
104
+ * Check if an error is a forbidden error (HTTP 403).
105
+ *
106
+ * @param error - Any error object
107
+ * @returns true if the error is a 403 Forbidden
108
+ */
109
+ export const isForbiddenError = (error) => {
110
+ return getErrorStatus(error) === HttpStatus.FORBIDDEN;
111
+ };
112
+ /**
113
+ * Check if an error is a not found error (HTTP 404).
114
+ *
115
+ * @param error - Any error object
116
+ * @returns true if the error is a 404 Not Found
117
+ */
118
+ export const isNotFoundError = (error) => {
119
+ return getErrorStatus(error) === HttpStatus.NOT_FOUND;
120
+ };
121
+ /**
122
+ * Check if an error is a rate limit error (HTTP 429).
123
+ *
124
+ * @param error - Any error object
125
+ * @returns true if the error is a 429 Too Many Requests
126
+ */
127
+ export const isRateLimitError = (error) => {
128
+ return getErrorStatus(error) === HttpStatus.TOO_MANY_REQUESTS;
129
+ };
130
+ /**
131
+ * Check if an error is a server error (HTTP 5xx).
132
+ *
133
+ * @param error - Any error object
134
+ * @returns true if the error is a 5xx server error
135
+ */
136
+ export const isServerError = (error) => {
137
+ const status = getErrorStatus(error);
138
+ return status !== undefined && status >= 500 && status < 600;
139
+ };
140
+ /**
141
+ * Check if an error is a network error (no response received).
142
+ *
143
+ * @param error - Any error object
144
+ * @returns true if the error appears to be a network error
145
+ */
146
+ export const isNetworkError = (error) => {
147
+ if (!error || typeof error !== 'object')
148
+ return false;
149
+ const err = error;
150
+ // Check for common network error indicators
151
+ if (err.name === 'NetworkError')
152
+ return true;
153
+ if (err.code === 'ENOTFOUND' || err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT')
154
+ return true;
155
+ // Axios-style: has request but no response
156
+ if (err.request && !err.response)
157
+ return true;
158
+ // Message-based detection
159
+ const message = getErrorMessage(error, '').toLowerCase();
160
+ return message.includes('network') || message.includes('connection') || message.includes('timeout');
161
+ };
162
+ /**
163
+ * Check if an error is retryable (network errors or 5xx server errors).
164
+ *
165
+ * @param error - Any error object
166
+ * @returns true if the request should be retried
167
+ */
168
+ export const isRetryableError = (error) => {
169
+ return isNetworkError(error) || isServerError(error) || isRateLimitError(error);
170
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Shared Utility Functions
3
+ *
4
+ * Re-exports all shared utilities for convenient importing.
5
+ *
6
+ * @module shared/utils
7
+ */
8
+ // Color utilities
9
+ export { darkenColor, lightenColor, hexToRgb, rgbToHex, withOpacity, isLightColor, getContrastTextColor, } from './colorUtils';
10
+ // Theme utilities
11
+ export { normalizeTheme, normalizeColorScheme, getOppositeTheme, systemPrefersDarkMode, getSystemColorScheme, } from './themeUtils';
12
+ // Error utilities
13
+ export { HttpStatus, getErrorStatus, getErrorMessage, isAlreadyRegisteredError, isUnauthorizedError, isForbiddenError, isNotFoundError, isRateLimitError, isServerError, isNetworkError, isRetryableError, } from './errorUtils';
14
+ // Network utilities
15
+ export { DEFAULT_CIRCUIT_BREAKER_CONFIG, createCircuitBreakerState, calculateBackoffInterval, recordFailure, recordSuccess, shouldAllowRequest, delay, withRetry, } from './networkUtils';
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Network Utility Functions
3
+ *
4
+ * Consolidated network utilities including circuit breaker pattern
5
+ * and exponential backoff for resilient API calls.
6
+ *
7
+ * @module shared/utils/networkUtils
8
+ */
9
+ /**
10
+ * Default circuit breaker configuration.
11
+ */
12
+ export const DEFAULT_CIRCUIT_BREAKER_CONFIG = {
13
+ baseInterval: 10000,
14
+ maxInterval: 60000,
15
+ maxFailures: 5,
16
+ recoveryTimeout: 30000,
17
+ };
18
+ /**
19
+ * Creates initial circuit breaker state.
20
+ *
21
+ * @param config - Optional custom configuration
22
+ * @returns Initial circuit breaker state
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const state = createCircuitBreakerState({ maxFailures: 3 });
27
+ * ```
28
+ */
29
+ export const createCircuitBreakerState = (config = {}) => {
30
+ const { baseInterval, maxInterval, maxFailures } = {
31
+ ...DEFAULT_CIRCUIT_BREAKER_CONFIG,
32
+ ...config,
33
+ };
34
+ return {
35
+ consecutiveFailures: 0,
36
+ currentInterval: baseInterval,
37
+ baseInterval,
38
+ maxInterval,
39
+ maxFailures,
40
+ isOpen: false,
41
+ };
42
+ };
43
+ /**
44
+ * Calculates next interval using exponential backoff.
45
+ *
46
+ * @param state - Current circuit breaker state
47
+ * @returns Next interval in milliseconds
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const nextInterval = calculateBackoffInterval(state);
52
+ * await delay(nextInterval);
53
+ * ```
54
+ */
55
+ export const calculateBackoffInterval = (state) => {
56
+ const { consecutiveFailures, baseInterval, maxInterval } = state;
57
+ if (consecutiveFailures === 0)
58
+ return baseInterval;
59
+ const backoffMultiplier = Math.min(Math.pow(2, consecutiveFailures - 1), maxInterval / baseInterval);
60
+ return Math.min(baseInterval * backoffMultiplier, maxInterval);
61
+ };
62
+ /**
63
+ * Records a failure and updates circuit breaker state.
64
+ *
65
+ * @param state - Current circuit breaker state
66
+ * @returns Updated state after recording failure
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * try {
71
+ * await apiCall();
72
+ * state = recordSuccess(state);
73
+ * } catch (error) {
74
+ * state = recordFailure(state);
75
+ * }
76
+ * ```
77
+ */
78
+ export const recordFailure = (state) => {
79
+ const newFailures = state.consecutiveFailures + 1;
80
+ const newInterval = calculateBackoffInterval({
81
+ ...state,
82
+ consecutiveFailures: newFailures,
83
+ });
84
+ const shouldOpenCircuit = newFailures >= state.maxFailures;
85
+ const finalInterval = shouldOpenCircuit ? state.maxInterval : newInterval;
86
+ return {
87
+ ...state,
88
+ consecutiveFailures: newFailures,
89
+ currentInterval: finalInterval,
90
+ isOpen: shouldOpenCircuit,
91
+ openedAt: shouldOpenCircuit ? Date.now() : state.openedAt,
92
+ };
93
+ };
94
+ /**
95
+ * Records a success and resets circuit breaker state.
96
+ *
97
+ * @param state - Current circuit breaker state
98
+ * @returns Reset state after successful request
99
+ */
100
+ export const recordSuccess = (state) => {
101
+ return {
102
+ ...state,
103
+ consecutiveFailures: 0,
104
+ currentInterval: state.baseInterval,
105
+ isOpen: false,
106
+ openedAt: undefined,
107
+ };
108
+ };
109
+ /**
110
+ * Checks if the circuit breaker should allow a request.
111
+ *
112
+ * When the circuit is open, it will only allow requests after
113
+ * the recovery timeout has passed (half-open state).
114
+ *
115
+ * @param state - Current circuit breaker state
116
+ * @param recoveryTimeout - Time to wait before allowing recovery attempts
117
+ * @returns true if request should be allowed
118
+ */
119
+ export const shouldAllowRequest = (state, recoveryTimeout = DEFAULT_CIRCUIT_BREAKER_CONFIG.recoveryTimeout) => {
120
+ if (!state.isOpen)
121
+ return true;
122
+ if (!state.openedAt)
123
+ return true;
124
+ const timeSinceOpen = Date.now() - state.openedAt;
125
+ return timeSinceOpen >= recoveryTimeout;
126
+ };
127
+ /**
128
+ * Delays execution for a specified duration.
129
+ *
130
+ * @param ms - Milliseconds to delay
131
+ * @returns Promise that resolves after the delay
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * await delay(1000); // Wait 1 second
136
+ * ```
137
+ */
138
+ export const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
139
+ /**
140
+ * Executes a function with exponential backoff retry.
141
+ *
142
+ * @param fn - Async function to execute
143
+ * @param options - Retry options
144
+ * @returns Result of the function
145
+ * @throws Last error if all retries fail
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const result = await withRetry(
150
+ * () => fetchData(),
151
+ * { maxRetries: 3, baseDelay: 1000 }
152
+ * );
153
+ * ```
154
+ */
155
+ export const withRetry = async (fn, options = {}) => {
156
+ const { maxRetries = 3, baseDelay = 1000, maxDelay = 30000, shouldRetry = () => true, onRetry, } = options;
157
+ let lastError;
158
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
159
+ try {
160
+ return await fn();
161
+ }
162
+ catch (error) {
163
+ lastError = error;
164
+ if (attempt === maxRetries || !shouldRetry(error)) {
165
+ throw error;
166
+ }
167
+ const delayMs = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
168
+ onRetry?.(error, attempt + 1);
169
+ await delay(delayMs);
170
+ }
171
+ }
172
+ throw lastError;
173
+ };
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Theme Utility Functions
3
+ *
4
+ * Consolidated theme utilities for normalizing and handling color schemes
5
+ * across the OxyServices ecosystem.
6
+ *
7
+ * @module shared/utils/themeUtils
8
+ */
9
+ /**
10
+ * Normalizes a theme value to ensure it's always 'light' or 'dark'.
11
+ *
12
+ * @param theme - Theme value (may be 'light' | 'dark' | string | undefined)
13
+ * @returns Normalized 'light' | 'dark' theme (defaults to 'light')
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * normalizeTheme('dark'); // 'dark'
18
+ * normalizeTheme('light'); // 'light'
19
+ * normalizeTheme('unknown'); // 'light'
20
+ * normalizeTheme(undefined); // 'light'
21
+ * normalizeTheme(null); // 'light'
22
+ * ```
23
+ */
24
+ export const normalizeTheme = (theme) => theme === 'light' || theme === 'dark' ? theme : 'light';
25
+ /**
26
+ * Normalizes a color scheme value with optional fallback chain.
27
+ *
28
+ * Handles null/undefined cases from React Native's useColorScheme() hook
29
+ * with a proper fallback chain.
30
+ *
31
+ * @param colorScheme - The color scheme from useColorScheme() hook (may be null/undefined)
32
+ * @param theme - Optional theme prop as fallback
33
+ * @returns Normalized 'light' | 'dark' color scheme
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * // In a React Native component:
38
+ * const systemScheme = useColorScheme(); // might be null
39
+ * const colorScheme = normalizeColorScheme(systemScheme, props.theme);
40
+ *
41
+ * normalizeColorScheme('dark', 'light'); // 'dark'
42
+ * normalizeColorScheme(null, 'dark'); // 'dark'
43
+ * normalizeColorScheme(undefined, undefined); // 'light'
44
+ * ```
45
+ */
46
+ export const normalizeColorScheme = (colorScheme, theme) => {
47
+ if (colorScheme === 'light' || colorScheme === 'dark') {
48
+ return colorScheme;
49
+ }
50
+ if (theme === 'light' || theme === 'dark') {
51
+ return theme;
52
+ }
53
+ return 'light';
54
+ };
55
+ /**
56
+ * Gets the opposite theme value.
57
+ *
58
+ * @param theme - Current theme value
59
+ * @returns The opposite theme ('light' → 'dark', 'dark' → 'light')
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * getOppositeTheme('light'); // 'dark'
64
+ * getOppositeTheme('dark'); // 'light'
65
+ * ```
66
+ */
67
+ export const getOppositeTheme = (theme) => theme === 'light' ? 'dark' : 'light';
68
+ /**
69
+ * Checks if the system prefers dark mode.
70
+ *
71
+ * This function only works in browser environments.
72
+ * Returns false in non-browser environments (Node.js, React Native).
73
+ *
74
+ * @returns true if system prefers dark mode, false otherwise
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * if (systemPrefersDarkMode()) {
79
+ * setTheme('dark');
80
+ * }
81
+ * ```
82
+ */
83
+ export const systemPrefersDarkMode = () => {
84
+ if (typeof window === 'undefined')
85
+ return false;
86
+ return window.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false;
87
+ };
88
+ /**
89
+ * Gets the system's preferred color scheme.
90
+ *
91
+ * @returns 'dark' if system prefers dark mode, 'light' otherwise
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const theme = getSystemColorScheme();
96
+ * ```
97
+ */
98
+ export const getSystemColorScheme = () => systemPrefersDarkMode() ? 'dark' : 'light';