@biltme/backend 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # @biltme/backend
2
+
3
+ Thin shim over [`@supabase/supabase-js`](https://github.com/supabase/supabase-js)
4
+ for Bilt projects. Public surface = supabase-js's surface; this package adds
5
+ storage adapter helpers, a stable import for the agent prompt template, and
6
+ re-exports the functions error classes for clean `catch` blocks.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ bun add @biltme/backend
12
+ # Expo only — also install AsyncStorage as the session store
13
+ bun add @react-native-async-storage/async-storage
14
+ ```
15
+
16
+ ## Quick start (Expo)
17
+
18
+ ```ts
19
+ import { createClient, asyncStorage } from '@biltme/backend'
20
+ import AsyncStorage from '@react-native-async-storage/async-storage'
21
+
22
+ const url = process.env.EXPO_PUBLIC_BILT_URL! // https://<pid>.cloud.bilt.me
23
+ const key = process.env.EXPO_PUBLIC_BILT_ANON_KEY!
24
+
25
+ export const bilt = createClient(url, key, {
26
+ auth: {
27
+ storage: asyncStorage(AsyncStorage),
28
+ persistSession: true,
29
+ autoRefreshToken: true,
30
+ detectSessionInUrl: false,
31
+ },
32
+ })
33
+ ```
34
+
35
+ Every supabase-js method works:
36
+
37
+ ```ts
38
+ const { data, error } = await bilt.auth.signInWithPassword({ email, password })
39
+ const { data: rows } = await bilt.from('notes').select('*')
40
+ ```
41
+
42
+ ## Calling functions
43
+
44
+ ```ts
45
+ const { data, error } = await bilt.functions.invoke('send-email', {
46
+ body: { to: 'user@example.com', subject: 'Welcome' },
47
+ })
48
+ ```
49
+
50
+ Auth header is set automatically: the active session's access token if signed
51
+ in, otherwise the anon key. Functions deployed with `verify_jwt: true` require
52
+ either signed-in session or the anon key; `verify_jwt: false` accepts unsigned
53
+ requests.
54
+
55
+ Catch the typed error classes:
56
+
57
+ ```ts
58
+ import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from '@biltme/backend'
59
+
60
+ const { data, error } = await bilt.functions.invoke('send-email', { body: {...} })
61
+ if (error instanceof FunctionsHttpError) {
62
+ // user function returned 4xx/5xx — inspect error.context (Response)
63
+ } else if (error instanceof FunctionsRelayError) {
64
+ // bilt-cloud / pool relay failed (function not found, cold start error)
65
+ } else if (error instanceof FunctionsFetchError) {
66
+ // transport-level (DNS, TCP, TLS)
67
+ }
68
+ ```
69
+
70
+ `region` option on `invoke()` is accepted but silently ignored — bilt-cloud is
71
+ single-region.
72
+
73
+ ## Storage
74
+
75
+ ```ts
76
+ // Admin (server-side, service key) — bucket lifecycle
77
+ await bilt.storage.createBucket('avatars', { public: false, fileSizeLimit: 5 * 1024 * 1024 })
78
+ await bilt.storage.createBucket('public-assets', { public: true })
79
+
80
+ // Upload (RLS-gated for anon-key / user-token; bypassed for service key)
81
+ const { data, error } = await bilt.storage
82
+ .from('avatars')
83
+ .upload(`${user.id}/photo.jpg`, file, { contentType: 'image/jpeg', upsert: true })
84
+
85
+ // Public bucket → URL is unauthenticated
86
+ const { data: { publicUrl } } = bilt.storage
87
+ .from('public-assets')
88
+ .getPublicUrl('logo.png')
89
+
90
+ // Private bucket → time-limited signed URL (up to 7 days)
91
+ const { data, error } = await bilt.storage
92
+ .from('avatars')
93
+ .createSignedUrl(`${user.id}/photo.jpg`, 3600)
94
+
95
+ // Catch storage errors:
96
+ import { StorageApiError } from '@biltme/backend'
97
+ if (error instanceof StorageApiError) { /* … */ }
98
+ ```
99
+
100
+ Bucket admin (`createBucket`, `updateBucket`, `deleteBucket`, `emptyBucket`,
101
+ `listBuckets`) is ServiceKey-only. Object ops are gated by Postgres RLS
102
+ policies on `storage.objects` — author them with the `bilt_storage_apply`
103
+ agent tool or the SQL editor.
104
+
105
+ ## Web
106
+
107
+ ```ts
108
+ import { createClient, webStorage } from '@biltme/backend'
109
+
110
+ const bilt = createClient(url, key, {
111
+ auth: { storage: webStorage(), persistSession: true, autoRefreshToken: true },
112
+ })
113
+ ```
114
+
115
+ ## Server (no session persistence)
116
+
117
+ ```ts
118
+ import { createClient, memoryStorage } from '@biltme/backend'
119
+
120
+ const bilt = createClient(url, key, {
121
+ auth: { storage: memoryStorage(), persistSession: false, autoRefreshToken: false },
122
+ })
123
+ ```
124
+
125
+ ## What this package adds vs `@supabase/supabase-js`
126
+
127
+ - `createClient` is re-exported verbatim — same positional signature.
128
+ - `asyncStorage(impl)` factory wrapping `@react-native-async-storage/async-storage` to the supabase-js storage shape.
129
+ - `webStorage()` and `memoryStorage()` for completeness.
130
+ - `FunctionsHttpError`, `FunctionsRelayError`, `FunctionsFetchError`, `FunctionRegion` re-exports for clean `catch` and option typing.
131
+ - `StorageApiError` re-export for storage `catch` blocks.
132
+ - Stable import path for the bilt-agent prompt template.
133
+
134
+ ## Docs
135
+
136
+ For everything else (auth flows, RLS, OAuth, realtime, functions reference),
137
+ use [Supabase's docs](https://supabase.com/docs/reference/javascript/introduction).
138
+ The bilt backend speaks the same wire protocol.