@spacelr/sdk 0.18.4 → 0.19.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 +42 -0
- package/dist/index.d.mts +83 -13
- package/dist/index.d.ts +83 -13
- package/dist/index.js +193 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +193 -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
|
}
|
|
@@ -1916,12 +1920,14 @@ function mapProfile(u) {
|
|
|
1916
1920
|
};
|
|
1917
1921
|
}
|
|
1918
1922
|
var AuthModule = class _AuthModule {
|
|
1919
|
-
constructor(http, tokenManager, config) {
|
|
1923
|
+
constructor(http, tokenManager, config, hooks = {}) {
|
|
1920
1924
|
this.stateListeners = /* @__PURE__ */ new Set();
|
|
1921
1925
|
this.lastEmittedState = null;
|
|
1922
1926
|
this.http = http;
|
|
1923
1927
|
this.tokenManager = tokenManager;
|
|
1924
1928
|
this.config = config;
|
|
1929
|
+
this.beforeLogout = hooks.beforeLogout;
|
|
1930
|
+
this.afterSignIn = hooks.afterSignIn;
|
|
1925
1931
|
this.tokenManager.setRefreshCallback(async (refreshToken) => {
|
|
1926
1932
|
const result = await this.refresh(refreshToken);
|
|
1927
1933
|
const expiresAt = result.expires_in ? Math.floor(Date.now() / 1e3) + result.expires_in : void 0;
|
|
@@ -1995,8 +2001,26 @@ var AuthModule = class _AuthModule {
|
|
|
1995
2001
|
};
|
|
1996
2002
|
this.stateListeners.clear();
|
|
1997
2003
|
this.lastEmittedState = null;
|
|
2004
|
+
this.beforeLogout = void 0;
|
|
2005
|
+
this.afterSignIn = void 0;
|
|
2006
|
+
}
|
|
2007
|
+
/**
|
|
2008
|
+
* Install the tokens of a completed sign-in. Signing in over a live session
|
|
2009
|
+
* (e.g. user B over user A without a logout) first runs `beforeLogout` with
|
|
2010
|
+
* the outgoing session's still-valid token, so this device's push leaves
|
|
2011
|
+
* the previous user before it is re-registered for the new one.
|
|
2012
|
+
*/
|
|
2013
|
+
async installSession(tokens) {
|
|
2014
|
+
if (this.beforeLogout && await this.tokenManager.getStoredTokens()) {
|
|
2015
|
+
await this.beforeLogout().catch(() => void 0);
|
|
2016
|
+
}
|
|
2017
|
+
await this.tokenManager.setTokens(tokens);
|
|
2018
|
+
this.emitState("authenticated");
|
|
1998
2019
|
}
|
|
1999
2020
|
emitState(state) {
|
|
2021
|
+
if (state === "authenticated") {
|
|
2022
|
+
this.afterSignIn?.().catch(() => void 0);
|
|
2023
|
+
}
|
|
2000
2024
|
if (state === this.lastEmittedState) return;
|
|
2001
2025
|
this.lastEmittedState = state;
|
|
2002
2026
|
for (const listener of this.stateListeners) {
|
|
@@ -2046,6 +2070,7 @@ var AuthModule = class _AuthModule {
|
|
|
2046
2070
|
return mapProfile(data.user ?? data);
|
|
2047
2071
|
}
|
|
2048
2072
|
async logout() {
|
|
2073
|
+
await this.beforeLogout?.().catch(() => void 0);
|
|
2049
2074
|
try {
|
|
2050
2075
|
await this.http.request({
|
|
2051
2076
|
method: "POST",
|
|
@@ -2133,12 +2158,11 @@ var AuthModule = class _AuthModule {
|
|
|
2133
2158
|
body
|
|
2134
2159
|
});
|
|
2135
2160
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2136
|
-
await this.
|
|
2161
|
+
await this.installSession({
|
|
2137
2162
|
accessToken: response.access_token,
|
|
2138
2163
|
refreshToken: response.refresh_token,
|
|
2139
2164
|
expiresAt
|
|
2140
2165
|
});
|
|
2141
|
-
this.emitState("authenticated");
|
|
2142
2166
|
return response;
|
|
2143
2167
|
}
|
|
2144
2168
|
async generatePKCE() {
|
|
@@ -2384,12 +2408,11 @@ var AuthModule = class _AuthModule {
|
|
|
2384
2408
|
}
|
|
2385
2409
|
async storeTokensFromLogin(response) {
|
|
2386
2410
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2387
|
-
await this.
|
|
2411
|
+
await this.installSession({
|
|
2388
2412
|
accessToken: response.access_token,
|
|
2389
2413
|
refreshToken: response.refresh_token,
|
|
2390
2414
|
expiresAt
|
|
2391
2415
|
});
|
|
2392
|
-
this.emitState("authenticated");
|
|
2393
2416
|
}
|
|
2394
2417
|
// ─── Recover-with-phrase (#784) ───
|
|
2395
2418
|
// For an email-less passkey account (#780) that has lost every passkey. The
|
|
@@ -2620,12 +2643,11 @@ var AuthModule = class _AuthModule {
|
|
|
2620
2643
|
async storeTokensFromRegister(response) {
|
|
2621
2644
|
if (!response.access_token) return;
|
|
2622
2645
|
const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;
|
|
2623
|
-
await this.
|
|
2646
|
+
await this.installSession({
|
|
2624
2647
|
accessToken: response.access_token,
|
|
2625
2648
|
refreshToken: response.refresh_token,
|
|
2626
2649
|
expiresAt
|
|
2627
2650
|
});
|
|
2628
|
-
this.emitState("authenticated");
|
|
2629
2651
|
}
|
|
2630
2652
|
};
|
|
2631
2653
|
|
|
@@ -4113,11 +4135,20 @@ var DatabaseModule = class {
|
|
|
4113
4135
|
|
|
4114
4136
|
// libs/sdk/src/modules/notifications.module.ts
|
|
4115
4137
|
var DEVICE_ID_KEY = "spacelr_device_id";
|
|
4116
|
-
var
|
|
4117
|
-
|
|
4138
|
+
var REGISTRATION_KEY_PREFIX = "spacelr_push_registration:";
|
|
4139
|
+
var PUSH_LOGOUT_TIMEOUT_MS = 3e3;
|
|
4140
|
+
var NotificationsModule = class _NotificationsModule {
|
|
4141
|
+
constructor(http, options = {}) {
|
|
4118
4142
|
this.http = http;
|
|
4119
4143
|
this.customDeviceId = null;
|
|
4120
4144
|
this.customDeviceName = null;
|
|
4145
|
+
// Fallback when localStorage is unavailable or refuses writes.
|
|
4146
|
+
this.memoryRegistration = null;
|
|
4147
|
+
// Push register/unregister calls run strictly in call order, so a late
|
|
4148
|
+
// re-register can never land after the logout's unregister (or overwrite a
|
|
4149
|
+
// newer registration the app just made).
|
|
4150
|
+
this.pushQueue = Promise.resolve();
|
|
4151
|
+
this.registrationKey = `${REGISTRATION_KEY_PREFIX}${options.projectId ?? "default"}`;
|
|
4121
4152
|
}
|
|
4122
4153
|
/** Set a custom device ID (e.g. from Capacitor Preferences for persistence beyond localStorage) */
|
|
4123
4154
|
setDeviceId(id) {
|
|
@@ -4131,12 +4162,16 @@ var NotificationsModule = class {
|
|
|
4131
4162
|
getDeviceId() {
|
|
4132
4163
|
if (this.customDeviceId) return this.customDeviceId;
|
|
4133
4164
|
if (typeof localStorage === "undefined") return void 0;
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4165
|
+
try {
|
|
4166
|
+
let id = localStorage.getItem(DEVICE_ID_KEY);
|
|
4167
|
+
if (!id) {
|
|
4168
|
+
id = crypto.randomUUID();
|
|
4169
|
+
localStorage.setItem(DEVICE_ID_KEY, id);
|
|
4170
|
+
}
|
|
4171
|
+
return id;
|
|
4172
|
+
} catch {
|
|
4173
|
+
return void 0;
|
|
4138
4174
|
}
|
|
4139
|
-
return id;
|
|
4140
4175
|
}
|
|
4141
4176
|
/** Get device name: custom name > auto-detected from user agent > undefined */
|
|
4142
4177
|
getDeviceName() {
|
|
@@ -4169,9 +4204,19 @@ var NotificationsModule = class {
|
|
|
4169
4204
|
authenticated: true
|
|
4170
4205
|
});
|
|
4171
4206
|
}
|
|
4172
|
-
/**
|
|
4207
|
+
/**
|
|
4208
|
+
* Register a push subscription (web, android, ios, or web-fcm).
|
|
4209
|
+
*
|
|
4210
|
+
* The SDK remembers this device's registration. Unless the client was
|
|
4211
|
+
* created with `autoManagePush: false`, it re-registers it after every
|
|
4212
|
+
* login and unregisters it on `auth.logout()`, so the subscription always
|
|
4213
|
+
* belongs to the signed-in user's current session.
|
|
4214
|
+
*/
|
|
4173
4215
|
async subscribe(subscription, deviceName) {
|
|
4174
|
-
return this.
|
|
4216
|
+
return this.enqueue(() => this.register(subscription, deviceName));
|
|
4217
|
+
}
|
|
4218
|
+
async register(subscription, deviceName) {
|
|
4219
|
+
const info = await this.http.request({
|
|
4175
4220
|
method: "POST",
|
|
4176
4221
|
path: "/notifications/subscribe",
|
|
4177
4222
|
body: {
|
|
@@ -4181,20 +4226,23 @@ var NotificationsModule = class {
|
|
|
4181
4226
|
},
|
|
4182
4227
|
authenticated: true
|
|
4183
4228
|
});
|
|
4229
|
+
this.saveRegistration({ id: info?.id, subscription, deviceName });
|
|
4230
|
+
return info;
|
|
4184
4231
|
}
|
|
4185
|
-
/**
|
|
4232
|
+
/**
|
|
4233
|
+
* Unregister a push subscription. Unregistering this device's own
|
|
4234
|
+
* subscription also stops the SDK from re-registering it after a login.
|
|
4235
|
+
*/
|
|
4186
4236
|
async unsubscribe(platform, identifier) {
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
body,
|
|
4197
|
-
authenticated: true
|
|
4237
|
+
return this.enqueue(async () => {
|
|
4238
|
+
try {
|
|
4239
|
+
return await this.deleteSubscription(platform, identifier);
|
|
4240
|
+
} finally {
|
|
4241
|
+
const stored = this.loadRegistration();
|
|
4242
|
+
if (stored?.subscription.platform === platform && _NotificationsModule.identifierOf(stored.subscription) === identifier) {
|
|
4243
|
+
this.saveRegistration(null);
|
|
4244
|
+
}
|
|
4245
|
+
}
|
|
4198
4246
|
});
|
|
4199
4247
|
}
|
|
4200
4248
|
/**
|
|
@@ -4204,12 +4252,114 @@ var NotificationsModule = class {
|
|
|
4204
4252
|
* by the listing and so cannot be passed to {@link unsubscribe}).
|
|
4205
4253
|
*/
|
|
4206
4254
|
async unsubscribeById(subscriptionId) {
|
|
4255
|
+
return this.enqueue(async () => {
|
|
4256
|
+
try {
|
|
4257
|
+
return await this.http.request({
|
|
4258
|
+
method: "DELETE",
|
|
4259
|
+
path: `/notifications/subscriptions/${encodeURIComponent(subscriptionId)}`,
|
|
4260
|
+
authenticated: true
|
|
4261
|
+
});
|
|
4262
|
+
} finally {
|
|
4263
|
+
if (this.loadRegistration()?.id === subscriptionId) {
|
|
4264
|
+
this.saveRegistration(null);
|
|
4265
|
+
}
|
|
4266
|
+
}
|
|
4267
|
+
});
|
|
4268
|
+
}
|
|
4269
|
+
/**
|
|
4270
|
+
* Re-register this device's remembered subscription for the user who just
|
|
4271
|
+
* signed in, binding it to their new session (#1003). The client calls this
|
|
4272
|
+
* after every sign-in unless `autoManagePush` is false; call it yourself
|
|
4273
|
+
* after signing in via `setTokens()`. No-op when this device never
|
|
4274
|
+
* subscribed. Best-effort: a failure leaves the device without push until
|
|
4275
|
+
* the app subscribes again.
|
|
4276
|
+
*/
|
|
4277
|
+
async resubscribeAfterLogin() {
|
|
4278
|
+
await this.enqueue(async () => {
|
|
4279
|
+
const stored = this.loadRegistration();
|
|
4280
|
+
if (!stored) return;
|
|
4281
|
+
try {
|
|
4282
|
+
await this.register(stored.subscription, stored.deviceName);
|
|
4283
|
+
} catch (error) {
|
|
4284
|
+
console.warn("[spacelr] Could not re-register push after login:", error);
|
|
4285
|
+
}
|
|
4286
|
+
});
|
|
4287
|
+
}
|
|
4288
|
+
/**
|
|
4289
|
+
* Unregister this device's subscription while the session is still valid,
|
|
4290
|
+
* so a signed-out device stops receiving the user's pushes. The remembered
|
|
4291
|
+
* registration is kept, so the next sign-in re-registers it. `auth.logout()`
|
|
4292
|
+
* calls this first unless `autoManagePush` is false; call it yourself before
|
|
4293
|
+
* clearing tokens via `clearTokens()`. Best-effort.
|
|
4294
|
+
*/
|
|
4295
|
+
async unsubscribeBeforeLogout() {
|
|
4296
|
+
const unregistered = this.enqueue(async () => {
|
|
4297
|
+
const stored = this.loadRegistration();
|
|
4298
|
+
if (!stored) return;
|
|
4299
|
+
await this.deleteSubscription(
|
|
4300
|
+
stored.subscription.platform,
|
|
4301
|
+
_NotificationsModule.identifierOf(stored.subscription)
|
|
4302
|
+
).catch(() => void 0);
|
|
4303
|
+
});
|
|
4304
|
+
let timer;
|
|
4305
|
+
await Promise.race([
|
|
4306
|
+
unregistered,
|
|
4307
|
+
new Promise((resolve) => {
|
|
4308
|
+
timer = setTimeout(resolve, PUSH_LOGOUT_TIMEOUT_MS);
|
|
4309
|
+
})
|
|
4310
|
+
]);
|
|
4311
|
+
clearTimeout(timer);
|
|
4312
|
+
}
|
|
4313
|
+
enqueue(op) {
|
|
4314
|
+
const run = this.pushQueue.then(op, op);
|
|
4315
|
+
this.pushQueue = run.catch(() => void 0);
|
|
4316
|
+
return run;
|
|
4317
|
+
}
|
|
4318
|
+
deleteSubscription(platform, identifier) {
|
|
4319
|
+
const body = { platform };
|
|
4320
|
+
if (platform === "web") {
|
|
4321
|
+
body["endpoint"] = identifier;
|
|
4322
|
+
} else {
|
|
4323
|
+
body["deviceToken"] = identifier;
|
|
4324
|
+
}
|
|
4207
4325
|
return this.http.request({
|
|
4208
4326
|
method: "DELETE",
|
|
4209
|
-
path:
|
|
4327
|
+
path: "/notifications/subscribe",
|
|
4328
|
+
body,
|
|
4210
4329
|
authenticated: true
|
|
4211
4330
|
});
|
|
4212
4331
|
}
|
|
4332
|
+
/** The value `unsubscribe()` identifies a subscription by: endpoint for web, token otherwise. */
|
|
4333
|
+
static identifierOf(subscription) {
|
|
4334
|
+
return (subscription.platform === "web" ? subscription.endpoint : subscription.deviceToken) ?? "";
|
|
4335
|
+
}
|
|
4336
|
+
static isStoredRegistration(value) {
|
|
4337
|
+
if (typeof value !== "object" || value === null) return false;
|
|
4338
|
+
const subscription = value.subscription;
|
|
4339
|
+
return typeof subscription === "object" && subscription !== null && typeof subscription.platform === "string";
|
|
4340
|
+
}
|
|
4341
|
+
loadRegistration() {
|
|
4342
|
+
if (typeof localStorage === "undefined") return this.memoryRegistration;
|
|
4343
|
+
try {
|
|
4344
|
+
const raw = localStorage.getItem(this.registrationKey);
|
|
4345
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
4346
|
+
return _NotificationsModule.isStoredRegistration(parsed) ? parsed : this.memoryRegistration;
|
|
4347
|
+
} catch {
|
|
4348
|
+
return this.memoryRegistration;
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
saveRegistration(registration) {
|
|
4352
|
+
this.memoryRegistration = registration;
|
|
4353
|
+
if (typeof localStorage === "undefined") return;
|
|
4354
|
+
try {
|
|
4355
|
+
if (registration) {
|
|
4356
|
+
localStorage.setItem(this.registrationKey, JSON.stringify(registration));
|
|
4357
|
+
} else {
|
|
4358
|
+
localStorage.removeItem(this.registrationKey);
|
|
4359
|
+
}
|
|
4360
|
+
} catch {
|
|
4361
|
+
}
|
|
4362
|
+
}
|
|
4213
4363
|
/** Get all subscriptions for the current user */
|
|
4214
4364
|
async getSubscriptions() {
|
|
4215
4365
|
return this.http.request({
|
|
@@ -4412,10 +4562,21 @@ function createClient(config) {
|
|
|
4412
4562
|
getToken: () => tokenManager.getAccessToken(),
|
|
4413
4563
|
onTokenRefreshed: (listener) => tokenManager.onTokenRefreshed((tokens) => listener(tokens.accessToken))
|
|
4414
4564
|
});
|
|
4415
|
-
const
|
|
4565
|
+
const notifications = new NotificationsModule(httpClient, {
|
|
4566
|
+
projectId: config.projectId
|
|
4567
|
+
});
|
|
4568
|
+
const autoManagePush = config.autoManagePush ?? true;
|
|
4569
|
+
const auth = new AuthModule(
|
|
4570
|
+
httpClient,
|
|
4571
|
+
tokenManager,
|
|
4572
|
+
config,
|
|
4573
|
+
autoManagePush ? {
|
|
4574
|
+
beforeLogout: () => notifications.unsubscribeBeforeLogout(),
|
|
4575
|
+
afterSignIn: () => notifications.resubscribeAfterLogin()
|
|
4576
|
+
} : {}
|
|
4577
|
+
);
|
|
4416
4578
|
const storage = new StorageModule(httpClient, tokenManager, config);
|
|
4417
4579
|
const db = new DatabaseModule(httpClient, config.projectId, realtime);
|
|
4418
|
-
const notifications = new NotificationsModule(httpClient);
|
|
4419
4580
|
const functions = new FunctionsModule(httpClient);
|
|
4420
4581
|
const schedule = new ScheduleModule(httpClient, config.projectId);
|
|
4421
4582
|
return {
|