UNPKG

weauthjs

Version:

WeYouMe Protocol Javascript SDK Library

239 lines (204 loc) 8.25 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var fetch = require('cross-fetch'); var SDKError = function (_Error) { _inherits(SDKError, _Error); function SDKError(message, obj) { _classCallCheck(this, SDKError); var _this = _possibleConstructorReturn(this, (SDKError.__proto__ || Object.getPrototypeOf(SDKError)).call(this, message)); _this.name = 'SDKError'; _this.error = obj.error; _this.error_description = obj.error_description; if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(_this, _this.constructor); } else { _this.stack = new Error(message).stack; } return _this; } return SDKError; }(Error); function weauthjs() { this.options = { baseURL: 'https://auth.WeYouMe.io', app: '', callbackURL: '', scope: [] }; } weauthjs.prototype.setBaseURL = function setBaseURL(baseURL) { this.options.baseURL = baseURL; }; weauthjs.prototype.setApp = function setApp(app) { this.options.app = app; }; weauthjs.prototype.setCallbackURL = function setCallbackURL(callbackURL) { this.options.callbackURL = callbackURL; }; weauthjs.prototype.setAccessToken = function setAccessToken(accessToken) { this.options.accessToken = accessToken; }; weauthjs.prototype.removeAccessToken = function removeAccessToken() { this.options.accessToken = undefined; }; weauthjs.prototype.setScope = function setScope(scope) { this.options.scope = scope; }; weauthjs.prototype.getLoginURL = function getLoginURL(state) { var loginURL = this.options.baseURL + '/oauth2/authorize?client_id=' + this.options.app + '&redirect_uri=' + encodeURIComponent(this.options.callbackURL); loginURL += this.options.scope ? '&scope=' + this.options.scope.join(',') : ''; loginURL += state ? '&state=' + encodeURIComponent(state) : ''; return loginURL; }; weauthjs.prototype.send = function send(route, method, body, cb) { var url = this.options.baseURL + '/api/' + route; var promise = fetch(url, { method: method, headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json', Authorization: this.options.accessToken }, body: JSON.stringify(body) }).then(function (res) { var json = res.json(); // If the status is something other than 200 we need // to reject the result since the request is not considered as a fail if (res.status !== 200) { return json.then(function (result) { return Promise.reject(new SDKError('weauthjs error', result)); }); } return json; }).then(function (res) { if (res.error) { return Promise.reject(new SDKError('weauthjs error', res)); } return res; }); if (!cb) return promise; return promise.then(function (res) { return cb(null, res); }).catch(function (err) { return cb(err, null); }); }; weauthjs.prototype.broadcast = function broadcast(operations, cb) { return this.send('broadcast', 'POST', { operations: operations }, cb); }; weauthjs.prototype.me = function me(cb) { return this.send('me', 'POST', {}, cb); }; weauthjs.prototype.vote = function vote(voter, author, permlink, weight, cb) { var params = { voter: voter, author: author, permlink: permlink, weight: weight }; return this.broadcast([['vote', params]], cb); }; weauthjs.prototype.comment = function comment(parentAuthor, parentPermlink, author, permlink, title, body, json, cb) { var params = { parent_author: parentAuthor, parent_permlink: parentPermlink, author: author, permlink: permlink, title: title, body: body, json: JSON.stringify(json) }; return this.broadcast([['comment', params]], cb); }; weauthjs.prototype.reblog = function reblog(account, author, permlink, cb) { var params = { required_auths: [], required_posting_auths: [account], id: 'follow', json: JSON.stringify(['reblog', { account: account, author: author, permlink: permlink }]) }; return this.broadcast([['customJson', params]], cb); }; weauthjs.prototype.follow = function follow(follower, following, cb) { var params = { required_auths: [], required_posting_auths: [follower], id: 'follow', json: JSON.stringify(['follow', { follower: follower, following: following, what: ['blog'] }]) }; return this.broadcast([['customJson', params]], cb); }; weauthjs.prototype.unfollow = function unfollow(unfollower, unfollowing, cb) { var params = { required_auths: [], required_posting_auths: [unfollower], id: 'follow', json: JSON.stringify(['follow', { follower: unfollower, following: unfollowing, what: [] }]) }; return this.broadcast([['customJson', params]], cb); }; weauthjs.prototype.ignore = function ignore(follower, following, cb) { var params = { required_auths: [], required_posting_auths: [follower], id: 'follow', json: JSON.stringify(['follow', { follower: follower, following: following, what: ['ignore'] }]) }; return this.broadcast([['customJson', params]], cb); }; weauthjs.prototype.claimRewardBalance = function claimRewardBalance(account, TMEreward, TSDreward, SCOREreward, cb) { var params = { account: account, TMEreward: TMEreward, TSDreward: TSDreward, SCOREreward: SCOREreward }; return this.broadcast([['claimRewardBalance', params]], cb); }; weauthjs.prototype.revokeToken = function revokeToken(cb) { var _this2 = this; return this.send('oauth2/token/revoke', 'POST', { token: this.options.accessToken }, cb).then(function () { return _this2.removeAccessToken(); }); }; weauthjs.prototype.updateUserMetadata = function updateUserMetadata() { var metadata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var cb = arguments[1]; return this.send('me', 'PUT', { user_metadata: metadata }, cb); }; weauthjs.prototype.sign = function sign(name, params, redirectUri) { if (typeof name !== 'string' || (typeof params === 'undefined' ? 'undefined' : _typeof(params)) !== 'object') { return new SDKError('weauthjs error', { error: 'invalid_request', error_description: 'Request has an invalid format' }); } var url = this.options.baseURL + '/sign/' + name + '?'; url += Object.keys(params).map(function (key) { return key + '=' + encodeURIComponent(params[key]); }).join('&'); url += redirectUri ? '&redirect_uri=' + encodeURIComponent(redirectUri) : ''; return url; }; exports.Initialize = function Initialize(config) { var instance = new weauthjs(); if (!config) { throw new Error('You have to provide config'); } if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) !== 'object') { throw new Error('Config must be an object'); } if (config.baseURL) instance.setBaseURL(config.baseURL); if (config.app) instance.setApp(config.app); if (config.callbackURL) instance.setCallbackURL(config.callbackURL); if (config.accessToken) instance.setAccessToken(config.accessToken); if (config.scope) instance.setScope(config.scope); return instance; };