firebase_js_sys/
lib.rs

1//! `firebase_js_sys` is a wrapper around the firebase javascript sdk, allowing you to use it in rust.
2//! 
3//! See [this package's README](https://github.com/ActuallyHappening/rust-firebase-js/blob/master/firebase-js-sys/README.md) for more information,
4//! and [the `firebase-js` package](https://github.com/ActuallyHappening/rust-firebase-js/tree/master/firebase-js) for a high level implementation
5//! layer ontop of this crate.
6//! 
7//! Don't use this crate unless you are prepared to implement a lot more conversion logic, as most
8//! of the functions return `JsValue`s instead of a more useful types.
9//! See crate `firebase-js` for a more ergonomic interface.
10//! 
11//! Uses bindings hand-written using `#[wasm_bindgen]` and `rollup`.
12//! 
13//! ## Examples:
14//! Usage in general:
15//! ```rs
16//! use firebase_js_sys::ModuleApp;
17//! 
18//! // Will give runtime console error
19//! ModuleApp::initialize_app(&JsValue::UNDEFINED);
20//! ```
21//! 
22//! Example main.rs for using `trunk` to build + run in browser:
23//! ```rs
24//! use log::info;
25//! use wasm_bindgen::JsValue;
26//! 
27//! fn main() {
28//! 	_ = console_log::init_with_level(log::Level::Debug);
29//! 	
30//! 	console_error_panic_hook::set_once();
31//! 
32//! 	info!("main.rs is running!");
33//! 	
34//! 	// Will not work, but only gives console run time error
35//! 	let app = firebase_js_sys::ModuleApp::initialize_app(&JsValue::UNDEFINED);
36//! 	// println!("returned: {:?}", app);
37//! }
38//! ```
39
40
41use wasm_bindgen::prelude::*;
42
43type closure<Args> = Closure<dyn FnMut(Args)>;
44
45pub use app::ModuleApp;
46pub use database::ModuleDatabase;
47
48mod app {
49	use wasm_bindgen::prelude::*;
50
51	#[wasm_bindgen(module = "/firebase-interop/bundle.js")]
52	extern "C" {
53		pub type ModuleApp;
54
55		/// Takes a config object and returns a firebase app instance
56		/// 
57		/// Equivalent to:
58		/// ```js
59		/// import { initializeApp } from 'firebase/app';
60		/// 
61		/// // Get your own config from somewhere, typically copy-paste from firebase console
62		/// const config = {
63		/// 	apiKey: "...",
64		/// 	projectId: "...",
65		/// 	...
66		/// }
67		/// 
68		/// initializeApp(config);
69		/// ```
70		#[wasm_bindgen(static_method_of = ModuleApp, js_name = "initializeApp")]
71		pub fn initialize_app(config: &JsValue) -> JsValue;
72	}
73}
74
75mod database {
76	use wasm_bindgen::prelude::*;
77	use crate::closure;
78
79	#[wasm_bindgen(module = "/firebase-interop/bundle.js")]
80	extern "C" {
81		pub type ModuleDatabase;
82
83		/// Takes a firebase app instance (reference) and returns a reference to the database associated with that app
84		/// 
85		/// Equivalent to:
86		/// ```js
87		/// import { getDatabase } from 'firebase/database';
88		/// 
89		/// const app = initializeApp(config); // Get your own config from somewhere, typically copy-paste from firebase console
90		/// 
91		/// const db = getDatabase(app);
92		/// ```
93		#[wasm_bindgen(static_method_of = ModuleDatabase, js_name = "getDatabase")]
94		pub fn get_database_from_url(db: &JsValue, url: String) -> JsValue;
95
96		/// Takes a database instance (reference) and returns a firebase reference to the database, representing a specific path of the database
97		/// You can use this in other functions, e.g. `on_value(db_ref:)`'s first argument (`db_ref`) is returned by this function
98		/// 
99		/// Equivalent to:
100		/// ```js
101		/// import { ref } from 'firebase/database';
102		/// 
103		/// const db = getDatabase(app); // Get your own app from somewhere
104		/// 
105		/// ref(db, path);
106		/// ```
107		/// 
108		/// See [get_ref_no_path] for usage without a `path` argument
109		#[wasm_bindgen(static_method_of = ModuleDatabase, js_name = "ref")]
110		pub fn get_ref(db: &JsValue, path: String) -> JsValue;
111
112		/// See `get_ref`'s documentation, returns a reference to the root of the database
113		/// 
114		/// Equivalent to:
115		/// ```js
116		/// import { ref } from 'firebase/database';
117		/// 
118		/// const db = getDatabase(app); // Get your own app from somewhere
119		/// 
120		/// ref(db);
121		/// // Note how is it NOT equivalent to:
122		/// // ref(db, "")
123		/// ```
124		#[wasm_bindgen(static_method_of = ModuleDatabase, js_name = "ref")]
125		pub fn get_ref_no_path(db: &JsValue) -> JsValue;
126
127		// #[wasm_bindgen(static_method_of = ModuleDatabase, js_name = "getDatabase")]
128		// pub fn get_default_database(db: &JsValue) -> JsValue;
129
130		/// Registers a callback to be called when the data at the specified path changes
131		/// 
132		/// Equivalent to:
133		/// ```js
134		/// import { onValue } from 'firebase/database';
135		/// ```
136		#[wasm_bindgen(static_method_of = ModuleDatabase, js_name = "onValue")]
137		pub fn on_value(db_ref: &JsValue, callback: &closure<JsValue>) -> JsValue;
138	}
139}
140
141// #[wasm_bindgen(module = "/firebase-interop/database.js")]
142// extern {
143
144// 	#[wasm_bindgen(js_name = "ref")]
145// 	pub fn get_ref(db: &JsValue, path: String) -> JsValue;
146
147// }