@tapcart/app-studio
v0.1.6
Published
Tapcart WebBridge SDK
Downloads
31
Maintainers
Keywords
Readme
Tapcart Codeblocks SDK
Tapcart Web Bridge enables web pages to render inside of Tapcart apps and synchronize state between the web and native layers, invoke native functionalities, and react to real-time updates from the native app.
Installation
All dependencies are already added into package.json
. Install them using yarn.
yarn install
Building
Webpack and Babel are used to build both the vanilla and react versions of the SDK. Run the following command to build after editing src
.
yarn build
The final build files are put in the dist
folder.
Generating a new SDK version / Deploying
brew install jq
install a dependency for the shell scriptsMake your changes, and update
package.json
to your new version.Run the following script to generate the necessary files prior to merging your code:
# staging
yarn new-version-staging
# prod
yarn new-version-prod
- ENVIRONMENT: Must be one of: staging, prod
Review the changes and commit to your branch, and create your PR
Once merged to master, the code will be auto-uploaded to the staging GCS bucket,
custom-blocks.tapcart.xyz
, in thetapcart-backend-staging
GCP project. This is done via the[Staging] Upload static files to GCS
actionTo deploy to prod, you would run the
[Production] Upload static files to GCS
GitHub Action manually. This deploys to the prod GCS bucketcustom-blocks.tapcart.com
, in thetapcart-150607
GCP project.
- Additional notes:
You can also run the [Staging] Upload static files to GCS
GitHub Action from any feature branch as well, if you
want to deploy to staging without merging. This can be useful for keeping the master branch clean. You would want to
run the new-sdk-version.sh
script prior to doing this, because all the GitHub Actions do is upload the contents of
this repository. They will not generate files for you.
You'll need to manually add the changes
messages to versions.json
, if you want them. It's probably better to
control what this messaging is manually, than read git commit messages / enforce them to be meaningful.
{
"tag": "1.6.0",
"released": "2024-02-12T17:57:18.000Z",
"release_notes": {
"changes": [
"COMMIT MESSAGE 1",
"COMMIT MESSAGE 2"
]
}
}
Examples
All sample projects which use the SDK are located in the examples
folder. These can be used for testing the SDK locally during development.
Vanilla
A sample server has been setup to serve the vanillaJS SDK. Once the SDK source files have been edited, run the following commands from root of the project.
yarn build
yarn serve-local
The server is setup with hot reloading so any updates to the SDK will be reflected automatically after running yarn build
.
Example Usage
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SDK Test HTML</title>
<script type="text/javascript" defer src="http://localhost:3000/tapcart.js"></script>
<script type="text/javascript" defer src="script.js"></script>
</head>
<body id="body" style="height: 100vh;">
<div id="tapcart-root"></div>
</body>
</html>
// script.js
(function () {
let merchantFunc = Function(`Tapcart.registerEventHandler("cart/updated",function(eventName,eventData){ console.log("Event Handler:", eventName ,eventData); })`);
Tapcart.registerMerchantJavascript(merchantFunc);
})()
// Test script - Simulates what app can/needs to do after loading codeblock/webview
setTimeout(function () {
Tapcart.init({ variables: { firstName: "Gopi" } });
Tapcart.triggerEvent("cart/updated",{"total": "10.00",currency: "USD"})
},3000);
React
To test the react SDK, yalc
is used to facilitate local npm package linking.
1.) First, install yalc
globally
npm i -g yalc
2.) Build/Publish to local yalc
repo. Run following from project root
yarn generate-react-library
yarn publish-local
3.) Run an example react app from ../examples/react/
directory
yalc add @tapcart/webbridge-react
yarn install
yarn run dev
4.) To reflect changes made to SDK in the example React App
- Build & Publish from project root
cd ../examples/react/
yarn run dev #start the dev server
cd tapcart-webbridge
yarn generate-react-library #build the changes
yarn publish-local #publish the changes
# the dev server will notice updates to the package and hotreload!
Example Usage
// App.js
import {TapcartProvider,createClient} from "@tapcart/webbridge-react";
import InnerComponent from "./InnerComponent";
function App() {
const tapcartClient = createClient({test: false});
return (
<div className="App">
<WebbridgeProvider webbridgeClient={webbridgeClient}>
<TapcartBlock /> {/* this is your apps main entry point*/}
</WebbridgeProvider>
</div>
);
}
export default App;
// InnerComponent.js
import {useActions} from "@tapcart/webbridge-react";
function InnerComponent(props) {
let {openProduct,openCollection,openCart} = useActions();
return <button onClick={() => openProduct("1232242232132")}>Inner Component</button>
}
export default InnerComponent;
Docs
To see the public facing documentation : https://docs.tapcart.com/docs/app-actions
The SDK acts as a communication bridge between the native iOS/Android Webviews and the Web Apps built by the merchants. This two way communication is powered by events
and actions
.
Actions allow the SDK users to interact with native app functionality via JS. The following actions are supported currently:
Tapcart.actions.addToCart(...)
Tapcart.actions.applyDiscount(...)
Tapcart.actions.applyGiftCard(...)
Tapcart.actions.cartBuyerIdentity(...)
Tapcart.actions.cartUpdateDeliveryOptions(...)
Tapcart.actions.getCustomerIdentity()
Tapcart.actions.openCart()
Tapcart.actions.openCollection(...)
Tapcart.actions.openProduct(...)
Tapcart.actions.openScreen(...)
Tapcart.actions.removeFromCart(...)
Tapcart.actions.renderView()
Tapcart.actions.scrollToBlockBottom()
Tapcart.actions.scrollToBlockTop()
Tapcart.actions.showToast(...)
Tapcart.actions.updateCartAttributes(...)
Tapcart.actions.updateCartNote(...)
Tapcart.actions.updateView(...)
Tapcart.actions.vault.initialize(...)
Events allow SDK users to respond to changes in the native apps; The following events are supported currently:
cart/updated
discount/applied
giftcard/applied
product/opened
collection/opened