@biltme/backend 0.9.0 → 0.11.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 CHANGED
@@ -11,6 +11,8 @@ re-exports the functions error classes for clean `catch` blocks.
11
11
  bun add @biltme/backend
12
12
  # Expo only — also install AsyncStorage as the session store
13
13
  bun add @react-native-async-storage/async-storage
14
+ # Expo only, optional — pauses session auto-refresh while the device is offline
15
+ npx expo install expo-network
14
16
  ```
15
17
 
16
18
  ## Quick start (Expo)
@@ -32,6 +34,16 @@ export const bilt = createClient(url, key, {
32
34
  })
33
35
  ```
34
36
 
37
+ A session refresh that fails on the network is retried for 10 seconds after
38
+ the first attempt, then gives up and keeps the session. Requests waiting on the
39
+ refresh wait that long. Set `auth.refreshRetryWindowMs` to change it; `0` tries
40
+ once. supabase-js on its own retries for 30 seconds.
41
+
42
+ On iOS and Android with `expo-network` installed, auto-refresh stops while the
43
+ device is offline and starts again when it comes back, refreshing at once if
44
+ the token is due. Without `expo-network`, or with `autoRefreshToken: false`,
45
+ nothing changes.
46
+
35
47
  Every supabase-js method works:
36
48
 
37
49
  ```ts
@@ -179,8 +191,9 @@ const { data, error } = await bilt.notifications.send({
179
191
  body: 'See you at 8',
180
192
  data: { chatId: chat.id }, // optional, passed to the app on open
181
193
  url: `/chat/${chat.id}`, // optional in-app path a tap opens
194
+ category: 'New messages', // optional: a kind of notification the user can turn off
182
195
  })
183
- // data.outcome: 'accepted' | 'no_devices' | 'notifications_off' | 'platform_off'
196
+ // data.outcome: 'accepted' | 'no_devices' | 'notifications_off' | 'platform_off' | 'opted_out' | 'suspended'
184
197
  ```
185
198
 
186
199
  A `to` typed as `string` could hold `'all'`, so its answer is typed as either
@@ -191,9 +204,14 @@ the way the client's other calls are: a `global.headers` Authorization header,
191
204
  else `accessToken`, else the session, else the client's key. The client's other
192
205
  `global.headers` go along. Bilt Cloud accepts the key only from an edge
193
206
  function, so an app without a session gets a 403 `auth_role_denied`. `accepted` means
194
- Apple or Google took the notification, not that it arrived. `url` must be an in-app path
207
+ Apple or Google took the notification, not that it arrived. `suspended` means Bilt has
208
+ stopped push for this app: nothing was sent, and the owner should contact Bilt support. `url` must be an in-app path
195
209
  starting with a single `/`, `data` may not have its own `url` key, and the two
196
- together stay under 2 KB. A user may send 10 a minute
210
+ together stay under 2 KB. `category`, at most 64 bytes, names a kind of
211
+ notification such as new messages or daily reminders. A recipient who turned it
212
+ off with `@biltme/notifications-expo`'s `setCategoryEnabled` gets `opted_out`:
213
+ nothing is sent, and the send counts against no limit. On Android the category is
214
+ also the notification channel, shown by this name in system settings. A user may send 10 a minute
197
215
  and 200 a day, and a recipient receives at most 30 a minute; over that, `error`
198
216
  is a `NotificationsError` with `code` `rate_limited` and `status` 429. The
199
217
  method never throws. The app registers devices and handles taps through
@@ -207,7 +225,7 @@ import { createClient } from 'npm:@biltme/backend'
207
225
  const bilt = createClient(Deno.env.get('EXPO_PUBLIC_BILT_URL')!, Deno.env.get('BILT_SERVICE_KEY')!)
208
226
  await bilt.notifications.send({ to: userId, title: 'Order shipped', body: 'It arrives Friday' })
209
227
  const { data } = await bilt.notifications.send({ to: 'all', title: 'We are back', body: 'Service is restored' })
210
- // data.counts: { accepted, notifications_off, platform_off, error, rate_limited }, in devices
228
+ // data.counts: { accepted, notifications_off, platform_off, error, rate_limited, opted_out, suspended }, in devices
211
229
  ```
212
230
 
213
231
  A function sends with its `BILT_SERVICE_KEY` and needs no session. Only a
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createClient as createSupabaseClient } from '@supabase/supabase-js';
2
2
  import { type Notifications } from './notifications';
3
+ import { type BiltAuthOptions } from './refresh';
3
4
  /**
4
5
  * Schema of the project's database, in postgrest-js's `Database` shape.
5
6
  *
@@ -83,20 +84,24 @@ export type TablesUpdate<Name extends keyof PublicTables> = PublicTables[Name] e
83
84
  Update: infer U;
84
85
  } ? U : never;
85
86
  /**
86
- * Creates a Bilt client. supabase-js's `createClient` with five changes: called
87
+ * Creates a Bilt client. supabase-js's `createClient` with these changes: called
87
88
  * with no type arguments, the schema is {@link BiltDatabase} instead of `any`,
88
89
  * a missing or malformed URL or key names Bilt Cloud rather than Supabase, on
89
90
  * React Native a Blob upload is sent as bytes so `contentType` reaches the
90
91
  * bucket's MIME allowlist (see {@link sendNativeBlobsAsBytes}), opening a
91
92
  * realtime channel throws because Bilt Cloud has no realtime (see {@link blockRealtime}),
92
- * and the client has `notifications` (see {@link Notifications}).
93
+ * the client has `notifications` (see {@link Notifications}), a session refresh
94
+ * that fails on the network stops retrying after `auth.refreshRetryWindowMs`
95
+ * (see {@link BiltAuthOptions}), and on React Native with `expo-network`
96
+ * installed, auto-refresh pauses while the device is offline (see {@link pauseRefreshWhileOffline}).
93
97
  *
94
98
  * The second member of the intersection is supabase-js's own signature. An
95
99
  * intersection of function types is an overload set, so a bare call takes the
96
100
  * first member and any call with explicit type arguments falls through to the
97
101
  * second. Restating the parameter list here instead would have to track
98
- * supabase-js's defaults by hand across a caret range. For the same reason only
99
- * the bare call's type carries `notifications`; it is there at runtime either way.
102
+ * supabase-js's defaults by hand across versions. For the same reason only
103
+ * the bare call's types carry `notifications` and {@link BiltAuthOptions};
104
+ * both work at runtime either way.
100
105
  */
101
106
  export declare const createClient: BareCreateClient & typeof createSupabaseClient;
102
107
  type BareCall = typeof createSupabaseClient<BiltDatabase>;
@@ -104,8 +109,13 @@ type BareCall = typeof createSupabaseClient<BiltDatabase>;
104
109
  export type BiltClient = ReturnType<BareCall> & {
105
110
  notifications: Notifications;
106
111
  };
107
- type BareCreateClient = (...args: Parameters<BareCall>) => BiltClient;
112
+ /** supabase-js's client options, plus {@link BiltAuthOptions} under `auth`. */
113
+ export type BiltClientOptions = NonNullable<Parameters<BareCall>[2]> & {
114
+ auth?: BiltAuthOptions;
115
+ };
116
+ type BareCreateClient = (url: Parameters<BareCall>[0], key: Parameters<BareCall>[1], options?: BiltClientOptions) => BiltClient;
108
117
  export { REALTIME_NOT_SUPPORTED } from './realtime';
118
+ export type { BiltAuthOptions } from './refresh';
109
119
  export { NotificationsError } from './notifications';
110
120
  export type { Notifications, NotificationMessage, NotificationOutcome, NotificationSendResult, NotificationCounts, NotificationSendAllResult, } from './notifications';
111
121
  export { webStorage, memoryStorage, asyncStorage } from './storage';