JS Bridge for SP3 web
Requirements
@terra-js/web-bridge
yarn add @terra-js/web-bridge
or
npm install @terra-js/web-bridge
Installation
yarn add @terra-js/sp3-module
or
npm install @terra-js/sp3-module
Methods
isLoggedIn() -> Promise<Result<boolean>>
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
async function checkLoginState() {
// use async/await
const result = await Sp3Module.isLoggedIn();
if (result.isSuccess()) {
console.log('isLoggedIn', result.getValue());
} else {
// error
}
// or use callback
Sp3Module.isLoggedIn().then((result) => {
if (result.isSuccess()) {
console.log('isLoggedIn', result.getValue());
} else {
// error
}
});
}
getAccessToken() -> Promise<Result<String>>
Return the saved access token of native app. Return error if failed.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
async function getAccessToken() {
// use async/await
const result = await Sp3Module.getAccessToken();
if (result.isSuccess()) {
const accessToken = result.getValue();
} else {
// error
}
// or use callback
Sp3Module.getAccessToken().then((result) => {
if (result.isSuccess()) {
const accessToken = result.getValue();
} else {
// error
}
});
}
refreshAccessToken() -> Promise<Result<String>>
Refresh the current access token and return the new access token. Return error if failed.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
async function refreshAccessToken() {
// use async/await
const result = await Sp3Module.refreshAccessToken();
if (result.isSuccess()) {
const accessToken = result.getValue();
} else {
// error
result.getError();
console.log('error', result.error);
}
// or use callback
Sp3Module.refreshAccessToken().then((result) => {
if (result.isSuccess()) {
const accessToken = result.getValue();
} else {
// error
}
});
}
getUserInfo() -> Promise<Result<User>>
Get current user info.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
async function getUserInfo() {
// use async/await
const result = await Sp3Module.getUserInfo();
if (result.isSuccess()) {
const userInfo = result.getValue();
} else {
// error
}
// or use callback
Sp3Module.getUserInfo().then((result) => {
if (result.isSuccess()) {
const userInfo = result.getValue();
} else {
// error
}
});
}
User interface:
interface User {
sub: string;
name: string;
picture?: string;
email?: string;
phoneNumber?: string;
birthday?: string;
address?: string;
roles?: string[];
permissions?: string[];
metadata?: any;
}
logOut() -> void
Log out and navigate to the Login screen.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
function logOut() {
Sp3Module.logOut();
}
openNotificationList() -> void
Open the notification list screen.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
function openNotificationList() {
Sp3Module.openNotificationList();
}
onUnreadNotificationsCountChange() -> DataStream<number>
Observe the number of unread notifications. This data should be shown in the corner of notification icon button.
Example with React
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
function FooComponent = () => {
useEffect(() => {
const subscription = Sp3Module.onUnreadNotificationsCountChange().subscribe({
next: (response) => {
const unreadNotificationsCount: number = response.result ?? 0
// update the UI
},
complete: () => {
},
});
return () => {
subscription.unsubscribe();
};
}, []);
return <div>/** UI code */</div>
}
handleBack() -> void
Request the native app to handle the default back action.
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
function handleBack() {
Sp3Module.handleBack();
}
onBackPressed() -> DataStream
Observe the pressing back button event.
Example with React
import `@terra-js/web-bridge`
import `@terra-js/sp3-module`
function FooComponent = () => {
useEffect(() => {
const subscription = Sp3Module.onBackPressed().subscribe({
next: () => {
// Back to the corresponding component based on business logic.
// In case of nothing to back, should call `handleBack` method
},
complete: () => {
},
});
return () => {
subscription.unsubscribe();
};
}, []);
return <div>/** UI code */</div>
}