You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .llms-snapshots/llms-full.txt
+37-1Lines changed: 37 additions & 1 deletion
Original file line number
Diff line number
Diff 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
7412
7412
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.
7413
7413
7414
7414
```
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 }); }});
7416
7416
```
7417
7417
7418
7418
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>;
12820
12820
12821
12821
---
12822
12822
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
+
12823
12859
## msgCaller
12824
12860
12825
12861
Returns the Principal ID of the caller of the current function.
Copy file name to clipboardExpand all lines: docs/reference/functions/typescript/ic-cdk.mdx
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,42 @@ function call<T>(params: CallParams): Promise<T | undefined>;
81
81
82
82
---
83
83
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
+
84
120
## msgCaller
85
121
86
122
Returns the Principal ID of the caller of the current function.
0 commit comments