@spacelr/sdk 0.18.4 → 0.19.1
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 +42 -0
- package/dist/index.d.mts +83 -13
- package/dist/index.d.ts +83 -13
- package/dist/index.js +205 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +205 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -393,6 +393,9 @@ var HttpClient = class {
|
|
|
393
393
|
};
|
|
394
394
|
|
|
395
395
|
// libs/sdk/src/core/token-storage.ts
|
|
396
|
+
function isStoredTokens(value) {
|
|
397
|
+
return typeof value === "object" && value !== null && "accessToken" in value && typeof value.accessToken === "string";
|
|
398
|
+
}
|
|
396
399
|
var MemoryTokenStorage = class {
|
|
397
400
|
constructor() {
|
|
398
401
|
this.tokens = null;
|
|
@@ -415,7 +418,8 @@ var BrowserTokenStorage = class {
|
|
|
415
418
|
try {
|
|
416
419
|
const raw = localStorage.getItem(this.storageKey);
|
|
417
420
|
if (!raw) return null;
|
|
418
|
-
|
|
421
|
+
const parsed = JSON.parse(raw);
|
|
422
|
+
return isStoredTokens(parsed) ? parsed : null;
|
|
419
423
|
} catch {
|
|
420
424
|
return null;
|
|
421
425
|
}
|
|
@@ -1124,6 +1128,18 @@ var RealtimeClient = class {
|
|
|
1124
1128
|
if (ack.error) {
|
|
1125
1129
|
const message = ack.message ?? ack.error;
|
|
1126
1130
|
const err = new SpacelrError(message, ack.error);
|
|
1131
|
+
if (keepOnConnectFailure && ack.error === "disconnected" && !this.disposed) {
|
|
1132
|
+
for (const sibling of [...this.streamSubscriptions.get(subscriptionKey) ?? []]) {
|
|
1133
|
+
try {
|
|
1134
|
+
sibling.options.onError?.(err);
|
|
1135
|
+
} catch {
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return {
|
|
1139
|
+
unsubscribe: () => this.unsubscribeStream(state),
|
|
1140
|
+
ack: syntheticAck
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1127
1143
|
this.evictStreamKey(subscriptionKey, err, state);
|
|
1128
1144
|
options.onError?.(err);
|
|
1129
1145
|
throw err;
|
|
@@ -1916,12 +1932,14 @@ function mapProfile(u) {
|
|
|
1916
1932
|
};
|
|
1917
1933
|
}
|
|
1918
1934
|
var AuthModule = class _AuthModule {
|
|
1919
|
-
constructor(http, tokenManager, config) {
|
|
1935
|
+
constructor(http, tokenManager, config, hooks = {}) {
|
|
1920
1936
|
this.stateListeners = /* @__PURE__ */ new Set();
|
|
1921
1937
|
this.lastEmittedState = null;
|
|
1922
1938
|
this.http = http;
|
|
1923
1939
|
this.tokenManager = tokenManager;
|
|
1924
1940
|
this.config = config;
|
|
1941
|
+
this.beforeLogout = hooks.beforeLogout;
|
|
1942
|
+
this.afterSignIn = hooks.afterSignIn;
|
|
1925
1943
|
this.tokenManager.setRefreshCallback(async (refreshToken) => {
|
|
1926
1944
|
const result = await this.refresh(refreshToken);
|
|
1927
1945
|
const expiresAt = result.expires_in ? Math.floor(Date.now() / 1e3) + result.expires_in : void 0;
|
|
@@ -1995,8 +2013,26 @@ var AuthModule = class _AuthModule {
|
|
|
1995
2013
|
};
|
|
1996
2014
|
this.stateListeners.clear();
|
|
1997
2015
|
this.lastEmittedState = null;
|
|
2016
|
+
this.beforeLogout = void 0;
|
|
2017
|
+
this.afterSignIn = void 0;
|
|
2018
|
+
}
|
|
2019
|
+
/**
|
|
2020
|
+
* Install the tokens of a completed sign-in. Signing in over a live session
|
|
2021
|
+
* (e.g. user B over user A without a logout) first runs `beforeLogout` with
|
|
2022
|
+
* the outgoing session's still-valid token, so this device's push leaves
|
|
2023
|
+
* the previous user before it is re-registered for the new one.
|
|
2024
|
+
*/
|
|
2025
|
+
async installSession(tokens) {
|
|
2026
|
+
if (this.beforeLogout && await this.tokenManager.getStoredTokens()) {
|
|
2027
|
+
await this.beforeLogout().catch(() => void 0);
|
|
2028
|
+
}
|
|
2029
|
+
await this.tokenManager.setTokens(tokens);
|
|
2030
|
+
this.emitState("authenticated");
|
|
1998
2031
|
}
|
|
1999
2032
|
emitState(state) {
|
|
2033
|
+
if (state === "authenticated") {
|
|
2034
|
+
this.afterSignIn?.().catch(() => void 0);
|
|
2035
|
+
}
|
|
2000
2036
|
if (state === this.lastEmittedState) return;
|
|
2001
2037
|
this.lastEmittedState = state;
|
|
2002
2038
|
for (const listener of this.stateListeners) {
|
|
@@ -2046,6 +2082,7 @@ var AuthModule = class _AuthModule {
|
|
|
2046
2082
|
return mapProfile(data.user ?? data);
|
|
2047
2083
|
}
|
|
2048
2084
|
async logout() {
|
|
2085
|
+
await this.beforeLogout?.().catch(() => void 0);
|
|
2049
2086
|
try {
|
|
2050
2087
|
await this.http.request({
|
|
2051
2088
|
method: "POST",
|
|
@@ -2133,12 +2170,11 @@ var AuthModule = class _AuthModule {
|
|
|
2133
2170
|
body
|
|
2134
2171
|
});
|
|
2135
2172
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2136
|
-
await this.
|
|
2173
|
+
await this.installSession({
|
|
2137
2174
|
accessToken: response.access_token,
|
|
2138
2175
|
refreshToken: response.refresh_token,
|
|
2139
2176
|
expiresAt
|
|
2140
2177
|
});
|
|
2141
|
-
this.emitState("authenticated");
|
|
2142
2178
|
return response;
|
|
2143
2179
|
}
|
|
2144
2180
|
async generatePKCE() {
|
|
@@ -2384,12 +2420,11 @@ var AuthModule = class _AuthModule {
|
|
|
2384
2420
|
}
|
|
2385
2421
|
async storeTokensFromLogin(response) {
|
|
2386
2422
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2387
|
-
await this.
|
|
2423
|
+
await this.installSession({
|
|
2388
2424
|
accessToken: response.access_token,
|
|
2389
2425
|
refreshToken: response.refresh_token,
|
|
2390
2426
|
expiresAt
|
|
2391
2427
|
});
|
|
2392
|
-
this.emitState("authenticated");
|
|
2393
2428
|
}
|
|
2394
2429
|
// ─── Recover-with-phrase (#784) ───
|
|
2395
2430
|
// For an email-less passkey account (#780) that has lost every passkey. The
|
|
@@ -2620,12 +2655,11 @@ var AuthModule = class _AuthModule {
|
|
|
2620
2655
|
async storeTokensFromRegister(response) {
|
|
2621
2656
|
if (!response.access_token) return;
|
|
2622
2657
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2623
|
-
await this.
|
|
2658
|
+
await this.installSession({
|
|
2624
2659
|
accessToken: response.access_token,
|
|
2625
2660
|
refreshToken: response.refresh_token,
|
|
2626
2661
|
expiresAt
|
|
2627
2662
|
});
|
|
2628
|
-
this.emitState("authenticated");
|
|
2629
2663
|
}
|
|
2630
2664
|
};
|
|
2631
2665
|
|
|
@@ -4113,11 +4147,20 @@ var DatabaseModule = class {
|
|
|
4113
4147
|
|
|
4114
4148
|
// libs/sdk/src/modules/notifications.module.ts
|
|
4115
4149
|
var DEVICE_ID_KEY = "spacelr_device_id";
|
|
4116
|
-
var
|
|
4117
|
-
|
|
4150
|
+
var REGISTRATION_KEY_PREFIX = "spacelr_push_registration:";
|
|
4151
|
+
var PUSH_LOGOUT_TIMEOUT_MS = 3e3;
|
|
4152
|
+
var NotificationsModule = class _NotificationsModule {
|
|
4153
|
+
constructor(http, options = {}) {
|
|
4118
4154
|
this.http = http;
|
|
4119
4155
|
this.customDeviceId = null;
|
|
4120
4156
|
this.customDeviceName = null;
|
|
4157
|
+
// Fallback when localStorage is unavailable or refuses writes.
|
|
4158
|
+
this.memoryRegistration = null;
|
|
4159
|
+
// Push register/unregister calls run strictly in call order, so a late
|
|
4160
|
+
// re-register can never land after the logout's unregister (or overwrite a
|
|
4161
|
+
// newer registration the app just made).
|
|
4162
|
+
this.pushQueue = Promise.resolve();
|
|
4163
|
+
this.registrationKey = `${REGISTRATION_KEY_PREFIX}${options.projectId ?? "default"}`;
|
|
4121
4164
|
}
|
|
4122
4165
|
/** Set a custom device ID (e.g. from Capacitor Preferences for persistence beyond localStorage) */
|
|
4123
4166
|
setDeviceId(id) {
|
|
@@ -4131,12 +4174,16 @@ var NotificationsModule = class {
|
|
|
4131
4174
|
getDeviceId() {
|
|
4132
4175
|
if (this.customDeviceId) return this.customDeviceId;
|
|
4133
4176
|
if (typeof localStorage === "undefined") return void 0;
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4177
|
+
try {
|
|
4178
|
+
let id = localStorage.getItem(DEVICE_ID_KEY);
|
|
4179
|
+
if (!id) {
|
|
4180
|
+
id = crypto.randomUUID();
|
|
4181
|
+
localStorage.setItem(DEVICE_ID_KEY, id);
|
|
4182
|
+
}
|
|
4183
|
+
return id;
|
|
4184
|
+
} catch {
|
|
4185
|
+
return void 0;
|
|
4138
4186
|
}
|
|
4139
|
-
return id;
|
|
4140
4187
|
}
|
|
4141
4188
|
/** Get device name: custom name > auto-detected from user agent > undefined */
|
|
4142
4189
|
getDeviceName() {
|
|
@@ -4169,9 +4216,19 @@ var NotificationsModule = class {
|
|
|
4169
4216
|
authenticated: true
|
|
4170
4217
|
});
|
|
4171
4218
|
}
|
|
4172
|
-
/**
|
|
4219
|
+
/**
|
|
4220
|
+
* Register a push subscription (web, android, ios, or web-fcm).
|
|
4221
|
+
*
|
|
4222
|
+
* The SDK remembers this device's registration. Unless the client was
|
|
4223
|
+
* created with `autoManagePush: false`, it re-registers it after every
|
|
4224
|
+
* login and unregisters it on `auth.logout()`, so the subscription always
|
|
4225
|
+
* belongs to the signed-in user's current session.
|
|
4226
|
+
*/
|
|
4173
4227
|
async subscribe(subscription, deviceName) {
|
|
4174
|
-
return this.
|
|
4228
|
+
return this.enqueue(() => this.register(subscription, deviceName));
|
|
4229
|
+
}
|
|
4230
|
+
async register(subscription, deviceName) {
|
|
4231
|
+
const info = await this.http.request({
|
|
4175
4232
|
method: "POST",
|
|
4176
4233
|
path: "/notifications/subscribe",
|
|
4177
4234
|
body: {
|
|
@@ -4181,20 +4238,23 @@ var NotificationsModule = class {
|
|
|
4181
4238
|
},
|
|
4182
4239
|
authenticated: true
|
|
4183
4240
|
});
|
|
4241
|
+
this.saveRegistration({ id: info?.id, subscription, deviceName });
|
|
4242
|
+
return info;
|
|
4184
4243
|
}
|
|
4185
|
-
/**
|
|
4244
|
+
/**
|
|
4245
|
+
* Unregister a push subscription. Unregistering this device's own
|
|
4246
|
+
* subscription also stops the SDK from re-registering it after a login.
|
|
4247
|
+
*/
|
|
4186
4248
|
async unsubscribe(platform, identifier) {
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
body,
|
|
4197
|
-
authenticated: true
|
|
4249
|
+
return this.enqueue(async () => {
|
|
4250
|
+
try {
|
|
4251
|
+
return await this.deleteSubscription(platform, identifier);
|
|
4252
|
+
} finally {
|
|
4253
|
+
const stored = this.loadRegistration();
|
|
4254
|
+
if (stored?.subscription.platform === platform && _NotificationsModule.identifierOf(stored.subscription) === identifier) {
|
|
4255
|
+
this.saveRegistration(null);
|
|
4256
|
+
}
|
|
4257
|
+
}
|
|
4198
4258
|
});
|
|
4199
4259
|
}
|
|
4200
4260
|
/**
|
|
@@ -4204,12 +4264,114 @@ var NotificationsModule = class {
|
|
|
4204
4264
|
* by the listing and so cannot be passed to {@link unsubscribe}).
|
|
4205
4265
|
*/
|
|
4206
4266
|
async unsubscribeById(subscriptionId) {
|
|
4267
|
+
return this.enqueue(async () => {
|
|
4268
|
+
try {
|
|
4269
|
+
return await this.http.request({
|
|
4270
|
+
method: "DELETE",
|
|
4271
|
+
path: `/notifications/subscriptions/${encodeURIComponent(subscriptionId)}`,
|
|
4272
|
+
authenticated: true
|
|
4273
|
+
});
|
|
4274
|
+
} finally {
|
|
4275
|
+
if (this.loadRegistration()?.id === subscriptionId) {
|
|
4276
|
+
this.saveRegistration(null);
|
|
4277
|
+
}
|
|
4278
|
+
}
|
|
4279
|
+
});
|
|
4280
|
+
}
|
|
4281
|
+
/**
|
|
4282
|
+
* Re-register this device's remembered subscription for the user who just
|
|
4283
|
+
* signed in, binding it to their new session (#1003). The client calls this
|
|
4284
|
+
* after every sign-in unless `autoManagePush` is false; call it yourself
|
|
4285
|
+
* after signing in via `setTokens()`. No-op when this device never
|
|
4286
|
+
* subscribed. Best-effort: a failure leaves the device without push until
|
|
4287
|
+
* the app subscribes again.
|
|
4288
|
+
*/
|
|
4289
|
+
async resubscribeAfterLogin() {
|
|
4290
|
+
await this.enqueue(async () => {
|
|
4291
|
+
const stored = this.loadRegistration();
|
|
4292
|
+
if (!stored) return;
|
|
4293
|
+
try {
|
|
4294
|
+
await this.register(stored.subscription, stored.deviceName);
|
|
4295
|
+
} catch (error) {
|
|
4296
|
+
console.warn("[spacelr] Could not re-register push after login:", error);
|
|
4297
|
+
}
|
|
4298
|
+
});
|
|
4299
|
+
}
|
|
4300
|
+
/**
|
|
4301
|
+
* Unregister this device's subscription while the session is still valid,
|
|
4302
|
+
* so a signed-out device stops receiving the user's pushes. The remembered
|
|
4303
|
+
* registration is kept, so the next sign-in re-registers it. `auth.logout()`
|
|
4304
|
+
* calls this first unless `autoManagePush` is false; call it yourself before
|
|
4305
|
+
* clearing tokens via `clearTokens()`. Best-effort.
|
|
4306
|
+
*/
|
|
4307
|
+
async unsubscribeBeforeLogout() {
|
|
4308
|
+
const unregistered = this.enqueue(async () => {
|
|
4309
|
+
const stored = this.loadRegistration();
|
|
4310
|
+
if (!stored) return;
|
|
4311
|
+
await this.deleteSubscription(
|
|
4312
|
+
stored.subscription.platform,
|
|
4313
|
+
_NotificationsModule.identifierOf(stored.subscription)
|
|
4314
|
+
).catch(() => void 0);
|
|
4315
|
+
});
|
|
4316
|
+
let timer;
|
|
4317
|
+
await Promise.race([
|
|
4318
|
+
unregistered,
|
|
4319
|
+
new Promise((resolve) => {
|
|
4320
|
+
timer = setTimeout(resolve, PUSH_LOGOUT_TIMEOUT_MS);
|
|
4321
|
+
})
|
|
4322
|
+
]);
|
|
4323
|
+
clearTimeout(timer);
|
|
4324
|
+
}
|
|
4325
|
+
enqueue(op) {
|
|
4326
|
+
const run = this.pushQueue.then(op, op);
|
|
4327
|
+
this.pushQueue = run.catch(() => void 0);
|
|
4328
|
+
return run;
|
|
4329
|
+
}
|
|
4330
|
+
deleteSubscription(platform, identifier) {
|
|
4331
|
+
const body = { platform };
|
|
4332
|
+
if (platform === "web") {
|
|
4333
|
+
body["endpoint"] = identifier;
|
|
4334
|
+
} else {
|
|
4335
|
+
body["deviceToken"] = identifier;
|
|
4336
|
+
}
|
|
4207
4337
|
return this.http.request({
|
|
4208
4338
|
method: "DELETE",
|
|
4209
|
-
path:
|
|
4339
|
+
path: "/notifications/subscribe",
|
|
4340
|
+
body,
|
|
4210
4341
|
authenticated: true
|
|
4211
4342
|
});
|
|
4212
4343
|
}
|
|
4344
|
+
/** The value `unsubscribe()` identifies a subscription by: endpoint for web, token otherwise. */
|
|
4345
|
+
static identifierOf(subscription) {
|
|
4346
|
+
return (subscription.platform === "web" ? subscription.endpoint : subscription.deviceToken) ?? "";
|
|
4347
|
+
}
|
|
4348
|
+
static isStoredRegistration(value) {
|
|
4349
|
+
if (typeof value !== "object" || value === null) return false;
|
|
4350
|
+
const subscription = value.subscription;
|
|
4351
|
+
return typeof subscription === "object" && subscription !== null && typeof subscription.platform === "string";
|
|
4352
|
+
}
|
|
4353
|
+
loadRegistration() {
|
|
4354
|
+
if (typeof localStorage === "undefined") return this.memoryRegistration;
|
|
4355
|
+
try {
|
|
4356
|
+
const raw = localStorage.getItem(this.registrationKey);
|
|
4357
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
4358
|
+
return _NotificationsModule.isStoredRegistration(parsed) ? parsed : this.memoryRegistration;
|
|
4359
|
+
} catch {
|
|
4360
|
+
return this.memoryRegistration;
|
|
4361
|
+
}
|
|
4362
|
+
}
|
|
4363
|
+
saveRegistration(registration) {
|
|
4364
|
+
this.memoryRegistration = registration;
|
|
4365
|
+
if (typeof localStorage === "undefined") return;
|
|
4366
|
+
try {
|
|
4367
|
+
if (registration) {
|
|
4368
|
+
localStorage.setItem(this.registrationKey, JSON.stringify(registration));
|
|
4369
|
+
} else {
|
|
4370
|
+
localStorage.removeItem(this.registrationKey);
|
|
4371
|
+
}
|
|
4372
|
+
} catch {
|
|
4373
|
+
}
|
|
4374
|
+
}
|
|
4213
4375
|
/** Get all subscriptions for the current user */
|
|
4214
4376
|
async getSubscriptions() {
|
|
4215
4377
|
return this.http.request({
|
|
@@ -4412,10 +4574,21 @@ function createClient(config) {
|
|
|
4412
4574
|
getToken: () => tokenManager.getAccessToken(),
|
|
4413
4575
|
onTokenRefreshed: (listener) => tokenManager.onTokenRefreshed((tokens) => listener(tokens.accessToken))
|
|
4414
4576
|
});
|
|
4415
|
-
const
|
|
4577
|
+
const notifications = new NotificationsModule(httpClient, {
|
|
4578
|
+
projectId: config.projectId
|
|
4579
|
+
});
|
|
4580
|
+
const autoManagePush = config.autoManagePush ?? true;
|
|
4581
|
+
const auth = new AuthModule(
|
|
4582
|
+
httpClient,
|
|
4583
|
+
tokenManager,
|
|
4584
|
+
config,
|
|
4585
|
+
autoManagePush ? {
|
|
4586
|
+
beforeLogout: () => notifications.unsubscribeBeforeLogout(),
|
|
4587
|
+
afterSignIn: () => notifications.resubscribeAfterLogin()
|
|
4588
|
+
} : {}
|
|
4589
|
+
);
|
|
4416
4590
|
const storage = new StorageModule(httpClient, tokenManager, config);
|
|
4417
4591
|
const db = new DatabaseModule(httpClient, config.projectId, realtime);
|
|
4418
|
-
const notifications = new NotificationsModule(httpClient);
|
|
4419
4592
|
const functions = new FunctionsModule(httpClient);
|
|
4420
4593
|
const schedule = new ScheduleModule(httpClient, config.projectId);
|
|
4421
4594
|
return {
|