Skip to content

Commit faf6a44

Browse files
committed
Improve RPC call
1 parent a24a19d commit faf6a44

5 files changed

Lines changed: 46 additions & 23 deletions

File tree

src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ function App(): JSX.Element {
1111
const [row3,setRow3] = useState([]);
1212

1313
useEffect((): void => {
14-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["80d2590ad04e36fb1d077a9b9e3a8bba76defdf8","b58dfaeab6c70754d792cdd9b56ff59b90aea334"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"release_time":">1731193200","include_purchase_receipt":true}).then(json => {
14+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["80d2590ad04e36fb1d077a9b9e3a8bba76defdf8","b58dfaeab6c70754d792cdd9b56ff59b90aea334"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"release_time":">1731193200","include_purchase_receipt":true},null,import.meta.env.VITE_DAEMON_PROXY==='true').then(json => {
1515
setRow1(json.result.items);
1616
});
1717
},[]);
1818

1919
useEffect((): void => {
20-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":[],"not_channel_ids":[],"order_by":["effective_amount"],"has_source":true,"release_time":">1762902000","limit_claims_per_channel":2,"include_purchase_receipt":true}).then(json => {
20+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'claim_search',{"page_size":4,"claim_type":["stream"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":[],"not_channel_ids":[],"order_by":["effective_amount"],"has_source":true,"release_time":">1762902000","limit_claims_per_channel":2,"include_purchase_receipt":true},null,import.meta.env.VITE_DAEMON_PROXY==='true').then(json => {
2121
setRow2(json.result.items);
2222
});
2323
},[]);
2424

2525
useEffect((): void => {
26-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["3fda836a92faaceedfe398225fb9b2ee2ed1f01a"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"include_purchase_receipt":true}).then(json => {
26+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["3fda836a92faaceedfe398225fb9b2ee2ed1f01a"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"include_purchase_receipt":true},null,import.meta.env.VITE_DAEMON_PROXY==='true').then(json => {
2727
setRow3(json.result.items);
2828
});
2929
},[]);

src/ClaimPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ function SettingsPage(): JSX.Element{
2525
const [markdown,setMarkdown] = useState('');
2626

2727
useEffect(() => {
28-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','resolve',{urls:[claim]}).then((json: object): void => {
28+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'resolve',{urls:[claim]},null,import.meta.env.VITE_DAEMON_PROXY==='true').then((json: object): void => {
2929
setClaimResolveData(json.result[claim] ?? null);
3030
});
3131
}, [claim]);
3232

3333
useEffect(() => {
34-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','get',{uri:claim}).then((json: object): void => {
34+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'get',{uri:claim},null,import.meta.env.VITE_DAEMON_PROXY==='true').then((json: object): void => {
3535
setClaimGetData(json.result);
3636
});
3737
}, [claim]);
@@ -54,7 +54,7 @@ function SettingsPage(): JSX.Element{
5454
{claimResolveData.value.source.media_type==='text/markdown'?(
5555
claimGetData?(
5656
<div className="markdown">
57-
{downloadMarkdownFile(claimGetData,setMarkdown)}
57+
{downloadMarkdownFile(claimGetData,setMarkdown) && null}
5858
<Markdown>{markdown}</Markdown>
5959
</div>
6060
):null

src/LBRY.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
1-
async function rpc(url: string,proxy: boolean,method: string,params?: object,authorization?: string): Promise<object>{
2-
const input: URL = new URL(proxy?'/api/rpc':url,window.location.href);
3-
if(proxy){
4-
input.searchParams.set('url',url);
5-
}
1+
const VERSION_2_0: string = '2.0';
2+
3+
function generateID(): number{
4+
return Math.ceil(Math.random()*65536)+1;
5+
}
66

7-
const headers: Headers = new Headers({
8-
'Content-Type': 'application/json',
9-
});
7+
async function rpcDirect(input: string|URL|Request,method: string,params?: object,authorization?: string): Promise<object>{
8+
const headers: Headers = new Headers();
109
if(authorization){
1110
headers.append('Authorization',authorization);
1211
}
12+
headers.append('Content-Type','application/json');
13+
14+
const message: object = {
15+
jsonrpc: VERSION_2_0,
16+
method: method,
17+
params: params,
18+
id: generateID(),
19+
};
1320

1421
return await (await fetch(input,{
1522
method: 'POST',
1623
headers: headers,
17-
body: JSON.stringify({
18-
jsonrpc: '2.0',
19-
method: method,
20-
params: params,
21-
id: Math.ceil(Math.random()*65536),
22-
}),
24+
body: JSON.stringify(message),
2325
})).json();
2426
}
2527

28+
async function rpcProxy(input: string|URL|Request,method: string,params?: object,authorization?: string): Promise<object>{
29+
const url: URL = new URL('/api/rpc',window.location.href);
30+
if(typeof input==='string'){
31+
url.searchParams.set('url',input);
32+
}
33+
if(input instanceof URL){
34+
url.searchParams.set('url',input.href);
35+
}
36+
if(input instanceof Request){
37+
url.searchParams.set('url',input.url);
38+
}
39+
return await rpcDirect(url,method,params,authorization);
40+
}
41+
42+
async function rpc(input: string|URL|Request,method: string,params?: object,authorization?: string,proxy?: boolean): Promise<object>{
43+
if(proxy){
44+
return await rpcProxy(input,method,params,authorization);
45+
}
46+
return await rpcDirect(input,method,params,authorization);
47+
}
48+
2649
export default {
2750
rpc,
2851
};

src/SettingsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function SettingsPage(){
66
const [settings,setSettings]: [object,(value: object) => object] = useState();
77

88
useEffect((): void => {
9-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','settings_get').then((json: object): void => {
9+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'settings_get',null,null,import.meta.env.VITE_DAEMON_PROXY==='true').then((json: object): void => {
1010
setSettings(json.result || json.error?.message || 'Unknown error');
1111
});
1212
},[]);

src/WalletPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function WalletPage(){
88
const [transactions,setTransactions] = useState([]);
99

1010
useEffect((): void => {
11-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','wallet_balance').then(json => {
11+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'wallet_balance',null,null,import.meta.env.VITE_DAEMON_PROXY==='true').then(json => {
1212
if(json.error){
1313
document.getElementById('wallet').innerHTML = json.error.message;
1414
return;
@@ -18,7 +18,7 @@ function WalletPage(){
1818
},[]);
1919

2020
useEffect((): void => {
21-
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','txo_list').then(json => {
21+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,'txo_list',null,null,import.meta.env.VITE_DAEMON_PROXY==='true').then(json => {
2222
if(json.error){
2323
document.getElementById('transactions').innerHTML = json.error.message;
2424
return;

0 commit comments

Comments
 (0)