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
* 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>
Copy file name to clipboardExpand all lines: .llms-snapshots/llms-full.txt
+181-4Lines changed: 181 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2231,6 +2231,10 @@ Write serverless backend logic for your app using TypeScript or Rust. These exam
2231
2231
2232
2232
4 items](/docs/examples/functions/rust.md)
2233
2233
2234
+
[## 🗃️ TypeScript
2235
+
2236
+
1 item](/docs/examples/functions/typescript.md)
2237
+
2234
2238
# Angular Example
2235
2239
2236
2240
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
3173
3177
3174
3178
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)
3175
3179
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
+
3176
3188
# Rust Assertion Example
3177
3189
3178
3190
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
* [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
3870
3882
* [junobuild-shared](https://docs.rs/junobuild-shared): Shared types and helpers for Juno projects. Used by all containers including the Console.
3871
3883
* [junobuild-storage](https://docs.rs/junobuild-storage): Storage helpers for working with assets and HTTP headers in Juno.
3872
3884
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.
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:
Copy file name to clipboardExpand all lines: .llms-snapshots/llms.txt
+5Lines changed: 5 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,7 @@ Juno is your self-contained serverless platform for building full-stack web apps
64
64
## Examples - Functions
65
65
66
66
- [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.
67
68
68
69
## Examples - Functions - Rust
69
70
@@ -72,6 +73,10 @@ Juno is your self-contained serverless platform for building full-stack web apps
72
73
- [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.
73
74
- [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.
74
75
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
+
75
80
## Guides
76
81
77
82
- [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.
0 commit comments