rn-linkrunner 0.5.2 → 0.6.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/src/index.ts CHANGED
@@ -1,64 +1,53 @@
1
1
  import { Linking } from 'react-native';
2
- import DeviceInfo, {
3
- getManufacturer,
4
- getSystemVersion,
5
- } from 'react-native-device-info';
2
+ import DeviceInfo from 'react-native-device-info';
3
+ import { device_data } from './helper';
4
+ import type { TriggerConfig, UserData } from './types';
6
5
 
7
- const package_version = '0.5.2';
6
+ const package_version = '0.6.0';
8
7
  const app_version: string = DeviceInfo.getVersion();
9
8
 
10
- const device_data = {
11
- android_id: DeviceInfo.getAndroidId(),
12
- api_level: DeviceInfo.getApiLevel(),
13
- application_name: DeviceInfo.getApplicationName(),
14
- base_os: DeviceInfo.getBaseOs(),
15
- build_id: DeviceInfo.getBuildId(),
16
- brand: DeviceInfo.getBrand(),
17
- build_number: DeviceInfo.getBuildNumber(),
18
- bundle_id: DeviceInfo.getBundleId(),
19
- carrier: DeviceInfo.getCarrier(),
20
- device: DeviceInfo.getDevice(),
21
- device_id: DeviceInfo.getDeviceId(),
22
- device_type: DeviceInfo.getDeviceType(),
23
- device_name: DeviceInfo.getDeviceName(),
24
- device_token: DeviceInfo.getDeviceToken(),
25
- device_ip: DeviceInfo.getIpAddress(),
26
- install_ref: DeviceInfo.getInstallReferrer(),
27
- manufacturer: getManufacturer(),
28
- system_version: getSystemVersion(),
29
- version: DeviceInfo.getVersion(),
30
- };
31
-
32
9
  const baseUrl = 'https://api.linkrunner.io';
33
10
 
34
- interface UserData {
35
- id: string;
36
- name?: string;
37
- phone?: string;
38
- email?: string;
39
- }
11
+ const initApiCall = async (
12
+ token: string,
13
+ source: 'GENERAL' | 'ADS',
14
+ link?: string
15
+ ) => {
16
+ try {
17
+ const fetch_result = await fetch(baseUrl + '/api/client/init', {
18
+ method: 'POST',
19
+ headers: {
20
+ 'Accept': 'application/json',
21
+ 'Content-Type': 'application/json',
22
+ },
23
+ body: JSON.stringify({
24
+ token,
25
+ package_version,
26
+ app_version,
27
+ device_data: await device_data(),
28
+ platform: 'REACT_NATIVE',
29
+ source,
30
+ link,
31
+ }),
32
+ });
40
33
 
41
- interface TriggerConfig {
42
- trigger_deeplink?: boolean;
43
- }
34
+ const result = await fetch_result.json();
44
35
 
45
- export type Response = {
46
- ip_location_data: IPLocationData;
47
- deeplink: string;
48
- root_domain: boolean;
49
- };
36
+ if (result?.status !== 200 && result?.status !== 201) {
37
+ throw new Error(result?.msg);
38
+ }
50
39
 
51
- export interface IPLocationData {
52
- ip: string;
53
- city: string;
54
- countryLong: string;
55
- countryShort: string;
56
- latitude: number;
57
- longitude: number;
58
- region: string;
59
- timeZone: string;
60
- zipCode: string;
61
- }
40
+ if (__DEV__) {
41
+ console.log('Linkrunner initialised successfully 🔥');
42
+
43
+ console.log('init response > ', result);
44
+ }
45
+
46
+ return result?.data;
47
+ } catch (error) {
48
+ console.error('Error initializing linkrunner', error);
49
+ }
50
+ };
62
51
 
63
52
  class Linkrunner {
64
53
  private token: string | null;
@@ -67,45 +56,15 @@ class Linkrunner {
67
56
  this.token = null;
68
57
  }
69
58
 
70
- async init(token: string): Promise<void | Response> {
59
+ async init(token: string): Promise<void | LRInitResponse> {
71
60
  if (!token) {
72
61
  console.error('Linkrunner needs your project token to initialize!');
73
62
  return;
74
63
  }
75
64
 
76
65
  this.token = token;
77
- try {
78
- const fetch_result = await fetch(baseUrl + '/api/client/init', {
79
- method: 'POST',
80
- headers: {
81
- 'Accept': 'application/json',
82
- 'Content-Type': 'application/json',
83
- },
84
- body: JSON.stringify({
85
- token,
86
- package_version,
87
- app_version,
88
- device_data,
89
- platform: 'REACT_NATIVE',
90
- }),
91
- });
92
-
93
- const result = await fetch_result.json();
94
-
95
- // if (!result) throw new Error('No response obtained!');
96
66
 
97
- if (result?.status !== 200 && result?.status !== 201) {
98
- throw new Error(result?.msg);
99
- }
100
-
101
- if (__DEV__) {
102
- console.log('Linkrunner initialised successfully 🔥');
103
- }
104
-
105
- return result?.data;
106
- } catch (error) {
107
- console.error('Error initializing linkrunner');
108
- }
67
+ return await initApiCall(token, 'GENERAL');
109
68
  }
110
69
 
111
70
  async trigger({
@@ -114,9 +73,9 @@ class Linkrunner {
114
73
  config,
115
74
  }: {
116
75
  config?: TriggerConfig;
117
- data: any;
76
+ data?: { [key: string]: any };
118
77
  user_data: UserData;
119
- }): Promise<void | Response> {
78
+ }): Promise<void | LRTriggerResponse> {
120
79
  if (!this.token) {
121
80
  console.error('Linkrunner: Trigger failed, token not initialized');
122
81
  return;
@@ -135,7 +94,7 @@ class Linkrunner {
135
94
  platform: 'REACT_NATIVE',
136
95
  data: {
137
96
  ...data,
138
- device_data,
97
+ device_data: await device_data(),
139
98
  },
140
99
  }),
141
100
  });
@@ -167,7 +126,14 @@ class Linkrunner {
167
126
  token: this.token,
168
127
  }),
169
128
  })
170
- .then((res) => res.json())
129
+ .then(() => {
130
+ if (__DEV__) {
131
+ console.log(
132
+ 'Linkrunner: Deeplink triggered successfully',
133
+ result?.data?.deeplink
134
+ );
135
+ }
136
+ })
171
137
  .catch(() => {});
172
138
  });
173
139
  }
@@ -182,8 +148,127 @@ class Linkrunner {
182
148
  console.error('Linkrunner: ', err.message);
183
149
  }
184
150
  }
151
+
152
+ async capturePayment({
153
+ amount,
154
+ userId,
155
+ paymentId,
156
+ }: {
157
+ paymentId?: string;
158
+ userId: string;
159
+ amount: number;
160
+ }) {
161
+ if (!this.token) {
162
+ console.error(
163
+ 'Linkrunner: Capture payment failed, token not initialized'
164
+ );
165
+ return;
166
+ }
167
+
168
+ try {
169
+ const response = await fetch(baseUrl + '/api/client/capture-payment', {
170
+ method: 'POST',
171
+ headers: {
172
+ 'Accept': 'application/json',
173
+ 'Content-Type': 'application/json',
174
+ },
175
+ body: JSON.stringify({
176
+ token: this.token,
177
+ user_id: userId,
178
+ platform: 'REACT_NATIVE',
179
+ data: {
180
+ device_data: await device_data(),
181
+ },
182
+ amount,
183
+ payment_id: paymentId,
184
+ }),
185
+ });
186
+
187
+ const result = await response.json();
188
+
189
+ if (result?.status !== 200 && result?.status !== 201) {
190
+ console.error('Linkrunner: Capture payment failed');
191
+ console.error('Linkrunner: ', result?.msg);
192
+ return;
193
+ }
194
+
195
+ if (__DEV__) {
196
+ console.log('Linkrunner: Payment captured successfully 💸', {
197
+ amount,
198
+ paymentId,
199
+ userId,
200
+ });
201
+ }
202
+ } catch (error) {
203
+ console.error('Linkrunner: Payment capturing failed!');
204
+ return;
205
+ }
206
+ }
207
+
208
+ async removePayment({
209
+ userId,
210
+ paymentId,
211
+ }: {
212
+ paymentId?: string;
213
+ userId: string;
214
+ }) {
215
+ if (!this.token) {
216
+ console.error('Linkrunner: Remove payment failed, token not initialized');
217
+ return;
218
+ }
219
+
220
+ if (!paymentId && !userId) {
221
+ return console.error(
222
+ 'Linkrunner: Either paymentId or userId must be provided!'
223
+ );
224
+ }
225
+
226
+ try {
227
+ const response = await fetch(
228
+ baseUrl + '/api/client/remove-captured-payment',
229
+ {
230
+ method: 'POST',
231
+ headers: {
232
+ 'Accept': 'application/json',
233
+ 'Content-Type': 'application/json',
234
+ },
235
+ body: JSON.stringify({
236
+ token: this.token,
237
+ user_id: userId,
238
+ platform: 'REACT_NATIVE',
239
+ data: {
240
+ device_data: await device_data(),
241
+ },
242
+ payment_id: paymentId,
243
+ }),
244
+ }
245
+ );
246
+
247
+ const result = await response.json();
248
+
249
+ if (result?.status !== 200 && result?.status !== 201) {
250
+ console.error('Linkrunner: Capture payment failed');
251
+ console.error('Linkrunner: ', result?.msg);
252
+ return;
253
+ }
254
+
255
+ if (__DEV__) {
256
+ console.log('Linkrunner: Payment entry removed successfully!', {
257
+ paymentId,
258
+ userId,
259
+ });
260
+ }
261
+ } catch (error) {
262
+ console.error('Linkrunner: Payment capturing failed!');
263
+ return;
264
+ }
265
+ }
185
266
  }
186
267
 
187
268
  const linkrunner = new Linkrunner();
188
269
 
270
+ export type LRInitResponse = Response;
271
+
272
+ export type LRTriggerResponse = Response;
273
+
189
274
  export default linkrunner;
package/src/types.ts ADDED
@@ -0,0 +1,29 @@
1
+ export interface LRIPLocationData {
2
+ ip: string;
3
+ city: string;
4
+ countryLong: string;
5
+ countryShort: string;
6
+ latitude: number;
7
+ longitude: number;
8
+ region: string;
9
+ timeZone: string;
10
+ zipCode: string;
11
+ }
12
+
13
+ export interface UserData {
14
+ id: string;
15
+ name?: string;
16
+ phone?: string;
17
+ email?: string;
18
+ }
19
+
20
+ export interface TriggerConfig {
21
+ trigger_deeplink?: boolean;
22
+ }
23
+
24
+ export type Response = {
25
+ ip_location_data: LRIPLocationData;
26
+ deeplink: string;
27
+ root_domain: boolean;
28
+ trigger?: boolean;
29
+ };