-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
92 lines (79 loc) · 2.81 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import axios from 'axios';
declare const _STD_: any;
_STD_.ws.open(
[
"wss://websocket-proxy-1.prod.gke.acurast.com/",
"wss://websocket-proxy-2.prod.gke.acurast.com/",
],
() => {
_STD_.ws.registerPayloadHandler(async ({ sender, recipient, payload }) => {
const decoded = Buffer.from(payload, "hex").toString("utf8");
try {
const parsed = JSON.parse(decoded);
let response;
let responseType: "success" | "error" = "success";
const body = parsed.body.method;
console.log("Body Method:", body); // "buckets"
const userAgent = parsed.headers["User-Agent"];
console.log("User-Agent:", userAgent); // "apillon-cloud-function/1.0"
const path = parsed.path;
console.log("Path:", path); // "/storage/buckets"
const queryStringParameters = parsed.queryStringParameters;
console.log("Query String Parameters:", queryStringParameters); // limit=100&offset=0
const multiValueQueryStringParameters = parsed.multiValueQueryStringParameters;
console.log("Multi Value Query String Parameters:", multiValueQueryStringParameters); // {"value": "1,2,3"}
const pathParameters = parsed.pathParameters;
console.log("Path Parameters:", pathParameters); // { function_uuid: '2cdf9f64...' }
switch (parsed.body.method) {
case "buckets":
try {
response = await sendGetRequest(
"https://api.apillon.io/storage/buckets",
_STD_.env["APILLON_API_KEY"]
);
} catch (e: any) {
responseType = "error";
response = e;
}
break;
case "nfts":
try {
response = await sendGetRequest(
"https://api.apillon.io/nfts/collections",
_STD_.env["APILLON_API_KEY"]
);
} catch (e: any) {
responseType = "error";
response = e;
}
break;
default:
responseType = "error";
response = `UNKNOWN METHOD: ${JSON.stringify(parsed)}`;
}
const responseObject = { type: responseType, data: response }
_STD_.ws.send(
sender,
Buffer.from(JSON.stringify(responseObject), "utf8").toString("hex")
);
} catch (e) {
_STD_.ws.send(
sender,
Buffer.from(`Error: ${e}`, "utf8").toString("hex")
);
}
});
},
(err: any) => console.error(`open: error ${err}`)
);
async function sendGetRequest(url: string, apiKey: string) {
try {
const { data } = await axios.get(url, {
headers: { Authorization: `Basic ${apiKey}` },
});
return data;
} catch (error) {
console.error("Error sending GET request:", error);
throw error;
}
}