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/package.json
CHANGED
package/src/auth.ts
CHANGED
|
@@ -31,13 +31,17 @@ export interface AuthModel {
|
|
|
31
31
|
/** Callback signature for auth state changes. */
|
|
32
32
|
export type AuthListener = (model: AuthModel | null, token: string) => void;
|
|
33
33
|
|
|
34
|
-
//
|
|
34
|
+
// Fallback only. The server's auth responses carry `expiresIn` (the access token's
|
|
35
|
+
// real lifetime, 15 minutes on the Rust server), which is what `setSession` records.
|
|
36
|
+
// The old constant assumed a 7-day Phoenix token, so a client that never used
|
|
37
|
+
// `expiresIn` never refreshed in time and was signed out mid-session.
|
|
35
38
|
const TOKEN_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
36
39
|
|
|
37
40
|
export class AuthStore {
|
|
38
41
|
private _token = "";
|
|
39
42
|
private _model: AuthModel | null = null;
|
|
40
43
|
private _tokenExpiresAt: number | null = null;
|
|
44
|
+
private _refreshToken = "";
|
|
41
45
|
private _collectionName: string | null = null;
|
|
42
46
|
private listeners = new Set<AuthListener>();
|
|
43
47
|
private storage: StorageAdapter;
|
|
@@ -59,6 +63,16 @@ export class AuthStore {
|
|
|
59
63
|
return this._token;
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
/**
|
|
67
|
+
* The refresh token for the current session, or empty string.
|
|
68
|
+
*
|
|
69
|
+
* Kept by the store so a session can outlive the access token (15 minutes on the
|
|
70
|
+
* server) even across a reload. Persisted under `auth_refresh_token`.
|
|
71
|
+
*/
|
|
72
|
+
get refreshToken(): string {
|
|
73
|
+
return this._refreshToken;
|
|
74
|
+
}
|
|
75
|
+
|
|
62
76
|
/** The current authenticated user record, or null. */
|
|
63
77
|
get model(): AuthModel | null {
|
|
64
78
|
return this._model;
|
|
@@ -101,14 +115,16 @@ export class AuthStore {
|
|
|
101
115
|
* Should be called once at application startup.
|
|
102
116
|
*/
|
|
103
117
|
async init(): Promise<void> {
|
|
104
|
-
const [token, model, expiresAt, collectionName] = await Promise.all([
|
|
118
|
+
const [token, model, expiresAt, collectionName, refreshToken] = await Promise.all([
|
|
105
119
|
this.storage.get("auth_token"),
|
|
106
120
|
this.storage.get("auth_model"),
|
|
107
121
|
this.storage.get("auth_expires_at"),
|
|
108
122
|
this.storage.get("auth_collection"),
|
|
123
|
+
this.storage.get("auth_refresh_token"),
|
|
109
124
|
]);
|
|
110
125
|
if (token) this._token = token;
|
|
111
126
|
if (expiresAt) this._tokenExpiresAt = parseInt(expiresAt, 10) || null;
|
|
127
|
+
if (refreshToken) this._refreshToken = refreshToken;
|
|
112
128
|
if (model) {
|
|
113
129
|
try {
|
|
114
130
|
this._model = JSON.parse(model);
|
|
@@ -128,6 +144,10 @@ export class AuthStore {
|
|
|
128
144
|
|
|
129
145
|
/**
|
|
130
146
|
* Update the current auth token and model, persist to storage, and notify listeners.
|
|
147
|
+
*
|
|
148
|
+
* Uses the fallback TTL; prefer {@link setSession} with a server auth response,
|
|
149
|
+
* which carries the real `expiresIn` and the refresh token.
|
|
150
|
+
*
|
|
131
151
|
* @param token The JWT token string.
|
|
132
152
|
* @param model The authenticated user record, or null for superusers.
|
|
133
153
|
*/
|
|
@@ -154,18 +174,49 @@ export class AuthStore {
|
|
|
154
174
|
}
|
|
155
175
|
|
|
156
176
|
/**
|
|
157
|
-
*
|
|
177
|
+
* Record a complete auth response: token, model, refresh token and the access
|
|
178
|
+
* token's real lifetime.
|
|
179
|
+
*
|
|
180
|
+
* `expiresIn` is seconds, the field the server's auth responses carry. Not passing
|
|
181
|
+
* it (an older server) falls back to the historical 7-day assumption.
|
|
182
|
+
*
|
|
183
|
+
* @param token The JWT access token.
|
|
184
|
+
* @param model The authenticated record, or null for a superuser.
|
|
185
|
+
* @param options `refreshToken` and `expiresIn` from the same response.
|
|
186
|
+
*/
|
|
187
|
+
setSession(
|
|
188
|
+
token: string,
|
|
189
|
+
model: AuthModel | null,
|
|
190
|
+
options?: { refreshToken?: string; expiresIn?: number },
|
|
191
|
+
): void {
|
|
192
|
+
this.set(token, model);
|
|
193
|
+
if (options?.refreshToken) {
|
|
194
|
+
this._refreshToken = options.refreshToken;
|
|
195
|
+
void this.storage.set("auth_refresh_token", options.refreshToken);
|
|
196
|
+
}
|
|
197
|
+
const expiresIn = options?.expiresIn;
|
|
198
|
+
if (typeof expiresIn === "number" && expiresIn > 0) {
|
|
199
|
+
this._tokenExpiresAt = Date.now() + expiresIn * 1000;
|
|
200
|
+
void this.storage.set("auth_expires_at", String(this._tokenExpiresAt));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Clear all auth state (token, model, expiry, refresh token, collection) and
|
|
206
|
+
* notify listeners.
|
|
158
207
|
*/
|
|
159
208
|
clear(): void {
|
|
160
209
|
this._token = "";
|
|
161
210
|
this._model = null;
|
|
162
211
|
this._tokenExpiresAt = null;
|
|
212
|
+
this._refreshToken = "";
|
|
163
213
|
this._collectionName = null;
|
|
164
214
|
void Promise.all([
|
|
165
215
|
this.storage.remove("auth_token"),
|
|
166
216
|
this.storage.remove("auth_expires_at"),
|
|
167
217
|
this.storage.remove("auth_model"),
|
|
168
218
|
this.storage.remove("auth_collection"),
|
|
219
|
+
this.storage.remove("auth_refresh_token"),
|
|
169
220
|
]);
|
|
170
221
|
this.notify();
|
|
171
222
|
}
|
|
@@ -192,6 +243,27 @@ export class AuthStore {
|
|
|
192
243
|
}
|
|
193
244
|
}
|
|
194
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Record a server auth response (login, setup, OAuth2 exchange, refresh) in the store.
|
|
248
|
+
*
|
|
249
|
+
* Every auth response carries `refreshToken` and `expiresIn`; keeping both is what
|
|
250
|
+
* lets a session outlive the access token. Call sites used to store only the token,
|
|
251
|
+
* so the refresh token was dropped and the local expiry was the 7-day fallback — the
|
|
252
|
+
* session was then used until the server refused it, with no way to refresh.
|
|
253
|
+
*/
|
|
254
|
+
export function rememberAuthResponse(
|
|
255
|
+
store: AuthStore,
|
|
256
|
+
data: Record<string, unknown> | null | undefined,
|
|
257
|
+
model: AuthModel | null,
|
|
258
|
+
): void {
|
|
259
|
+
if (!data || typeof data.token !== "string" || !data.token) return;
|
|
260
|
+
store.setSession(data.token, model, {
|
|
261
|
+
refreshToken:
|
|
262
|
+
typeof data.refreshToken === "string" ? data.refreshToken : undefined,
|
|
263
|
+
expiresIn: typeof data.expiresIn === "number" ? data.expiresIn : undefined,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
195
267
|
// ── Memory-only storage (default, works everywhere) ─────
|
|
196
268
|
/** Default storage adapter using `localStorage` with graceful fallback. */
|
|
197
269
|
export const memoryStorage: StorageAdapter = {
|
package/src/collection.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Typed CRUD for a single collection (like PocketBase's pb.collection(name))
|
|
3
3
|
|
|
4
4
|
import type { HttpClient } from "./http";
|
|
5
|
-
import type
|
|
5
|
+
import { rememberAuthResponse, type AuthStore, type AuthModel } from "./auth";
|
|
6
6
|
import {
|
|
7
7
|
ApiError,
|
|
8
8
|
type ApiRecord,
|
|
@@ -48,6 +48,19 @@ type StableRequestValue =
|
|
|
48
48
|
* Strips request-transport keys (`requestKey`, `singleFlight`, `fetch`,
|
|
49
49
|
* `signal`) so functionally-identical calls share one key.
|
|
50
50
|
*/
|
|
51
|
+
/**
|
|
52
|
+
* Largest `perPage` the server accepts (`MAX_PER_PAGE` in lazypock's pagination).
|
|
53
|
+
* `getFullList()` pages at this size by default and never asks for more: a larger
|
|
54
|
+
* `perPage` is rejected with a 400 rather than clamped.
|
|
55
|
+
*/
|
|
56
|
+
export const MAX_PER_PAGE = 500;
|
|
57
|
+
|
|
58
|
+
/** Keep a requested page size within what the server accepts (1..MAX_PER_PAGE). */
|
|
59
|
+
export function clampBatch(batch: number): number {
|
|
60
|
+
if (!Number.isFinite(batch)) return MAX_PER_PAGE;
|
|
61
|
+
return Math.min(Math.max(1, Math.floor(batch)), MAX_PER_PAGE);
|
|
62
|
+
}
|
|
63
|
+
|
|
51
64
|
export function stableStringify(value: unknown): string {
|
|
52
65
|
const seen = new Set<object>();
|
|
53
66
|
const sort = (v: unknown): StableRequestValue => {
|
|
@@ -771,7 +784,7 @@ export class CollectionService<
|
|
|
771
784
|
}
|
|
772
785
|
|
|
773
786
|
/**
|
|
774
|
-
* Fetch all records at once (auto-paginates). Mirrors PocketBase's
|
|
787
|
+
* Fetch all records at once (auto-paginates; `batch` defaults to and is capped at 500, the server max `perPage`). Mirrors PocketBase's
|
|
775
788
|
* `pb.collection(name).getFullList()`.
|
|
776
789
|
*
|
|
777
790
|
* @param options Query params (`sort`, `filter`, `batch`, etc.) + request options.
|
|
@@ -784,7 +797,8 @@ export class CollectionService<
|
|
|
784
797
|
>(
|
|
785
798
|
options?: ListOptions<TFields, E, S, F> & RequestOptions,
|
|
786
799
|
): Promise<Array<T2 & { expand?: ExpandObj<E, TExpand> }>> {
|
|
787
|
-
const { batch =
|
|
800
|
+
const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
|
|
801
|
+
const batch = clampBatch(requestedBatch as number);
|
|
788
802
|
|
|
789
803
|
// Resolve a `filter` callback before computing the dedup key so the key
|
|
790
804
|
// reflects the actual filter expression (a function is not serializable).
|
|
@@ -1121,7 +1135,11 @@ export class CollectionService<
|
|
|
1121
1135
|
this.authStore.setCollectionName(this.collectionName);
|
|
1122
1136
|
// SAFETY: the server's auth response record is a superset of
|
|
1123
1137
|
// AuthModel; the auth store consumes it generically.
|
|
1124
|
-
|
|
1138
|
+
rememberAuthResponse(
|
|
1139
|
+
this.authStore,
|
|
1140
|
+
data,
|
|
1141
|
+
data.record as unknown as AuthModel,
|
|
1142
|
+
);
|
|
1125
1143
|
}
|
|
1126
1144
|
return data;
|
|
1127
1145
|
}
|
|
@@ -1139,14 +1157,18 @@ export class CollectionService<
|
|
|
1139
1157
|
{ token: string; record: ApiRecord } & Record<string, unknown>
|
|
1140
1158
|
>(
|
|
1141
1159
|
this.collectionPath() + "/auth-refresh",
|
|
1142
|
-
|
|
1160
|
+
{ refreshToken: this.authStore?.refreshToken },
|
|
1143
1161
|
options,
|
|
1144
1162
|
);
|
|
1145
1163
|
if (data && this.authStore) {
|
|
1146
1164
|
this.authStore.setCollectionName(this.collectionName);
|
|
1147
1165
|
// SAFETY: the server's auth response record is a superset of
|
|
1148
1166
|
// AuthModel; the auth store consumes it generically.
|
|
1149
|
-
|
|
1167
|
+
rememberAuthResponse(
|
|
1168
|
+
this.authStore,
|
|
1169
|
+
data,
|
|
1170
|
+
data.record as unknown as AuthModel,
|
|
1171
|
+
);
|
|
1150
1172
|
}
|
|
1151
1173
|
return data;
|
|
1152
1174
|
}
|
|
@@ -1376,7 +1398,11 @@ export class CollectionService<
|
|
|
1376
1398
|
this.authStore.setCollectionName(this.collectionName);
|
|
1377
1399
|
// SAFETY: the server's auth response record is a superset of
|
|
1378
1400
|
// AuthModel; the auth store consumes it generically.
|
|
1379
|
-
|
|
1401
|
+
rememberAuthResponse(
|
|
1402
|
+
this.authStore,
|
|
1403
|
+
data as unknown as Record<string, unknown>,
|
|
1404
|
+
data.record as unknown as AuthModel,
|
|
1405
|
+
);
|
|
1380
1406
|
}
|
|
1381
1407
|
// SAFETY: `data.record` is the server's auth record for this
|
|
1382
1408
|
// collection; it satisfies `T` at runtime — `T` is a compile-time
|
package/src/collections.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import type { HttpClient } from "./http";
|
|
14
14
|
import type { RealtimeService } from "./realtime";
|
|
15
|
-
import { stableStringify } from "./collection";
|
|
15
|
+
import { clampBatch, MAX_PER_PAGE, stableStringify } from "./collection";
|
|
16
16
|
import type { ListResult, RequestOptions, ApiRecord } from "./types";
|
|
17
17
|
|
|
18
18
|
const REGISTRY_TOPIC = "collections";
|
|
@@ -74,7 +74,8 @@ export class CollectionsService {
|
|
|
74
74
|
options?: Record<string, unknown> & RequestOptions,
|
|
75
75
|
): Promise<Array<T>> {
|
|
76
76
|
if (!this.http) return [];
|
|
77
|
-
const { batch =
|
|
77
|
+
const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
|
|
78
|
+
const batch = clampBatch(requestedBatch as number);
|
|
78
79
|
|
|
79
80
|
// Extract request-transport options so they never leak into query params.
|
|
80
81
|
const {
|
package/src/http.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
import { ApiError, type Method, type RequestOptions } from "./types";
|
|
5
5
|
import type { AuthModel, AuthStore } from "./auth";
|
|
6
6
|
|
|
7
|
+
/** The auth collection the server serves superusers on, and the refresh path used
|
|
8
|
+
* when a session belongs to no collection (a superuser login). */
|
|
9
|
+
const SUPERUSERS = "_superusers";
|
|
10
|
+
|
|
7
11
|
/**
|
|
8
12
|
* Low-level HTTP client wrapping `fetch` with automatic auth token injection.
|
|
9
13
|
* Only relies on `globalThis.fetch` — works in browser, React Native, and Node 18+.
|
|
@@ -62,46 +66,102 @@ export class HttpClient {
|
|
|
62
66
|
return this._baseUrl;
|
|
63
67
|
}
|
|
64
68
|
|
|
65
|
-
|
|
69
|
+
/** A refresh already in flight, so concurrent requests rotate once, not N times. */
|
|
70
|
+
private refreshInFlight: Promise<boolean> | null = null;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Exchange the stored refresh token for a new access token.
|
|
74
|
+
*
|
|
75
|
+
* The server's `auth-refresh` wants the **refresh** token — in the body, or as the
|
|
76
|
+
* bearer — not the access token this used to send, which is why every refresh
|
|
77
|
+
* attempt 401'd and cleared the session. The collection is the one the session
|
|
78
|
+
* belongs to, or `_superusers` when the store has none (a superuser login).
|
|
79
|
+
*
|
|
80
|
+
* A network failure keeps the session (the next request can retry); only a refusal
|
|
81
|
+
* by the server ends it.
|
|
82
|
+
*/
|
|
83
|
+
private async refreshAuth(): Promise<{ token: string; record: Record<string, unknown> } | null> {
|
|
84
|
+
const refreshToken = this.authStore.refreshToken;
|
|
85
|
+
if (!refreshToken) return null;
|
|
86
|
+
if (this.refreshInFlight) {
|
|
87
|
+
const refreshed = await this.refreshInFlight;
|
|
88
|
+
return refreshed ? this.currentAuthResponse() : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.refreshInFlight = this.performRefresh(refreshToken);
|
|
92
|
+
try {
|
|
93
|
+
const refreshed = await this.refreshInFlight;
|
|
94
|
+
return refreshed ? this.currentAuthResponse() : null;
|
|
95
|
+
} finally {
|
|
96
|
+
this.refreshInFlight = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The store's current session, in the shape the old refresh returned. */
|
|
101
|
+
private currentAuthResponse(): {
|
|
66
102
|
token: string;
|
|
67
103
|
record: Record<string, unknown>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
104
|
+
} {
|
|
105
|
+
return {
|
|
106
|
+
token: this.authStore.token,
|
|
107
|
+
record: (this.authStore.model ?? {}) as unknown as Record<string, unknown>,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async performRefresh(refreshToken: string): Promise<boolean> {
|
|
112
|
+
const collection = this.refreshCollection();
|
|
71
113
|
try {
|
|
72
|
-
const
|
|
114
|
+
const res = await this.defaultFetch(
|
|
73
115
|
this.baseUrl +
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const res = await this.defaultFetch(url, {
|
|
84
|
-
method: "POST",
|
|
85
|
-
headers,
|
|
86
|
-
});
|
|
116
|
+
"/collections/" +
|
|
117
|
+
encodeURIComponent(collection) +
|
|
118
|
+
"/auth-refresh",
|
|
119
|
+
{
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers: { "Content-Type": "application/json" },
|
|
122
|
+
body: JSON.stringify({ refreshToken }),
|
|
123
|
+
},
|
|
124
|
+
);
|
|
87
125
|
if (!res.ok) {
|
|
88
|
-
|
|
89
|
-
|
|
126
|
+
// Revoked, expired, replayed or from another collection: there is no
|
|
127
|
+
// session left to keep.
|
|
128
|
+
if (res.status === 400 || res.status === 401) {
|
|
129
|
+
this.authStore.clear();
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
90
132
|
}
|
|
91
133
|
const data = (await res.json()) as Record<string, unknown>;
|
|
92
|
-
if (data
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
record && typeof record === "object" ? (record as AuthModel) : null;
|
|
96
|
-
this.authStore.set(data.token, model);
|
|
97
|
-
return data as { token: string; record: Record<string, unknown> };
|
|
134
|
+
if (!data || typeof data.token !== "string") {
|
|
135
|
+
this.authStore.clear();
|
|
136
|
+
return false;
|
|
98
137
|
}
|
|
99
|
-
|
|
138
|
+
const record = data["record"];
|
|
139
|
+
const model: AuthModel | null =
|
|
140
|
+
record && typeof record === "object" ? (record as AuthModel) : null;
|
|
141
|
+
this.authStore.setCollectionName(
|
|
142
|
+
collection === SUPERUSERS ? null : collection,
|
|
143
|
+
);
|
|
144
|
+
this.authStore.setSession(data.token, model, {
|
|
145
|
+
refreshToken:
|
|
146
|
+
typeof data.refreshToken === "string" ? data.refreshToken : undefined,
|
|
147
|
+
expiresIn: typeof data.expiresIn === "number" ? data.expiresIn : undefined,
|
|
148
|
+
});
|
|
149
|
+
return true;
|
|
100
150
|
} catch {
|
|
101
|
-
return
|
|
151
|
+
return false;
|
|
102
152
|
}
|
|
103
153
|
}
|
|
104
154
|
|
|
155
|
+
/** The auth collection a refresh is addressed to, including superusers. */
|
|
156
|
+
private refreshCollection(): string {
|
|
157
|
+
if (this.authStore.collectionName) return this.authStore.collectionName;
|
|
158
|
+
const model = this.authStore.model as { collectionName?: unknown } | null;
|
|
159
|
+
if (model && typeof model.collectionName === "string" && model.collectionName) {
|
|
160
|
+
return model.collectionName;
|
|
161
|
+
}
|
|
162
|
+
return SUPERUSERS;
|
|
163
|
+
}
|
|
164
|
+
|
|
105
165
|
/**
|
|
106
166
|
* Globally enable or disable auto-cancellation of duplicated pending requests.
|
|
107
167
|
* Fluent — returns `this` for chaining.
|
|
@@ -155,8 +215,10 @@ export class HttpClient {
|
|
|
155
215
|
body?: unknown,
|
|
156
216
|
options?: RequestOptions,
|
|
157
217
|
): Promise<T | null> {
|
|
158
|
-
//
|
|
159
|
-
|
|
218
|
+
// Refresh just before the access token lapses. A superuser session has no
|
|
219
|
+
// collection name, so guarding on it (as this used to) skipped every superuser
|
|
220
|
+
// refresh and signed the Studio out at the 15-minute mark.
|
|
221
|
+
if (this.authStore.isExpired && this.authStore.refreshToken) {
|
|
160
222
|
await this.refreshAuth();
|
|
161
223
|
}
|
|
162
224
|
|
|
@@ -292,8 +354,9 @@ export class HttpClient {
|
|
|
292
354
|
|
|
293
355
|
let res: Response | null = null;
|
|
294
356
|
const fetcher = options?.fetch ?? this.defaultFetch;
|
|
357
|
+
const send = async (): Promise<Response> => fetcher(url, init);
|
|
295
358
|
try {
|
|
296
|
-
res = await
|
|
359
|
+
res = await send();
|
|
297
360
|
} catch (err) {
|
|
298
361
|
// Aborted (auto-cancelled duplicate, manual cancel, or external signal)
|
|
299
362
|
// → normalized ApiError with isAbort === true, like PocketBase.
|
|
@@ -317,6 +380,32 @@ export class HttpClient {
|
|
|
317
380
|
}
|
|
318
381
|
}
|
|
319
382
|
|
|
383
|
+
// A refused token does not always mean a dead session: it can be a stale access
|
|
384
|
+
// token the recorded expiry did not predict (clock skew, a reloaded tab, a
|
|
385
|
+
// revocation elsewhere). Refresh once and retry — after that, the 401 is the
|
|
386
|
+
// answer.
|
|
387
|
+
if (res!.status === 401 && this.authStore.refreshToken) {
|
|
388
|
+
// Release the refused response before reusing the connection.
|
|
389
|
+
await res!.text().catch(() => {});
|
|
390
|
+
const refreshed = await this.refreshAuth();
|
|
391
|
+
if (refreshed) {
|
|
392
|
+
headers["Authorization"] = "Bearer " + this.authStore.token;
|
|
393
|
+
try {
|
|
394
|
+
res = await send();
|
|
395
|
+
} catch (err) {
|
|
396
|
+
if (isAbortError(err)) {
|
|
397
|
+
throw new ApiError(
|
|
398
|
+
"The request was aborted (most likely auto-cancelled by a newer request with the same requestKey)",
|
|
399
|
+
{},
|
|
400
|
+
0,
|
|
401
|
+
true,
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
throw err;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
320
409
|
if (res!.status === 204) return null;
|
|
321
410
|
|
|
322
411
|
// Safely parse JSON — some errored responses may have empty or non-JSON bodies
|
package/src/lazypock.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { HttpClient } from "./http";
|
|
|
6
6
|
import {
|
|
7
7
|
AuthStore,
|
|
8
8
|
memoryStorage,
|
|
9
|
+
rememberAuthResponse,
|
|
9
10
|
type StorageAdapter,
|
|
10
11
|
type AuthModel,
|
|
11
12
|
} from "./auth";
|
|
@@ -329,7 +330,7 @@ export class LazypockClient {
|
|
|
329
330
|
>("/superusers/setup", { email, password });
|
|
330
331
|
if (data) {
|
|
331
332
|
this.authStore.setCollectionName(null);
|
|
332
|
-
this.authStore
|
|
333
|
+
rememberAuthResponse(this.authStore, data, null);
|
|
333
334
|
}
|
|
334
335
|
return data;
|
|
335
336
|
}
|
|
@@ -365,7 +366,11 @@ export class LazypockClient {
|
|
|
365
366
|
this.authStore.setCollectionName(collection);
|
|
366
367
|
// SAFETY: the server's auth response record is a superset of
|
|
367
368
|
// AuthModel; the auth store consumes it generically.
|
|
368
|
-
|
|
369
|
+
rememberAuthResponse(
|
|
370
|
+
this.authStore,
|
|
371
|
+
data,
|
|
372
|
+
data.record as unknown as AuthModel,
|
|
373
|
+
);
|
|
369
374
|
}
|
|
370
375
|
} else {
|
|
371
376
|
// Superuser login — PocketBase parity: `_superusers` is an auth collection.
|
|
@@ -387,7 +392,7 @@ export class LazypockClient {
|
|
|
387
392
|
}
|
|
388
393
|
if (data) {
|
|
389
394
|
this.authStore.setCollectionName(null);
|
|
390
|
-
this.authStore
|
|
395
|
+
rememberAuthResponse(this.authStore, data, null);
|
|
391
396
|
}
|
|
392
397
|
}
|
|
393
398
|
return data;
|
|
@@ -437,7 +442,7 @@ export class LazypockClient {
|
|
|
437
442
|
this.authStore.setCollectionName(collection);
|
|
438
443
|
// SAFETY: the server's auth response record is a superset of
|
|
439
444
|
// AuthModel; the auth store consumes it generically.
|
|
440
|
-
this.authStore
|
|
445
|
+
rememberAuthResponse(this.authStore, data, data.record as unknown as AuthModel);
|
|
441
446
|
}
|
|
442
447
|
return data;
|
|
443
448
|
}
|
|
@@ -459,14 +464,14 @@ export class LazypockClient {
|
|
|
459
464
|
{ token: string; record: ApiRecord } & Record<string, unknown>
|
|
460
465
|
>(
|
|
461
466
|
"/collections/" + encodeURIComponent(collection) + "/auth-refresh",
|
|
462
|
-
|
|
467
|
+
{ refreshToken: this.authStore.refreshToken },
|
|
463
468
|
options,
|
|
464
469
|
);
|
|
465
470
|
if (data) {
|
|
466
471
|
this.authStore.setCollectionName(collection);
|
|
467
472
|
// SAFETY: the server's auth response record is a superset of
|
|
468
473
|
// AuthModel; the auth store consumes it generically.
|
|
469
|
-
this.authStore
|
|
474
|
+
rememberAuthResponse(this.authStore, data, data.record as unknown as AuthModel);
|
|
470
475
|
}
|
|
471
476
|
return data;
|
|
472
477
|
}
|