-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b59fb1d
commit e730f74
Showing
1 changed file
with
21 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |