Skip to content

Commit

Permalink
Merge branch 'master' into jasonliu/add-spell-check-precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspk06 committed Oct 31, 2024
2 parents 9c6392e + de85938 commit 484ab66
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 79 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });
Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<script src="https://js.radar.com/v4.4.6-beta.1/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.6-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.6-beta.1/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.6-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.6-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.6-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.6-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.4.4",
"version": "4.4.6-beta.1",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
9 changes: 5 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
RadarSearchPlacesParams,
RadarStartTrackingVerifiedParams,
RadarTrackParams,
RadarTrackVerifiedParams,
RadarTrackVerifiedResponse,
RadarTripOptions,
RadarValidateAddressParams,
Expand Down Expand Up @@ -75,7 +76,7 @@ class Radar {

Logger.info(`initialized with ${live ? 'live' : 'test'} publishableKey.`);
if (options.debug) {
Logger.info(`using options: ${JSON.stringify(options)}`);
Logger.debug('using options', options);
}

// NOTE(jasonl): this allows us to run jest tests
Expand Down Expand Up @@ -129,7 +130,7 @@ class Radar {
}
}

public static trackVerified(params: RadarTrackParams = {}) {
public static trackVerified(params: RadarTrackVerifiedParams = {}) {
return VerifyAPI.trackVerified(params);
}

Expand All @@ -141,8 +142,8 @@ class Radar {
return VerifyAPI.stopTrackingVerified();
}

public static getVerifiedLocationToken() {
return VerifyAPI.getVerifiedLocationToken();
public static getVerifiedLocationToken(params: RadarTrackVerifiedParams = {}) {
return VerifyAPI.getVerifiedLocationToken(params);
}

public static setExpectedJurisdiction(countryCode?: string, stateCode?: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class TrackAPI {

let sclVal = -1;
let cslVal = -1;
/*
try {
const [sclRes, csl] = await Promise.all([
Http.request({
Expand All @@ -126,6 +127,7 @@ class TrackAPI {
} catch (err) {
// do nothing, send scl = -1 and csl = -1
}
*/

const payload = {
payload: JSON.stringify({
Expand Down
133 changes: 73 additions & 60 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Http from '../http';
import Logger from '../logger';
import Session from '../session';
import Storage from '../storage';
import TrackAPI from './track';

import type { RadarStartTrackingVerifiedParams, RadarTrackParams, RadarTrackVerifiedResponse } from '../types';
import type { RadarStartTrackingVerifiedParams, RadarTrackVerifiedParams, RadarTrackVerifiedResponse } from '../types';

let tokenTimeoutId: any | null = null;
let tokenCallback: ((token: RadarTrackVerifiedResponse) => void) | null = null;
Expand All @@ -16,13 +17,15 @@ let expectedCountryCode: string | null = null;
let expectedStateCode: string | null = null;

class VerifyAPI {
static async trackVerified(params: RadarTrackParams, encrypted: Boolean = false) {
static async trackVerified(params: RadarTrackVerifiedParams, encrypted: Boolean = false) {
const options = Config.get();

const { skipVerifyApp } = params;

// user indentification fields
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();
const deviceId = Device.getDeviceId();
const installId = Device.getInstallId();
const sessionId = Session.getSessionId();
const description = params.description || Storage.getItem(Storage.DESCRIPTION);

Expand All @@ -36,59 +39,69 @@ class VerifyAPI {
// other info
const metadata = params.metadata || Storage.getJSON(Storage.METADATA);

const body = {
...params,
description,
deviceId,
foreground: true,
installId,
sessionId,
metadata,
sdkVersion: SDK_VERSION,
stopped: true,
userId,
encrypted,
expectedCountryCode,
expectedStateCode,
};

let userAgent = navigator.userAgent;
const apple = userAgent && (userAgent.toLowerCase().includes('mac') || userAgent.toLowerCase().includes('iphone') || userAgent.toLowerCase().includes('ipod') || userAgent.toLowerCase().includes('ipad'));

const response: any = await Http.request({
method: 'GET',
path: 'verify',
data: body,
host: apple ? 'https://radar-verify.com:52516' : 'http://localhost:52516',
});

let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
let location;
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
latitude: user.location.coordinates[1],
longitude: user.location.coordinates[0],
accuracy: user.locationAccuracy,
let trackRes: RadarTrackVerifiedResponse;
if (skipVerifyApp) {
trackRes = await TrackAPI.trackOnce({
userId: userId ?? undefined,
description: description ?? undefined,
metadata: metadata,
fraud: true,
});
} else {
const body = {
...params,
description,
deviceId,
foreground: true,
installId,
sessionId,
metadata,
sdkVersion: SDK_VERSION,
stopped: true,
userId,
encrypted,
expectedCountryCode,
expectedStateCode,
};
}
if (expiresAt) {
expiresAt = new Date(expiresAt);
}

const trackRes = {
user,
events,
location,
token,
expiresAt,
expiresIn,
passed,
failureReasons,
_id,
} as RadarTrackVerifiedResponse;

if (options.debug) {
trackRes.response = response;

let userAgent = navigator.userAgent;
const apple = userAgent && (userAgent.toLowerCase().includes('mac') || userAgent.toLowerCase().includes('iphone') || userAgent.toLowerCase().includes('ipod') || userAgent.toLowerCase().includes('ipad'));

const response: any = await Http.request({
method: 'GET',
path: 'verify',
data: body,
host: apple ? 'https://radar-verify.com:52516' : 'http://localhost:52516',
});

let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
let location;
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
latitude: user.location.coordinates[1],
longitude: user.location.coordinates[0],
accuracy: user.locationAccuracy,
};
}
if (expiresAt) {
expiresAt = new Date(expiresAt);
}

trackRes = {
user,
events,
location,
token,
expiresAt,
expiresIn,
passed,
failureReasons,
_id,
} as RadarTrackVerifiedResponse;

if (options.debug) {
trackRes.response = response;
}
}

lastToken = trackRes;
Expand All @@ -103,7 +116,7 @@ class VerifyAPI {

static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) {
const doTrackVerified = async () => {
const trackRes = await this.trackVerified({});
const trackRes = await this.trackVerified(params);

const { interval } = params;

Expand Down Expand Up @@ -143,16 +156,16 @@ class VerifyAPI {
}
}

static async getVerifiedLocationToken() {
static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) {
const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000;

if (lastToken) {
if (lastToken && lastToken.passed) {
if (lastTokenElapsed < (lastToken.expiresIn || 0)) {
return lastToken;
}
}

return this.trackVerified({});
return this.trackVerified(params);
}

static setExpectedJurisdiction(countryCode?: string, stateCode?: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const getLevel = (): number => {
}

class Logger {
public static debug(message: string) {
public static debug(message: string, options?: any) {
if (getLevel() === LOG_LEVELS.debug) {
console.log(`Radar SDK (debug): ${message.trim()}`);
console.log(`Radar SDK (debug): ${message.trim()}`, options);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ export interface RadarTrackParams {
fraud?: boolean;
}

export interface RadarTrackVerifiedParams {
userId?: string;
description?: string;
metadata?: RadarMetadata;
skipVerifyApp?: boolean;
}

export interface RadarStartTrackingVerifiedParams {
interval: number;
skipVerifyApp?: boolean;
}

export enum RadarEventConfidence {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/RadarMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RadarMap extends maplibregl.Map {
radarMapOptions,
{ style },
);
Logger.debug(`initialize map with options: ${JSON.stringify(mapOptions)}`);
Logger.debug('map initailized with options', mapOptions);

(mapOptions as maplibregl.MapOptions).transformRequest = (url, resourceType) => {
// this handles when a style is switched
Expand Down
2 changes: 1 addition & 1 deletion src/ui/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class AutocompleteUI {
this.inputField.addEventListener('blur', this.close.bind(this), true);
}

Logger.debug(`AutocompleteUI iniailized with options: ${JSON.stringify(this.config)}`);
Logger.debug('AutocompleteUI initialized with options', this.config);
}

public handleInput() {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.4.4';
export default '4.4.6-beta.1';

0 comments on commit 484ab66

Please sign in to comment.