From ed94218cdb3409b963aec136ffe3c11c9e3bc875 Mon Sep 17 00:00:00 2001 From: Jessop Breth Date: Thu, 30 May 2019 11:24:46 -0400 Subject: [PATCH] adds headers and means to set them to Lbry --- .gitignore | 6 +++++- dist/bundle.es.js | 9 +++++++++ dist/flow-typed/Lbry.js | 3 +++ flow-typed/Lbry.js | 3 +++ src/lbry.js | 9 +++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f495bd8..cb86625 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /node_modules yarn-error.log -package-lock.json \ No newline at end of file +package-lock.json + +# Jetbrains +.idea/ + diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 2977116..78f961a 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -606,12 +606,20 @@ const Lbry = { isConnected: false, connectPromise: null, daemonConnectionString: 'http://localhost:5279', + apiRequestHeaders: { 'Content-Type': 'application/json-rpc' }, // Allow overriding daemon connection string (e.g. to `/api/proxy` for lbryweb) setDaemonConnectionString: value => { Lbry.daemonConnectionString = value; }, + setApiHeader: (key, value) => { + Lbry.apiRequestHeaders = Object.assign(Lbry.apiRequestHeaders, { [key]: value }); + }, + + unsetApiHeader: key => { + Object.keys(Lbry.apiRequestHeaders).includes(key) && delete Lbry.apiRequestHeaders['key']; + }, // Allow overriding Lbry methods overrides: {}, setOverride: (methodName, newMethod) => { @@ -730,6 +738,7 @@ function apiCall(method, params, resolve, reject) { const counter = new Date().getTime(); const options = { method: 'POST', + headers: Lbry.apiRequestHeaders, body: JSON.stringify({ jsonrpc: '2.0', method, diff --git a/dist/flow-typed/Lbry.js b/dist/flow-typed/Lbry.js index 303de6f..e4d9090 100644 --- a/dist/flow-typed/Lbry.js +++ b/dist/flow-typed/Lbry.js @@ -153,7 +153,10 @@ declare type LbryTypes = { connectPromise: ?Promise, connect: () => void, daemonConnectionString: string, + apiRequestHeaders: {[key: string]: string}, setDaemonConnectionString: string => void, + setApiHeader: (string, string) => void, + unsetApiHeader: (string) => void, overrides: { [string]: ?Function }, setOverride: (string, Function) => void, getMediaType: (string, ?string) => string, diff --git a/flow-typed/Lbry.js b/flow-typed/Lbry.js index 303de6f..e4d9090 100644 --- a/flow-typed/Lbry.js +++ b/flow-typed/Lbry.js @@ -153,7 +153,10 @@ declare type LbryTypes = { connectPromise: ?Promise, connect: () => void, daemonConnectionString: string, + apiRequestHeaders: {[key: string]: string}, setDaemonConnectionString: string => void, + setApiHeader: (string, string) => void, + unsetApiHeader: (string) => void, overrides: { [string]: ?Function }, setOverride: (string, Function) => void, getMediaType: (string, ?string) => string, diff --git a/src/lbry.js b/src/lbry.js index 03e7451..3ac88ad 100644 --- a/src/lbry.js +++ b/src/lbry.js @@ -11,12 +11,20 @@ const Lbry: LbryTypes = { isConnected: false, connectPromise: null, daemonConnectionString: 'http://localhost:5279', + apiRequestHeaders: { 'Content-Type': 'application/json-rpc' }, // Allow overriding daemon connection string (e.g. to `/api/proxy` for lbryweb) setDaemonConnectionString: (value: string) => { Lbry.daemonConnectionString = value; }, + setApiHeader: (key: string, value: string) => { + Lbry.apiRequestHeaders = Object.assign(Lbry.apiRequestHeaders, { [key]: value }); + }, + + unsetApiHeader: key => { + Object.keys(Lbry.apiRequestHeaders).includes(key) && delete Lbry.apiRequestHeaders['key']; + }, // Allow overriding Lbry methods overrides: {}, setOverride: (methodName, newMethod) => { @@ -141,6 +149,7 @@ function apiCall(method: string, params: ?{}, resolve: Function, reject: Functio const counter = new Date().getTime(); const options = { method: 'POST', + headers: Lbry.apiRequestHeaders, body: JSON.stringify({ jsonrpc: '2.0', method,