Skip to content

Commit

Permalink
Update srpc-fc.js
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomlsh authored Oct 17, 2024
1 parent b59fb1d commit e730f74
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/utils/srpc-fc.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
let url = 'https://sas.njsc.ltd/srpc'
// ES Module SRPC Client
// https://github.com/yzITI/srpc

const getFunction = name => ((...args) => fetch(url, {
method: 'POST', mode: 'cors', cache: 'no-cache',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ N: name, A: args })
}).then(async r => {
if (r.status === 200) return (await r.json()).R
else throw (await r.text())
}))

const proxyGet = (target, prop) => {
const key = target.key, newKey = key ? key + '.' + prop : prop, f = getFunction(newKey)
f.key = newKey
return new Proxy(f, { get: proxyGet })
export const SRPC = () => {
let url = 'https://sas.njsc.ltd/srpc'
const getFunction = N => ((...A) => fetch(url, {
method: 'POST', mode: 'cors', cache: 'no-cache',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ N, A })
}).then(async r => {
if (r.status === 200) return (await r.json()).R
else throw (await r.text())
}))
const proxyGet = (target, key) => {
const N = target.N || [], newN = [...N, key], f = getFunction(newN)
f.N = newN
return new Proxy(f, { get: proxyGet })
}
return new Proxy((endpoint = '/') => url = endpoint, { get: proxyGet })
}

export default new Proxy((endpoint = '/') => url = endpoint, { get: proxyGet })
export const srpc = SRPC()

export default srpc

0 comments on commit e730f74

Please sign in to comment.