@smess/networking 1.2.0 → 2.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 (313) hide show
  1. package/CHANGELOG.md +211 -0
  2. package/README.md +165 -114
  3. package/dist/core/body.d.ts +10 -0
  4. package/dist/core/body.d.ts.map +1 -0
  5. package/dist/core/body.js +22 -0
  6. package/dist/core/body.js.map +1 -0
  7. package/dist/core/client.d.ts +74 -0
  8. package/dist/core/client.d.ts.map +1 -0
  9. package/dist/core/client.js +124 -0
  10. package/dist/core/client.js.map +1 -0
  11. package/dist/core/decoders.d.ts +8 -0
  12. package/dist/core/decoders.d.ts.map +1 -0
  13. package/dist/core/decoders.js +26 -0
  14. package/dist/core/decoders.js.map +1 -0
  15. package/dist/core/endpoint.d.ts +69 -0
  16. package/dist/core/endpoint.d.ts.map +1 -0
  17. package/dist/core/endpoint.js +6 -0
  18. package/dist/core/endpoint.js.map +1 -0
  19. package/dist/core/hosts.d.ts +25 -0
  20. package/dist/core/hosts.d.ts.map +1 -0
  21. package/dist/core/hosts.js +22 -0
  22. package/dist/core/hosts.js.map +1 -0
  23. package/dist/core/pagination.d.ts +33 -0
  24. package/dist/core/pagination.d.ts.map +1 -0
  25. package/dist/core/pagination.js +23 -0
  26. package/dist/core/pagination.js.map +1 -0
  27. package/dist/core/pipeline.d.ts +25 -0
  28. package/dist/core/pipeline.d.ts.map +1 -0
  29. package/dist/core/pipeline.js +12 -0
  30. package/dist/core/pipeline.js.map +1 -0
  31. package/dist/core/retry.d.ts +18 -0
  32. package/dist/core/retry.d.ts.map +1 -0
  33. package/dist/core/retry.js +47 -0
  34. package/dist/core/retry.js.map +1 -0
  35. package/dist/core/transport.d.ts +30 -0
  36. package/dist/core/transport.d.ts.map +1 -0
  37. package/dist/core/transport.js +73 -0
  38. package/dist/core/transport.js.map +1 -0
  39. package/dist/core/types.d.ts +24 -0
  40. package/dist/core/types.d.ts.map +1 -0
  41. package/dist/core/types.js +3 -0
  42. package/dist/core/types.js.map +1 -0
  43. package/dist/core/url.d.ts +15 -0
  44. package/dist/core/url.d.ts.map +1 -0
  45. package/dist/core/url.js +46 -0
  46. package/dist/core/url.js.map +1 -0
  47. package/dist/errors/parse-error-body.d.ts +20 -0
  48. package/dist/errors/parse-error-body.d.ts.map +1 -0
  49. package/dist/errors/parse-error-body.js +73 -0
  50. package/dist/errors/parse-error-body.js.map +1 -0
  51. package/dist/errors/smess-api-error.d.ts +70 -0
  52. package/dist/errors/smess-api-error.d.ts.map +1 -0
  53. package/dist/errors/smess-api-error.js +84 -0
  54. package/dist/errors/smess-api-error.js.map +1 -0
  55. package/dist/index.d.ts +27 -25
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +15 -29
  58. package/dist/index.js.map +1 -0
  59. package/dist/modules/authentication/index.d.ts +89 -0
  60. package/dist/modules/authentication/index.d.ts.map +1 -0
  61. package/dist/modules/authentication/index.js +128 -0
  62. package/dist/modules/authentication/index.js.map +1 -0
  63. package/dist/modules/campaign/index.d.ts +212 -0
  64. package/dist/modules/campaign/index.d.ts.map +1 -0
  65. package/dist/modules/campaign/index.js +193 -0
  66. package/dist/modules/campaign/index.js.map +1 -0
  67. package/dist/modules/campaign/types.d.ts +562 -0
  68. package/dist/modules/campaign/types.d.ts.map +1 -0
  69. package/dist/modules/campaign/types.js +2 -0
  70. package/dist/modules/campaign/types.js.map +1 -0
  71. package/dist/modules/danara/index.d.ts +153 -0
  72. package/dist/modules/danara/index.d.ts.map +1 -0
  73. package/dist/modules/danara/index.js +144 -0
  74. package/dist/modules/danara/index.js.map +1 -0
  75. package/dist/modules/danara/types.d.ts +479 -0
  76. package/dist/modules/danara/types.d.ts.map +1 -0
  77. package/dist/modules/danara/types.js +2 -0
  78. package/dist/modules/danara/types.js.map +1 -0
  79. package/dist/modules/smeup-file/index.d.ts +27 -0
  80. package/dist/modules/smeup-file/index.d.ts.map +1 -0
  81. package/dist/modules/smeup-file/index.js +17 -0
  82. package/dist/modules/smeup-file/index.js.map +1 -0
  83. package/dist/modules/sso/index.d.ts +40 -0
  84. package/dist/modules/sso/index.d.ts.map +1 -0
  85. package/dist/modules/sso/index.js +41 -0
  86. package/dist/modules/sso/index.js.map +1 -0
  87. package/dist/rx/index.d.ts +11 -0
  88. package/dist/rx/index.d.ts.map +1 -0
  89. package/dist/rx/index.js +20 -0
  90. package/dist/rx/index.js.map +1 -0
  91. package/dist/session/auth-middleware.d.ts +27 -0
  92. package/dist/session/auth-middleware.d.ts.map +1 -0
  93. package/dist/session/auth-middleware.js +122 -0
  94. package/dist/session/auth-middleware.js.map +1 -0
  95. package/dist/session/jwt.d.ts +20 -0
  96. package/dist/session/jwt.d.ts.map +1 -0
  97. package/dist/session/jwt.js +25 -0
  98. package/dist/session/jwt.js.map +1 -0
  99. package/dist/session/stores.d.ts +19 -0
  100. package/dist/session/stores.d.ts.map +1 -0
  101. package/dist/session/stores.js +103 -0
  102. package/dist/session/stores.js.map +1 -0
  103. package/dist/session/types.d.ts +28 -0
  104. package/dist/session/types.d.ts.map +1 -0
  105. package/dist/session/types.js +2 -0
  106. package/dist/session/types.js.map +1 -0
  107. package/package.json +80 -30
  108. package/dist/core/axios.d.ts +0 -51
  109. package/dist/core/axios.js +0 -65
  110. package/dist/core/axios.observable.d.ts +0 -11
  111. package/dist/core/axios.observable.js +0 -58
  112. package/dist/core/base.provider.advanced.test.d.ts +0 -1
  113. package/dist/core/base.provider.advanced.test.js +0 -29
  114. package/dist/core/base.provider.d.ts +0 -15
  115. package/dist/core/base.provider.js +0 -13
  116. package/dist/core/base.provider.test.d.ts +0 -1
  117. package/dist/core/base.provider.test.js +0 -36
  118. package/dist/core/base.service.wrapper.advanced.test.d.ts +0 -1
  119. package/dist/core/base.service.wrapper.advanced.test.js +0 -39
  120. package/dist/core/base.service.wrapper.d.ts +0 -7
  121. package/dist/core/base.service.wrapper.js +0 -11
  122. package/dist/core/base.service.wrapper.test.d.ts +0 -1
  123. package/dist/core/base.service.wrapper.test.js +0 -25
  124. package/dist/core/rx.http.service.d.ts +0 -51
  125. package/dist/core/rx.http.service.js +0 -65
  126. package/dist/services/auth.service.d.ts +0 -65
  127. package/dist/services/auth.service.js +0 -197
  128. package/dist/services/auth.service.test.d.ts +0 -1
  129. package/dist/services/auth.service.test.js +0 -334
  130. package/dist/services/cache.service.d.ts +0 -31
  131. package/dist/services/cache.service.js +0 -77
  132. package/dist/services/cache.service.test.d.ts +0 -1
  133. package/dist/services/cache.service.test.js +0 -88
  134. package/dist/services/customer.service.d.ts +0 -211
  135. package/dist/services/customer.service.js +0 -128
  136. package/dist/services/customer.service.test.d.ts +0 -1
  137. package/dist/services/customer.service.test.js +0 -430
  138. package/dist/services/employee.service.d.ts +0 -73
  139. package/dist/services/employee.service.js +0 -74
  140. package/dist/services/employee.service.test.d.ts +0 -1
  141. package/dist/services/employee.service.test.js +0 -127
  142. package/dist/services/logger.service.d.ts +0 -31
  143. package/dist/services/logger.service.js +0 -93
  144. package/dist/services/logger.service.test.d.ts +0 -1
  145. package/dist/services/logger.service.test.js +0 -31
  146. package/dist/services/order.service.d.ts +0 -73
  147. package/dist/services/order.service.js +0 -97
  148. package/dist/services/order.service.test.d.ts +0 -1
  149. package/dist/services/order.service.test.js +0 -163
  150. package/dist/services/product.service.d.ts +0 -21
  151. package/dist/services/product.service.js +0 -42
  152. package/dist/services/product.service.test.d.ts +0 -1
  153. package/dist/services/product.service.test.js +0 -61
  154. package/dist/services/smess.service.d.ts +0 -36
  155. package/dist/services/smess.service.js +0 -128
  156. package/dist/services/smess.service.test.d.ts +0 -1
  157. package/dist/services/smess.service.test.js +0 -103
  158. package/dist/services/store.cart.service.d.ts +0 -158
  159. package/dist/services/store.cart.service.js +0 -177
  160. package/dist/services/store.cart.service.test.d.ts +0 -1
  161. package/dist/services/store.cart.service.test.js +0 -394
  162. package/dist/services/store.product.service.d.ts +0 -124
  163. package/dist/services/store.product.service.js +0 -102
  164. package/dist/services/store.product.service.test.d.ts +0 -1
  165. package/dist/services/store.product.service.test.js +0 -319
  166. package/dist/services/store.service.d.ts +0 -79
  167. package/dist/services/store.service.js +0 -235
  168. package/dist/services/store.service.test.d.ts +0 -1
  169. package/dist/services/store.service.test.js +0 -274
  170. package/dist/services/storefront.cart.service.d.ts +0 -73
  171. package/dist/services/storefront.cart.service.js +0 -167
  172. package/dist/services/storefront.cart.service.test.d.ts +0 -1
  173. package/dist/services/storefront.cart.service.test.js +0 -211
  174. package/dist/services/storefront.checkout.service.d.ts +0 -62
  175. package/dist/services/storefront.checkout.service.js +0 -75
  176. package/dist/services/storefront.checkout.service.test.d.ts +0 -1
  177. package/dist/services/storefront.checkout.service.test.js +0 -158
  178. package/dist/services/storefront.customer.metadata.service.d.ts +0 -91
  179. package/dist/services/storefront.customer.metadata.service.js +0 -225
  180. package/dist/services/storefront.customer.metadata.service.test.d.ts +0 -1
  181. package/dist/services/storefront.customer.metadata.service.test.js +0 -271
  182. package/dist/services/storefront.order.service.d.ts +0 -45
  183. package/dist/services/storefront.order.service.js +0 -99
  184. package/dist/services/storefront.order.service.test.d.ts +0 -1
  185. package/dist/services/storefront.order.service.test.js +0 -158
  186. package/dist/services/storefront.product.service.d.ts +0 -52
  187. package/dist/services/storefront.product.service.js +0 -75
  188. package/dist/services/storefront.product.service.test.d.ts +0 -1
  189. package/dist/services/storefront.product.service.test.js +0 -120
  190. package/dist/services/storefront.store.service.d.ts +0 -54
  191. package/dist/services/storefront.store.service.js +0 -142
  192. package/dist/services/storefront.store.service.test.d.ts +0 -1
  193. package/dist/services/storefront.store.service.test.js +0 -90
  194. package/dist/services/user.service.d.ts +0 -16
  195. package/dist/services/user.service.js +0 -37
  196. package/dist/services/user.service.test.d.ts +0 -1
  197. package/dist/services/user.service.test.js +0 -52
  198. package/dist/services/utils/withAuthReset.d.ts +0 -3
  199. package/dist/services/utils/withAuthReset.js +0 -8
  200. package/dist/src/core/axios.d.ts +0 -51
  201. package/dist/src/core/axios.js +0 -65
  202. package/dist/src/core/axios.observable.d.ts +0 -11
  203. package/dist/src/core/axios.observable.js +0 -58
  204. package/dist/src/core/base.provider.advanced.test.d.ts +0 -1
  205. package/dist/src/core/base.provider.advanced.test.js +0 -29
  206. package/dist/src/core/base.provider.d.ts +0 -15
  207. package/dist/src/core/base.provider.js +0 -13
  208. package/dist/src/core/base.provider.test.d.ts +0 -1
  209. package/dist/src/core/base.provider.test.js +0 -36
  210. package/dist/src/core/base.service.wrapper.advanced.test.d.ts +0 -1
  211. package/dist/src/core/base.service.wrapper.advanced.test.js +0 -39
  212. package/dist/src/core/base.service.wrapper.d.ts +0 -7
  213. package/dist/src/core/base.service.wrapper.js +0 -11
  214. package/dist/src/core/base.service.wrapper.test.d.ts +0 -1
  215. package/dist/src/core/base.service.wrapper.test.js +0 -25
  216. package/dist/src/core/rx.http.service.d.ts +0 -51
  217. package/dist/src/core/rx.http.service.js +0 -65
  218. package/dist/src/index.d.ts +0 -25
  219. package/dist/src/index.js +0 -29
  220. package/dist/src/services/auth.service.d.ts +0 -65
  221. package/dist/src/services/auth.service.js +0 -197
  222. package/dist/src/services/auth.service.test.d.ts +0 -1
  223. package/dist/src/services/auth.service.test.js +0 -334
  224. package/dist/src/services/cache.service.d.ts +0 -31
  225. package/dist/src/services/cache.service.js +0 -77
  226. package/dist/src/services/cache.service.test.d.ts +0 -1
  227. package/dist/src/services/cache.service.test.js +0 -88
  228. package/dist/src/services/customer.service.d.ts +0 -199
  229. package/dist/src/services/customer.service.js +0 -114
  230. package/dist/src/services/customer.service.test.d.ts +0 -1
  231. package/dist/src/services/customer.service.test.js +0 -430
  232. package/dist/src/services/employee.service.d.ts +0 -73
  233. package/dist/src/services/employee.service.js +0 -74
  234. package/dist/src/services/employee.service.test.d.ts +0 -1
  235. package/dist/src/services/employee.service.test.js +0 -127
  236. package/dist/src/services/logger.service.d.ts +0 -31
  237. package/dist/src/services/logger.service.js +0 -93
  238. package/dist/src/services/logger.service.test.d.ts +0 -1
  239. package/dist/src/services/logger.service.test.js +0 -31
  240. package/dist/src/services/order.service.d.ts +0 -73
  241. package/dist/src/services/order.service.js +0 -97
  242. package/dist/src/services/order.service.test.d.ts +0 -1
  243. package/dist/src/services/order.service.test.js +0 -163
  244. package/dist/src/services/product.service.d.ts +0 -21
  245. package/dist/src/services/product.service.js +0 -42
  246. package/dist/src/services/product.service.test.d.ts +0 -1
  247. package/dist/src/services/product.service.test.js +0 -61
  248. package/dist/src/services/smess.service.d.ts +0 -36
  249. package/dist/src/services/smess.service.js +0 -128
  250. package/dist/src/services/smess.service.test.d.ts +0 -1
  251. package/dist/src/services/smess.service.test.js +0 -103
  252. package/dist/src/services/store.cart.service.d.ts +0 -158
  253. package/dist/src/services/store.cart.service.js +0 -177
  254. package/dist/src/services/store.cart.service.test.d.ts +0 -1
  255. package/dist/src/services/store.cart.service.test.js +0 -394
  256. package/dist/src/services/store.product.service.d.ts +0 -124
  257. package/dist/src/services/store.product.service.js +0 -102
  258. package/dist/src/services/store.product.service.test.d.ts +0 -1
  259. package/dist/src/services/store.product.service.test.js +0 -319
  260. package/dist/src/services/store.service.d.ts +0 -79
  261. package/dist/src/services/store.service.js +0 -235
  262. package/dist/src/services/store.service.test.d.ts +0 -1
  263. package/dist/src/services/store.service.test.js +0 -274
  264. package/dist/src/services/storefront.cart.service.d.ts +0 -73
  265. package/dist/src/services/storefront.cart.service.js +0 -167
  266. package/dist/src/services/storefront.cart.service.test.d.ts +0 -1
  267. package/dist/src/services/storefront.cart.service.test.js +0 -211
  268. package/dist/src/services/storefront.checkout.service.d.ts +0 -62
  269. package/dist/src/services/storefront.checkout.service.js +0 -75
  270. package/dist/src/services/storefront.checkout.service.test.d.ts +0 -1
  271. package/dist/src/services/storefront.checkout.service.test.js +0 -158
  272. package/dist/src/services/storefront.customer.metadata.service.d.ts +0 -91
  273. package/dist/src/services/storefront.customer.metadata.service.js +0 -225
  274. package/dist/src/services/storefront.customer.metadata.service.test.d.ts +0 -1
  275. package/dist/src/services/storefront.customer.metadata.service.test.js +0 -271
  276. package/dist/src/services/storefront.order.service.d.ts +0 -45
  277. package/dist/src/services/storefront.order.service.js +0 -99
  278. package/dist/src/services/storefront.order.service.test.d.ts +0 -1
  279. package/dist/src/services/storefront.order.service.test.js +0 -158
  280. package/dist/src/services/storefront.product.service.d.ts +0 -52
  281. package/dist/src/services/storefront.product.service.js +0 -75
  282. package/dist/src/services/storefront.product.service.test.d.ts +0 -1
  283. package/dist/src/services/storefront.product.service.test.js +0 -120
  284. package/dist/src/services/storefront.store.service.d.ts +0 -52
  285. package/dist/src/services/storefront.store.service.js +0 -140
  286. package/dist/src/services/storefront.store.service.test.d.ts +0 -1
  287. package/dist/src/services/storefront.store.service.test.js +0 -90
  288. package/dist/src/services/user.service.d.ts +0 -16
  289. package/dist/src/services/user.service.js +0 -37
  290. package/dist/src/services/user.service.test.d.ts +0 -1
  291. package/dist/src/services/user.service.test.js +0 -52
  292. package/dist/src/services/utils/withAuthReset.d.ts +0 -3
  293. package/dist/src/services/utils/withAuthReset.js +0 -8
  294. package/dist/src/types/types.d.ts +0 -93
  295. package/dist/src/types/types.js +0 -82
  296. package/dist/src/types/types.test.d.ts +0 -1
  297. package/dist/src/types/types.test.js +0 -25
  298. package/dist/src/utils/cache-import.d.ts +0 -3
  299. package/dist/src/utils/cache-import.js +0 -2
  300. package/dist/src/utils/networking-import.d.ts +0 -4
  301. package/dist/src/utils/networking-import.js +0 -3
  302. package/dist/src/utils/rxjs-import.d.ts +0 -5
  303. package/dist/src/utils/rxjs-import.js +0 -5
  304. package/dist/types/types.d.ts +0 -96
  305. package/dist/types/types.js +0 -82
  306. package/dist/types/types.test.d.ts +0 -1
  307. package/dist/types/types.test.js +0 -25
  308. package/dist/utils/cache-import.d.ts +0 -3
  309. package/dist/utils/cache-import.js +0 -2
  310. package/dist/utils/networking-import.d.ts +0 -4
  311. package/dist/utils/networking-import.js +0 -3
  312. package/dist/utils/rxjs-import.d.ts +0 -5
  313. package/dist/utils/rxjs-import.js +0 -5
@@ -1,197 +0,0 @@
1
- import { Accept, SMESSHeader, SmessService } from "./smess.service";
2
- import { BehaviorSubject } from "rxjs";
3
- import { tap } from "rxjs/operators";
4
- // LocalStorage keys for auth persistence
5
- const AUTH_TOKEN_KEY = 'smess_auth_token';
6
- const AUTH_RESPONSE_KEY = 'smess_auth_response';
7
- export class AuthProvider {
8
- constructor(config, target, $authToken) {
9
- this.config = config;
10
- this.target = target;
11
- this.$authToken = $authToken;
12
- }
13
- get baseURL() {
14
- return this.config.version
15
- ? `${this.config.baseUrl}/${this.config.version}`
16
- : this.config.baseUrl;
17
- }
18
- get path() {
19
- switch (this.target) {
20
- case "login":
21
- return "/authentication/login_check";
22
- case "password-forgot":
23
- return "/authentication/forgot-password";
24
- case "reset-password":
25
- return "/authentication/reset-password";
26
- case "verify-reset-password-token":
27
- return "/authentication/verify-reset-password-token";
28
- case "logout":
29
- return "/authentication/logout";
30
- case "change-password":
31
- return "/authentication/change-password";
32
- default:
33
- return "/authentication";
34
- }
35
- }
36
- get method() {
37
- switch (this.target) {
38
- default:
39
- return "POST";
40
- }
41
- }
42
- get headers() {
43
- let authHeaders = {};
44
- switch (this.target) {
45
- case "login":
46
- case "password-forgot":
47
- case "reset-password":
48
- case "verify-reset-password-token":
49
- authHeaders = SmessService.getHeaders(Accept.V1);
50
- break;
51
- default:
52
- authHeaders = SmessService.getHeaders(Accept.V1, this.$authToken.value);
53
- }
54
- if (this.config.clientId) {
55
- authHeaders[SMESSHeader.ClientId] = this.config.clientId;
56
- }
57
- if (this.config.apiKey) {
58
- authHeaders[SMESSHeader.ApiKey] = this.config.apiKey;
59
- }
60
- return SmessService.filterHeaders(authHeaders);
61
- }
62
- getPath(...queryParams) {
63
- let definedPath = this.path;
64
- const params = definedPath.match(/\$\{(.+?)\}/g);
65
- params?.forEach((value, index) => {
66
- definedPath = definedPath.replace(value, String(queryParams[index]));
67
- });
68
- return definedPath;
69
- }
70
- }
71
- export class AuthService {
72
- constructor(config) {
73
- this.config = config;
74
- this.$_authToken = new BehaviorSubject(null);
75
- this.loadAuthFromStorage();
76
- }
77
- // Getter that returns BehaviorSubject with token validation
78
- get $authToken() {
79
- const currentToken = this.getValidToken();
80
- if (currentToken !== this.$_authToken.value) {
81
- this.$_authToken.next(currentToken);
82
- }
83
- return this.$_authToken;
84
- }
85
- // Get current auth token value
86
- get currentAuthToken() {
87
- return this.getValidToken();
88
- }
89
- // Get valid token from memory or localStorage
90
- getValidToken() {
91
- // If we have a token in memory, return it (assume it's valid)
92
- if (this.$_authToken.value) {
93
- return this.$_authToken.value;
94
- }
95
- // Otherwise, try to load from localStorage
96
- return this.loadFromStorage();
97
- }
98
- // Load token from localStorage and check expiration
99
- loadFromStorage() {
100
- try {
101
- const storedResponse = localStorage.getItem(AUTH_RESPONSE_KEY);
102
- const storedAt = localStorage.getItem(`${AUTH_RESPONSE_KEY}_stored_at`);
103
- if (storedResponse && storedAt) {
104
- const authResponse = JSON.parse(storedResponse);
105
- const storedTime = parseInt(storedAt, 10);
106
- // Check if token is expired
107
- if (authResponse.expires_in && typeof authResponse.expires_in === 'number') {
108
- const expirationTime = storedTime + (authResponse.expires_in * 1000);
109
- if (Date.now() >= expirationTime) {
110
- // Token expired, clear storage
111
- this.clearAuthFromStorage();
112
- return null;
113
- }
114
- }
115
- // Token is valid, store in memory and return
116
- const token = String(authResponse.token);
117
- this.$_authToken.next(token);
118
- return token;
119
- }
120
- }
121
- catch (error) {
122
- console.warn('Failed to load token from localStorage:', error);
123
- this.clearAuthFromStorage();
124
- }
125
- return null;
126
- }
127
- // Load auth token from localStorage on initialization
128
- loadAuthFromStorage() {
129
- this.loadFromStorage();
130
- }
131
- // Save auth response to localStorage
132
- saveAuthToStorage(authResponse) {
133
- try {
134
- localStorage.setItem(AUTH_TOKEN_KEY, String(authResponse.token));
135
- localStorage.setItem(AUTH_RESPONSE_KEY, JSON.stringify(authResponse));
136
- localStorage.setItem(`${AUTH_RESPONSE_KEY}_stored_at`, Date.now().toString());
137
- // Update memory
138
- this.$_authToken.next(String(authResponse.token));
139
- }
140
- catch (error) {
141
- console.warn('Failed to save auth to localStorage:', error);
142
- }
143
- }
144
- // Clear auth from localStorage
145
- clearAuthFromStorage() {
146
- try {
147
- localStorage.removeItem(AUTH_TOKEN_KEY);
148
- localStorage.removeItem(AUTH_RESPONSE_KEY);
149
- localStorage.removeItem(`${AUTH_RESPONSE_KEY}_stored_at`);
150
- // Clear memory
151
- this.$_authToken.next(null);
152
- }
153
- catch (error) {
154
- console.warn('Failed to clear auth from localStorage:', error);
155
- }
156
- }
157
- provider(target) {
158
- const targetWrapper = new AuthProvider(this.config, target, this.$authToken);
159
- return new SmessService(targetWrapper);
160
- }
161
- login(payload) {
162
- return this.provider("login")
163
- .request(payload)
164
- .pipe(tap((result) => {
165
- if (result.hasSuccess && result.response?.token) {
166
- // Set the auth token when login is successful
167
- this.$_authToken.next(String(result.response.token));
168
- // Save to localStorage if rememberMe is true
169
- if (payload.rememberMe && result.response) {
170
- this.saveAuthToStorage(result.response);
171
- }
172
- }
173
- }));
174
- }
175
- logout() {
176
- return this.provider("logout")
177
- .request()
178
- .pipe(tap((result) => {
179
- if (result.hasSuccess) {
180
- // Clear from localStorage (this will also clear memory)
181
- this.clearAuthFromStorage();
182
- }
183
- }));
184
- }
185
- forgotPassword(payload) {
186
- return this.provider("password-forgot").request(payload);
187
- }
188
- changePassword(payload) {
189
- return this.provider("change-password").request(payload);
190
- }
191
- resetPassword(payload) {
192
- return this.provider("reset-password").request(payload);
193
- }
194
- verifyResetPasswordToken(token) {
195
- return this.provider("verify-reset-password-token").request({ token });
196
- }
197
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,334 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
- import { AuthService, } from "./auth.service";
5
- import { NetworkingUIEventWrapped, AlertModel, } from "../types/types";
6
- import { of } from "rxjs";
7
- describe("AuthService", () => {
8
- const config = {
9
- baseUrl: "https://api.example.com",
10
- isProd: false,
11
- clientId: "test-client-id",
12
- apiKey: "test-api-key",
13
- };
14
- let service;
15
- let mockLocalStorage;
16
- beforeEach(() => {
17
- // Mock localStorage for all tests
18
- mockLocalStorage = {
19
- setItem: jest.fn(),
20
- getItem: jest.fn(),
21
- removeItem: jest.fn(),
22
- };
23
- Object.defineProperty(window, 'localStorage', {
24
- value: mockLocalStorage,
25
- writable: true,
26
- });
27
- service = new AuthService(config);
28
- });
29
- it("login success", async () => {
30
- const payload = { identifier: "user", password: "pass" };
31
- const response = {
32
- token_type: "Bearer",
33
- expires_in: 3600,
34
- token: "token",
35
- refresh_token: "refresh",
36
- };
37
- jest.spyOn(service, "provider").mockReturnValue({
38
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
39
- });
40
- const result = await service.login(payload).toPromise();
41
- expect(result?.hasSuccess).toBe(true);
42
- expect(result?.response).toEqual(response);
43
- // Verify that auth token is set after successful login
44
- expect(service.currentAuthToken).toBe("token");
45
- expect(service.$authToken.value).toBe("token");
46
- });
47
- it("login error", async () => {
48
- const payload = { identifier: "user", password: "wrong" };
49
- jest.spyOn(service, "provider").mockReturnValue({
50
- request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
51
- });
52
- const result = await service.login(payload).toPromise();
53
- expect(result?.hasFailed).toBe(true);
54
- expect(result?.alertModel).toBeInstanceOf(AlertModel);
55
- });
56
- it("login success with rememberMe", async () => {
57
- const payload = {
58
- identifier: "user",
59
- password: "pass",
60
- rememberMe: true
61
- };
62
- const response = {
63
- token_type: "Bearer",
64
- expires_in: 3600,
65
- token: "token",
66
- refresh_token: "refresh",
67
- };
68
- jest.spyOn(service, "provider").mockReturnValue({
69
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
70
- });
71
- const result = await service.login(payload).toPromise();
72
- expect(result?.hasSuccess).toBe(true);
73
- expect(result?.response).toEqual(response);
74
- // Verify that auth token is set after successful login
75
- expect(service.currentAuthToken).toBe("token");
76
- expect(service.$authToken.value).toBe("token");
77
- // Verify that localStorage was called to save the auth data
78
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('smess_auth_token', 'token');
79
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('smess_auth_response', JSON.stringify(response));
80
- });
81
- it("login success without rememberMe", async () => {
82
- const payload = {
83
- identifier: "user",
84
- password: "pass",
85
- rememberMe: false
86
- };
87
- const response = {
88
- token_type: "Bearer",
89
- expires_in: 3600,
90
- token: "token",
91
- refresh_token: "refresh",
92
- };
93
- jest.spyOn(service, "provider").mockReturnValue({
94
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
95
- });
96
- const result = await service.login(payload).toPromise();
97
- expect(result?.hasSuccess).toBe(true);
98
- expect(result?.response).toEqual(response);
99
- // Verify that auth token is set after successful login
100
- expect(service.currentAuthToken).toBe("token");
101
- expect(service.$authToken.value).toBe("token");
102
- // Verify that localStorage was NOT called to save the auth data
103
- expect(mockLocalStorage.setItem).not.toHaveBeenCalled();
104
- });
105
- it("logout success", async () => {
106
- // First set a token to test clearing it
107
- service.$authToken.next("test-token");
108
- expect(service.currentAuthToken).toBe("test-token");
109
- jest.spyOn(service, "provider").mockReturnValue({
110
- request: () => of(NetworkingUIEventWrapped.succeeded(undefined)),
111
- });
112
- const result = await service.logout().toPromise();
113
- expect(result?.hasSuccess).toBe(true);
114
- // Verify that auth token is cleared after successful logout
115
- expect(service.currentAuthToken).toBe(null);
116
- expect(service.$authToken.value).toBe(null);
117
- // Verify that localStorage was cleared
118
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_token');
119
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response');
120
- });
121
- it("loads auth token from localStorage on initialization", () => {
122
- // Mock localStorage to return a stored auth response
123
- const storedResponse = {
124
- token_type: "Bearer",
125
- expires_in: 3600,
126
- token: "stored-token",
127
- refresh_token: "refresh",
128
- };
129
- const mockLocalStorage = {
130
- setItem: jest.fn(),
131
- getItem: jest.fn().mockImplementation((key) => {
132
- if (key === 'smess_auth_response') {
133
- return JSON.stringify(storedResponse);
134
- }
135
- if (key === 'smess_auth_response_stored_at') {
136
- return Date.now().toString();
137
- }
138
- return null;
139
- }),
140
- removeItem: jest.fn(),
141
- };
142
- Object.defineProperty(window, 'localStorage', {
143
- value: mockLocalStorage,
144
- writable: true,
145
- });
146
- // Create a new service instance
147
- const newService = new AuthService(config);
148
- // Verify that the stored token was loaded
149
- expect(newService.currentAuthToken).toBe('stored-token');
150
- expect(newService.$authToken.value).toBe('stored-token');
151
- expect(mockLocalStorage.getItem).toHaveBeenCalledWith('smess_auth_response');
152
- });
153
- it("handles localStorage errors gracefully", () => {
154
- // Mock localStorage to throw an error
155
- const mockLocalStorage = {
156
- setItem: jest.fn().mockImplementation(() => {
157
- throw new Error('localStorage error');
158
- }),
159
- getItem: jest.fn().mockImplementation(() => {
160
- throw new Error('localStorage error');
161
- }),
162
- removeItem: jest.fn().mockImplementation(() => {
163
- throw new Error('localStorage error');
164
- }),
165
- };
166
- Object.defineProperty(window, 'localStorage', {
167
- value: mockLocalStorage,
168
- writable: true,
169
- });
170
- // Should not throw an error during initialization
171
- expect(() => new AuthService(config)).not.toThrow();
172
- // Should not throw an error during login with rememberMe
173
- const service = new AuthService(config);
174
- const payload = {
175
- identifier: "user",
176
- password: "pass",
177
- rememberMe: true
178
- };
179
- const response = {
180
- token_type: "Bearer",
181
- expires_in: 3600,
182
- token: "token",
183
- refresh_token: "refresh",
184
- };
185
- jest.spyOn(service, "provider").mockReturnValue({
186
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
187
- });
188
- expect(async () => {
189
- await service.login(payload).toPromise();
190
- }).not.toThrow();
191
- });
192
- it("handles expired token from localStorage", () => {
193
- // Mock localStorage with expired token
194
- const expiredResponse = {
195
- token_type: "Bearer",
196
- expires_in: 1, // 1 second
197
- token: "expired-token",
198
- refresh_token: "refresh",
199
- };
200
- mockLocalStorage.getItem.mockImplementation((key) => {
201
- if (key === 'smess_auth_response') {
202
- return JSON.stringify(expiredResponse);
203
- }
204
- if (key === 'smess_auth_response_stored_at') {
205
- // Set stored time to 2 seconds ago (expired)
206
- return (Date.now() - 2000).toString();
207
- }
208
- return null;
209
- });
210
- // Create a new service instance
211
- const newService = new AuthService(config);
212
- // Verify that expired token is not returned
213
- expect(newService.currentAuthToken).toBe(null);
214
- expect(newService.$authToken.value).toBe(null);
215
- // Verify that localStorage was cleared
216
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_token');
217
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response');
218
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response_stored_at');
219
- });
220
- it("handles valid token from localStorage", () => {
221
- // Mock localStorage with valid token
222
- const validResponse = {
223
- token_type: "Bearer",
224
- expires_in: 3600, // 1 hour
225
- token: "valid-token",
226
- refresh_token: "refresh",
227
- };
228
- mockLocalStorage.getItem.mockImplementation((key) => {
229
- if (key === 'smess_auth_response') {
230
- return JSON.stringify(validResponse);
231
- }
232
- if (key === 'smess_auth_response_stored_at') {
233
- // Set stored time to 30 minutes ago (still valid)
234
- return (Date.now() - 1800000).toString();
235
- }
236
- return null;
237
- });
238
- // Create a new service instance
239
- const newService = new AuthService(config);
240
- // Verify that valid token is returned
241
- expect(newService.currentAuthToken).toBe("valid-token");
242
- expect(newService.$authToken.value).toBe("valid-token");
243
- });
244
- it("forgotPassword success", async () => {
245
- const payload = { identifier: "user" };
246
- const response = { data: "ok" };
247
- jest.spyOn(service, "provider").mockReturnValue({
248
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
249
- });
250
- const result = await service.forgotPassword(payload).toPromise();
251
- expect(result?.hasSuccess).toBe(true);
252
- expect(result?.response).toEqual(response);
253
- });
254
- it("changePassword success", async () => {
255
- const payload = {
256
- old_password: "old",
257
- password: "new",
258
- };
259
- const response = { data: "changed" };
260
- jest.spyOn(service, "provider").mockReturnValue({
261
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
262
- });
263
- const result = await service.changePassword(payload).toPromise();
264
- expect(result?.hasSuccess).toBe(true);
265
- expect(result?.response).toEqual(response);
266
- });
267
- it("resetPassword success", async () => {
268
- const payload = {
269
- token: "token",
270
- password: "newpass",
271
- };
272
- const response = { data: "reset" };
273
- jest.spyOn(service, "provider").mockReturnValue({
274
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
275
- });
276
- const result = await service.resetPassword(payload).toPromise();
277
- expect(result?.hasSuccess).toBe(true);
278
- expect(result?.response).toEqual(response);
279
- });
280
- it("verifyResetPasswordToken success", async () => {
281
- const token = "token";
282
- const response = {
283
- token_type: "Bearer",
284
- expires_in: 3600,
285
- token: "token",
286
- refresh_token: "refresh",
287
- };
288
- jest.spyOn(service, "provider").mockReturnValue({
289
- request: () => of(NetworkingUIEventWrapped.succeeded(response)),
290
- });
291
- const result = await service.verifyResetPasswordToken(token).toPromise();
292
- expect(result?.hasSuccess).toBe(true);
293
- expect(result?.response).toEqual(response);
294
- });
295
- it("forgotPassword error", async () => {
296
- const payload = { identifier: "user" };
297
- jest.spyOn(service, "provider").mockReturnValue({
298
- request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
299
- });
300
- const result = await service.forgotPassword(payload).toPromise();
301
- expect(result?.hasFailed).toBe(true);
302
- expect(result?.alertModel).toBeInstanceOf(AlertModel);
303
- });
304
- it("changePassword error", async () => {
305
- const payload = {
306
- old_password: "old",
307
- password: "bad",
308
- };
309
- jest.spyOn(service, "provider").mockReturnValue({
310
- request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
311
- });
312
- const result = await service.changePassword(payload).toPromise();
313
- expect(result?.hasFailed).toBe(true);
314
- expect(result?.alertModel).toBeInstanceOf(AlertModel);
315
- });
316
- it("resetPassword error", async () => {
317
- const payload = { token: "bad", password: "bad" };
318
- jest.spyOn(service, "provider").mockReturnValue({
319
- request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
320
- });
321
- const result = await service.resetPassword(payload).toPromise();
322
- expect(result?.hasFailed).toBe(true);
323
- expect(result?.alertModel).toBeInstanceOf(AlertModel);
324
- });
325
- it("verifyResetPasswordToken error", async () => {
326
- const token = "bad";
327
- jest.spyOn(service, "provider").mockReturnValue({
328
- request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
329
- });
330
- const result = await service.verifyResetPasswordToken(token).toPromise();
331
- expect(result?.hasFailed).toBe(true);
332
- expect(result?.alertModel).toBeInstanceOf(AlertModel);
333
- });
334
- });
@@ -1,31 +0,0 @@
1
- import { Dexie, Table } from "../utils/cache-import";
2
- export interface CacheMetadata {
3
- key: string;
4
- cachedAt: number;
5
- expiresAt: number;
6
- version: string;
7
- }
8
- export declare class GenericDB<T> extends Dexie {
9
- data: Table<T>;
10
- cache: Table<CacheMetadata>;
11
- constructor(name: string, dataStoreSchema: string);
12
- }
13
- export declare class GenericIndexedDBService<T, TCached> {
14
- private toCached;
15
- private fromCached;
16
- private readonly CACHE_DURATION;
17
- readonly db: GenericDB<TCached>;
18
- constructor(dbName: string, storeSchema: string, toCached: (raw: T) => TCached, fromCached: (cached: TCached) => T);
19
- saveAll(items: T[], cacheKey: string): Promise<void>;
20
- getAll(cacheKey: string): Promise<T[] | null>;
21
- saveOne(item: T): Promise<void>;
22
- getOne(id: string): Promise<T | null>;
23
- delete(id: string): Promise<void>;
24
- clearAll(): Promise<void>;
25
- getCacheInfo(key: string): Promise<{
26
- count: number;
27
- lastUpdated?: Date;
28
- }>;
29
- setStats(key: string, stats: unknown): Promise<void>;
30
- getStats(key: string): Promise<unknown | null>;
31
- }
@@ -1,77 +0,0 @@
1
- import { Dexie } from "../utils/cache-import";
2
- export class GenericDB extends Dexie {
3
- constructor(name, dataStoreSchema) {
4
- super(name);
5
- this.version(1).stores({
6
- data: dataStoreSchema,
7
- cache: "key, cachedAt, expiresAt",
8
- });
9
- }
10
- }
11
- export class GenericIndexedDBService {
12
- constructor(dbName, storeSchema, toCached, fromCached) {
13
- this.toCached = toCached;
14
- this.fromCached = fromCached;
15
- this.CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours
16
- this.db = new GenericDB(dbName, storeSchema);
17
- }
18
- async saveAll(items, cacheKey) {
19
- const cachedItems = items.map(this.toCached);
20
- await this.db.transaction("rw", this.db.data, this.db.cache, async () => {
21
- await this.db.data.clear();
22
- await this.db.data.bulkAdd(cachedItems);
23
- await this.db.cache.put({
24
- key: cacheKey,
25
- cachedAt: Date.now(),
26
- expiresAt: Date.now() + this.CACHE_DURATION,
27
- version: "1.0",
28
- });
29
- });
30
- }
31
- async getAll(cacheKey) {
32
- const cacheInfo = await this.db.cache.get(cacheKey);
33
- if (!cacheInfo || Date.now() > cacheInfo.expiresAt)
34
- return null;
35
- const cachedItems = await this.db.data.toArray();
36
- return cachedItems.map(this.fromCached);
37
- }
38
- async saveOne(item) {
39
- const cached = this.toCached(item);
40
- await this.db.data.put(cached);
41
- }
42
- async getOne(id) {
43
- const cached = await this.db.data.get(id);
44
- return cached ? this.fromCached(cached) : null;
45
- }
46
- async delete(id) {
47
- await this.db.data.delete(id);
48
- }
49
- async clearAll() {
50
- await this.db.transaction("rw", this.db.data, this.db.cache, async () => {
51
- await this.db.data.clear();
52
- await this.db.cache.clear();
53
- });
54
- }
55
- async getCacheInfo(key) {
56
- const count = await this.db.data.count();
57
- const cacheInfo = await this.db.cache.get(key);
58
- return {
59
- count,
60
- lastUpdated: cacheInfo ? new Date(cacheInfo.cachedAt) : undefined,
61
- };
62
- }
63
- async setStats(key, stats) {
64
- await this.db.cache.put({
65
- key,
66
- cachedAt: Date.now(),
67
- expiresAt: Date.now() + this.CACHE_DURATION,
68
- version: JSON.stringify(stats),
69
- });
70
- }
71
- async getStats(key) {
72
- const cacheInfo = await this.db.cache.get(key);
73
- if (!cacheInfo || Date.now() > cacheInfo.expiresAt)
74
- return null;
75
- return JSON.parse(cacheInfo.version);
76
- }
77
- }
@@ -1 +0,0 @@
1
- import "fake-indexeddb/auto";