junobuild_satellite/
lib.rs

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#![doc = include_str!("../README.md")]

mod auth;
mod controllers;
mod db;
mod guards;
mod hooks;
mod impls;
mod logs;
mod memory;
mod random;
mod rules;
mod satellite;
mod storage;
mod types;
mod version;

use crate::auth::types::config::AuthenticationConfig;
use crate::db::types::config::DbConfig;
use crate::guards::{caller_is_admin_controller, caller_is_controller};
use crate::types::interface::{Config, RulesType};
use crate::version::SATELLITE_VERSION;
use ic_cdk::api::trap;
use ic_cdk_macros::{init, post_upgrade, pre_upgrade, query, update};
use junobuild_collections::types::core::CollectionKey;
use junobuild_collections::types::interface::{DelRule, SetRule};
use junobuild_collections::types::rules::Rule;
use junobuild_shared::types::core::DomainName;
use junobuild_shared::types::core::{Blob, Key};
use junobuild_shared::types::domain::CustomDomains;
use junobuild_shared::types::interface::{
    DeleteControllersArgs, DepositCyclesArgs, MemorySize, SetControllersArgs,
};
use junobuild_shared::types::list::ListParams;
use junobuild_shared::types::list::ListResults;
use junobuild_shared::types::state::Controllers;
use junobuild_storage::http::types::{
    HttpRequest, HttpResponse, StreamingCallbackHttpResponse, StreamingCallbackToken,
};
use junobuild_storage::types::config::StorageConfig;
use junobuild_storage::types::interface::{
    AssetNoContent, CommitBatch, InitAssetKey, InitUploadResult, UploadChunk, UploadChunkResult,
};
use junobuild_storage::types::state::FullPath;

// ============================================================================================
// START: Re-exported Types
//
// These types are made available for use in Serverless Functions.
// ============================================================================================
pub use crate::controllers::store::{get_admin_controllers, get_controllers};
pub use crate::db::store::{
    count_collection_docs_store, count_docs_store, delete_doc_store, delete_docs_store,
    delete_filtered_docs_store, get_doc_store, list_docs_store, set_doc_store,
};
pub use crate::db::types::interface::{DelDoc, SetDoc};
pub use crate::db::types::state::Doc;
pub use crate::logs::loggers::{
    debug, debug_with_data, error, error_with_data, info, info_with_data, log, log_with_data, warn,
    warn_with_data,
};
pub use crate::logs::types::logs::{Log, LogLevel};
pub use crate::storage::handlers::set_asset_handler;
pub use crate::storage::store::{
    count_assets_store, count_collection_assets_store, delete_asset_store, delete_assets_store,
    delete_filtered_assets_store, get_asset_store, get_content_chunks_store, list_assets_store,
};
pub use crate::types::hooks::{
    AssertDeleteAssetContext, AssertDeleteDocContext, AssertSetDocContext,
    AssertUploadAssetContext, HookContext, OnDeleteAssetContext, OnDeleteDocContext,
    OnDeleteFilteredAssetsContext, OnDeleteFilteredDocsContext, OnDeleteManyAssetsContext,
    OnDeleteManyDocsContext, OnSetDocContext, OnSetManyDocsContext, OnUploadAssetContext,
};
// ============================================================================================
// END: Re-exported Types
// ============================================================================================

// ---------------------------------------------------------
// Init and Upgrade
// ---------------------------------------------------------

#[doc(hidden)]
#[init]
pub fn init() {
    satellite::init();
}

#[doc(hidden)]
#[pre_upgrade]
pub fn pre_upgrade() {
    satellite::pre_upgrade();
}

#[doc(hidden)]
#[post_upgrade]
pub fn post_upgrade() {
    satellite::post_upgrade();
}

// ---------------------------------------------------------
// Db
// ---------------------------------------------------------

#[doc(hidden)]
#[update]
pub fn set_doc(collection: CollectionKey, key: Key, doc: SetDoc) -> Doc {
    satellite::set_doc(collection, key, doc)
}

#[doc(hidden)]
#[query]
pub fn get_doc(collection: CollectionKey, key: Key) -> Option<Doc> {
    satellite::get_doc(collection, key)
}

#[doc(hidden)]
#[update]
pub fn del_doc(collection: CollectionKey, key: Key, doc: DelDoc) {
    satellite::del_doc(collection, key, doc);
}

#[doc(hidden)]
#[query]
pub fn list_docs(collection: CollectionKey, filter: ListParams) -> ListResults<Doc> {
    satellite::list_docs(collection, filter)
}

#[doc(hidden)]
#[query]
pub fn count_docs(collection: CollectionKey, filter: ListParams) -> usize {
    satellite::count_docs(collection, filter)
}

#[doc(hidden)]
#[query]
pub fn get_many_docs(docs: Vec<(CollectionKey, Key)>) -> Vec<(Key, Option<Doc>)> {
    satellite::get_many_docs(docs)
}

#[doc(hidden)]
#[update]
pub fn set_many_docs(docs: Vec<(CollectionKey, Key, SetDoc)>) -> Vec<(Key, Doc)> {
    satellite::set_many_docs(docs)
}

#[doc(hidden)]
#[update]
pub fn del_many_docs(docs: Vec<(CollectionKey, Key, DelDoc)>) {
    satellite::del_many_docs(docs)
}

#[doc(hidden)]
#[update]
pub fn del_filtered_docs(collection: CollectionKey, filter: ListParams) {
    satellite::del_filtered_docs(collection, filter)
}

#[doc(hidden)]
#[update(guard = "caller_is_controller")]
pub fn del_docs(collection: CollectionKey) {
    satellite::del_docs(collection)
}

#[doc(hidden)]
#[query(guard = "caller_is_controller")]
pub fn count_collection_docs(collection: CollectionKey) -> usize {
    satellite::count_collection_docs(collection)
}

// ---------------------------------------------------------
// Rules
// ---------------------------------------------------------

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn get_rule(rules_type: RulesType, collection: CollectionKey) -> Option<Rule> {
    satellite::get_rule(&rules_type, &collection)
}

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn list_rules(rules_type: RulesType) -> Vec<(CollectionKey, Rule)> {
    satellite::list_rules(rules_type)
}

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_rule(rules_type: RulesType, collection: CollectionKey, rule: SetRule) -> Rule {
    satellite::set_rule(rules_type, collection, rule)
}

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn del_rule(rules_type: RulesType, collection: CollectionKey, rule: DelRule) {
    satellite::del_rule(rules_type, collection, rule)
}

// ---------------------------------------------------------
// Controllers
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_controllers(args: SetControllersArgs) -> Controllers {
    satellite::set_controllers(args)
}

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn del_controllers(args: DeleteControllersArgs) -> Controllers {
    satellite::del_controllers(args)
}

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn list_controllers() -> Controllers {
    satellite::list_controllers()
}

// ---------------------------------------------------------
// Custom domains
// ---------------------------------------------------------

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn list_custom_domains() -> CustomDomains {
    satellite::list_custom_domains()
}

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_custom_domain(domain_name: DomainName, bn_id: Option<String>) {
    satellite::set_custom_domain(domain_name, bn_id);
}

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn del_custom_domain(domain_name: DomainName) {
    satellite::del_custom_domain(domain_name);
}

// ---------------------------------------------------------
// Config
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn get_config() -> Config {
    satellite::get_config()
}

// ---------------------------------------------------------
// Authentication config
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_auth_config(config: AuthenticationConfig) {
    satellite::set_auth_config(config);
}

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn get_auth_config() -> Option<AuthenticationConfig> {
    satellite::get_auth_config()
}

// ---------------------------------------------------------
// Db config
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_db_config(config: DbConfig) {
    satellite::set_db_config(config);
}

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn get_db_config() -> Option<DbConfig> {
    satellite::get_db_config()
}

// ---------------------------------------------------------
// Storage config
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub fn set_storage_config(config: StorageConfig) {
    satellite::set_storage_config(config);
}

#[doc(hidden)]
#[query(guard = "caller_is_admin_controller")]
pub fn get_storage_config() -> StorageConfig {
    satellite::get_storage_config()
}

// ---------------------------------------------------------
// Http
// ---------------------------------------------------------

#[doc(hidden)]
#[query]
pub fn http_request(request: HttpRequest) -> HttpResponse {
    satellite::http_request(request)
}

#[doc(hidden)]
#[query]
pub fn http_request_streaming_callback(
    callback: StreamingCallbackToken,
) -> StreamingCallbackHttpResponse {
    satellite::http_request_streaming_callback(callback)
}

//
// Storage
//

#[doc(hidden)]
#[update]
pub fn init_asset_upload(init: InitAssetKey) -> InitUploadResult {
    satellite::init_asset_upload(init)
}

#[doc(hidden)]
#[update]
pub fn upload_asset_chunk(chunk: UploadChunk) -> UploadChunkResult {
    satellite::upload_asset_chunk(chunk)
}

#[doc(hidden)]
#[update]
pub fn commit_asset_upload(commit: CommitBatch) {
    satellite::commit_asset_upload(commit);
}

#[doc(hidden)]
#[query]
pub fn list_assets(collection: CollectionKey, filter: ListParams) -> ListResults<AssetNoContent> {
    satellite::list_assets(collection, filter)
}

#[doc(hidden)]
#[query]
pub fn count_assets(collection: CollectionKey, filter: ListParams) -> usize {
    satellite::count_assets(collection, filter)
}

#[doc(hidden)]
#[update]
pub fn del_asset(collection: CollectionKey, full_path: FullPath) {
    satellite::del_asset(collection, full_path);
}

#[doc(hidden)]
#[update]
pub fn del_many_assets(assets: Vec<(CollectionKey, String)>) {
    satellite::del_many_assets(assets);
}

#[doc(hidden)]
#[update]
pub fn del_filtered_assets(collection: CollectionKey, filter: ListParams) {
    satellite::del_filtered_assets(collection, filter)
}

#[doc(hidden)]
#[update(guard = "caller_is_controller")]
pub fn del_assets(collection: CollectionKey) {
    satellite::del_assets(collection);
}

#[doc(hidden)]
#[query(guard = "caller_is_controller")]
pub fn count_collection_assets(collection: CollectionKey) -> usize {
    satellite::count_collection_assets(collection)
}

#[doc(hidden)]
#[query]
pub fn get_asset(collection: CollectionKey, full_path: FullPath) -> Option<AssetNoContent> {
    satellite::get_asset(collection, full_path)
}

#[doc(hidden)]
#[query]
pub fn get_many_assets(
    assets: Vec<(CollectionKey, FullPath)>,
) -> Vec<(FullPath, Option<AssetNoContent>)> {
    satellite::get_many_assets(assets)
}

// ---------------------------------------------------------
// Mgmt
// ---------------------------------------------------------

#[doc(hidden)]
#[update(guard = "caller_is_admin_controller")]
pub async fn deposit_cycles(args: DepositCyclesArgs) {
    junobuild_shared::mgmt::ic::deposit_cycles(args)
        .await
        .unwrap_or_else(|e| trap(&e))
}

#[doc(hidden)]
#[query]
pub fn version() -> String {
    SATELLITE_VERSION.to_string()
}

#[doc(hidden)]
#[query(guard = "caller_is_controller")]
pub fn memory_size() -> MemorySize {
    junobuild_shared::canister::memory_size()
}

/// Include the stock Juno satellite features into your Juno application.
///
/// The `include_satellite!` macro allows you to easily import and use all the essential features and
/// functionalities provided by the Juno satellite crate (`junobuild_satellite`). These features include
/// various functions and utilities for managing documents, assets, controllers, rules, custom domains,
/// and more, effectively supercharging the functionality of your Juno dapp.
///
/// Example:
/// ```rust
/// use junobuild_satellite::include_satellite;
///
/// // Include Juno satellite features
/// include_satellite!();
/// ```
///
#[macro_export]
macro_rules! include_satellite {
    () => {
        use junobuild_satellite::{
            commit_asset_upload, count_assets, count_collection_assets, count_collection_docs,
            count_docs, del_asset, del_assets, del_controllers, del_custom_domain, del_doc,
            del_docs, del_filtered_assets, del_filtered_docs, del_many_assets, del_many_docs,
            del_rule, deposit_cycles, get_asset, get_auth_config, get_config, get_db_config,
            get_doc, get_many_assets, get_many_docs, get_storage_config, http_request,
            http_request_streaming_callback, init, init_asset_upload, list_assets,
            list_controllers, list_custom_domains, list_docs, list_rules, memory_size,
            post_upgrade, pre_upgrade, set_auth_config, set_controllers, set_custom_domain,
            set_db_config, set_doc, set_many_docs, set_rule, set_storage_config,
            upload_asset_chunk, version,
        };

        #[ic_cdk::query]
        pub fn build_version() -> String {
            env!("CARGO_PKG_VERSION").to_string()
        }

        ic_cdk::export_candid!();
    };
}