0.1.3-rc8 • Published 4 years ago
pwa-rpc v0.1.3-rc8
pwa-rpc
Description
A simple JavaScript library which provides PWA's the ability to retrieve OAuth tokens and their related information from PACE native apps.
Installation
As NPM package
yarn add pwa-rpc
or
npm i pwa-rpc
Usage
// OAuthApp.js
import OAuthApplication from 'pwa-rpc';
const OAuthApp = new OAuthApplication({
clientId: 'my_client_id',
disableRemoteDiagnostics: false, //optional, default is false
scope: 'my_scope',
});
export async function getAccessToken() {
try {
const token = await OAuthApp.getAccessToken();
const { access_token } = token;
return access_token;
} catch (e) {
// Handle error
}
}
export async function getQueryParamValue(key) {
try {
const queryParam = await OAuthApp.getQuery(key);
return queryParam;
} catch (e) {
// Handle error
}
}
export function close() {
OAuthApp.close();
}
// API.js
import { getAccessToken } from '../OAuthApp';
async function makeAPICall() {
try {
const accessToken = await getAccessToken();
let response = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
let responseJSON = await response.json();
// Do sth with responseJSON
} catch (error) {
// Handle error
}
}
License
MIT