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
//! # suparust
//!
//! A crate for interacting with Supabase. Also supports WASM targets.
//!
//! ## Usage
//!
//! Create your Supabase client with `Supabase::new` and start using it. The client will automatically
//! handle authentication for you after you have logged in with the client.
//!
//! ### Postgrest
//!
//! Use the functions [`from`](Supabase::from) and [`rpc`](Supabase::rpc) to get a [`postgrest::Builder`] that
//! you can use to build your queries. The builder will automatically have authentication (if it's available)
//! when it's first created.
//!
//! ### Storage
//!
//! Use the function [`storage`](Supabase::storage) to get a one-time-use [`storage::Storage`] client for interacting
//! with the storage part of Supabase. The client will automatically have authentication (if it's available)
//! when it's first created.
//!
//! ### Auth
//!
//! Auth functions are available directly on the Supabase client. Use the functions [`login_with_email`](Supabase::login_with_email),
//! and [`logout`](Supabase::logout) for basic authentication. The client will automatically handle
//! refreshing if needed when making requests.
//!
//! The session refresh happens if it is less than [`auth::SESSION_REFRESH_GRACE_PERIOD_SECONDS`] seconds
//! from expiring. This means that you should not keep authenticated builders/temporary clients for
//! too long before using them, as they might time out.
//!
//! <div class="warning">
//! Don't keep authenticated builders/clients from postgrest and storage too long, as they might
//! time out after some time. See details in Auth description above.
//! </div>
//!
//! ## Examples
//!
//! ### Simple postgrest example
//! ```no_run
//! # pub async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let client = suparust::Supabase::new(
//! "https://your.postgrest.endpoint",
//! "your_api_key",
//! None,
//! suparust::auth::SessionChangeListener::Ignore);
//!
//! client.login_with_email(
//! "myemail@example.com",
//! "mypassword").await?;
//!
//! #[derive(serde::Deserialize)]
//! struct MyStruct {
//! id: i64,
//! field: String
//! }
//!
//! // Postgrest example (see postgrest crate for more details on API)
//! let table_contents = client
//! .from("your_table")
//! .await?
//! .select("*")
//! .execute()
//! .await?
//! .json::<Vec<MyStruct>>();
//!
//! # Ok(())
//! # }
//! ```
//!
//! ### Storage example
//! ```no_run
//! # pub async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let client = suparust::Supabase::new(
//! "https://your.postgrest.endpoint",
//! "your_api_key",
//! None,
//! suparust::auth::SessionChangeListener::Ignore);
//!
//! // Login here
//!
//! # use suparust::storage::object::*;
//! let list_request = ListRequest::new("my_folder".to_string())
//! .limit(10)
//! .sort_by("my_column", SortOrder::Ascending);
//! let objects = client
//! .storage()
//! .await?
//! .object()
//! .list("my_bucket", list_request)
//! .await?;
//!
//! let object_names = objects
//! .iter()
//! .map(|object| object.name.clone());
//!
//! let mut downloaded_objects = vec![];
//!
//! for object in objects {
//! let downloaded = client
//! .storage()
//! .await?
//! .object()
//! .get_one("my_bucket", &object.name)
//! .await?;
//! downloaded_objects.push(downloaded);
//! }
//!
//! # Ok(())
//! # }
//! ```
use Arc;
use RwLock;
pub type Result<Type> = Result;
/// The main Supabase client. This is safely cloneable.