deno.land / x / lightrpc@1.0.0 / src / client.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export function clientFunc< T extends { [key: string]: (...args: any[]) => unknown },>( endPoint: string, headers?: { [key: string]: string }, credentials?: RequestCredentials,): T { return new Proxy({}, { get(_target, prop, _receive) { return async (...args: unknown[]) => { try { const response = await fetch(endPoint, { method: "POST", body: JSON.stringify({ methodName: prop, params: args }), headers, credentials, });
if (!response.ok) throw new Error("Response status is not OK!");
const responseJson = await response.json(); if (!responseJson) throw new Error("Response is not json!"); if (!responseJson?.result) { throw new Error("Response is not have property 'result'!"); }
return responseJson.result; } catch (e) { console.error(e); } }; }, }) as T;}
lightrpc

Version Info

Tagged at
a year ago