Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core build to CDN output #171

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0/radar.min.js"></script>
<script src="https://js.radar.com/v4.4.1-beta.0/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.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.1-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.1-beta.0/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.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.1-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.1-beta.0/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.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.0/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.1-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.1-beta.0/radar.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion 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.0",
"version": "4.4.1-beta.0",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
25 changes: 25 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,30 @@ export default [
minimize: true,
}),
],
},

// IIFE (core SDK feature - no maps)
{
input: 'src/api.ts',
output: [
{
file: 'cdn/radar-core.js',
format: 'iife',
name: 'Radar',
plugins: [onlyEmitFile()],
},
{
file: 'cdn/radar-core.min.js',
format: 'iife',
name: 'Radar',
plugins: [terser(), onlyEmitFile()],
},
],
plugins: [
typescript(),
nodeResolve(),
commonjs(),
json(),
],
}
];
237 changes: 237 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import Config from './config';
import Logger from './logger';
import Storage from './storage';
import Navigator from './navigator';
import { RadarPublishableKeyError } from './errors';

import AddressesAPI from './api/addresses';
import ConfigAPI from './api/config';
import ContextAPI from './api/context';
import ConversionsAPI from './api/conversions';
import GeocodingAPI from './api/geocoding';
import RoutingAPI from './api/routing';
import SearchAPI from './api/search';
import TrackAPI from './api/track';
import TripsAPI from './api/trips';
import VerifyAPI from './api/verify';

import SDK_VERSION from './version';

import type {
Location,
RadarAutocompleteParams,
RadarConversionParams,
RadarDistanceParams,
RadarForwardGeocodeParams,
RadarMatrixParams,
RadarMetadata,
RadarOptions,
RadarReverseGeocodeParams,
RadarSearchGeofencesParams,
RadarSearchPlacesParams,
RadarStartTrackingVerifiedParams,
RadarTrackParams,
RadarTrackVerifiedResponse,
RadarTripOptions,
RadarValidateAddressParams,
} from './types';

const isSecretKey = (key: string): boolean => (
key.includes('_sk_')
);
const isLiveKey = (key: string): boolean => (
key.includes('_live_')
);

class Radar {
public static get VERSION() {
return SDK_VERSION;
}

public static initialize(publishableKey: string, options: RadarOptions = {}) {
if (!publishableKey) {
throw new RadarPublishableKeyError('Publishable key required in initialization.');
}

if (isSecretKey(publishableKey)) {
throw new RadarPublishableKeyError('Secret keys are not allowed. Please use your Radar publishable key.');
}

// store settings in global config
const live = isLiveKey(publishableKey);
const logLevel = live ? 'error' : 'info';
const debug = !live;
const radarOptions = Object.assign(
Config.defaultOptions,
{
publishableKey,
live,
logLevel,
debug,
},
options,
);
Config.setup(radarOptions);

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

// NOTE(jasonl): this allows us to run jest tests
// without having to mock the ConfigAPI.getConfig call
if (!(window as any)?.RADAR_TEST_ENV) {
ConfigAPI.getConfig();
}
}

public static clear() {
Config.clear();
}

///////////////////////
// Geofencing Platform
///////////////////////

public static setUserId(userId?: string) {
if (!userId) {
Storage.removeItem(Storage.USER_ID);
return;
}
Storage.setItem(Storage.USER_ID, String(userId).trim());
}

public static setDescription(description?: string) {
if (!description) {
Storage.removeItem(Storage.DESCRIPTION);
return;
}
Storage.setItem(Storage.DESCRIPTION, String(description).trim());
}

public static setMetadata(metadata?: RadarMetadata) {
if (!metadata) {
Storage.removeItem(Storage.METADATA);
return;
}
Storage.setItem(Storage.METADATA, JSON.stringify(metadata));
}

public static getLocation() {
return Navigator.getCurrentPosition();
}

public static trackOnce(params: RadarTrackParams = {}) {
try {
return TrackAPI.trackOnce(params);
} finally {
ConfigAPI.getConfig(params); // call with updated permissions
}
}

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

public static startTrackingVerified(params: RadarStartTrackingVerifiedParams) {
return VerifyAPI.startTrackingVerified(params);
}

public static stopTrackingVerified() {
return VerifyAPI.stopTrackingVerified();
}

public static getVerifiedLocationToken() {
return VerifyAPI.getVerifiedLocationToken();
}

public static setExpectedJurisdiction(countryCode?: string, stateCode?: string) {
VerifyAPI.setExpectedJurisdiction(countryCode, stateCode);
}

public static getContext(params: Location) {
return ContextAPI.getContext(params);
}

public static setTripOptions(tripOptions?: RadarTripOptions) {
TripsAPI.setTripOptions(tripOptions);
}

public static clearTripOptions() {
TripsAPI.clearTripOptions();
}

public static getTripOptions() {
return TripsAPI.getTripOptions();
}

public static startTrip(tripOptions: RadarTripOptions) {
return TripsAPI.startTrip(tripOptions);
}

public static updateTrip(tripOptions: RadarTripOptions) {
return TripsAPI.updateTrip(tripOptions);
}

public static completeTrip() {
return TripsAPI.completeTrip();
}

public static cancelTrip() {
return TripsAPI.cancelTrip();
}

public static logConversion(params: RadarConversionParams) {
return ConversionsAPI.logConversion(params);
}

///////////////////////
// Listeners
///////////////////////
public static onTokenUpdated(callback: (token: RadarTrackVerifiedResponse) => void) {
VerifyAPI.onTokenUpdated(callback);
}


/////////////////
// Maps Platform
/////////////////

public static forwardGeocode(params: RadarForwardGeocodeParams) {
return GeocodingAPI.forwardGeocode(params);
}

public static reverseGeocode(params: RadarReverseGeocodeParams) {
return GeocodingAPI.reverseGeocode(params);
}

public static ipGeocode() {
return GeocodingAPI.ipGeocode();
}

public static autocomplete(params: RadarAutocompleteParams) {
return SearchAPI.autocomplete(params);
}

public static searchGeofences(params: RadarSearchGeofencesParams) {
return SearchAPI.searchGeofences(params);
}

public static searchPlaces(params: RadarSearchPlacesParams) {
return SearchAPI.searchPlaces(params);
}

public static validateAddress(params: RadarValidateAddressParams) {
return AddressesAPI.validateAddress(params);
}

public static distance(params: RadarDistanceParams) {
return RoutingAPI.distance(params);
}

public static matrix(params: RadarMatrixParams) {
return RoutingAPI.matrix(params);
}
}

export default Radar;
Loading
Loading