lbryinc base code containing lbryio.js
This commit is contained in:
parent
0739116bf4
commit
88f32dfb12
17 changed files with 12788 additions and 0 deletions
4
.babelrc
Normal file
4
.babelrc
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"presets": ["env", "stage-2"],
|
||||||
|
"plugins": ["transform-flow-comments"]
|
||||||
|
}
|
30
.eslintrc.json
Normal file
30
.eslintrc.json
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"plugins": ["flowtype"],
|
||||||
|
"extends": [
|
||||||
|
"airbnb-base",
|
||||||
|
"plugin:import/electron",
|
||||||
|
"plugin:flowtype/recommended",
|
||||||
|
"plugin:prettier/recommended"
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"import/resolver": {
|
||||||
|
"webpack": {
|
||||||
|
"config": "webpack.config.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"parser": "babel-eslint",
|
||||||
|
"env": {
|
||||||
|
"browser": true,
|
||||||
|
"node": true
|
||||||
|
},
|
||||||
|
"globals": {
|
||||||
|
"__": true
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"import/no-commonjs": "warn",
|
||||||
|
"import/no-amd": "warn",
|
||||||
|
"import/prefer-default-export": "ignore",
|
||||||
|
"func-names": ["warn", "as-needed"]
|
||||||
|
}
|
||||||
|
}
|
9
.flowconfig
Normal file
9
.flowconfig
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[ignore]
|
||||||
|
|
||||||
|
[include]
|
||||||
|
|
||||||
|
[libs]
|
||||||
|
|
||||||
|
[options]
|
||||||
|
module.system.node.resolve_dirname=src
|
||||||
|
module.system.node.resolve_dirname=node_modules
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/node_modules
|
||||||
|
yarn-error.log
|
12
.lintstagedrc.json
Normal file
12
.lintstagedrc.json
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"linters": {
|
||||||
|
"src/**/*.{js,json}": [
|
||||||
|
"prettier --write",
|
||||||
|
"git add"
|
||||||
|
],
|
||||||
|
"src/**/*.js": [
|
||||||
|
"eslint --fix",
|
||||||
|
"git add"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
5
.prettierrc.json
Normal file
5
.prettierrc.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"printWidth": 100,
|
||||||
|
"singleQuote": true
|
||||||
|
}
|
15
LICENSE
Normal file
15
LICENSE
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2017-2018 LBRY Inc
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||||
|
following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
6675
dist/bundle.js
vendored
Normal file
6675
dist/bundle.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
58
package.json
Normal file
58
package.json
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
{
|
||||||
|
"name": "lbryinc",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "Shared code for api.lbry.io internal APIs.",
|
||||||
|
"keywords": [
|
||||||
|
"lbry"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"homepage": "https://lbry.io/",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/lbryio/lbryinc/issues"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/lbryio/lbryinc"
|
||||||
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "LBRY Inc.",
|
||||||
|
"email": "hello@lbry.io"
|
||||||
|
},
|
||||||
|
"main": "dist/bundle.js",
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"precommit": "lint-staged",
|
||||||
|
"lint": "eslint 'src/**/*.js' --fix",
|
||||||
|
"format": "prettier 'src/**/*.{js,json}' --write"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"lbry-redux": "lbryio/lbry-redux",
|
||||||
|
"reselect": "^3.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-core": "^6.26.0",
|
||||||
|
"babel-eslint": "^8.0.3",
|
||||||
|
"babel-loader": "^7.1.4",
|
||||||
|
"babel-plugin-module-resolver": "^3.0.0",
|
||||||
|
"babel-preset-env": "^1.6.1",
|
||||||
|
"babel-preset-stage-2": "^6.18.0",
|
||||||
|
"eslint": "^4.19.1",
|
||||||
|
"eslint-config-airbnb-base": "^12.1.0",
|
||||||
|
"eslint-config-prettier": "^2.9.0",
|
||||||
|
"eslint-import-resolver-webpack": "^0.9.0",
|
||||||
|
"eslint-plugin-flowtype": "^2.40.1",
|
||||||
|
"eslint-plugin-import": "^2.10.0",
|
||||||
|
"eslint-plugin-prettier": "^2.4.0",
|
||||||
|
"flow-babel-webpack-plugin": "^1.1.1",
|
||||||
|
"flow-bin": "^0.69.0",
|
||||||
|
"flow-typed": "^2.4.0",
|
||||||
|
"husky": "^0.14.3",
|
||||||
|
"lint-staged": "^7.0.4",
|
||||||
|
"prettier": "^1.4.2",
|
||||||
|
"webpack": "^4.5.0",
|
||||||
|
"webpack-cli": "^2.0.14"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"yarn": "^1.3"
|
||||||
|
}
|
||||||
|
}
|
2
src/constants/action_types.js
Normal file
2
src/constants/action_types.js
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const GET_AUTH_TOKEN_STARTED = 'GET_AUTH_TOKEN_STARTED';
|
||||||
|
export const GET_AUTH_TOKEN_COMPLETED = 'GET_AUTH_TOKEN_COMPLETED';
|
1
src/index.js
Normal file
1
src/index.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export { Lbryio } from 'lbryio';
|
142
src/lbryio.js
Normal file
142
src/lbryio.js
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
import { Lbry } from 'lbry-redux';
|
||||||
|
import { doNewInstallation } from 'redux/actions/auth';
|
||||||
|
import querystring from 'querystring';
|
||||||
|
|
||||||
|
const Lbryio = {
|
||||||
|
enabled: true,
|
||||||
|
authenticationPromise: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const CONNECTION_STRING = process.env.LBRY_APP_API_URL
|
||||||
|
? process.env.LBRY_APP_API_URL.replace(/\/*$/, '/') // exactly one slash at the end
|
||||||
|
: 'https://api.lbry.io/';
|
||||||
|
|
||||||
|
Lbryio.call = (resource, action, params = {}, method = 'get') => {
|
||||||
|
if (!Lbryio.enabled) {
|
||||||
|
return Promise.reject(new Error(__('LBRY internal API is disabled')));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(method === 'get' || method === 'post')) {
|
||||||
|
return Promise.reject(new Error(__('Invalid method')));
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkAndParse(response) {
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
return response.json().then(json => {
|
||||||
|
let error;
|
||||||
|
if (json.error) {
|
||||||
|
error = new Error(json.error);
|
||||||
|
} else {
|
||||||
|
error = new Error('Unknown API error signature');
|
||||||
|
}
|
||||||
|
error.response = response; // This is primarily a hack used in actions/user.js
|
||||||
|
return Promise.reject(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeRequest(url, options) {
|
||||||
|
return fetch(url, options).then(checkAndParse);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Lbryio.getAuthToken().then(token => {
|
||||||
|
const fullParams = { auth_token: token, ...params };
|
||||||
|
const qs = querystring.stringify(fullParams);
|
||||||
|
let url = `${CONNECTION_STRING}${resource}/${action}?${qs}`;
|
||||||
|
|
||||||
|
let options = {
|
||||||
|
method: 'GET',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (method === 'post') {
|
||||||
|
options = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
body: qs,
|
||||||
|
};
|
||||||
|
url = `${CONNECTION_STRING}${resource}/${action}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return makeRequest(url, options).then(response => response.data);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Lbryio.authToken = null;
|
||||||
|
|
||||||
|
Lbryio.getAuthToken = () =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
if (Lbryio.authToken) {
|
||||||
|
resolve(Lbryio.authToken);
|
||||||
|
} else {
|
||||||
|
const store = global.store || (window.app ? window.app.store : null);
|
||||||
|
if (store) {
|
||||||
|
const state = store.getState();
|
||||||
|
const token = state.auth ? state.auth.authToken : null;
|
||||||
|
Lbryio.authToken = token;
|
||||||
|
resolve(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Lbryio.getCurrentUser = () => Lbryio.call('user', 'me');
|
||||||
|
|
||||||
|
Lbryio.authenticate = () => {
|
||||||
|
if (!Lbryio.enabled) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
resolve({
|
||||||
|
id: 1,
|
||||||
|
language: 'en',
|
||||||
|
primary_email: 'disabled@lbry.io',
|
||||||
|
has_verified_email: true,
|
||||||
|
is_identity_verified: true,
|
||||||
|
is_reward_approved: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Lbryio.authenticationPromise === null) {
|
||||||
|
Lbryio.authenticationPromise = new Promise((resolve, reject) => {
|
||||||
|
Lbryio.getAuthToken()
|
||||||
|
.then(token => {
|
||||||
|
if (!token || token.length > 60) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that token works
|
||||||
|
return Lbryio.getCurrentUser()
|
||||||
|
.then(() => true)
|
||||||
|
.catch(() => false);
|
||||||
|
})
|
||||||
|
.then(isTokenValid => {
|
||||||
|
if (isTokenValid) {
|
||||||
|
return reject;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Lbry.status().then(status => {
|
||||||
|
const store = global.store || (window.app ? window.app.store : null);
|
||||||
|
if (store) {
|
||||||
|
store.dispatch(doNewInstallation(status.installation_id));
|
||||||
|
return resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return reject();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(resolve, reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Lbryio.authenticationPromise;
|
||||||
|
};
|
||||||
|
|
||||||
|
Lbryio.getStripeToken = () =>
|
||||||
|
CONNECTION_STRING.startsWith('http://localhost:')
|
||||||
|
? 'pk_test_NoL1JWL7i1ipfhVId5KfDZgo'
|
||||||
|
: 'pk_live_e8M4dRNnCCbmpZzduEUZBgJO';
|
||||||
|
|
||||||
|
export default Lbryio;
|
40
src/redux/actions/auth.js
Normal file
40
src/redux/actions/auth.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import * as ACTIONS from 'constants/action_types';
|
||||||
|
import Lbryio from 'lbryio';
|
||||||
|
|
||||||
|
export function doNewInstallation(installationId) {
|
||||||
|
return dispatch => {
|
||||||
|
dispatch({
|
||||||
|
type: ACTIONS.GET_AUTH_TOKEN_STARTED,
|
||||||
|
});
|
||||||
|
|
||||||
|
Lbryio.call(
|
||||||
|
'user',
|
||||||
|
'new',
|
||||||
|
{
|
||||||
|
auth_token: '',
|
||||||
|
language: 'en',
|
||||||
|
app_id: installationId,
|
||||||
|
},
|
||||||
|
'post'
|
||||||
|
)
|
||||||
|
.then(response => {
|
||||||
|
if (!response.auth_token) {
|
||||||
|
dispatch({
|
||||||
|
type: ACTIONS.GET_AUTH_TOKEN_COMPLETED,
|
||||||
|
data: { authToken: null },
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
dispatch({
|
||||||
|
type: ACTIONS.GET_AUTH_TOKEN_COMPLETED,
|
||||||
|
data: { authToken: response.auth_token },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
dispatch({
|
||||||
|
type: ACTIONS.GET_AUTH_TOKEN_COMPLETED,
|
||||||
|
data: { authToken: null },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
23
src/redux/reducers/auth.js
Normal file
23
src/redux/reducers/auth.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import * as ACTIONS from 'constants/action_types';
|
||||||
|
|
||||||
|
const reducers = {};
|
||||||
|
const defaultState = {
|
||||||
|
authenticating: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
reducers[ACTIONS.GET_AUTH_TOKEN_STARTED] = state =>
|
||||||
|
Object.assign({}, state, {
|
||||||
|
authenticating: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
reducers[ACTIONS.GET_AUTH_TOKEN_COMPLETED] = (state, action) =>
|
||||||
|
Object.assign({}, state, {
|
||||||
|
authToken: action.authToken,
|
||||||
|
authenticating: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
export function authReducer(state = defaultState, action) {
|
||||||
|
const handler = reducers[action.type];
|
||||||
|
if (handler) return handler(state, action);
|
||||||
|
return state;
|
||||||
|
}
|
5
src/redux/selectors/auth.js
Normal file
5
src/redux/selectors/auth.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { createSelector } from 'reselect';
|
||||||
|
|
||||||
|
const selectState = state => state.authToken || {};
|
||||||
|
|
||||||
|
export const selectAuthToken = createSelector(selectState, state => state.authToken);
|
26
webpack.config.js
Normal file
26
webpack.config.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/* eslint-disable import/no-commonjs */
|
||||||
|
const path = require('path');
|
||||||
|
const FlowBabelWebpackPlugin = require('flow-babel-webpack-plugin');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: 'none',
|
||||||
|
entry: './src/index.js',
|
||||||
|
output: {
|
||||||
|
filename: 'bundle.js',
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
libraryTarget: 'umd'
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.js$/,
|
||||||
|
exclude: /node_modules/,
|
||||||
|
loader: 'babel-loader',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
|
||||||
|
},
|
||||||
|
plugins: [new FlowBabelWebpackPlugin()],
|
||||||
|
};
|
Loading…
Reference in a new issue