@clix-so/react-native-sdk 1.0.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/lib/module/core/Clix.js +40 -95
- package/lib/module/core/Clix.js.map +1 -1
- package/lib/module/core/ClixInitCoordinator.js +3 -14
- package/lib/module/core/ClixInitCoordinator.js.map +1 -1
- package/lib/module/core/ClixNotification.js +25 -28
- package/lib/module/core/ClixNotification.js.map +1 -1
- package/lib/module/models/ClixDevice.js +0 -6
- package/lib/module/models/ClixDevice.js.map +1 -1
- package/lib/module/models/ClixPushNotificationPayload.js +0 -19
- package/lib/module/models/ClixPushNotificationPayload.js.map +1 -1
- package/lib/module/services/ClixAPIClient.js +50 -99
- package/lib/module/services/ClixAPIClient.js.map +1 -1
- package/lib/module/services/DeviceAPIService.js +37 -45
- package/lib/module/services/DeviceAPIService.js.map +1 -1
- package/lib/module/services/DeviceService.js +97 -116
- package/lib/module/services/DeviceService.js.map +1 -1
- package/lib/module/services/EventAPIService.js +3 -5
- package/lib/module/services/EventAPIService.js.map +1 -1
- package/lib/module/services/EventService.js +13 -20
- package/lib/module/services/EventService.js.map +1 -1
- package/lib/module/services/NotificationService.js +252 -402
- package/lib/module/services/NotificationService.js.map +1 -1
- package/lib/module/services/TokenService.js +3 -59
- package/lib/module/services/TokenService.js.map +1 -1
- package/lib/module/utils/http/HTTPClient.js +101 -0
- package/lib/module/utils/http/HTTPClient.js.map +1 -0
- package/lib/module/utils/http/HTTPMethod.js +10 -0
- package/lib/module/utils/http/HTTPMethod.js.map +1 -0
- package/lib/module/utils/http/HTTPRequest.js +4 -0
- package/lib/module/utils/http/HTTPRequest.js.map +1 -0
- package/lib/module/utils/http/HTTPResponse.js +2 -0
- package/lib/module/utils/http/HTTPResponse.js.map +1 -0
- package/lib/module/utils/types.js +2 -0
- package/lib/module/utils/types.js.map +1 -0
- package/lib/typescript/src/core/Clix.d.ts +13 -15
- package/lib/typescript/src/core/Clix.d.ts.map +1 -1
- package/lib/typescript/src/core/ClixConfig.d.ts +3 -3
- package/lib/typescript/src/core/ClixConfig.d.ts.map +1 -1
- package/lib/typescript/src/core/ClixInitCoordinator.d.ts +0 -3
- package/lib/typescript/src/core/ClixInitCoordinator.d.ts.map +1 -1
- package/lib/typescript/src/core/ClixNotification.d.ts +6 -5
- package/lib/typescript/src/core/ClixNotification.d.ts.map +1 -1
- package/lib/typescript/src/models/ClixDevice.d.ts +0 -2
- package/lib/typescript/src/models/ClixDevice.d.ts.map +1 -1
- package/lib/typescript/src/models/ClixPushNotificationPayload.d.ts +8 -21
- package/lib/typescript/src/models/ClixPushNotificationPayload.d.ts.map +1 -1
- package/lib/typescript/src/services/ClixAPIClient.d.ts +6 -22
- package/lib/typescript/src/services/ClixAPIClient.d.ts.map +1 -1
- package/lib/typescript/src/services/DeviceAPIService.d.ts +1 -1
- package/lib/typescript/src/services/DeviceAPIService.d.ts.map +1 -1
- package/lib/typescript/src/services/DeviceService.d.ts +10 -5
- package/lib/typescript/src/services/DeviceService.d.ts.map +1 -1
- package/lib/typescript/src/services/EventAPIService.d.ts.map +1 -1
- package/lib/typescript/src/services/EventService.d.ts +1 -0
- package/lib/typescript/src/services/EventService.d.ts.map +1 -1
- package/lib/typescript/src/services/NotificationService.d.ts +50 -57
- package/lib/typescript/src/services/NotificationService.d.ts.map +1 -1
- package/lib/typescript/src/services/TokenService.d.ts +1 -7
- package/lib/typescript/src/services/TokenService.d.ts.map +1 -1
- package/lib/typescript/src/utils/http/HTTPClient.d.ts +15 -0
- package/lib/typescript/src/utils/http/HTTPClient.d.ts.map +1 -0
- package/lib/typescript/src/utils/http/HTTPMethod.d.ts +7 -0
- package/lib/typescript/src/utils/http/HTTPMethod.d.ts.map +1 -0
- package/lib/typescript/src/utils/http/HTTPRequest.d.ts +9 -0
- package/lib/typescript/src/utils/http/HTTPRequest.d.ts.map +1 -0
- package/lib/typescript/src/utils/http/HTTPResponse.d.ts +6 -0
- package/lib/typescript/src/utils/http/HTTPResponse.d.ts.map +1 -0
- package/lib/typescript/src/utils/types.d.ts +5 -0
- package/lib/typescript/src/utils/types.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/core/Clix.ts +62 -115
- package/src/core/ClixConfig.ts +3 -3
- package/src/core/ClixInitCoordinator.ts +5 -17
- package/src/core/ClixNotification.ts +36 -37
- package/src/models/ClixDevice.ts +17 -25
- package/src/models/ClixPushNotificationPayload.ts +8 -37
- package/src/services/ClixAPIClient.ts +84 -144
- package/src/services/DeviceAPIService.ts +39 -47
- package/src/services/DeviceService.ts +122 -156
- package/src/services/EventAPIService.ts +3 -5
- package/src/services/EventService.ts +26 -33
- package/src/services/NotificationService.ts +318 -533
- package/src/services/TokenService.ts +4 -71
- package/src/utils/http/HTTPClient.ts +141 -0
- package/src/utils/http/HTTPMethod.ts +6 -0
- package/src/utils/http/HTTPRequest.ts +9 -0
- package/src/utils/http/HTTPResponse.ts +5 -0
- 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
|
|
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
|
-
|
|
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
|
-
|
|
21
|
-
|
|
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
|
+
}
|