@purple-dot/purple-dot-js
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "@purple-dot/purple-dot-js", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "An ES module wrapper for the purpledot.js SDK", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,32 @@ | ||
# purple-dot-js | ||
An ES module wrapper for the purpledot.js SDK | ||
# PurpleDot.js SDK ES Module | ||
An ES module wrapper for the [purpledot.js SDK](https://www.purpledotprice.com/docs/reference/javascript-sdk). | ||
**Note:** This package wraps the PurpleDot object provided by the purpledot.js | ||
library and dynamically loads the underlying script. It does not bundle the full | ||
source code. | ||
## Installation | ||
``` | ||
npm install @purple-dot/purple-dot-js | ||
``` | ||
## Usage | ||
### `loadPurpleDot` | ||
This function returns a promise that resolves with the `PurpleDot` object | ||
provided by the SDK. If not included already, it will inject the script tag into your | ||
page and load it. When called in a server environment it returns `null`. | ||
```javascript | ||
import { loadPurpleDot } from '@purple-dot/purple-dot-js'; | ||
// Injects the script and loads the SDK | ||
const purpleDot = await loadPurpleDot(); | ||
// Use the SDK as usual | ||
purpleDot.init({ apiKey: '5f6b6189-5380-423d-8f59-6c34eb61bbff' }); | ||
purpleDot.load({ placementType: 'button' }); | ||
``` |
@@ -35,2 +35,15 @@ /* eslint-disable global-require */ | ||
it('does not inject the script if the script is already present', async () => { | ||
document.head.innerHTML = '<script src="https://www.purpledotprice.com/api/v1/purpledot.js" async></script>'; | ||
const { loadPurpleDot } = require('./index'); | ||
loadPurpleDot(); | ||
await Promise.resolve(); // Wait a tick | ||
expect( | ||
document.querySelectorAll('script[src="https://www.purpledotprice.com/api/v1/purpledot.js"]').length, | ||
).toBe(1); | ||
}); | ||
it('does not inject the script twice', async () => { | ||
@@ -37,0 +50,0 @@ const { loadPurpleDot } = require('./index'); |
9219
127
33