@clix-so/react-native-sdk 0.0.2-beta.5 → 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.
Files changed (88) hide show
  1. package/lib/module/core/Clix.js +40 -95
  2. package/lib/module/core/Clix.js.map +1 -1
  3. package/lib/module/core/ClixInitCoordinator.js +3 -14
  4. package/lib/module/core/ClixInitCoordinator.js.map +1 -1
  5. package/lib/module/core/ClixNotification.js +25 -28
  6. package/lib/module/core/ClixNotification.js.map +1 -1
  7. package/lib/module/models/ClixDevice.js +0 -6
  8. package/lib/module/models/ClixDevice.js.map +1 -1
  9. package/lib/module/models/ClixPushNotificationPayload.js +0 -19
  10. package/lib/module/models/ClixPushNotificationPayload.js.map +1 -1
  11. package/lib/module/services/ClixAPIClient.js +50 -99
  12. package/lib/module/services/ClixAPIClient.js.map +1 -1
  13. package/lib/module/services/DeviceAPIService.js +37 -45
  14. package/lib/module/services/DeviceAPIService.js.map +1 -1
  15. package/lib/module/services/DeviceService.js +97 -116
  16. package/lib/module/services/DeviceService.js.map +1 -1
  17. package/lib/module/services/EventAPIService.js +3 -5
  18. package/lib/module/services/EventAPIService.js.map +1 -1
  19. package/lib/module/services/EventService.js +13 -20
  20. package/lib/module/services/EventService.js.map +1 -1
  21. package/lib/module/services/NotificationService.js +252 -402
  22. package/lib/module/services/NotificationService.js.map +1 -1
  23. package/lib/module/services/TokenService.js +3 -59
  24. package/lib/module/services/TokenService.js.map +1 -1
  25. package/lib/module/utils/http/HTTPClient.js +101 -0
  26. package/lib/module/utils/http/HTTPClient.js.map +1 -0
  27. package/lib/module/utils/http/HTTPMethod.js +10 -0
  28. package/lib/module/utils/http/HTTPMethod.js.map +1 -0
  29. package/lib/module/utils/http/HTTPRequest.js +4 -0
  30. package/lib/module/utils/http/HTTPRequest.js.map +1 -0
  31. package/lib/module/utils/http/HTTPResponse.js +2 -0
  32. package/lib/module/utils/http/HTTPResponse.js.map +1 -0
  33. package/lib/module/utils/types.js +2 -0
  34. package/lib/module/utils/types.js.map +1 -0
  35. package/lib/typescript/src/core/Clix.d.ts +13 -15
  36. package/lib/typescript/src/core/Clix.d.ts.map +1 -1
  37. package/lib/typescript/src/core/ClixConfig.d.ts +3 -3
  38. package/lib/typescript/src/core/ClixConfig.d.ts.map +1 -1
  39. package/lib/typescript/src/core/ClixInitCoordinator.d.ts +0 -3
  40. package/lib/typescript/src/core/ClixInitCoordinator.d.ts.map +1 -1
  41. package/lib/typescript/src/core/ClixNotification.d.ts +6 -5
  42. package/lib/typescript/src/core/ClixNotification.d.ts.map +1 -1
  43. package/lib/typescript/src/models/ClixDevice.d.ts +0 -2
  44. package/lib/typescript/src/models/ClixDevice.d.ts.map +1 -1
  45. package/lib/typescript/src/models/ClixPushNotificationPayload.d.ts +8 -21
  46. package/lib/typescript/src/models/ClixPushNotificationPayload.d.ts.map +1 -1
  47. package/lib/typescript/src/services/ClixAPIClient.d.ts +6 -22
  48. package/lib/typescript/src/services/ClixAPIClient.d.ts.map +1 -1
  49. package/lib/typescript/src/services/DeviceAPIService.d.ts +1 -1
  50. package/lib/typescript/src/services/DeviceAPIService.d.ts.map +1 -1
  51. package/lib/typescript/src/services/DeviceService.d.ts +10 -5
  52. package/lib/typescript/src/services/DeviceService.d.ts.map +1 -1
  53. package/lib/typescript/src/services/EventAPIService.d.ts.map +1 -1
  54. package/lib/typescript/src/services/EventService.d.ts +1 -0
  55. package/lib/typescript/src/services/EventService.d.ts.map +1 -1
  56. package/lib/typescript/src/services/NotificationService.d.ts +50 -57
  57. package/lib/typescript/src/services/NotificationService.d.ts.map +1 -1
  58. package/lib/typescript/src/services/TokenService.d.ts +1 -7
  59. package/lib/typescript/src/services/TokenService.d.ts.map +1 -1
  60. package/lib/typescript/src/utils/http/HTTPClient.d.ts +15 -0
  61. package/lib/typescript/src/utils/http/HTTPClient.d.ts.map +1 -0
  62. package/lib/typescript/src/utils/http/HTTPMethod.d.ts +7 -0
  63. package/lib/typescript/src/utils/http/HTTPMethod.d.ts.map +1 -0
  64. package/lib/typescript/src/utils/http/HTTPRequest.d.ts +9 -0
  65. package/lib/typescript/src/utils/http/HTTPRequest.d.ts.map +1 -0
  66. package/lib/typescript/src/utils/http/HTTPResponse.d.ts +6 -0
  67. package/lib/typescript/src/utils/http/HTTPResponse.d.ts.map +1 -0
  68. package/lib/typescript/src/utils/types.d.ts +5 -0
  69. package/lib/typescript/src/utils/types.d.ts.map +1 -0
  70. package/package.json +1 -1
  71. package/src/core/Clix.ts +62 -115
  72. package/src/core/ClixConfig.ts +3 -3
  73. package/src/core/ClixInitCoordinator.ts +5 -17
  74. package/src/core/ClixNotification.ts +36 -37
  75. package/src/models/ClixDevice.ts +17 -25
  76. package/src/models/ClixPushNotificationPayload.ts +8 -37
  77. package/src/services/ClixAPIClient.ts +84 -144
  78. package/src/services/DeviceAPIService.ts +39 -47
  79. package/src/services/DeviceService.ts +122 -156
  80. package/src/services/EventAPIService.ts +3 -5
  81. package/src/services/EventService.ts +26 -33
  82. package/src/services/NotificationService.ts +318 -533
  83. package/src/services/TokenService.ts +4 -71
  84. package/src/utils/http/HTTPClient.ts +141 -0
  85. package/src/utils/http/HTTPMethod.ts +6 -0
  86. package/src/utils/http/HTTPRequest.ts +9 -0
  87. package/src/utils/http/HTTPResponse.ts +5 -0
  88. package/src/utils/types.ts +7 -0
@@ -1,82 +1,15 @@
1
- import { ClixLogger } from '../utils/logging/ClixLogger';
2
1
  import { StorageService } from './StorageService';
3
2
 
4
3
  export class TokenService {
5
- private static readonly CURRENT_TOKEN_KEY = 'clix_current_push_token';
6
- private static readonly PREVIOUS_TOKENS_KEY = 'clix_push_tokens';
7
- private static readonly MAX_TOKENS = 5;
4
+ private currentTokenKey = 'clix_current_push_token';
8
5
 
9
6
  constructor(private readonly storageService: StorageService) {}
10
7
 
11
8
  getCurrentToken(): string | undefined {
12
- try {
13
- return this.storageService.get<string>(TokenService.CURRENT_TOKEN_KEY);
14
- } catch (error) {
15
- ClixLogger.error('Failed to get current token', error);
16
- return undefined;
17
- }
9
+ return this.storageService.get<string>(this.currentTokenKey);
18
10
  }
19
11
 
20
- getPreviousTokens(): string[] {
21
- try {
22
- const result = this.storageService.get<string[]>(
23
- TokenService.PREVIOUS_TOKENS_KEY
24
- );
25
- if (result === undefined) return [];
26
-
27
- return Array.isArray(result) ? result : [];
28
- } catch (error) {
29
- ClixLogger.error('Failed to get previous tokens', error);
30
- return [];
31
- }
32
- }
33
-
34
- saveToken(token: string) {
35
- try {
36
- this.storageService.set(TokenService.CURRENT_TOKEN_KEY, token);
37
-
38
- let tokens = this.getPreviousTokens();
39
-
40
- // Remove existing token if present
41
- const currentIndex = tokens.indexOf(token);
42
- if (currentIndex !== -1) {
43
- tokens.splice(currentIndex, 1);
44
- }
45
-
46
- // Add new token
47
- tokens.push(token);
48
-
49
- // Keep only the last MAX_TOKENS
50
- if (tokens.length > TokenService.MAX_TOKENS) {
51
- tokens = tokens.slice(-TokenService.MAX_TOKENS);
52
- }
53
-
54
- this.storageService.set(TokenService.PREVIOUS_TOKENS_KEY, tokens);
55
- ClixLogger.debug('Token saved successfully');
56
- } catch (error) {
57
- ClixLogger.error('Failed to save token', error);
58
- throw error;
59
- }
60
- }
61
-
62
- clearTokens() {
63
- try {
64
- this.storageService.remove(TokenService.PREVIOUS_TOKENS_KEY);
65
- this.storageService.remove(TokenService.CURRENT_TOKEN_KEY);
66
- ClixLogger.debug('All tokens cleared');
67
- } catch (error) {
68
- ClixLogger.error('Failed to clear tokens', error);
69
- throw error;
70
- }
71
- }
72
-
73
- convertTokenToString(deviceToken: number[]): string {
74
- return deviceToken
75
- .map((data) => data.toString(16).padStart(2, '0'))
76
- .join('');
77
- }
78
-
79
- reset() {
80
- this.clearTokens();
12
+ saveToken(token: string): void {
13
+ this.storageService.set(this.currentTokenKey, token);
81
14
  }
82
15
  }
@@ -0,0 +1,141 @@
1
+ import { HTTPMethod } from './HTTPMethod';
2
+ import type { HTTPRequest } from './HTTPRequest';
3
+ import type { HTTPResponse } from './HTTPResponse';
4
+
5
+ export class HTTPClient {
6
+ static shared = new HTTPClient();
7
+
8
+ async request<T>(request: HTTPRequest): Promise<HTTPResponse<T>> {
9
+ const url = this.buildUrlWithParams(request.url, request.params);
10
+ const headers = { ...(request.headers || {}) };
11
+ const init: RequestInit = { method: request.method, headers };
12
+
13
+ const body = this.prepareBody(request.data);
14
+ if (
15
+ body !== undefined &&
16
+ request.method !== HTTPMethod.GET &&
17
+ request.method !== HTTPMethod.DELETE
18
+ ) {
19
+ init.body = body;
20
+ }
21
+
22
+ const response = await fetch(url, init);
23
+ const data = await this.parseResponse<T>(response);
24
+
25
+ return {
26
+ data,
27
+ statusCode: response.status,
28
+ headers: this.headersToRecord(response.headers),
29
+ };
30
+ }
31
+
32
+ async get<T>(
33
+ url: string,
34
+ params?: Record<string, any>,
35
+ headers?: Record<string, string>
36
+ ): Promise<HTTPResponse<T>> {
37
+ return this.request<T>({ method: HTTPMethod.GET, url, params, headers });
38
+ }
39
+
40
+ async post<T>(
41
+ url: string,
42
+ data?: any,
43
+ params?: Record<string, any>,
44
+ headers?: Record<string, string>
45
+ ): Promise<HTTPResponse<T>> {
46
+ return this.request<T>({
47
+ method: HTTPMethod.POST,
48
+ url,
49
+ data,
50
+ params,
51
+ headers,
52
+ });
53
+ }
54
+
55
+ async put<T>(
56
+ url: string,
57
+ data?: any,
58
+ params?: Record<string, any>,
59
+ headers?: Record<string, string>
60
+ ): Promise<HTTPResponse<T>> {
61
+ return this.request<T>({
62
+ method: HTTPMethod.PUT,
63
+ url,
64
+ data,
65
+ params,
66
+ headers,
67
+ });
68
+ }
69
+
70
+ async delete<T>(
71
+ url: string,
72
+ params?: Record<string, any>,
73
+ headers?: Record<string, string>
74
+ ): Promise<HTTPResponse<T>> {
75
+ return this.request<T>({ method: HTTPMethod.DELETE, url, params, headers });
76
+ }
77
+
78
+ private buildUrlWithParams(
79
+ url: string,
80
+ params?: Record<string, any>
81
+ ): string {
82
+ if (!params || Object.keys(params).length === 0) {
83
+ return url;
84
+ }
85
+
86
+ const queryString = Object.entries(params)
87
+ .flatMap(([key, value]) => {
88
+ if (value === null || value === undefined) {
89
+ return [];
90
+ }
91
+
92
+ if (Array.isArray(value)) {
93
+ return value
94
+ .filter((item) => item !== null && item !== undefined)
95
+ .map(
96
+ (item) =>
97
+ `${encodeURIComponent(key)}=${encodeURIComponent(String(item))}`
98
+ );
99
+ }
100
+
101
+ return `${encodeURIComponent(key)}=${encodeURIComponent(
102
+ String(value)
103
+ )}`;
104
+ })
105
+ .filter(Boolean)
106
+ .join('&');
107
+
108
+ if (!queryString) {
109
+ return url;
110
+ }
111
+
112
+ const separator = url.includes('?') ? '&' : '?';
113
+ return `${url}${separator}${queryString}`;
114
+ }
115
+
116
+ private prepareBody(data: any): BodyInit | undefined {
117
+ if (data === undefined || data === null) {
118
+ return undefined;
119
+ }
120
+
121
+ return JSON.stringify(data);
122
+ }
123
+
124
+ private async parseResponse<T>(response: Response): Promise<T> {
125
+ const contentType = response.headers.get('content-type');
126
+
127
+ if (contentType && contentType.includes('application/json')) {
128
+ return (await response.json()) as T;
129
+ }
130
+
131
+ return (await response.text()) as unknown as T;
132
+ }
133
+
134
+ private headersToRecord(headers: Headers): Record<string, string> {
135
+ const record: Record<string, string> = {};
136
+ headers.forEach((value: string, key: string) => {
137
+ record[key] = value;
138
+ });
139
+ return record;
140
+ }
141
+ }
@@ -0,0 +1,6 @@
1
+ export enum HTTPMethod {
2
+ GET = 'GET',
3
+ POST = 'POST',
4
+ PUT = 'PUT',
5
+ DELETE = 'DELETE',
6
+ }
@@ -0,0 +1,9 @@
1
+ import { HTTPMethod } from './HTTPMethod';
2
+
3
+ export interface HTTPRequest {
4
+ url: string;
5
+ method: HTTPMethod;
6
+ params?: Record<string, any>;
7
+ headers?: Record<string, string>;
8
+ data?: any;
9
+ }
@@ -0,0 +1,5 @@
1
+ export interface HTTPResponse<T> {
2
+ data: T;
3
+ statusCode: number;
4
+ headers: Record<string, string>;
5
+ }
@@ -0,0 +1,7 @@
1
+ export type Prettify<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & {};
4
+
5
+ export type PickPartial<T, K extends keyof T> = Prettify<
6
+ Omit<T, K> & Partial<Pick<T, K>>
7
+ >;