Skip to content

Commit 3a74888

Browse files
docs: example assertion typescript (#472)
* docs: example assertion typescript 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 3246f36 commit 3a74888

7 files changed

Lines changed: 366 additions & 5 deletions

File tree

.llms-snapshots/llms-full.txt

Lines changed: 181 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,10 @@ Write serverless backend logic for your app using TypeScript or Rust. These exam
22312231

22322232
4 items](/docs/examples/functions/rust.md)
22332233

2234+
[## 🗃️ TypeScript
2235+
2236+
1 item](/docs/examples/functions/typescript.md)
2237+
22342238
# Angular Example
22352239

22362240
This project is a note-taking app template built with **Angular**, **TypeScript**, and **Tailwind CSS**, designed to demonstrate integration with Juno for app development. It showcases authentication, data storage, and file storage using Juno's Satellite container.
@@ -3173,6 +3177,14 @@ An example showing how to dynamically generate and store assets (like JSON) in S
31733177

31743178
An example showing how to call external canisters (e.g., ICRC ledger) from a serverless function written in Rust using Juno Satellites.](/docs/examples/functions/rust/canister-calls.md)
31753179

3180+
# TypeScript
3181+
3182+
Examples of writing serverless functions in TypeScript for Juno. Includes patterns like custom assertions, data manipulation and calls.
3183+
3184+
[## 📄️ Assertion
3185+
3186+
An example demonstrating how to write custom assertions in TypeScript for Juno serverless functions.](/docs/examples/functions/typescript/assertion.md)
3187+
31763188
# Rust Assertion Example
31773189

31783190
This example demonstrates how to write a **custom assertion** in **Rust** for a Juno **serverless function**. It shows how to intercept and validate data operations—such as rejecting specific content—before it's written to the datastore.
@@ -3329,7 +3341,7 @@ It’s a great reference for more advanced setups and multi-collection coordinat
33293341
## References
33303342

33313343
* [Serverless Functions Guide](/docs/guides/rust.md)
3332-
* [Rust Functions Development](/docs/build/functions.md)
3344+
* [Functions Development](/docs/build/functions.md)
33333345
* [Rust SDK Reference](/docs/reference/functions/rust/sdk.md)
33343346
* [Rust Utils Reference](/docs/reference/functions/rust/utils.md)
33353347
* [Run Local Development](/docs/guides/local-development.md)
@@ -3513,7 +3525,7 @@ It’s a great reference for more advanced setups and multi-collection coordinat
35133525
## References
35143526

35153527
* [Serverless Functions Guide](/docs/guides/rust.md)
3516-
* [Rust Functions Development](/docs/build/functions.md)
3528+
* [Functions Development](/docs/build/functions.md)
35173529
* [Rust SDK Reference](/docs/reference/functions/rust/sdk.md)
35183530
* [Rust Utils Reference](/docs/reference/functions/rust/utils.md)
35193531
* [Run Local Development](/docs/guides/local-development.md)
@@ -3676,7 +3688,7 @@ juno functions buildjuno functions upgrade
36763688
## References
36773689

36783690
* [Serverless Functions Guide](/docs/guides/rust.md)
3679-
* [Rust Functions Development](/docs/build/functions.md)
3691+
* [Functions Development](/docs/build/functions.md)
36803692
* [Rust SDK Reference](/docs/reference/functions/rust/sdk.md)
36813693
* [Rust Utils Reference](/docs/reference/functions/rust/utils.md)
36823694
* [Run Local Development](/docs/guides/local-development.md)
@@ -3850,7 +3862,7 @@ It’s a great reference for more advanced setups and multi-collection coordinat
38503862
## References
38513863

38523864
* [Serverless Functions Guide](/docs/guides/rust.md)
3853-
* [Rust Functions Development](/docs/build/functions.md)
3865+
* [Functions Development](/docs/build/functions.md)
38543866
* [Rust SDK Reference](/docs/reference/functions/rust/sdk.md)
38553867
* [Rust Utils Reference](/docs/reference/functions/rust/utils.md)
38563868
* [Run Local Development](/docs/guides/local-development.md)
@@ -3870,6 +3882,171 @@ These crates are used to build and extend serverless functions in Rust with Juno
38703882
* [junobuild-shared](https://docs.rs/junobuild-shared): Shared types and helpers for Juno projects. Used by all containers including the Console.
38713883
* [junobuild-storage](https://docs.rs/junobuild-storage): Storage helpers for working with assets and HTTP headers in Juno.
38723884

3885+
# TypeScript Assertion Example
3886+
3887+
This example demonstrates how to write a **custom assertion** in **TypeScript** for a Juno **serverless function**. It shows how to intercept and validate data operations—such as rejecting specific content—before it's written to the datastore.
3888+
3889+
The project includes a minimal frontend to help trigger and test the logic, but the primary focus is the backend assertion.
3890+
3891+
You can browse the source code here: [github.com/junobuild/examples/tree/main/functions/typescript/assertions](https://github.com/junobuild/examples/tree/main/functions/typescript/assertions)
3892+
3893+
---
3894+
3895+
## Folder Structure
3896+
3897+
```
3898+
typescript/assertions/├── src/│ ├── satellite/ # TypeScript Satellite serverless function│ │ └── index.ts # Main TypeScript logic for Satellite│ ├── types/│ │ └── note.ts # Note type and schema│ └── components/ # Minimal frontend React components├── juno.config.ts # Juno Satellite configuration├── package.json # Frontend and serverless dependencies└── ...
3899+
```
3900+
3901+
---
3902+
3903+
## Key Features
3904+
3905+
* **Custom Assertions in TypeScript**: Demonstrates how to reject or validate data before it's saved, using TypeScript serverless functions.
3906+
* **Serverless Integration**: Runs as a Satellite function and integrates with Juno's datastore and authentication system.
3907+
* **Minimal UI for Testing**: A simple frontend is included to test and demonstrate the assertion logic in action.
3908+
3909+
---
3910+
3911+
## Main Backend Components
3912+
3913+
* **src/satellite/index.ts**: The core TypeScript logic for the Satellite serverless function. Implements the custom assertions (e.g., only allow certain valid inputs, etc.).
3914+
* **src/satellite/Cargo.toml**: TypeScript package configuration for the Satellite function.
3915+
3916+
---
3917+
3918+
## Example: Custom Assertion in TypeScript
3919+
3920+
Here’s the actual TypeScript logic from `index.ts`:
3921+
3922+
```
3923+
import { type AssertSetDoc, defineAssert } from "@junobuild/functions";import { decodeDocData } from "@junobuild/functions/sdk";import { type NoteData, NoteDataSchema } from "../types/note";export const assertSetDoc = defineAssert<AssertSetDoc>({ collections: ["notes"], assert: (context) => { const note = decodeDocData<NoteData>(context.data.data.proposed.data); NoteDataSchema.parse(note); if (note.text.toLowerCase().includes("hello")) { console.log("❌ Rejected note containing 'hello':", note.text); throw new Error("The note should not contain the keyword 'hello'."); } }});
3924+
```
3925+
3926+
**Explanation:**
3927+
3928+
* Defines a `NoteData` type and `NoteDataSchema` using [zod](https://zod.dev/) for runtime validation.
3929+
* Uses `defineAssert` to create a custom assertion for the `notes` collection.
3930+
* When a note is created or updated, the assertion checks if the note's text contains the word "hello" (case-insensitive).
3931+
* If it does, the note is rejected and an error is thrown; otherwise, the note is accepted.
3932+
* Prints a message to the log for both accepted and rejected notes.
3933+
3934+
---
3935+
3936+
## How to Run
3937+
3938+
1. **Clone the repo**:
3939+
3940+
```
3941+
git clone https://github.com/junobuild/examplescd typescript/assertions
3942+
```
3943+
3944+
2. **Install dependencies**:
3945+
3946+
```
3947+
npm install
3948+
```
3949+
3950+
3. **Start Juno local emulator**:
3951+
3952+
**Important:**
3953+
3954+
Requires the Juno CLI to be available `npm i -g @junobuild/cli`
3955+
3956+
```
3957+
juno dev start
3958+
```
3959+
3960+
4. **Create a Satellite** for local dev:
3961+
3962+
* Visit [http://localhost:5866](http://localhost:5866) and follow the instructions.
3963+
* Update `juno.config.ts` with your Satellite ID.
3964+
3965+
5. **Create required collections**:
3966+
3967+
* `notes` in Datastore: [http://localhost:5866/datastore](http://localhost:5866/datastore)
3968+
* `images` in Storage: [http://localhost:5866/storage](http://localhost:5866/storage)
3969+
3970+
6. **Start the frontend dev server** (in a separate terminal):
3971+
3972+
```
3973+
npm run dev
3974+
```
3975+
3976+
7. **Build the serverless functions** (in a separate terminal):
3977+
3978+
```
3979+
juno functions build
3980+
```
3981+
3982+
The emulator will automatically upgrade your Satellite and live reload the changes.
3983+
3984+
---
3985+
3986+
## Juno-Specific Configuration
3987+
3988+
* **juno.config.ts**: Defines Satellite IDs for development/production, build source, and predeploy steps. See the [Configuration reference](/docs/reference/configuration.md) for details.
3989+
* **vite.config.ts**: Registers the `juno` plugin to inject environment variables automatically. See the [Vite Plugin reference](/docs/reference/plugins.md#vite-plugin) for more information.
3990+
3991+
---
3992+
3993+
## Production Deployment
3994+
3995+
* Create a Satellite on the [Juno Console](https://console.juno.build) for mainnet.
3996+
* Update `juno.config.ts` with the production Satellite ID.
3997+
* Build and deploy the frontend:
3998+
3999+
```
4000+
npm run buildjuno deploy
4001+
```
4002+
4003+
* Build and upgrade the serverless functions:
4004+
4005+
```
4006+
juno functions buildjuno functions upgrade
4007+
```
4008+
4009+
---
4010+
4011+
## Notes
4012+
4013+
* This example focuses on the TypeScript serverless function; the frontend is intentionally minimal and only included for demonstration purposes.
4014+
* Use this project as a starting point for writing custom assertions and backend logic in TypeScript with Juno.
4015+
4016+
---
4017+
4018+
## Real-World Example
4019+
4020+
Want to see how assertions and serverless logic are used in a live project?
4021+
4022+
Check out [cycles.watch](https://cycles.watch), an open-source app built with Juno:
4023+
4024+
* GitHub: [github.com/peterpeterparker/cycles.watch](https://github.com/peterpeterparker/cycles.watch)
4025+
* Example logic: [src/satellite/index.ts](https://github.com/peterpeterparker/cycles.watch/blob/main/src/satellite/index.ts)
4026+
4027+
This app uses:
4028+
4029+
* `assertSetDoc` to validate requests
4030+
* `onSetDoc` to implement a swap-like feature that performs various canister calls
4031+
* Service modules to keep logic organized
4032+
* A real-world pattern for chaining calls and document insertions with assertions
4033+
4034+
It’s a great reference for more advanced setups and orchestration.
4035+
4036+
---
4037+
4038+
## References
4039+
4040+
* [Serverless Functions Guide](/docs/guides/typescript.md)
4041+
* [Functions Development](/docs/build/functions.md)
4042+
* [TypeScript SDK Reference](/docs/reference/functions/typescript/sdk.md)
4043+
* [TypeScript ic-cdk Reference](/docs/reference/functions/typescript/ic-cdk.md)
4044+
* [TypeScript Utils Reference](/docs/reference/functions/typescript/utils.md)
4045+
* [Run Local Development](/docs/guides/local-development.md)
4046+
* [CLI Reference](/docs/reference/cli.md)
4047+
* [Configuration Reference](/docs/reference/configuration.md)
4048+
* [Datastore Collections](/docs/build/datastore/collections.md)
4049+
38734050
# Using Juno with AI
38744051

38754052
If you're using AI to build with Juno, you can use our `llms.txt` files to help AI tools better understand the platform.

.llms-snapshots/llms.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Juno is your self-contained serverless platform for building full-stack web apps
6464
## Examples - Functions
6565

6666
- [Rust](https://juno.build/docs/examples/functions/rust.md): Examples of writing serverless functions in Rust for Juno. Includes patterns like custom assertions, data manipulation and calls.
67+
- [TypeScript](https://juno.build/docs/examples/functions/typescript.md): Examples of writing serverless functions in TypeScript for Juno. Includes patterns like custom assertions, data manipulation and calls.
6768

6869
## Examples - Functions - Rust
6970

@@ -72,6 +73,10 @@ Juno is your self-contained serverless platform for building full-stack web apps
7273
- [Generating Assets with Rust Serverless Functions](https://juno.build/docs/examples/functions/rust/generating-assets.md): An example showing how to dynamically generate and store assets (like JSON) in Storage using Rust in Juno Satellites.
7374
- [Mutating Documents with Rust Hooks](https://juno.build/docs/examples/functions/rust/mutating-docs.md): An example demonstrating how to modify and re-save documents in Juno Satellites using Rust hooks.
7475

76+
## Examples - Functions - Typescript
77+
78+
- [TypeScript Assertions Example](https://juno.build/docs/examples/functions/typescript/assertion.md): An example demonstrating how to write custom assertions in TypeScript for Juno serverless functions.
79+
7580
## Guides
7681

7782
- [AI](https://juno.build/docs/guides/ai.md): Learn how to use Juno's llms.txt files to provide AI tools with better context for building serverless functions, deploying satellites, and integrating the SDK.

docs/examples/functions/rust/components/references.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## References
22

33
- [Serverless Functions Guide](../../../../guides/rust.mdx)
4-
- [Rust Functions Development](../../../../build/functions/index.md)
4+
- [Functions Development](../../../../build/functions/index.md)
55
- [Rust SDK Reference](../../../../reference/functions/rust/sdk.mdx)
66
- [Rust Utils Reference](../../../../reference/functions/rust/utils.mdx)
77
- [Run Local Development](../../../../guides/local-development.mdx)

0 commit comments

Comments
 (0)