UNPKG

dop-sdk

Version:

Mini App SDK for JavaScript by VTB

183 lines 9.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserInfo = void 0; var event_types_1 = require("../event-types"); var common_bridge_1 = require("../helpers/utils/common-bridge"); var sdkbridge_1 = require("../sdkbridge"); var error_types_1 = require("../types/error-types"); var token_data_1 = require("../types/token-data"); var decoder_1 = require("./decoder"); /** @internal */ var UserInfo = /** @class */ (function () { function UserInfo() { } /** * Associating getUserName function to MiniAppBridge object. * This function returns username from the user profile * (provided the rakuten.miniapp.user.USER_NAME custom permission is allowed by the user) * It returns error info if user had denied the custom permission */ UserInfo.prototype.getUserName = function () { var platform = (0, sdkbridge_1.getBridge)().platform; return (0, sdkbridge_1.getBridge)() .sendToNative(event_types_1.UserInfoEvent.GET_USER_NAME, null) .then(function (userName) { var value; if (platform === 'iOS') { value = (0, common_bridge_1.convertUnicodeCharacters)(userName); } else { value = (0, common_bridge_1.convertUnicodeCharactersForAndroid)(userName); } return value; }) .catch(function (error) { throw error; }); }; /** * Associating getProfilePhoto function to MiniAppBridge object. * This function returns username from the user profile. * (provided the rakuten.miniapp.user.PROFILE_PHOTO is allowed by the user) * It returns error info if user had denied the custom permission */ UserInfo.prototype.getProfilePhoto = function () { return (0, sdkbridge_1.getBridge)().sendToNative(event_types_1.UserInfoEvent.GET_PROFILE_PHOTO, null); }; /** * Associating getContacts function to MiniAppBridge object. * This function returns contact list from the user profile. * (provided the rakuten.miniapp.user.CONTACT_LIST is allowed by the user) * It returns error info if user had denied the custom permission */ UserInfo.prototype.getContacts = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { try { return [2 /*return*/, (0, sdkbridge_1.getBridge)() .sendToNative(event_types_1.UserInfoEvent.GET_CONTACTS, { isContactsEncodingRequired: true, }) .then(function (contacts) { var contactList = JSON.parse(contacts); var decoder = new decoder_1.DecodeManager(); return decoder.decodeContacts(contactList); }) .catch(function (error) { return error; })]; } catch (error) { console.error("Error retrieving contacts: ".concat(error.message)); return [2 /*return*/, []]; } return [2 /*return*/]; }); }); }; /** * Associating getAccessToken function to MiniAppBridge object. * This function returns access token details from the host app. * (provided the rakuten.miniapp.user.ACCESS_TOKEN is allowed by the user) * It returns error info if user had denied the custom permission * @param {string} audience the audience the MiniApp requests for the token * @param {string[]} scopes the associated scopes with the requested audience */ UserInfo.prototype.getAccessToken = function (audience, scopes) { return __awaiter(this, void 0, void 0, function () { var tokenData, nativeTokenData, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, (0, sdkbridge_1.getBridge)().sendToNative(event_types_1.UserInfoEvent.GET_ACCESS_TOKEN, { audience: audience, scopes: scopes, })]; case 1: tokenData = _a.sent(); nativeTokenData = JSON.parse(tokenData); return [2 /*return*/, new token_data_1.AccessTokenData(nativeTokenData)]; case 2: error_1 = _a.sent(); return [2 /*return*/, (0, error_types_1.parseMiniAppError)(error_1)]; case 3: return [2 /*return*/]; } }); }); }; /** * Associating get point balance function to MiniAppBridge object. * (provided rakuten.miniapp.user.POINTS is allowed by the user) */ UserInfo.prototype.getPoints = function () { return __awaiter(this, void 0, void 0, function () { var points, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, (0, sdkbridge_1.getBridge)().sendToNative(event_types_1.UserInfoEvent.GET_POINT, null)]; case 1: points = _a.sent(); return [2 /*return*/, JSON.parse(points)]; case 2: error_2 = _a.sent(); return [2 /*return*/, (0, error_types_1.parseMiniAppError)(error_2)]; case 3: return [2 /*return*/]; } }); }); }; /** * Associating getPhoneNumber function to MiniAppBridge object. * This function returns phone number of the User */ UserInfo.prototype.getPhoneNumber = function () { return (0, sdkbridge_1.getBridge)().sendToNative(event_types_1.UserInfoEvent.GET_PHONE_NUMBER, null); }; /** * This interface is used to know if the user login status * @returns true/false based on the user profile status */ UserInfo.prototype.isLoggedIn = function () { return (0, sdkbridge_1.getBridge)().sendToNative(event_types_1.UserInfoEvent.IS_LOGGED_IN, null); }; return UserInfo; }()); exports.UserInfo = UserInfo; //# sourceMappingURL=user-info.js.map