Skip to content

Commit 34f68c9

Browse files
JoRoRadarmai-radar
andauthored
add fraud product support (#205)
* add fraud product support Add get/set methods for product type and add product header to verify calls. * rename function 'setProduct' * move product out of fraud * refactor * refactor --------- Co-authored-by: mai-radar <mai.nguyen@radar.com>
1 parent b785a4d commit 34f68c9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ class Radar {
190190
return ConversionsAPI.logConversion(params);
191191
}
192192

193+
public static setProduct(product?: string) {
194+
if (!product) {
195+
Storage.removeItem(Storage.PRODUCT);
196+
return;
197+
}
198+
Storage.setItem(Storage.PRODUCT, String(product).trim());
199+
}
200+
193201
///////////////////////
194202
// Listeners
195203
///////////////////////

src/api/track.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import { ping } from '../util/net';
1212

1313
import type { RadarTrackParams, RadarTrackResponse, RadarTrackVerifiedResponse } from '../types';
1414

15+
type TrackRequestHeaders = {
16+
'X-Radar-Body-Is-Token'?: string;
17+
'X-Radar-Product'?: string;
18+
}
19+
1520
class TrackAPI {
1621
static async trackOnce(params: RadarTrackParams) {
1722
const options = Config.get();
@@ -66,6 +71,13 @@ class TrackAPI {
6671
tripOptions.version = '2';
6772
}
6873

74+
const headers: TrackRequestHeaders = {}
75+
76+
const product = Storage.getItem(Storage.PRODUCT)
77+
if (product) {
78+
headers['X-Radar-Product'] = product
79+
}
80+
6981
const body = {
7082
...params,
7183
locationAuthorization,
@@ -117,6 +129,7 @@ class TrackAPI {
117129
};
118130

119131
const reqToken = await signJWT(payload, dk);
132+
headers['X-Radar-Body-Is-Token'] = 'true'
120133

121134
response = await Http.request({
122135
host,
@@ -125,9 +138,7 @@ class TrackAPI {
125138
data: {
126139
token: reqToken,
127140
},
128-
headers: {
129-
'X-Radar-Body-Is-Token': 'true',
130-
},
141+
headers,
131142
});
132143

133144
let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
@@ -159,6 +170,7 @@ class TrackAPI {
159170
method: 'POST',
160171
path: 'track',
161172
data: body,
173+
headers,
162174
});
163175

164176
const { user, events } = response;

src/storage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Storage {
2828
return 'radar-trip-options';
2929
}
3030

31+
public static get PRODUCT() {
32+
return 'radar-product';
33+
}
34+
3135
private static getStorage() {
3236
const storage = window?.localStorage;
3337
if (!storage) {

0 commit comments

Comments
 (0)