lazypock 0.18.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lazypock",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -24,7 +24,7 @@
24
24
  "dev": "tsup --watch",
25
25
  "typecheck": "tsc --noEmit",
26
26
  "generate": "node dist/cli.js",
27
- "smoke": "npm run build && node smoke-test.mjs && node auth-store-test.mjs && node oauth2-relay-test.mjs && tsc --noEmit --strict --target es2020 --module esnext --moduleResolution bundler type-test.ts",
27
+ "smoke": "npm run build && node smoke-test.mjs && node realtime-test.mjs && node auth-store-test.mjs && node oauth2-relay-test.mjs && tsc --noEmit --strict --target es2020 --module esnext --moduleResolution bundler type-test.ts",
28
28
  "bench": "npm run build && node bench/codegen.mjs && node bench/http-overhead.mjs",
29
29
  "test:integration": "npm run build && node integration-test.mjs",
30
30
  "prepublishOnly": "npm run build"
package/src/cli.ts CHANGED
@@ -92,7 +92,7 @@ async function fetchCollections(
92
92
  // Step 1: login as superuser to get a token.
93
93
  // Prefer the PocketBase-parity `_superusers` auth collection endpoint,
94
94
  // fall back to the legacy /superusers/login for older servers.
95
- let loginRes = await fetch(base + "/_superusers/auth-with-password", {
95
+ let loginRes = await fetch(base + "/collections/_superusers/auth-with-password", {
96
96
  method: "POST",
97
97
  headers: { "Content-Type": "application/json" },
98
98
  body: JSON.stringify({ identity: opts.email, password: opts.password }),
package/src/collection.ts CHANGED
@@ -227,6 +227,21 @@ export class CollectionService<
227
227
  return encodeURIComponent(id);
228
228
  }
229
229
 
230
+ /**
231
+ * The records endpoint for this collection.
232
+ *
233
+ * Centralised because the server's shape is `/api/collections/<name>/records`,
234
+ * and a path repeated eighteen times is a path that drifts.
235
+ */
236
+ private recordsPath(): string {
237
+ return "/collections/" + this.encodeId(this.collectionName) + "/records";
238
+ }
239
+
240
+ /** The collection's own endpoint, which is where its auth routes live. */
241
+ private collectionPath(): string {
242
+ return "/collections/" + this.encodeId(this.collectionName);
243
+ }
244
+
230
245
  // ── Field projection (`.select`) ──
231
246
 
232
247
  /**
@@ -735,7 +750,7 @@ export class CollectionService<
735
750
  ).toString();
736
751
  const result = await this.http.get<
737
752
  ListResult<T2 & { expand?: ExpandObj<E, TExpand> }>
738
- >("/" + this.encodeId(this.collectionName) + "?" + qs, {
753
+ >(this.recordsPath() + "?" + qs, {
739
754
  requestKey,
740
755
  autoCancel,
741
756
  cancelKey,
@@ -856,10 +871,7 @@ export class CollectionService<
856
871
  }
857
872
  const qsStr = qs.toString();
858
873
  const record = await this.http.get<T & { expand?: ExpandObj<E, TExpand> }>(
859
- "/" +
860
- this.encodeId(this.collectionName) +
861
- "/" +
862
- this.encodeId(id) +
874
+ this.recordsPath() + "/" + this.encodeId(id) +
863
875
  (qsStr ? "?" + qsStr : ""),
864
876
  options,
865
877
  );
@@ -884,7 +896,7 @@ export class CollectionService<
884
896
  options?: RequestOptions,
885
897
  ): Promise<T | null> {
886
898
  return this.http.post<T>(
887
- "/" + this.encodeId(this.collectionName),
899
+ this.recordsPath(),
888
900
  data,
889
901
  options,
890
902
  );
@@ -907,7 +919,7 @@ export class CollectionService<
907
919
  options?: RequestOptions,
908
920
  ): Promise<T | null> {
909
921
  return this.http.patch<T>(
910
- "/" + this.encodeId(this.collectionName) + "/" + this.encodeId(id),
922
+ this.recordsPath() + "/" + this.encodeId(id),
911
923
  data,
912
924
  options,
913
925
  );
@@ -920,7 +932,7 @@ export class CollectionService<
920
932
  */
921
933
  delete(id: string, options?: RequestOptions): Promise<null> {
922
934
  return this.http.delete(
923
- "/" + this.encodeId(this.collectionName) + "/" + this.encodeId(id),
935
+ this.recordsPath() + "/" + this.encodeId(id),
924
936
  options,
925
937
  );
926
938
  }
@@ -1101,7 +1113,7 @@ export class CollectionService<
1101
1113
  const data = await this.http.post<
1102
1114
  { token: string; record: ApiRecord } & Record<string, unknown>
1103
1115
  >(
1104
- "/" + this.encodeId(this.collectionName) + "/auth-with-password",
1116
+ this.collectionPath() + "/auth-with-password",
1105
1117
  { identity, password },
1106
1118
  options,
1107
1119
  );
@@ -1126,7 +1138,7 @@ export class CollectionService<
1126
1138
  const data = await this.http.post<
1127
1139
  { token: string; record: ApiRecord } & Record<string, unknown>
1128
1140
  >(
1129
- "/" + this.encodeId(this.collectionName) + "/auth-refresh",
1141
+ this.collectionPath() + "/auth-refresh",
1130
1142
  undefined,
1131
1143
  options,
1132
1144
  );
@@ -1148,7 +1160,7 @@ export class CollectionService<
1148
1160
  options?: RequestOptions,
1149
1161
  ): Promise<AuthMethodsList | null> {
1150
1162
  return this.http.get<AuthMethodsList>(
1151
- "/" + this.encodeId(this.collectionName) + "/auth-methods",
1163
+ this.collectionPath() + "/auth-methods",
1152
1164
  options,
1153
1165
  );
1154
1166
  }
@@ -1173,7 +1185,7 @@ export class CollectionService<
1173
1185
  options?: RequestOptions,
1174
1186
  ): Promise<boolean> {
1175
1187
  await this.http.post(
1176
- "/" + this.encodeId(this.collectionName) + "/request-password-reset",
1188
+ this.collectionPath() + "/request-password-reset",
1177
1189
  { email },
1178
1190
  options,
1179
1191
  );
@@ -1194,7 +1206,7 @@ export class CollectionService<
1194
1206
  options?: RequestOptions,
1195
1207
  ): Promise<boolean> {
1196
1208
  await this.http.post(
1197
- "/" + this.encodeId(this.collectionName) + "/confirm-password-reset",
1209
+ this.collectionPath() + "/confirm-password-reset",
1198
1210
  { token: passwordResetToken, password, passwordConfirm },
1199
1211
  options,
1200
1212
  );
@@ -1211,7 +1223,7 @@ export class CollectionService<
1211
1223
  options?: RequestOptions,
1212
1224
  ): Promise<boolean> {
1213
1225
  await this.http.post(
1214
- "/" + this.encodeId(this.collectionName) + "/request-verification",
1226
+ this.collectionPath() + "/request-verification",
1215
1227
  { email },
1216
1228
  options,
1217
1229
  );
@@ -1227,7 +1239,7 @@ export class CollectionService<
1227
1239
  options?: RequestOptions,
1228
1240
  ): Promise<T | null> {
1229
1241
  const data = await this.http.post<{ record: ApiRecord }>(
1230
- "/" + this.encodeId(this.collectionName) + "/confirm-verification",
1242
+ this.collectionPath() + "/confirm-verification",
1231
1243
  { token: verificationToken },
1232
1244
  options,
1233
1245
  );
@@ -1243,7 +1255,7 @@ export class CollectionService<
1243
1255
  options?: RequestOptions,
1244
1256
  ): Promise<boolean> {
1245
1257
  await this.http.post(
1246
- "/" + this.encodeId(this.collectionName) + "/request-email-change",
1258
+ this.collectionPath() + "/request-email-change",
1247
1259
  { newEmail },
1248
1260
  options,
1249
1261
  );
@@ -1261,7 +1273,7 @@ export class CollectionService<
1261
1273
  options?: RequestOptions,
1262
1274
  ): Promise<T | null> {
1263
1275
  const data = await this.http.post<{ record: ApiRecord }>(
1264
- "/" + this.encodeId(this.collectionName) + "/confirm-email-change",
1276
+ this.collectionPath() + "/confirm-email-change",
1265
1277
  { token: emailChangeToken, password },
1266
1278
  options,
1267
1279
  );
@@ -1340,7 +1352,7 @@ export class CollectionService<
1340
1352
  const { provider, code, codeVerifier, redirectUrl, createData } = options;
1341
1353
 
1342
1354
  const data = await this.http.post<RecordAuth<ApiRecord>>(
1343
- "/" + this.encodeId(this.collectionName) + "/auth-with-oauth2",
1355
+ this.collectionPath() + "/auth-with-oauth2",
1344
1356
  {
1345
1357
  provider,
1346
1358
  code,
package/src/files.ts CHANGED
@@ -30,42 +30,49 @@ export interface DirectUpload {
30
30
  }
31
31
 
32
32
  /**
33
- * Construct a file URL from the API base URL and file ID.
33
+ * Percent-encode a storage key, keeping its separators.
34
+ *
35
+ * A key is `collection/record/filename`, so the slashes are structure and must
36
+ * survive, while each segment is encoded on its own.
37
+ */
38
+ function encodeKey(key: string): string {
39
+ return key.split("/").map(encodeURIComponent).join("/");
40
+ }
41
+
42
+ /**
43
+ * Construct a file URL from the API base URL and a storage **key**.
44
+ *
45
+ * The key is `collection/record/filename` — for example
46
+ * `posts/p1/abc123_photo.png` — which is what `GET /api/files` returns as `key`.
47
+ * The server addresses an object by its key rather than by an opaque id, since
48
+ * in this design an object *is* its key.
49
+ *
50
+ * Prefer the `url` the listing already provides; build one only when all you hold
51
+ * is a key.
34
52
  */
35
- export function getFileUrl(baseUrl: string, fileId: string): string {
36
- return baseUrl.replace(/\/+$/, "") + "/files/" + encodeURIComponent(fileId);
53
+ export function getFileUrl(baseUrl: string, key: string): string {
54
+ return baseUrl.replace(/\/+$/, "") + "/files/" + encodeKey(key);
37
55
  }
38
56
 
39
57
  /**
40
- * Construct a thumbnail URL from the API base URL, file ID, and thumb size.
41
- * @param size e.g. "50x50"
58
+ * Construct a thumbnail URL for a storage key.
59
+ *
60
+ * @param size A box like `200x200`, or a preset name from the server's list.
42
61
  */
43
- export function getThumbUrl(baseUrl: string, fileId: string, size: string): string {
44
- return (
45
- baseUrl.replace(/\/+$/, "") +
46
- "/files/" +
47
- encodeURIComponent(fileId) +
48
- "/thumbs/" +
49
- encodeURIComponent(size)
50
- );
62
+ export function getThumbUrl(baseUrl: string, key: string, size: string): string {
63
+ return getFileUrl(baseUrl, key) + "?thumb=" + encodeURIComponent(size);
51
64
  }
52
65
 
53
66
  /**
54
- * Construct an on-demand scaled image URL from the API base URL, file ID, and size.
67
+ * Construct a scaled image URL for a storage key.
55
68
  *
56
- * The size is an ImageMagick geometry: "100" (width, keep aspect), "100x100"
57
- * (fit within box), "100x100!" (exact crop), "x200" (height). The server
58
- * generates and caches the scaled image on first request.
59
- * @param size e.g. "100x100"
69
+ * @param size A preset name (`md`), or a box (`400x300`) for an ad-hoc size.
60
70
  */
61
- export function getScaleUrl(baseUrl: string, fileId: string, size: string): string {
62
- return (
63
- baseUrl.replace(/\/+$/, "") +
64
- "/files/" +
65
- encodeURIComponent(fileId) +
66
- "/scale/" +
67
- encodeURIComponent(size)
68
- );
71
+ export function getScaleUrl(baseUrl: string, key: string, size: string): string {
72
+ // A preset name is looked up on the server; a `WxH` box is generated on
73
+ // demand. Both end up as a cached variant of the same object.
74
+ const parameter = size.includes("x") ? "thumb" : "preset";
75
+ return getFileUrl(baseUrl, key) + "?" + parameter + "=" + encodeURIComponent(size);
69
76
  }
70
77
 
71
78
  /**
package/src/http.ts CHANGED
@@ -70,7 +70,10 @@ export class HttpClient {
70
70
  if (!collection) return null;
71
71
  try {
72
72
  const url =
73
- this.baseUrl + "/" + encodeURIComponent(collection) + "/auth-refresh";
73
+ this.baseUrl +
74
+ "/collections/" +
75
+ encodeURIComponent(collection) +
76
+ "/auth-refresh";
74
77
  const headers: Record<string, string> = {
75
78
  "Content-Type": "application/json",
76
79
  };
package/src/index.ts CHANGED
@@ -13,7 +13,7 @@ export {
13
13
  ApiError,
14
14
  HttpClient,
15
15
  RealtimeService,
16
- wsUrlFromBaseUrl,
16
+ sseUrlFromBaseUrl,
17
17
  FilesService,
18
18
  getFileUrl,
19
19
  getThumbUrl,
package/src/lazypock.ts CHANGED
@@ -30,7 +30,7 @@ import {
30
30
  type FilterInput,
31
31
  type FilterWhere,
32
32
  } from "./types";
33
- import { RealtimeService, wsUrlFromBaseUrl } from "./realtime";
33
+ import { RealtimeService, sseUrlFromBaseUrl } from "./realtime";
34
34
  import type { RealtimeConnectOpts, RealtimeTokenProvider } from "./realtime";
35
35
  import {
36
36
  FilesService,
@@ -73,7 +73,7 @@ export {
73
73
  ApiError,
74
74
  HttpClient,
75
75
  RealtimeService,
76
- wsUrlFromBaseUrl,
76
+ sseUrlFromBaseUrl,
77
77
  FilesService,
78
78
  getFileUrl,
79
79
  getThumbUrl,
@@ -194,7 +194,7 @@ export class LazypockClient {
194
194
  this.realtime = options.realtime ?? new RealtimeService();
195
195
  // Cache the socket URL so collection-level subscribe() can auto-connect.
196
196
  if (!options.realtime) {
197
- this.realtime.setUrl(wsUrlFromBaseUrl(baseUrl));
197
+ this.realtime.setUrl(sseUrlFromBaseUrl(baseUrl));
198
198
  }
199
199
  this.realtime.setConnectionId(connectionId);
200
200
  // Keep the realtime socket authenticated with the current auth token
@@ -357,7 +357,7 @@ export class LazypockClient {
357
357
  string,
358
358
  unknown
359
359
  >
360
- >("/" + encodeURIComponent(collection) + "/auth-with-password", {
360
+ >("/collections/" + encodeURIComponent(collection) + "/auth-with-password", {
361
361
  identity: email,
362
362
  password,
363
363
  });
@@ -373,7 +373,7 @@ export class LazypockClient {
373
373
  try {
374
374
  data = await this.http.post<
375
375
  { token: string; record: ApiRecord } & Record<string, unknown>
376
- >("/_superusers/auth-with-password", {
376
+ >("/collections/_superusers/auth-with-password", {
377
377
  identity: email,
378
378
  password,
379
379
  });
@@ -429,7 +429,7 @@ export class LazypockClient {
429
429
  const data = await this.http.post<
430
430
  { token: string; record: ApiRecord } & Record<string, unknown>
431
431
  >(
432
- "/" + encodeURIComponent(collection) + "/auth-with-password",
432
+ "/collections/" + encodeURIComponent(collection) + "/auth-with-password",
433
433
  { identity, password },
434
434
  options,
435
435
  );
@@ -458,7 +458,7 @@ export class LazypockClient {
458
458
  const data = await this.http.post<
459
459
  { token: string; record: ApiRecord } & Record<string, unknown>
460
460
  >(
461
- "/" + encodeURIComponent(collection) + "/auth-refresh",
461
+ "/collections/" + encodeURIComponent(collection) + "/auth-refresh",
462
462
  undefined,
463
463
  options,
464
464
  );