forked from Phala-Network/phat-function-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
43 lines (42 loc) · 1.79 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
import '@phala/pink-env'
export default function main() {
/*
The `pink.httpRequest()` allows for you to make a single HTTP request from your function to an HTTP endpoint.
You will have to define your args:
- `url: string` – The URL to send the request to.
- `method: string` – The HTTP method to use for the request (e.g. GET, POST, PUT). Defaults to GET.
- `headers: Headers` – An map-like object containing the headers to send with the request.
- `body: Uint8Array | string` – The body of the request, either as a Uint8Array or a string.
- `returnTextBody: boolean` – A flag indicating whether the response body should be returned as a string (true) or a Uint8Array (false).
Returned is the `Object` response from the HTTP request containing the following fields:
- `{number} statusCode` - The HTTP status code of the response.
- `{string} reasonPhrase` - The reason phrase of the response.
- `{Headers} headers` - An object containing the headers of the response.
- `{(Uint8Array|string)} body` - The response body, either as a `Uint8Array` or a string depending on the value of `args.returnTextBody`.
*/
const response = pink.httpRequest({
url: 'https://api-mumbai.lens.dev/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'phat-contract',
},
body: JSON.stringify({
query: `query Profile {
profile(request: { profileId: "0x01" }) {
stats {
totalFollowers
totalFollowing
totalPosts
totalComments
totalMirrors
totalPublications
totalCollects
}
}
}`
}),
returnTextBody: true,
})
return response.body
}