Skip to content

Commit 31a7fba

Browse files
docs: httpRequest reference (#690)
* docs: httpRequest reference Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * 📄 Update LLMs.txt snapshot for PR review --------- Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 683b20f commit 31a7fba

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

.llms-snapshots/llms-full.txt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7412,7 +7412,7 @@ Here is an example of an hook which fetches an API to get the link to an image o
74127412
Here is an example of an `onSetDoc` hook which fetches an API to get the link to an image of a dog and saves that information within the Datastore. While this might not be a practical real-world use case, it is simple enough to demonstrate the feature.
74137413

74147414
```
7415-
import { defineHook, type OnSetDoc, SetDoc } from "@junobuild/functions";import { j } from "@junobuild/schema";import { httpRequest, HttpRequestArgs } from "@junobuild/functions/ic-cdk";import { encodeDocData, setDocStore } from "@junobuild/functions/sdk";// The data of the document we are looking to update in the Satellite's Datastore.const DogDataSchema = j.strictObject({ src: j.string().optional()});// We are using the Dog CEO API in this example.// https://dog.ceo/dog-api///// Its endpoint "random" returns such JSON data:// {// "message": "https://images.dog.ceo/breeds/mountain-swiss/n02107574_1118.jpg",// "status": "success"// }//// That's why we declare a struct that matches the structure of the answer.const DogApiResponseSchema = j.strictObject({ message: j.url(), status: j.string()});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["dogs"], run: async ({ caller, data: { collection, key, data: { after: { description, version } } } }) => { // 1. Prepare the HTTP GET request const url = "https://dog.ceo/api/breeds/image/random"; const args: HttpRequestArgs = { url, method: "GET", headers: [], // Use a single node as we do not require that a trust level for fetching a dog image for demo purposes. 😉 isReplicated: false }; // 2. Execute the HTTP request. const result = await httpRequest(args); // 3. Transform the response to a structured data object. const decoder = new TextDecoder(); const body = decoder.decode(result.body); const dogResponse = DogApiResponseSchema.parse(JSON.parse(body)); // 4. Our goal is to update the document in the Datastore with an update that contains the link to the image fetched from the API we just called. const dogData = DogDataSchema.parse({ src: dogResponse.message }); // 5. We encode those data back to blob because the Datastore holds data as blob. const encodedData = encodeDocData(dogData); // 6. Then we construct the parameters required to call the function that save the data in the Datastore. const doc: SetDoc = { description, version, data: encodedData }; // 7. We store the data in the Datastore for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller, collection, key, doc }); }});
7415+
import { defineHook, type OnSetDoc, SetDoc } from "@junobuild/functions";import { j } from "@junobuild/schema";import { httpRequest, HttpRequestArgs } from "@junobuild/functions/ic-cdk";import { encodeDocData, setDocStore } from "@junobuild/functions/sdk";// The data of the document we are looking to update in the Satellite's Datastore.const DogDataSchema = j.strictObject({ src: j.string().optional()});// We are using the Dog CEO API in this example.// https://dog.ceo/dog-api///// Its endpoint "random" returns such JSON data:// {// "message": "https://images.dog.ceo/breeds/mountain-swiss/n02107574_1118.jpg",// "status": "success"// }//// That's why we declare a struct that matches the structure of the answer.const DogApiResponseSchema = j.strictObject({ message: j.url(), status: j.string()});export const onSetDoc = defineHook<OnSetDoc>({ collections: ["dogs"], run: async ({ caller, data: { collection, key, data: { after: { description, version } } } }) => { // 1. Prepare the HTTP GET request const url = "https://dog.ceo/api/breeds/image/random"; const args: HttpRequestArgs = { url, method: "GET", // Use a single node as we do not require that a trust level for fetching a dog image for demo purposes. 😉 isReplicated: false }; // 2. Execute the HTTP request. const result = await httpRequest(args); // 3. Transform the response to a structured data object. const decoder = new TextDecoder(); const body = decoder.decode(result.body); const dogResponse = DogApiResponseSchema.parse(JSON.parse(body)); // 4. Our goal is to update the document in the Datastore with an update that contains the link to the image fetched from the API we just called. const dogData = DogDataSchema.parse({ src: dogResponse.message }); // 5. We encode those data back to blob because the Datastore holds data as blob. const encodedData = encodeDocData(dogData); // 6. Then we construct the parameters required to call the function that save the data in the Datastore. const doc: SetDoc = { description, version, data: encodedData }; // 7. We store the data in the Datastore for the same caller as the one who triggered the original on_set_doc, in the same collection with the same key as well. setDocStore({ caller, collection, key, doc }); }});
74167416
```
74177417

74187418
As with the previous example, the hook will asynchronously update the document. If you wait a bit before retrieving the document in your frontend, you might notice that the source of the image has been updated by your hook.
@@ -12820,6 +12820,42 @@ function call<T>(params: CallParams): Promise<T | undefined>;
1282012820

1282112821
---
1282212822

12823+
## httpRequest
12824+
12825+
Performs an HTTP request from a Juno serverless function.
12826+
12827+
```
12828+
function httpRequest(args: HttpRequestArgs): Promise<HttpRequestResult>;
12829+
```
12830+
12831+
📦 Import from `@junobuild/functions/ic-cdk`
12832+
12833+
#### Parameters:
12834+
12835+
* `args`: The HTTP request parameters.
12836+
* `url`: The requested URL.
12837+
* `method`: The HTTP method — `GET`, `POST`, or `HEAD`.
12838+
* `headers` (optional): A list of HTTP request headers.
12839+
* `body` (optional): The request body as a `Uint8Array`.
12840+
* `maxResponseBytes` (optional): The maximal size of the response in bytes.
12841+
* `transform` (optional): The name of a query function used to transform the response before consensus. If provided, a corresponding query must be declared using `defineQuery`.
12842+
* `isReplicated` (optional): Whether all nodes should perform the request and agree on the response, or just one node. Defaults to all nodes if not specified.
12843+
12844+
#### Returns:
12845+
12846+
* `Promise<HttpRequestResult>`: A promise resolving to the HTTP response, containing `status` (bigint), `headers`, and `body` (Uint8Array).
12847+
12848+
#### Throws:
12849+
12850+
* `ZodError` if the provided arguments do not match the expected schema.
12851+
* `Error` if the HTTP request fails.
12852+
12853+
#### Notes:
12854+
12855+
* This function is a JavaScript binding for the Rust function [ic\_cdk::management\_canister::http\_request()](https://docs.rs/ic-cdk/0.19.0/ic_cdk/management_canister/fn.http_request.html).
12856+
12857+
---
12858+
1282312859
## msgCaller
1282412860

1282512861
Returns the Principal ID of the caller of the current function.

docs/reference/functions/typescript/ic-cdk.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ function call<T>(params: CallParams): Promise<T | undefined>;
8181

8282
---
8383

84+
## httpRequest
85+
86+
Performs an HTTP request from a Juno serverless function.
87+
88+
```typescript
89+
function httpRequest(args: HttpRequestArgs): Promise<HttpRequestResult>;
90+
```
91+
92+
📦 Import from `@junobuild/functions/ic-cdk`
93+
94+
#### Parameters:
95+
96+
- `args`: The HTTP request parameters.
97+
- `url`: The requested URL.
98+
- `method`: The HTTP method — `GET`, `POST`, or `HEAD`.
99+
- `headers` (optional): A list of HTTP request headers.
100+
- `body` (optional): The request body as a `Uint8Array`.
101+
- `maxResponseBytes` (optional): The maximal size of the response in bytes.
102+
- `transform` (optional): The name of a query function used to transform the response before consensus. If provided, a corresponding query must be declared using `defineQuery`.
103+
- `isReplicated` (optional): Whether all nodes should perform the request and agree on the response, or just one node. Defaults to all nodes if not specified.
104+
105+
#### Returns:
106+
107+
- `Promise<HttpRequestResult>`: A promise resolving to the HTTP response, containing `status` (bigint), `headers`, and `body` (Uint8Array).
108+
109+
#### Throws:
110+
111+
- `ZodError` if the provided arguments do not match the expected schema.
112+
- `Error` if the HTTP request fails.
113+
114+
#### Notes:
115+
116+
- This function is a JavaScript binding for the Rust function [ic_cdk::management_canister::http_request()](https://docs.rs/ic-cdk/0.19.0/ic_cdk/management_canister/fn.http_request.html).
117+
118+
---
119+
84120
## msgCaller
85121

86122
Returns the Principal ID of the caller of the current function.

0 commit comments

Comments
 (0)