-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.ts
50 lines (42 loc) · 1.2 KB
/
encode.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
interface RpcRequest {
id: string;
args: string[];
}
interface Params {
host: string;
app: string;
rpcs: RpcRequest[];
}
interface Result {
url: URL;
headers: Headers;
body: URLSearchParams;
}
function generateReqId() {
return Math.floor(Math.random() * 900000) + 100000;
}
function buildFreqList(rpcs: RpcRequest[]) {
function envelope(rpc: RpcRequest, index: number) {
return [rpc.id, JSON.stringify(rpc.args), null, index > 0 ? index.toString() : "generic"];
}
if (rpcs.length === 1) {
return [envelope(rpcs[0], 0)];
}
const freq = [];
for (let i = 0; i < rpcs.length; i++) {
freq.push(envelope(rpcs[i], i + 1));
}
return freq;
}
export function preparedBatchExecute(params: Params): Result {
const url = new URL(`https://${params.host}/_/${params.app}/data/batchexecute`);
url.searchParams.append("rpcids", params.rpcs.map((rpc) => rpc.id).join(","));
url.searchParams.append("_reqid", generateReqId().toString());
const headers = new Headers({
"content-type": "application/x-www-form-urlencoded;charset=utf-8",
});
const body = new URLSearchParams({
"f.req": JSON.stringify([buildFreqList(params.rpcs)]),
});
return { url, headers, body };
}