lazypock 0.19.0 → 0.19.2
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 +35 -14
- package/dist/index.cjs +185 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -2
- package/dist/index.d.ts +48 -2
- package/dist/index.global.js +185 -41
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +185 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/auth.ts +75 -3
- package/src/collection.ts +33 -7
- package/src/collections.ts +3 -2
- package/src/http.ts +120 -31
- package/src/lazypock.ts +11 -6
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ var ApiError = class extends Error {
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
// src/http.ts
|
|
22
|
+
var SUPERUSERS = "_superusers";
|
|
22
23
|
function isAbortError(err) {
|
|
23
24
|
return err instanceof Error && (err.name === "AbortError" || err.message === "Aborted");
|
|
24
25
|
}
|
|
@@ -45,6 +46,8 @@ var HttpClient = class {
|
|
|
45
46
|
this.inflight = {};
|
|
46
47
|
/** Global toggle for the auto-cancellation behaviour (default: on). */
|
|
47
48
|
this.enableAutoCancellation = true;
|
|
49
|
+
/** A refresh already in flight, so concurrent requests rotate once, not N times. */
|
|
50
|
+
this.refreshInFlight = null;
|
|
48
51
|
this._baseUrl = baseUrl.replace(/\/+$/, "");
|
|
49
52
|
this.authStore = authStore;
|
|
50
53
|
this.connectionId = connectionId;
|
|
@@ -54,36 +57,83 @@ var HttpClient = class {
|
|
|
54
57
|
get baseUrl() {
|
|
55
58
|
return this._baseUrl;
|
|
56
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Exchange the stored refresh token for a new access token.
|
|
62
|
+
*
|
|
63
|
+
* The server's `auth-refresh` wants the **refresh** token — in the body, or as the
|
|
64
|
+
* bearer — not the access token this used to send, which is why every refresh
|
|
65
|
+
* attempt 401'd and cleared the session. The collection is the one the session
|
|
66
|
+
* belongs to, or `_superusers` when the store has none (a superuser login).
|
|
67
|
+
*
|
|
68
|
+
* A network failure keeps the session (the next request can retry); only a refusal
|
|
69
|
+
* by the server ends it.
|
|
70
|
+
*/
|
|
57
71
|
async refreshAuth() {
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
72
|
+
const refreshToken = this.authStore.refreshToken;
|
|
73
|
+
if (!refreshToken) return null;
|
|
74
|
+
if (this.refreshInFlight) {
|
|
75
|
+
const refreshed = await this.refreshInFlight;
|
|
76
|
+
return refreshed ? this.currentAuthResponse() : null;
|
|
77
|
+
}
|
|
78
|
+
this.refreshInFlight = this.performRefresh(refreshToken);
|
|
60
79
|
try {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
80
|
+
const refreshed = await this.refreshInFlight;
|
|
81
|
+
return refreshed ? this.currentAuthResponse() : null;
|
|
82
|
+
} finally {
|
|
83
|
+
this.refreshInFlight = null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** The store's current session, in the shape the old refresh returned. */
|
|
87
|
+
currentAuthResponse() {
|
|
88
|
+
return {
|
|
89
|
+
token: this.authStore.token,
|
|
90
|
+
record: this.authStore.model ?? {}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async performRefresh(refreshToken) {
|
|
94
|
+
const collection = this.refreshCollection();
|
|
95
|
+
try {
|
|
96
|
+
const res = await this.defaultFetch(
|
|
97
|
+
this.baseUrl + "/collections/" + encodeURIComponent(collection) + "/auth-refresh",
|
|
98
|
+
{
|
|
99
|
+
method: "POST",
|
|
100
|
+
headers: { "Content-Type": "application/json" },
|
|
101
|
+
body: JSON.stringify({ refreshToken })
|
|
102
|
+
}
|
|
103
|
+
);
|
|
72
104
|
if (!res.ok) {
|
|
73
|
-
|
|
74
|
-
|
|
105
|
+
if (res.status === 400 || res.status === 401) {
|
|
106
|
+
this.authStore.clear();
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
75
109
|
}
|
|
76
110
|
const data = await res.json();
|
|
77
|
-
if (data
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
this.authStore.set(data.token, model);
|
|
81
|
-
return data;
|
|
111
|
+
if (!data || typeof data.token !== "string") {
|
|
112
|
+
this.authStore.clear();
|
|
113
|
+
return false;
|
|
82
114
|
}
|
|
83
|
-
|
|
115
|
+
const record = data["record"];
|
|
116
|
+
const model = record && typeof record === "object" ? record : null;
|
|
117
|
+
this.authStore.setCollectionName(
|
|
118
|
+
collection === SUPERUSERS ? null : collection
|
|
119
|
+
);
|
|
120
|
+
this.authStore.setSession(data.token, model, {
|
|
121
|
+
refreshToken: typeof data.refreshToken === "string" ? data.refreshToken : void 0,
|
|
122
|
+
expiresIn: typeof data.expiresIn === "number" ? data.expiresIn : void 0
|
|
123
|
+
});
|
|
124
|
+
return true;
|
|
84
125
|
} catch {
|
|
85
|
-
return
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** The auth collection a refresh is addressed to, including superusers. */
|
|
130
|
+
refreshCollection() {
|
|
131
|
+
if (this.authStore.collectionName) return this.authStore.collectionName;
|
|
132
|
+
const model = this.authStore.model;
|
|
133
|
+
if (model && typeof model.collectionName === "string" && model.collectionName) {
|
|
134
|
+
return model.collectionName;
|
|
86
135
|
}
|
|
136
|
+
return SUPERUSERS;
|
|
87
137
|
}
|
|
88
138
|
/**
|
|
89
139
|
* Globally enable or disable auto-cancellation of duplicated pending requests.
|
|
@@ -130,7 +180,7 @@ var HttpClient = class {
|
|
|
130
180
|
* (aborted requests throw an `ApiError` with `isAbort === true`).
|
|
131
181
|
*/
|
|
132
182
|
async request(method, path, body, options) {
|
|
133
|
-
if (this.authStore.isExpired && this.authStore.
|
|
183
|
+
if (this.authStore.isExpired && this.authStore.refreshToken) {
|
|
134
184
|
await this.refreshAuth();
|
|
135
185
|
}
|
|
136
186
|
let requestKey = options?.requestKey === void 0 ? options?.cancelKey ?? `${method} ${path}` : options.requestKey;
|
|
@@ -221,8 +271,9 @@ var HttpClient = class {
|
|
|
221
271
|
}
|
|
222
272
|
let res = null;
|
|
223
273
|
const fetcher = options?.fetch ?? this.defaultFetch;
|
|
274
|
+
const send = async () => fetcher(url, init);
|
|
224
275
|
try {
|
|
225
|
-
res = await
|
|
276
|
+
res = await send();
|
|
226
277
|
} catch (err) {
|
|
227
278
|
if (isAbortError(err)) {
|
|
228
279
|
throw new ApiError(
|
|
@@ -238,6 +289,27 @@ var HttpClient = class {
|
|
|
238
289
|
delete this.cancelControllers[requestKey];
|
|
239
290
|
}
|
|
240
291
|
}
|
|
292
|
+
if (res.status === 401 && this.authStore.refreshToken) {
|
|
293
|
+
await res.text().catch(() => {
|
|
294
|
+
});
|
|
295
|
+
const refreshed = await this.refreshAuth();
|
|
296
|
+
if (refreshed) {
|
|
297
|
+
headers["Authorization"] = "Bearer " + this.authStore.token;
|
|
298
|
+
try {
|
|
299
|
+
res = await send();
|
|
300
|
+
} catch (err) {
|
|
301
|
+
if (isAbortError(err)) {
|
|
302
|
+
throw new ApiError(
|
|
303
|
+
"The request was aborted (most likely auto-cancelled by a newer request with the same requestKey)",
|
|
304
|
+
{},
|
|
305
|
+
0,
|
|
306
|
+
true
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
throw err;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
241
313
|
if (res.status === 204) return null;
|
|
242
314
|
let bodyText = "";
|
|
243
315
|
let data = {};
|
|
@@ -312,6 +384,7 @@ var AuthStore = class {
|
|
|
312
384
|
this._token = "";
|
|
313
385
|
this._model = null;
|
|
314
386
|
this._tokenExpiresAt = null;
|
|
387
|
+
this._refreshToken = "";
|
|
315
388
|
this._collectionName = null;
|
|
316
389
|
this.listeners = /* @__PURE__ */ new Set();
|
|
317
390
|
this.storage = storage ?? {
|
|
@@ -326,6 +399,15 @@ var AuthStore = class {
|
|
|
326
399
|
get token() {
|
|
327
400
|
return this._token;
|
|
328
401
|
}
|
|
402
|
+
/**
|
|
403
|
+
* The refresh token for the current session, or empty string.
|
|
404
|
+
*
|
|
405
|
+
* Kept by the store so a session can outlive the access token (15 minutes on the
|
|
406
|
+
* server) even across a reload. Persisted under `auth_refresh_token`.
|
|
407
|
+
*/
|
|
408
|
+
get refreshToken() {
|
|
409
|
+
return this._refreshToken;
|
|
410
|
+
}
|
|
329
411
|
/** The current authenticated user record, or null. */
|
|
330
412
|
get model() {
|
|
331
413
|
return this._model;
|
|
@@ -360,14 +442,16 @@ var AuthStore = class {
|
|
|
360
442
|
* Should be called once at application startup.
|
|
361
443
|
*/
|
|
362
444
|
async init() {
|
|
363
|
-
const [token, model, expiresAt, collectionName] = await Promise.all([
|
|
445
|
+
const [token, model, expiresAt, collectionName, refreshToken] = await Promise.all([
|
|
364
446
|
this.storage.get("auth_token"),
|
|
365
447
|
this.storage.get("auth_model"),
|
|
366
448
|
this.storage.get("auth_expires_at"),
|
|
367
|
-
this.storage.get("auth_collection")
|
|
449
|
+
this.storage.get("auth_collection"),
|
|
450
|
+
this.storage.get("auth_refresh_token")
|
|
368
451
|
]);
|
|
369
452
|
if (token) this._token = token;
|
|
370
453
|
if (expiresAt) this._tokenExpiresAt = parseInt(expiresAt, 10) || null;
|
|
454
|
+
if (refreshToken) this._refreshToken = refreshToken;
|
|
371
455
|
if (model) {
|
|
372
456
|
try {
|
|
373
457
|
this._model = JSON.parse(model);
|
|
@@ -382,6 +466,10 @@ var AuthStore = class {
|
|
|
382
466
|
}
|
|
383
467
|
/**
|
|
384
468
|
* Update the current auth token and model, persist to storage, and notify listeners.
|
|
469
|
+
*
|
|
470
|
+
* Uses the fallback TTL; prefer {@link setSession} with a server auth response,
|
|
471
|
+
* which carries the real `expiresIn` and the refresh token.
|
|
472
|
+
*
|
|
385
473
|
* @param token The JWT token string.
|
|
386
474
|
* @param model The authenticated user record, or null for superusers.
|
|
387
475
|
*/
|
|
@@ -401,18 +489,44 @@ var AuthStore = class {
|
|
|
401
489
|
this.notify();
|
|
402
490
|
}
|
|
403
491
|
/**
|
|
404
|
-
*
|
|
492
|
+
* Record a complete auth response: token, model, refresh token and the access
|
|
493
|
+
* token's real lifetime.
|
|
494
|
+
*
|
|
495
|
+
* `expiresIn` is seconds, the field the server's auth responses carry. Not passing
|
|
496
|
+
* it (an older server) falls back to the historical 7-day assumption.
|
|
497
|
+
*
|
|
498
|
+
* @param token The JWT access token.
|
|
499
|
+
* @param model The authenticated record, or null for a superuser.
|
|
500
|
+
* @param options `refreshToken` and `expiresIn` from the same response.
|
|
501
|
+
*/
|
|
502
|
+
setSession(token, model, options) {
|
|
503
|
+
this.set(token, model);
|
|
504
|
+
if (options?.refreshToken) {
|
|
505
|
+
this._refreshToken = options.refreshToken;
|
|
506
|
+
void this.storage.set("auth_refresh_token", options.refreshToken);
|
|
507
|
+
}
|
|
508
|
+
const expiresIn = options?.expiresIn;
|
|
509
|
+
if (typeof expiresIn === "number" && expiresIn > 0) {
|
|
510
|
+
this._tokenExpiresAt = Date.now() + expiresIn * 1e3;
|
|
511
|
+
void this.storage.set("auth_expires_at", String(this._tokenExpiresAt));
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Clear all auth state (token, model, expiry, refresh token, collection) and
|
|
516
|
+
* notify listeners.
|
|
405
517
|
*/
|
|
406
518
|
clear() {
|
|
407
519
|
this._token = "";
|
|
408
520
|
this._model = null;
|
|
409
521
|
this._tokenExpiresAt = null;
|
|
522
|
+
this._refreshToken = "";
|
|
410
523
|
this._collectionName = null;
|
|
411
524
|
void Promise.all([
|
|
412
525
|
this.storage.remove("auth_token"),
|
|
413
526
|
this.storage.remove("auth_expires_at"),
|
|
414
527
|
this.storage.remove("auth_model"),
|
|
415
|
-
this.storage.remove("auth_collection")
|
|
528
|
+
this.storage.remove("auth_collection"),
|
|
529
|
+
this.storage.remove("auth_refresh_token")
|
|
416
530
|
]);
|
|
417
531
|
this.notify();
|
|
418
532
|
}
|
|
@@ -434,6 +548,13 @@ var AuthStore = class {
|
|
|
434
548
|
}
|
|
435
549
|
}
|
|
436
550
|
};
|
|
551
|
+
function rememberAuthResponse(store, data, model) {
|
|
552
|
+
if (!data || typeof data.token !== "string" || !data.token) return;
|
|
553
|
+
store.setSession(data.token, model, {
|
|
554
|
+
refreshToken: typeof data.refreshToken === "string" ? data.refreshToken : void 0,
|
|
555
|
+
expiresIn: typeof data.expiresIn === "number" ? data.expiresIn : void 0
|
|
556
|
+
});
|
|
557
|
+
}
|
|
437
558
|
var memoryStorage = {
|
|
438
559
|
get(key) {
|
|
439
560
|
try {
|
|
@@ -783,6 +904,11 @@ function authorizeWithOAuth2Popup(opts) {
|
|
|
783
904
|
}
|
|
784
905
|
|
|
785
906
|
// src/collection.ts
|
|
907
|
+
var MAX_PER_PAGE = 500;
|
|
908
|
+
function clampBatch(batch) {
|
|
909
|
+
if (!Number.isFinite(batch)) return MAX_PER_PAGE;
|
|
910
|
+
return Math.min(Math.max(1, Math.floor(batch)), MAX_PER_PAGE);
|
|
911
|
+
}
|
|
786
912
|
function stableStringify(value) {
|
|
787
913
|
const seen = /* @__PURE__ */ new Set();
|
|
788
914
|
const sort = (v) => {
|
|
@@ -1279,13 +1405,14 @@ var CollectionService = class _CollectionService {
|
|
|
1279
1405
|
return result;
|
|
1280
1406
|
}
|
|
1281
1407
|
/**
|
|
1282
|
-
* Fetch all records at once (auto-paginates). Mirrors PocketBase's
|
|
1408
|
+
* Fetch all records at once (auto-paginates; `batch` defaults to and is capped at 500, the server max `perPage`). Mirrors PocketBase's
|
|
1283
1409
|
* `pb.collection(name).getFullList()`.
|
|
1284
1410
|
*
|
|
1285
1411
|
* @param options Query params (`sort`, `filter`, `batch`, etc.) + request options.
|
|
1286
1412
|
*/
|
|
1287
1413
|
async getFullList(options) {
|
|
1288
|
-
const { batch =
|
|
1414
|
+
const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
|
|
1415
|
+
const batch = clampBatch(requestedBatch);
|
|
1289
1416
|
if (typeof rest.filter === "function") {
|
|
1290
1417
|
rest.filter = this.resolveFilter(rest.filter);
|
|
1291
1418
|
}
|
|
@@ -1502,7 +1629,11 @@ var CollectionService = class _CollectionService {
|
|
|
1502
1629
|
);
|
|
1503
1630
|
if (data && this.authStore) {
|
|
1504
1631
|
this.authStore.setCollectionName(this.collectionName);
|
|
1505
|
-
|
|
1632
|
+
rememberAuthResponse(
|
|
1633
|
+
this.authStore,
|
|
1634
|
+
data,
|
|
1635
|
+
data.record
|
|
1636
|
+
);
|
|
1506
1637
|
}
|
|
1507
1638
|
return data;
|
|
1508
1639
|
}
|
|
@@ -1513,12 +1644,16 @@ var CollectionService = class _CollectionService {
|
|
|
1513
1644
|
async authRefresh(options) {
|
|
1514
1645
|
const data = await this.http.post(
|
|
1515
1646
|
this.collectionPath() + "/auth-refresh",
|
|
1516
|
-
|
|
1647
|
+
{ refreshToken: this.authStore?.refreshToken },
|
|
1517
1648
|
options
|
|
1518
1649
|
);
|
|
1519
1650
|
if (data && this.authStore) {
|
|
1520
1651
|
this.authStore.setCollectionName(this.collectionName);
|
|
1521
|
-
|
|
1652
|
+
rememberAuthResponse(
|
|
1653
|
+
this.authStore,
|
|
1654
|
+
data,
|
|
1655
|
+
data.record
|
|
1656
|
+
);
|
|
1522
1657
|
}
|
|
1523
1658
|
return data;
|
|
1524
1659
|
}
|
|
@@ -1701,7 +1836,11 @@ var CollectionService = class _CollectionService {
|
|
|
1701
1836
|
}
|
|
1702
1837
|
if (this.authStore) {
|
|
1703
1838
|
this.authStore.setCollectionName(this.collectionName);
|
|
1704
|
-
|
|
1839
|
+
rememberAuthResponse(
|
|
1840
|
+
this.authStore,
|
|
1841
|
+
data,
|
|
1842
|
+
data.record
|
|
1843
|
+
);
|
|
1705
1844
|
}
|
|
1706
1845
|
return data;
|
|
1707
1846
|
}
|
|
@@ -2166,7 +2305,8 @@ var CollectionsService = class {
|
|
|
2166
2305
|
*/
|
|
2167
2306
|
async getFullList(options) {
|
|
2168
2307
|
if (!this.http) return [];
|
|
2169
|
-
const { batch =
|
|
2308
|
+
const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
|
|
2309
|
+
const batch = clampBatch(requestedBatch);
|
|
2170
2310
|
const {
|
|
2171
2311
|
requestKey: reqKey,
|
|
2172
2312
|
singleFlight: _singleFlight,
|
|
@@ -2422,7 +2562,7 @@ var LazypockClient = class {
|
|
|
2422
2562
|
const data = await this.http.post("/superusers/setup", { email, password });
|
|
2423
2563
|
if (data) {
|
|
2424
2564
|
this.authStore.setCollectionName(null);
|
|
2425
|
-
this.authStore
|
|
2565
|
+
rememberAuthResponse(this.authStore, data, null);
|
|
2426
2566
|
}
|
|
2427
2567
|
return data;
|
|
2428
2568
|
}
|
|
@@ -2446,7 +2586,11 @@ var LazypockClient = class {
|
|
|
2446
2586
|
});
|
|
2447
2587
|
if (data && data.record) {
|
|
2448
2588
|
this.authStore.setCollectionName(collection);
|
|
2449
|
-
|
|
2589
|
+
rememberAuthResponse(
|
|
2590
|
+
this.authStore,
|
|
2591
|
+
data,
|
|
2592
|
+
data.record
|
|
2593
|
+
);
|
|
2450
2594
|
}
|
|
2451
2595
|
} else {
|
|
2452
2596
|
try {
|
|
@@ -2462,7 +2606,7 @@ var LazypockClient = class {
|
|
|
2462
2606
|
}
|
|
2463
2607
|
if (data) {
|
|
2464
2608
|
this.authStore.setCollectionName(null);
|
|
2465
|
-
this.authStore
|
|
2609
|
+
rememberAuthResponse(this.authStore, data, null);
|
|
2466
2610
|
}
|
|
2467
2611
|
}
|
|
2468
2612
|
return data;
|
|
@@ -2496,7 +2640,7 @@ var LazypockClient = class {
|
|
|
2496
2640
|
);
|
|
2497
2641
|
if (data) {
|
|
2498
2642
|
this.authStore.setCollectionName(collection);
|
|
2499
|
-
this.authStore
|
|
2643
|
+
rememberAuthResponse(this.authStore, data, data.record);
|
|
2500
2644
|
}
|
|
2501
2645
|
return data;
|
|
2502
2646
|
}
|
|
@@ -2510,12 +2654,12 @@ var LazypockClient = class {
|
|
|
2510
2654
|
async authRefresh(collection, options) {
|
|
2511
2655
|
const data = await this.http.post(
|
|
2512
2656
|
"/collections/" + encodeURIComponent(collection) + "/auth-refresh",
|
|
2513
|
-
|
|
2657
|
+
{ refreshToken: this.authStore.refreshToken },
|
|
2514
2658
|
options
|
|
2515
2659
|
);
|
|
2516
2660
|
if (data) {
|
|
2517
2661
|
this.authStore.setCollectionName(collection);
|
|
2518
|
-
this.authStore
|
|
2662
|
+
rememberAuthResponse(this.authStore, data, data.record);
|
|
2519
2663
|
}
|
|
2520
2664
|
return data;
|
|
2521
2665
|
}
|