@truv/bridge
v2.3.2
Published
Truv Bridge initialization package
Readme
Truv Bridge 
Library for integrating with the Truv Bridge and Embedded Orders
Install
npm install @truv/bridgeUsage
import { init } from '@truv/bridge'
const bridge = init({
bridgeToken: '<bridge token>', // create it on your server via the Truv API: https://docs.truv.com/api-reference/bridge-token/users_tokens
onSuccess: (publicToken, metaData) => {
console.log(publicToken, metaData);
},
});
// when the widget needs to be opened
bridge.open();Orders
For Embedded Orders pass isOrder: true and the bridge_token of an order created via the Truv API:
const bridge = init({
bridgeToken: '<bridge token of the order>',
isOrder: true,
onSuccess: () => {},
});Deferred loading and a specific version
@truv/bridge starts loading the latest version of the Bridge script as soon as it is imported. Import from @truv/bridge/lazy instead to decide when the script is loaded and to pin its version. init is the same:
import { init, load } from '@truv/bridge/lazy'
// The latest version
load();
// or a specific version, loaded from https://cdn.truv.com/bridge/vX.X.X/bridge.js
load({ source: 'cdn', version: 'vX.X.X' });
// or a specific version checked with integrity (generate the hash yourself or see below)
load({ source: 'cdn', version: 'vX.X.X', integrity: 'sha384-<hash of the script>' });
// or the script you included on the page yourself, see "Bridge script included on the page" below
load({ source: 'window' });
const bridge = init({
// ...
});
bridge.open();How it works:
load()must be called: until theninitwaits for it, and the widget never opens.load()can be called any number of times, only the first call is used.load()can also be called afterinit, but the earlier the script starts loading, the sooner the widget opens. Call it as early as possible, e.g. on app start.
Warning: don't use
@truv/bridgeand@truv/bridge/lazyin the same app. They don't share state, so the script is loaded twice, and the one loaded by@truv/bridgeignores the options passed toload().
The latest version and its Subresource Integrity hash are listed in latest.json. Details of a specific version, including its hash, are in https://cdn.truv.com/bridge/vX.X.X/manifest.json. Copy the version and the hash into your code rather than fetching these files at runtime, so the hash does not come from the same CDN as the script.
For a complete guide to pinning the Bridge version, see the Truv documentation.
Bridge script included on the page
If you include the Bridge script on the page yourself, pass source: 'window' to load() to use window.TruvBridge instead of loading the script again:
<!-- crossorigin is required for integrity -->
<script
src="https://cdn.truv.com/bridge/vX.X.X/bridge.js"
integrity="sha384-<hash of the script>"
crossorigin="anonymous"
></script>import { init, load } from '@truv/bridge/lazy'
load({ source: 'window' });
const bridge = init({
// ...
});
bridge.open();- The script is never loaded from the CDN in this mode. If
window.TruvBridgeis not defined,initfails with an error. - The Bridge script must run before
initis called: put its<script>tag withoutasyncbefore the script of your app.
