diff --git a/README.md b/README.md index 2fd02de1..aeebca1b 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index 0fe7086e..2cf9a466 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.0", + "version": "4.4.1-beta.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 10491062..c54cc52d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.js b/rollup.config.js index 3c345946..00c347e9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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(), + ], } ]; diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 00000000..dca330dc --- /dev/null +++ b/src/api.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index 29c8e8f0..d62715a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,61 +1,13 @@ -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 RadarAPI from './api'; import MapUI from './ui/map'; import AutocompleteUI from './ui/autocomplete'; -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'; - import 'maplibre-gl/dist/maplibre-gl.css'; import '../styles/radar.css'; - -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; - } - - // "ui" namespace +class Radar extends RadarAPI { + // "ui" namespace for Maps and Autocomplete public static get ui() { return { maplibregl: MapUI.getMapLibre(), @@ -65,190 +17,6 @@ class Radar { autocomplete: AutocompleteUI.createAutocomplete, }; } - - 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; diff --git a/src/version.ts b/src/version.ts index 577799bc..62c4e3a3 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.0'; +export default '4.4.1-beta.0';