@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.
- 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,9 +1,10 @@
|
|
|
1
1
|
import { type NotificationSettings } from '@notifee/react-native';
|
|
2
2
|
import type {
|
|
3
3
|
BackgroundMessageHandler,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
ForegroundEventHandler,
|
|
5
|
+
MessageHandler,
|
|
6
|
+
NotificationOpenedAppHandler,
|
|
7
|
+
TokenRefreshHandler,
|
|
7
8
|
} from '../services/NotificationService';
|
|
8
9
|
import { ClixLogger } from '../utils/logging/ClixLogger';
|
|
9
10
|
import { Clix } from './Clix';
|
|
@@ -34,7 +35,7 @@ export class ClixNotification {
|
|
|
34
35
|
return;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
notificationService.
|
|
38
|
+
notificationService.autoHandleLandingUrl = autoHandleLandingURL;
|
|
38
39
|
|
|
39
40
|
if (autoRequestPermission) {
|
|
40
41
|
await notificationService.requestPermission();
|
|
@@ -44,32 +45,22 @@ export class ClixNotification {
|
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
async requestPermission(): Promise<NotificationSettings |
|
|
48
|
+
async requestPermission(): Promise<NotificationSettings | undefined> {
|
|
48
49
|
try {
|
|
49
50
|
await Clix.initCoordinator.waitForInitialization();
|
|
50
|
-
|
|
51
|
-
if (!notificationService) {
|
|
52
|
-
ClixLogger.warn('Notification service is not initialized');
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return await notificationService.requestPermission();
|
|
51
|
+
return await Clix.shared?.notificationService?.requestPermission();
|
|
57
52
|
} catch (error) {
|
|
58
53
|
ClixLogger.error('Failed to request notification permission', error);
|
|
59
|
-
return
|
|
54
|
+
return undefined;
|
|
60
55
|
}
|
|
61
56
|
}
|
|
62
57
|
|
|
63
58
|
async setPermissionGranted(isGranted: boolean): Promise<void> {
|
|
64
59
|
try {
|
|
65
60
|
await Clix.initCoordinator.waitForInitialization();
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
await notificationService.setPermissionGranted(isGranted);
|
|
61
|
+
return await Clix.shared?.notificationService?.setPermissionGranted(
|
|
62
|
+
isGranted
|
|
63
|
+
);
|
|
73
64
|
} catch (error) {
|
|
74
65
|
ClixLogger.error(
|
|
75
66
|
'Failed to update push permission status on server',
|
|
@@ -81,21 +72,14 @@ export class ClixNotification {
|
|
|
81
72
|
async getToken(): Promise<string | undefined> {
|
|
82
73
|
try {
|
|
83
74
|
await Clix.initCoordinator.waitForInitialization();
|
|
84
|
-
|
|
85
|
-
if (!notificationService) {
|
|
86
|
-
ClixLogger.warn('Notification service is not initialized');
|
|
87
|
-
return undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const token = await notificationService.getCurrentToken();
|
|
91
|
-
return token ?? undefined;
|
|
75
|
+
return Clix.shared?.tokenService?.getCurrentToken();
|
|
92
76
|
} catch (error) {
|
|
93
77
|
ClixLogger.error('Failed to get push token', error);
|
|
94
78
|
return undefined;
|
|
95
79
|
}
|
|
96
80
|
}
|
|
97
81
|
|
|
98
|
-
async onMessage(handler?:
|
|
82
|
+
async onMessage(handler?: MessageHandler): Promise<void> {
|
|
99
83
|
try {
|
|
100
84
|
await Clix.initCoordinator.waitForInitialization();
|
|
101
85
|
const notificationService = Clix.shared?.notificationService;
|
|
@@ -103,7 +87,7 @@ export class ClixNotification {
|
|
|
103
87
|
ClixLogger.warn('Notification service is not initialized');
|
|
104
88
|
return;
|
|
105
89
|
}
|
|
106
|
-
notificationService.
|
|
90
|
+
notificationService.messageHandler = handler;
|
|
107
91
|
} catch (error) {
|
|
108
92
|
ClixLogger.error('Failed to register onMessage handler', error);
|
|
109
93
|
}
|
|
@@ -117,14 +101,14 @@ export class ClixNotification {
|
|
|
117
101
|
ClixLogger.warn('Notification service is not initialized');
|
|
118
102
|
return;
|
|
119
103
|
}
|
|
120
|
-
notificationService.
|
|
104
|
+
notificationService.backgroundMessageHandler = handler;
|
|
121
105
|
} catch (error) {
|
|
122
106
|
ClixLogger.error('Failed to register onBackgroundMessage handler', error);
|
|
123
107
|
}
|
|
124
108
|
}
|
|
125
109
|
|
|
126
|
-
async
|
|
127
|
-
handler?:
|
|
110
|
+
async onNotificationOpenedApp(
|
|
111
|
+
handler?: NotificationOpenedAppHandler
|
|
128
112
|
): Promise<void> {
|
|
129
113
|
try {
|
|
130
114
|
await Clix.initCoordinator.waitForInitialization();
|
|
@@ -133,7 +117,7 @@ export class ClixNotification {
|
|
|
133
117
|
ClixLogger.warn('Notification service is not initialized');
|
|
134
118
|
return;
|
|
135
119
|
}
|
|
136
|
-
notificationService.
|
|
120
|
+
notificationService.notificationOpenedAppHandler = handler;
|
|
137
121
|
} catch (error) {
|
|
138
122
|
ClixLogger.error(
|
|
139
123
|
'Failed to register onNotificationOpened handler',
|
|
@@ -141,7 +125,22 @@ export class ClixNotification {
|
|
|
141
125
|
);
|
|
142
126
|
}
|
|
143
127
|
}
|
|
144
|
-
|
|
128
|
+
|
|
129
|
+
async onTokenRefresh(handler?: TokenRefreshHandler): Promise<void> {
|
|
130
|
+
try {
|
|
131
|
+
await Clix.initCoordinator.waitForInitialization();
|
|
132
|
+
const notificationService = Clix.shared?.notificationService;
|
|
133
|
+
if (!notificationService) {
|
|
134
|
+
ClixLogger.warn('Notification service is not initialized');
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
notificationService.tokenRefreshHandler = handler;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
ClixLogger.error('Failed to register onTokenRefresh handler', error);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async onForegroundEvent(handler?: ForegroundEventHandler): Promise<void> {
|
|
145
144
|
try {
|
|
146
145
|
await Clix.initCoordinator.waitForInitialization();
|
|
147
146
|
const notificationService = Clix.shared?.notificationService;
|
|
@@ -149,9 +148,9 @@ export class ClixNotification {
|
|
|
149
148
|
ClixLogger.warn('Notification service is not initialized');
|
|
150
149
|
return;
|
|
151
150
|
}
|
|
152
|
-
notificationService.
|
|
151
|
+
notificationService.foregroundEventHandler = handler;
|
|
153
152
|
} catch (error) {
|
|
154
|
-
ClixLogger.error('Failed to register
|
|
153
|
+
ClixLogger.error('Failed to register onForegroundEvent handler', error);
|
|
155
154
|
}
|
|
156
155
|
}
|
|
157
156
|
}
|
package/src/models/ClixDevice.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
export class ClixDevice {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
readonly id: string;
|
|
3
|
+
readonly platform: string;
|
|
4
|
+
readonly model: string;
|
|
5
|
+
readonly manufacturer: string;
|
|
6
|
+
readonly osName: string;
|
|
7
|
+
readonly osVersion: string;
|
|
8
|
+
readonly localeRegion: string;
|
|
9
|
+
readonly localeLanguage: string;
|
|
10
|
+
readonly timezone: string;
|
|
11
|
+
readonly appName: string;
|
|
12
|
+
readonly appVersion: string;
|
|
13
|
+
readonly sdkType: string;
|
|
14
|
+
readonly sdkVersion: string;
|
|
15
|
+
readonly adId?: string;
|
|
16
|
+
readonly isPushPermissionGranted: boolean;
|
|
17
|
+
readonly pushToken?: string;
|
|
18
|
+
readonly pushTokenType?: string;
|
|
19
19
|
|
|
20
20
|
constructor(device: {
|
|
21
21
|
id: string;
|
|
@@ -77,12 +77,4 @@ export class ClixDevice {
|
|
|
77
77
|
pushTokenType: updates.pushTokenType ?? this.pushTokenType,
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
equals(other: ClixDevice): boolean {
|
|
82
|
-
return this.id === other.id;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
toString(): string {
|
|
86
|
-
return `ClixDevice(id: ${this.id}, platform: ${this.platform}, model: ${this.model})`;
|
|
87
|
-
}
|
|
88
80
|
}
|
|
@@ -1,38 +1,9 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
public readonly customProperties?: Record<string, any>;
|
|
10
|
-
|
|
11
|
-
constructor(payload: {
|
|
12
|
-
messageId: string;
|
|
13
|
-
campaignId?: string;
|
|
14
|
-
userId?: string;
|
|
15
|
-
deviceId?: string;
|
|
16
|
-
trackingId?: string;
|
|
17
|
-
landingUrl?: string;
|
|
18
|
-
imageUrl?: string;
|
|
19
|
-
customProperties?: Record<string, any>;
|
|
20
|
-
}) {
|
|
21
|
-
this.messageId = payload.messageId;
|
|
22
|
-
this.campaignId = payload.campaignId;
|
|
23
|
-
this.userId = payload.userId;
|
|
24
|
-
this.deviceId = payload.deviceId;
|
|
25
|
-
this.trackingId = payload.trackingId;
|
|
26
|
-
this.landingUrl = payload.landingUrl;
|
|
27
|
-
this.imageUrl = payload.imageUrl;
|
|
28
|
-
this.customProperties = payload.customProperties;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
equals(other: ClixPushNotificationPayload): boolean {
|
|
32
|
-
return this.messageId === other.messageId;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
toString(): string {
|
|
36
|
-
return `ClixPushNotificationPayload(messageId: ${this.messageId}, campaignId: ${this.campaignId})`;
|
|
37
|
-
}
|
|
1
|
+
export interface ClixPushNotificationPayload {
|
|
2
|
+
messageId: string;
|
|
3
|
+
title: string;
|
|
4
|
+
body: string;
|
|
5
|
+
imageUrl?: string;
|
|
6
|
+
landingUrl?: string;
|
|
7
|
+
userJourneyId?: string;
|
|
8
|
+
userJourneyNodeId?: string;
|
|
38
9
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ClixConfig } from '../core/ClixConfig';
|
|
2
2
|
import { ClixVersion } from '../core/ClixVersion';
|
|
3
|
+
import { HTTPClient } from '../utils/http/HTTPClient';
|
|
4
|
+
import type { HTTPResponse } from '../utils/http/HTTPResponse';
|
|
3
5
|
import { ClixLogger } from '../utils/logging/ClixLogger';
|
|
4
6
|
|
|
5
7
|
export interface APIResponse {
|
|
@@ -9,23 +11,20 @@ export interface APIResponse {
|
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
export class ClixAPIClient {
|
|
12
|
-
private static readonly API_BASE_PATH = '/api/v1';
|
|
13
|
-
|
|
14
14
|
constructor(private readonly config: ClixConfig) {}
|
|
15
15
|
|
|
16
|
+
private static readonly API_BASE_PATH = '/api/v1';
|
|
17
|
+
|
|
16
18
|
private async getCommonHeaders(): Promise<Record<string, string>> {
|
|
17
19
|
const version = await ClixVersion.getVersion();
|
|
18
20
|
const headers: Record<string, string> = {
|
|
19
21
|
'Content-Type': 'application/json',
|
|
20
22
|
'X-Clix-Project-ID': this.config.projectId,
|
|
21
23
|
'X-Clix-API-Key': this.config.apiKey,
|
|
22
|
-
'User-Agent': `clix-react-native-sdk
|
|
24
|
+
'User-Agent': `clix-react-native-sdk@${version}`,
|
|
25
|
+
...this.config.extraHeaders,
|
|
23
26
|
};
|
|
24
27
|
|
|
25
|
-
if (this.config.extraHeaders) {
|
|
26
|
-
Object.assign(headers, this.config.extraHeaders);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
28
|
return headers;
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -37,212 +36,153 @@ export class ClixAPIClient {
|
|
|
37
36
|
return `${baseUrl}${ClixAPIClient.API_BASE_PATH}${fullPath}`;
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
|
|
39
|
+
async get<T>(
|
|
41
40
|
path: string,
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
params?: Record<string, any>,
|
|
42
|
+
headers?: Record<string, string>
|
|
43
|
+
): Promise<HTTPResponse<T>> {
|
|
44
44
|
const url = this.buildUrl(path);
|
|
45
|
-
|
|
46
|
-
if (queryParameters && Object.keys(queryParameters).length > 0) {
|
|
47
|
-
const searchParams = new URLSearchParams();
|
|
48
|
-
Object.entries(queryParameters).forEach(([key, value]) => {
|
|
49
|
-
if (value !== null && value !== undefined) {
|
|
50
|
-
searchParams.append(key, String(value));
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return `${url}?${searchParams.toString()}`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return url;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async get(
|
|
60
|
-
path: string,
|
|
61
|
-
options?: {
|
|
62
|
-
headers?: Record<string, string>;
|
|
63
|
-
queryParameters?: Record<string, any>;
|
|
64
|
-
}
|
|
65
|
-
): Promise<APIResponse> {
|
|
66
|
-
const url = this.buildUrlWithQuery(path, options?.queryParameters);
|
|
67
45
|
const commonHeaders = await this.getCommonHeaders();
|
|
68
46
|
const requestHeaders = {
|
|
69
47
|
...commonHeaders,
|
|
70
|
-
...
|
|
48
|
+
...headers,
|
|
71
49
|
};
|
|
72
50
|
|
|
73
51
|
ClixLogger.debug(`API GET ${path}`);
|
|
74
52
|
ClixLogger.debug(`Making request to: ${url}`);
|
|
53
|
+
if (params && Object.keys(params).length > 0) {
|
|
54
|
+
ClixLogger.debug(`Query Parameters: ${JSON.stringify(params)}`);
|
|
55
|
+
}
|
|
75
56
|
|
|
76
57
|
try {
|
|
77
|
-
const response = await
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const data = await this.parseResponse(response);
|
|
58
|
+
const response = await HTTPClient.shared.get<T>(
|
|
59
|
+
url,
|
|
60
|
+
params,
|
|
61
|
+
requestHeaders
|
|
62
|
+
);
|
|
83
63
|
|
|
84
|
-
ClixLogger.debug(`Response Status: ${response.
|
|
85
|
-
ClixLogger.debug(`Response Body: ${JSON.stringify(data)}`);
|
|
64
|
+
ClixLogger.debug(`Response Status: ${response.statusCode}`);
|
|
65
|
+
ClixLogger.debug(`Response Body: ${JSON.stringify(response.data)}`);
|
|
86
66
|
|
|
87
|
-
return
|
|
88
|
-
status: response.status,
|
|
89
|
-
data,
|
|
90
|
-
headers: this.headersToRecord(response.headers),
|
|
91
|
-
};
|
|
67
|
+
return response;
|
|
92
68
|
} catch (error) {
|
|
93
69
|
ClixLogger.error(`GET ${path} failed`, error);
|
|
94
70
|
throw error;
|
|
95
71
|
}
|
|
96
72
|
}
|
|
97
73
|
|
|
98
|
-
async post(
|
|
74
|
+
async post<T>(
|
|
99
75
|
path: string,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
): Promise<APIResponse> {
|
|
106
|
-
const url = this.buildUrlWithQuery(path, options?.queryParameters);
|
|
76
|
+
data?: any,
|
|
77
|
+
params?: Record<string, any>,
|
|
78
|
+
headers?: Record<string, string>
|
|
79
|
+
): Promise<HTTPResponse<T>> {
|
|
80
|
+
const url = this.buildUrl(path);
|
|
107
81
|
const commonHeaders = await this.getCommonHeaders();
|
|
108
82
|
const requestHeaders = {
|
|
109
83
|
...commonHeaders,
|
|
110
|
-
...
|
|
84
|
+
...headers,
|
|
111
85
|
};
|
|
112
86
|
|
|
113
87
|
ClixLogger.debug(`API POST ${path}`);
|
|
114
|
-
|
|
115
|
-
|
|
88
|
+
ClixLogger.debug(`Making request to: ${url}`);
|
|
89
|
+
if (data) {
|
|
90
|
+
ClixLogger.debug(`Request Body: ${JSON.stringify(data)}`);
|
|
116
91
|
}
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
Object.keys(options.queryParameters).length > 0
|
|
120
|
-
) {
|
|
121
|
-
ClixLogger.debug(
|
|
122
|
-
`Query Parameters: ${JSON.stringify(options.queryParameters)}`
|
|
123
|
-
);
|
|
92
|
+
if (params && Object.keys(params).length > 0) {
|
|
93
|
+
ClixLogger.debug(`Query Parameters: ${JSON.stringify(params)}`);
|
|
124
94
|
}
|
|
125
95
|
|
|
126
|
-
const body = options?.body ? JSON.stringify(options.body) : undefined;
|
|
127
|
-
|
|
128
96
|
try {
|
|
129
|
-
const response = await
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const data = await this.parseResponse(response);
|
|
97
|
+
const response = await HTTPClient.shared.post<T>(
|
|
98
|
+
url,
|
|
99
|
+
data,
|
|
100
|
+
params,
|
|
101
|
+
requestHeaders
|
|
102
|
+
);
|
|
136
103
|
|
|
137
|
-
ClixLogger.debug(`Response Status: ${response.
|
|
138
|
-
ClixLogger.debug(`Response Body: ${JSON.stringify(data)}`);
|
|
104
|
+
ClixLogger.debug(`Response Status: ${response.statusCode}`);
|
|
105
|
+
ClixLogger.debug(`Response Body: ${JSON.stringify(response.data)}`);
|
|
139
106
|
|
|
140
|
-
return
|
|
141
|
-
status: response.status,
|
|
142
|
-
data,
|
|
143
|
-
headers: this.headersToRecord(response.headers),
|
|
144
|
-
};
|
|
107
|
+
return response;
|
|
145
108
|
} catch (error) {
|
|
146
109
|
ClixLogger.error(`POST ${path} failed`, error);
|
|
147
110
|
throw error;
|
|
148
111
|
}
|
|
149
112
|
}
|
|
150
113
|
|
|
151
|
-
async put(
|
|
114
|
+
async put<T>(
|
|
152
115
|
path: string,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
): Promise<APIResponse> {
|
|
159
|
-
const url = this.buildUrlWithQuery(path, options?.queryParameters);
|
|
116
|
+
data?: any,
|
|
117
|
+
params?: Record<string, any>,
|
|
118
|
+
headers?: Record<string, string>
|
|
119
|
+
): Promise<HTTPResponse<T>> {
|
|
120
|
+
const url = this.buildUrl(path);
|
|
160
121
|
const commonHeaders = await this.getCommonHeaders();
|
|
161
122
|
const requestHeaders = {
|
|
162
123
|
...commonHeaders,
|
|
163
|
-
...
|
|
124
|
+
...headers,
|
|
164
125
|
};
|
|
165
126
|
|
|
166
127
|
ClixLogger.debug(`API PUT ${path}`);
|
|
167
|
-
|
|
168
|
-
|
|
128
|
+
ClixLogger.debug(`Making request to: ${url}`);
|
|
129
|
+
if (data) {
|
|
130
|
+
ClixLogger.debug(`Request Body: ${JSON.stringify(data)}`);
|
|
131
|
+
}
|
|
132
|
+
if (params && Object.keys(params).length > 0) {
|
|
133
|
+
ClixLogger.debug(`Query Parameters: ${JSON.stringify(params)}`);
|
|
134
|
+
}
|
|
169
135
|
|
|
170
136
|
try {
|
|
171
|
-
const response = await
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const data = await this.parseResponse(response);
|
|
137
|
+
const response = await HTTPClient.shared.put<T>(
|
|
138
|
+
url,
|
|
139
|
+
data,
|
|
140
|
+
params,
|
|
141
|
+
requestHeaders
|
|
142
|
+
);
|
|
178
143
|
|
|
179
|
-
ClixLogger.debug(`Response Status: ${response.
|
|
180
|
-
ClixLogger.debug(`Response Body: ${JSON.stringify(data)}`);
|
|
144
|
+
ClixLogger.debug(`Response Status: ${response.statusCode}`);
|
|
145
|
+
ClixLogger.debug(`Response Body: ${JSON.stringify(response.data)}`);
|
|
181
146
|
|
|
182
|
-
return
|
|
183
|
-
status: response.status,
|
|
184
|
-
data,
|
|
185
|
-
headers: this.headersToRecord(response.headers),
|
|
186
|
-
};
|
|
147
|
+
return response;
|
|
187
148
|
} catch (error) {
|
|
188
149
|
ClixLogger.error(`PUT ${path} failed`, error);
|
|
189
150
|
throw error;
|
|
190
151
|
}
|
|
191
152
|
}
|
|
192
153
|
|
|
193
|
-
async delete(
|
|
154
|
+
async delete<T>(
|
|
194
155
|
path: string,
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
): Promise<APIResponse> {
|
|
200
|
-
const url = this.buildUrlWithQuery(path, options?.queryParameters);
|
|
156
|
+
params?: Record<string, any>,
|
|
157
|
+
headers?: Record<string, string>
|
|
158
|
+
): Promise<HTTPResponse<T>> {
|
|
159
|
+
const url = this.buildUrl(path);
|
|
201
160
|
const commonHeaders = await this.getCommonHeaders();
|
|
202
161
|
const requestHeaders = {
|
|
203
162
|
...commonHeaders,
|
|
204
|
-
...
|
|
163
|
+
...headers,
|
|
205
164
|
};
|
|
206
165
|
|
|
207
166
|
ClixLogger.debug(`API DELETE ${path}`);
|
|
167
|
+
ClixLogger.debug(`Making request to: ${url}`);
|
|
168
|
+
if (params && Object.keys(params).length > 0) {
|
|
169
|
+
ClixLogger.debug(`Query Parameters: ${JSON.stringify(params)}`);
|
|
170
|
+
}
|
|
208
171
|
|
|
209
172
|
try {
|
|
210
|
-
const response = await
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const data = await this.parseResponse(response);
|
|
173
|
+
const response = await HTTPClient.shared.delete<T>(
|
|
174
|
+
url,
|
|
175
|
+
params,
|
|
176
|
+
requestHeaders
|
|
177
|
+
);
|
|
216
178
|
|
|
217
|
-
ClixLogger.debug(`Response Status: ${response.
|
|
218
|
-
ClixLogger.debug(`Response Body: ${JSON.stringify(data)}`);
|
|
179
|
+
ClixLogger.debug(`Response Status: ${response.statusCode}`);
|
|
180
|
+
ClixLogger.debug(`Response Body: ${JSON.stringify(response.data)}`);
|
|
219
181
|
|
|
220
|
-
return
|
|
221
|
-
status: response.status,
|
|
222
|
-
data,
|
|
223
|
-
headers: this.headersToRecord(response.headers),
|
|
224
|
-
};
|
|
182
|
+
return response;
|
|
225
183
|
} catch (error) {
|
|
226
184
|
ClixLogger.error(`DELETE ${path} failed`, error);
|
|
227
185
|
throw error;
|
|
228
186
|
}
|
|
229
187
|
}
|
|
230
|
-
|
|
231
|
-
private async parseResponse(response: Response): Promise<any> {
|
|
232
|
-
const contentType = response.headers.get('content-type');
|
|
233
|
-
|
|
234
|
-
if (contentType && contentType.includes('application/json')) {
|
|
235
|
-
return await response.json();
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return await response.text();
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
private headersToRecord(headers: Headers): Record<string, string> {
|
|
242
|
-
const record: Record<string, string> = {};
|
|
243
|
-
headers.forEach((value: any, key: string) => {
|
|
244
|
-
record[key] = value;
|
|
245
|
-
});
|
|
246
|
-
return record;
|
|
247
|
-
}
|
|
248
188
|
}
|