1.1.2033 • Published 2 years ago

react-sprie-sdk v1.1.2033

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Sprie SDK

Version : 1.1.2031

Quick Start

Make sure you acquire apikey from Sprie and have a product registered and catalogued by Sprie beforehand.

<!-- STEP 1. Load React CDN Scripts -->
<script crossorigin="" src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin="" src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.8.0/prop-types.min.js" integrity="sha512-M1OTu9xD1JPdXo2cOeJI+Z/f8P6E/pqK9ug3G8PRNLw1caUePewEmpFYKSYh4LEz483qnyB/UTlgX1Q4VCEsKg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>


<!-- STEP 2. Load Sprie SDK -->
<script src="https://cdn.jsdelivr.net/npm/react-sprie-sdk"> </script>


<!-- STEP 3. Intialize  -->
<script>
SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>


<!-- STEP 6. Trigger -->
<button onclick="SprieSDK.Loader.Load('product-sku');"> Preview </button>

SDK Reference

Sprie SDK is built on React and is released as umd library which depends on react libs (must be loaded from external cdn). To support both react projects and vanilla JS projects, SprieSDK lib has a Loader object which has following methods to run the react SDK as native vanilla SDK.

Methods

  1. Init ({apiKey})
  2. Load (sku)
  3. Check (sku)
  4. CheckBatch (skuArray)
  5. UnmountAll ( )
  6. GetSprieLink ( sku )
  7. HideWidget ( )

Init ( {apiKey } )

SprieSDK.Loader.Init( {apiKey:'xxx-xxx-xxx'} ); Must be called only once for apikey authentication. N.B. Must not be called every time product is loaded.

Params
  1. apiKey : string - apiKey received from Sprie Cloud

Load ( sku )

SprieSDK.Loader.Load( 'your-product-sku' ); Call everytime you want to load a new asset on AR on Sprie. Only SKU should be provided.

CheckSKU ( sku )

SprieSDK.Check( 'your-product-sku' ); (optional). Can be called to check if the product is registered with Sprie or not. For bulk assets, refer to CheckBatch. Should be used after authentication is done. Response : {<sku>:<boolean>}
Response Example : {"centena-bert-stone":true}

CheckSKUBatch ( skuArray )

SprieSDK.CheckSKUBatch( ['your-product-sku', 'your-product-sku2'] ); (optional). Can be called to check if an array of product skus is registered with Sprie or not. Should be used after authentication is done. Response : [{<sku>:<boolean>]}
Response Example :

[
    { "your-product-sku":true }, // this sku exists
    { "your-product-sku2": false} // this doesn't
]

UnmountAll ( )

SprieSDK.Loader.UnmountAll( ); Removes current instance of SprieSDK from website. This action is stateless, which means, refreshing the page will re-mount it again.

GetSprieLink (sku)

SprieSDK.GetSprieLink (sku)
Fetches the share link of the product (with the product page), typically to share it via an app, or show a QR code. this link appends sprie-sku query param with the given sku which helps sprie-widget to open automatically.

HideWidget ( )

SprieSDK.HideWidget ( );
Hides the widget as well as the widget icon.

Events

All Events are based on CustomEvent or Event. You can find the respective data for particular event by standard event.detail.[field].

Auth

  1. SprieEvent:onAuthLoading ({isLoading: boolean}) - ✅
  2. SprieEvent:onAuthOk () - ✅
  3. SprieEvent:onAuthErr ({err: string}) - ✅
  4. SprieEvent:onSDKReady () - ✅
  5. SprieEvent:onSDKErr ({err: string}) - ✅

IO

  1. SprieEvent:onCameraAllowed ( ) - ✅
  2. SprieEvent:onCameraDenied ( ) - ✅
  3. SprieEvent:onCameraError ({err: string}) - ✅

Assets

  1. SprieEvent:onAssetCart ({sku: string}) - ✅
  2. SprieEvent:onAssetWishlist ({sku : string}) - ✅
  3. SprieEvent:onAssetLoading ({sku: string, isLoading: boolean}) - ✅
  4. SprieEvent:onAssetLoadOk ({sku: string}) - ✅
  5. SprieEvent:onAssetLoadErr ({sku: string, err: string}) - ✅
  6. SprieEvent:onCheckAssetSKU ({sku:boolean}) - ✅ . e.g. Response : {'your-product-sku':true} // if it already exists
  7. SprieEvent:onCheckAssetSKUBatch ({sku:boolean, ...}) - ✅ . e.g. Response : {'your-product-sku':true, 'sku-2':false}

Widget

  1. SprieEvent:onWidgetMinimised ( ) - ✅

Events Example

<script>
    SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
    document.addEventListener("SprieEvent:onAssetCart", function (e) {
        alert("Added to cart " + e.detail.sku);
    });

    document.addEventListener("SprieEvent:onCameraDenied", function (e) {
        alert("Camera Denied!");
    });

    document.addEventListener("SprieEvent:onCameraError", function (e) {
        alert("Camera Error : " + e.detail.err);
    });
</script>

Advanced Examples

Check particular SKU before rendering the preview trigger button

This code checks the sku with Sprie Network and hides/shows a preview button based on the result. The checking code can return a promise and triggers a convinient event as well (where promises are not supported). This check method must be called only after authentication is successful otherwise unauthenticated API calls will return inconsistent result.

<script>
    const sku = 'my-precious-sku'; 
    let showPreviewButton = false;
    const onAuthDone = function (){
        SprieSDK.CheckSKU(sku);
    }

    const onCheckSKU = function (e){
        const result = e.detail; // {'my-precious-sku':true}
        if(result && Object.keys(result)[0]===sku){
            showPreviewButton = result[sku];
        }
    }

    document.addEventListener("SprieEvent:onAuthOk", onAuthDone);
    document.addEventListener("SprieEvent:onCheckAssetSKU", onCheckSKU);

    SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>

Check array of SKUs on product list page

Similar to CheckSKU method. but takes an array of skus. This method is in alpha. Expect future changes.

<script>
    const arrSku = ['my-precious-sku','my-secondary-sku']; 
    let validSkus = [];
    const onAuthDone = function (){
        SprieSDK.CheckSKUBatch(arrSku);
    }

    const onCheckSKUBatch = function (e){
        const result = e.detail; // [{ 'my-precious-sku':true, 'my-secondary-sku':false }]
        validSkus = Object.keys(result).map(x=>x).filter(x=>result[x]).map(x=>x); // validSkus=['my-precious-sku'];
    }

    document.addEventListener("SprieEvent:onAuthOk", onAuthDone);
    document.addEventListener("SprieEvent:onCheckAssetSKUBatch", onCheckSKUBatch);

    SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>

Hide widget on minimise

This code hides the widget on receiving the onWidgetMinimised event callback

<script>
     document.addEventListener("SprieEvent:onWidgetMinimised", function () {
        SprieSDK.Loader.HideWidget();
     });
</script>

Reset CDN cache

https://purge.jsdelivr.net/npm/react-sprie-sdk@latest

Test update version

https://cdn.jsdelivr.net/npm/react-sprie-sdk@latest

1.1.2033

2 years ago

1.1.2031

2 years ago

1.1.2032

2 years ago

1.1.2030

2 years ago

1.1.2028

2 years ago

1.1.2029

2 years ago

1.1.2026

2 years ago

1.1.2027

2 years ago

1.1.2025

2 years ago

1.1.2024

2 years ago

1.1.2023

2 years ago

1.1.2022

2 years ago

1.1.2021

2 years ago

1.1.2020

2 years ago

1.1.2019

2 years ago

1.1.2018

2 years ago

1.1.2017

2 years ago

1.1.2016

2 years ago

1.1.2015

2 years ago

1.1.2014

2 years ago

1.1.2013

2 years ago

1.1.2012

2 years ago

1.1.2011

2 years ago

1.1.2010

2 years ago

1.1.2009

2 years ago

1.1.2008

2 years ago

1.1.2006

2 years ago

1.1.2005

2 years ago

1.1.2004

2 years ago

1.1.2003

2 years ago

1.1.2002

2 years ago

1.1.2001

2 years ago

1.1.2

2 years ago

1.0.2017

2 years ago

1.0.2016

2 years ago

1.0.2004

2 years ago

1.0.2015

2 years ago

1.0.2014

2 years ago

1.0.2013

2 years ago

1.0.2010

2 years ago

1.0.0

2 years ago