lazypock 0.19.2 → 0.20.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 +36 -0
- package/dist/index.cjs +905 -674
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +232 -82
- package/dist/index.d.ts +232 -82
- package/dist/index.global.js +904 -674
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +904 -674
- package/dist/index.js.map +1 -1
- package/package.json +19 -2
- package/src/auth.ts +29 -0
- package/src/collection/auth.ts +289 -0
- package/src/collection/index.ts +754 -0
- package/src/collection/paths.ts +21 -0
- package/src/collection/query.ts +111 -0
- package/src/collection/realtime.ts +141 -0
- package/src/collection/session.ts +76 -0
- package/src/collections.ts +18 -52
- package/src/expand.ts +286 -0
- package/src/files.ts +15 -2
- package/src/filter.ts +2 -2
- package/src/http.ts +37 -6
- package/src/index.ts +12 -0
- package/src/lazypock.ts +65 -71
- package/src/mail.ts +290 -0
- package/src/paginate.ts +111 -0
- package/src/projection.ts +139 -0
- package/src/query-params.ts +74 -0
- package/src/realtime.ts +6 -0
- package/src/types.ts +1 -1
- package/src/collection.ts +0 -1412
package/dist/index.js
CHANGED
|
@@ -67,15 +67,20 @@ var HttpClient = class {
|
|
|
67
67
|
*
|
|
68
68
|
* A network failure keeps the session (the next request can retry); only a refusal
|
|
69
69
|
* by the server ends it.
|
|
70
|
+
*
|
|
71
|
+
* `fetcher` is the per-request `options.fetch` override, so a caller (or a test)
|
|
72
|
+
* that injected its own transport also intercepts the refresh. Falling back to
|
|
73
|
+
* the global fetch here meant those callers — React Native and the test suite —
|
|
74
|
+
* silently did not cover this path at all.
|
|
70
75
|
*/
|
|
71
|
-
async refreshAuth() {
|
|
76
|
+
async refreshAuth(fetcher) {
|
|
72
77
|
const refreshToken = this.authStore.refreshToken;
|
|
73
78
|
if (!refreshToken) return null;
|
|
74
79
|
if (this.refreshInFlight) {
|
|
75
80
|
const refreshed = await this.refreshInFlight;
|
|
76
81
|
return refreshed ? this.currentAuthResponse() : null;
|
|
77
82
|
}
|
|
78
|
-
this.refreshInFlight = this.performRefresh(refreshToken);
|
|
83
|
+
this.refreshInFlight = this.performRefresh(refreshToken, fetcher);
|
|
79
84
|
try {
|
|
80
85
|
const refreshed = await this.refreshInFlight;
|
|
81
86
|
return refreshed ? this.currentAuthResponse() : null;
|
|
@@ -87,13 +92,18 @@ var HttpClient = class {
|
|
|
87
92
|
currentAuthResponse() {
|
|
88
93
|
return {
|
|
89
94
|
token: this.authStore.token,
|
|
95
|
+
// SAFETY: `AuthModel` is an index-signature interface (`[key: string]:
|
|
96
|
+
// unknown`), so its values are readable as a plain property bag; the
|
|
97
|
+
// `{}` fallback preserves the "no model, empty record" contract of the
|
|
98
|
+
// response shape this mirrors. The cast only bridges the empty-object
|
|
99
|
+
// literal, which TypeScript gives no index signature.
|
|
90
100
|
record: this.authStore.model ?? {}
|
|
91
101
|
};
|
|
92
102
|
}
|
|
93
|
-
async performRefresh(refreshToken) {
|
|
103
|
+
async performRefresh(refreshToken, fetcher) {
|
|
94
104
|
const collection = this.refreshCollection();
|
|
95
105
|
try {
|
|
96
|
-
const res = await this.defaultFetch(
|
|
106
|
+
const res = await (fetcher ?? this.defaultFetch)(
|
|
97
107
|
this.baseUrl + "/collections/" + encodeURIComponent(collection) + "/auth-refresh",
|
|
98
108
|
{
|
|
99
109
|
method: "POST",
|
|
@@ -181,7 +191,7 @@ var HttpClient = class {
|
|
|
181
191
|
*/
|
|
182
192
|
async request(method, path, body, options) {
|
|
183
193
|
if (this.authStore.isExpired && this.authStore.refreshToken) {
|
|
184
|
-
await this.refreshAuth();
|
|
194
|
+
await this.refreshAuth(options?.fetch);
|
|
185
195
|
}
|
|
186
196
|
let requestKey = options?.requestKey === void 0 ? options?.cancelKey ?? `${method} ${path}` : options.requestKey;
|
|
187
197
|
if (options?.autoCancel === false) requestKey = null;
|
|
@@ -292,7 +302,7 @@ var HttpClient = class {
|
|
|
292
302
|
if (res.status === 401 && this.authStore.refreshToken) {
|
|
293
303
|
await res.text().catch(() => {
|
|
294
304
|
});
|
|
295
|
-
const refreshed = await this.refreshAuth();
|
|
305
|
+
const refreshed = await this.refreshAuth(options?.fetch);
|
|
296
306
|
if (refreshed) {
|
|
297
307
|
headers["Authorization"] = "Bearer " + this.authStore.token;
|
|
298
308
|
try {
|
|
@@ -354,6 +364,17 @@ var HttpClient = class {
|
|
|
354
364
|
post(path, body, options) {
|
|
355
365
|
return this.request("POST", path, body, options);
|
|
356
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* HTTP PUT — a full replacement. Used where the server models a resource as a
|
|
369
|
+
* complete unit rather than a set of patches (e.g. a mail template, which only
|
|
370
|
+
* exists once all three of its parts do).
|
|
371
|
+
* @param path URL path.
|
|
372
|
+
* @param body Request body.
|
|
373
|
+
* @param options Optional request options.
|
|
374
|
+
*/
|
|
375
|
+
put(path, body, options) {
|
|
376
|
+
return this.request("PUT", path, body, options);
|
|
377
|
+
}
|
|
357
378
|
/**
|
|
358
379
|
* HTTP PATCH.
|
|
359
380
|
* @param path URL path.
|
|
@@ -488,6 +509,29 @@ var AuthStore = class {
|
|
|
488
509
|
]);
|
|
489
510
|
this.notify();
|
|
490
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* Replace the cached auth model **without** touching the token, its recorded
|
|
514
|
+
* expiry, or the refresh token.
|
|
515
|
+
*
|
|
516
|
+
* Use this when a read refreshes the identity record (e.g. `me()`, or the
|
|
517
|
+
* server echoing the record back). Routing that through {@link set} would
|
|
518
|
+
* overwrite the access token's real `expiresIn` with the 7-day fallback TTL,
|
|
519
|
+
* so `isExpired` stays false long past the real 15-minute lifetime and the
|
|
520
|
+
* proactive refresh never fires — every request then pays a 401 + retry.
|
|
521
|
+
*
|
|
522
|
+
* @param model The authenticated user record, or null for superusers.
|
|
523
|
+
*/
|
|
524
|
+
setModel(model) {
|
|
525
|
+
this._model = model;
|
|
526
|
+
if (!this._collectionName && model && typeof model.collectionName === "string") {
|
|
527
|
+
this._collectionName = model.collectionName;
|
|
528
|
+
}
|
|
529
|
+
void Promise.all([
|
|
530
|
+
model ? this.storage.set("auth_model", JSON.stringify(model)) : this.storage.remove("auth_model"),
|
|
531
|
+
this.persistCollectionName()
|
|
532
|
+
]);
|
|
533
|
+
this.notify();
|
|
534
|
+
}
|
|
491
535
|
/**
|
|
492
536
|
* Record a complete auth response: token, model, refresh token and the access
|
|
493
537
|
* token's real lifetime.
|
|
@@ -715,6 +759,377 @@ var FilterBuilder = class {
|
|
|
715
759
|
}
|
|
716
760
|
};
|
|
717
761
|
|
|
762
|
+
// src/projection.ts
|
|
763
|
+
var SYSTEM_FIELDS = [
|
|
764
|
+
"id",
|
|
765
|
+
"created",
|
|
766
|
+
"updated",
|
|
767
|
+
"collectionId",
|
|
768
|
+
"collectionName"
|
|
769
|
+
];
|
|
770
|
+
var visibleFieldsCache = /* @__PURE__ */ new WeakMap();
|
|
771
|
+
function visibleFields(schema) {
|
|
772
|
+
if (!schema?.fields) return void 0;
|
|
773
|
+
const cached = visibleFieldsCache.get(schema);
|
|
774
|
+
if (cached !== void 0 || visibleFieldsCache.has(schema)) return cached;
|
|
775
|
+
const visible = schema.fields.filter(
|
|
776
|
+
(f) => !f.hidden && f.type !== "password" && f.type !== "autodate"
|
|
777
|
+
).map((f) => f.name);
|
|
778
|
+
const result = visible.length === 0 ? void 0 : (
|
|
779
|
+
// Dedupe in case a schema ever names a field after a system key.
|
|
780
|
+
[
|
|
781
|
+
.../* @__PURE__ */ new Set([...SYSTEM_FIELDS, ...visible])
|
|
782
|
+
].join(",")
|
|
783
|
+
);
|
|
784
|
+
visibleFieldsCache.set(schema, result);
|
|
785
|
+
return result;
|
|
786
|
+
}
|
|
787
|
+
function effectiveFields(schema, preset, optionsFields) {
|
|
788
|
+
if (optionsFields !== void 0) return optionsFields;
|
|
789
|
+
if (preset !== void 0) {
|
|
790
|
+
return preset === "*" ? visibleFields(schema) : preset;
|
|
791
|
+
}
|
|
792
|
+
return visibleFields(schema);
|
|
793
|
+
}
|
|
794
|
+
function mergeExpandFields(base, info) {
|
|
795
|
+
if (info.fieldSelectors.length === 0) return base;
|
|
796
|
+
if (base === void 0) return void 0;
|
|
797
|
+
const parts = base.split(",").map((f) => f.trim()).filter(Boolean);
|
|
798
|
+
const seen = new Set(parts);
|
|
799
|
+
const push = (value) => {
|
|
800
|
+
if (seen.has(value)) return;
|
|
801
|
+
seen.add(value);
|
|
802
|
+
parts.push(value);
|
|
803
|
+
};
|
|
804
|
+
const fullKeys = /* @__PURE__ */ new Set();
|
|
805
|
+
for (const selector of info.fieldSelectors) {
|
|
806
|
+
const seg = selector.split(".");
|
|
807
|
+
if (seg.length === 2) fullKeys.add(seg[1]);
|
|
808
|
+
}
|
|
809
|
+
for (const selector of info.fieldSelectors) {
|
|
810
|
+
const seg = selector.split(".");
|
|
811
|
+
if (seg.length > 2 && fullKeys.has(seg[1])) continue;
|
|
812
|
+
if (seg.length >= 2) push(seg[1]);
|
|
813
|
+
push(selector);
|
|
814
|
+
}
|
|
815
|
+
return parts.join(",");
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// src/expand.ts
|
|
819
|
+
function pickFields(value, fields) {
|
|
820
|
+
if (!value || typeof value !== "object") return {};
|
|
821
|
+
const src = value;
|
|
822
|
+
const out = {};
|
|
823
|
+
for (const f of fields) {
|
|
824
|
+
if (f in src) out[f] = src[f];
|
|
825
|
+
}
|
|
826
|
+
return out;
|
|
827
|
+
}
|
|
828
|
+
function resolveTargetCollection(field, ctx) {
|
|
829
|
+
const key = field.options?.collection ?? field.options?.collectionId ?? field.collectionId ?? void 0;
|
|
830
|
+
if (!key) return void 0;
|
|
831
|
+
return ctx.resolveCollection(String(key));
|
|
832
|
+
}
|
|
833
|
+
function resolveRelationChain(segments, ctx) {
|
|
834
|
+
let currentFields = ctx.schema?.fields;
|
|
835
|
+
if (!currentFields) return { depth: 0, canInspect: false };
|
|
836
|
+
for (let i = 0; i < segments.length; i++) {
|
|
837
|
+
const field = currentFields.find((f) => f.name === segments[i]);
|
|
838
|
+
if (!field || field.type !== "relation") {
|
|
839
|
+
return { depth: i, canInspect: true };
|
|
840
|
+
}
|
|
841
|
+
if (i === segments.length - 1) {
|
|
842
|
+
return { depth: segments.length, canInspect: true };
|
|
843
|
+
}
|
|
844
|
+
const target = resolveTargetCollection(field, ctx);
|
|
845
|
+
if (!target?.fields) {
|
|
846
|
+
return { depth: i + 1, canInspect: false };
|
|
847
|
+
}
|
|
848
|
+
currentFields = target.fields;
|
|
849
|
+
}
|
|
850
|
+
return { depth: segments.length, canInspect: true };
|
|
851
|
+
}
|
|
852
|
+
function warnNonRelation(name, ctx) {
|
|
853
|
+
console.warn(
|
|
854
|
+
`[lazypock] expand("${name}"): field is not a relation on collection "${ctx.collectionName}"`
|
|
855
|
+
);
|
|
856
|
+
}
|
|
857
|
+
function normalizeExpand(expand, ctx) {
|
|
858
|
+
const narrow = /* @__PURE__ */ new Map();
|
|
859
|
+
if (!expand) return { fieldSelectors: [], narrow };
|
|
860
|
+
const tokens = expand.split(",").map((t) => t.trim()).filter(Boolean);
|
|
861
|
+
if (tokens.length === 0) return { fieldSelectors: [], narrow };
|
|
862
|
+
const relations = ctx.schema?.fields ? new Set(
|
|
863
|
+
ctx.schema.fields.filter((f) => f.type === "relation").map((f) => f.name)
|
|
864
|
+
) : void 0;
|
|
865
|
+
const parsed = [];
|
|
866
|
+
for (const token of tokens) {
|
|
867
|
+
const segments = token.split(".").filter(Boolean);
|
|
868
|
+
const base = segments[0] ?? token;
|
|
869
|
+
if (segments.length <= 1) {
|
|
870
|
+
if (relations && !relations.has(base)) {
|
|
871
|
+
warnNonRelation(base, ctx);
|
|
872
|
+
}
|
|
873
|
+
parsed.push({ token, base, segments, kind: "plain", depth: 0 });
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
if (!relations) {
|
|
877
|
+
parsed.push({ token, base, segments, kind: "unknown", depth: 1 });
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
const { depth, canInspect } = resolveRelationChain(segments, ctx);
|
|
881
|
+
if (depth === 0) {
|
|
882
|
+
warnNonRelation(base, ctx);
|
|
883
|
+
parsed.push({ token, base, segments, kind: "plain", depth: 0 });
|
|
884
|
+
} else if (depth === segments.length) {
|
|
885
|
+
parsed.push({ token, base, segments, kind: "nested", depth });
|
|
886
|
+
} else if (canInspect) {
|
|
887
|
+
parsed.push({ token, base, segments, kind: "narrow", depth });
|
|
888
|
+
} else {
|
|
889
|
+
parsed.push({ token, base, segments, kind: "unknown", depth });
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
const byBase = /* @__PURE__ */ new Map();
|
|
893
|
+
for (const t of parsed) {
|
|
894
|
+
const arr = byBase.get(t.base);
|
|
895
|
+
if (arr) arr.push(t);
|
|
896
|
+
else byBase.set(t.base, [t]);
|
|
897
|
+
}
|
|
898
|
+
for (const [base, group] of byBase) {
|
|
899
|
+
const unknowns = group.filter((t) => t.kind === "unknown");
|
|
900
|
+
if (unknowns.length === 0) continue;
|
|
901
|
+
const distinct = new Set(unknowns.map((t) => t.token));
|
|
902
|
+
const hasNarrow = group.some((t) => t.kind === "narrow");
|
|
903
|
+
if (!hasNarrow && distinct.size < 2) {
|
|
904
|
+
for (const t of unknowns) {
|
|
905
|
+
t.kind = "nested";
|
|
906
|
+
console.warn(
|
|
907
|
+
`[lazypock] expand("${t.token}"): treated as a nested relation. Pass types.schemas (or use fields=) to select fields of "${base}".`
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
} else {
|
|
911
|
+
for (const t of unknowns) t.kind = "narrow";
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
const expandParams = [];
|
|
915
|
+
const expandSeen = /* @__PURE__ */ new Set();
|
|
916
|
+
const fieldSelectors = [];
|
|
917
|
+
const selectorSeen = /* @__PURE__ */ new Set();
|
|
918
|
+
const pushParam = (p) => {
|
|
919
|
+
if (!expandSeen.has(p)) {
|
|
920
|
+
expandSeen.add(p);
|
|
921
|
+
expandParams.push(p);
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
const pushSelector = (s) => {
|
|
925
|
+
if (!selectorSeen.has(s)) {
|
|
926
|
+
selectorSeen.add(s);
|
|
927
|
+
fieldSelectors.push(s);
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
for (const t of parsed) {
|
|
931
|
+
if (t.kind === "plain") {
|
|
932
|
+
pushParam(t.token);
|
|
933
|
+
pushSelector(`expand.${t.token}`);
|
|
934
|
+
} else if (t.kind === "nested") {
|
|
935
|
+
for (let i = 1; i <= t.segments.length; i++) {
|
|
936
|
+
pushParam(t.segments.slice(0, i).join("."));
|
|
937
|
+
}
|
|
938
|
+
pushSelector(`expand.${t.segments[0]}`);
|
|
939
|
+
} else {
|
|
940
|
+
const depth = Math.max(1, t.depth);
|
|
941
|
+
const relationPath = t.segments.slice(0, depth).join(".");
|
|
942
|
+
pushParam(relationPath);
|
|
943
|
+
pushSelector(`expand.${t.segments.slice(0, depth + 1).join(".")}`);
|
|
944
|
+
if (t.segments.length === depth + 1) {
|
|
945
|
+
const base = t.segments[0];
|
|
946
|
+
const leaf = t.segments[depth];
|
|
947
|
+
const leaves = narrow.get(base) ?? [];
|
|
948
|
+
if (!leaves.includes(leaf)) leaves.push(leaf);
|
|
949
|
+
narrow.set(base, leaves);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
return {
|
|
954
|
+
expand: expandParams.length > 0 ? expandParams.join(",") : void 0,
|
|
955
|
+
fieldSelectors,
|
|
956
|
+
narrow
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
function applyExpandNarrowing(record, narrow) {
|
|
960
|
+
if (narrow.size === 0) return record;
|
|
961
|
+
const rec = record;
|
|
962
|
+
const expand = rec.expand;
|
|
963
|
+
if (!expand || typeof expand !== "object") return record;
|
|
964
|
+
const next = { ...expand };
|
|
965
|
+
for (const [relation, leaves] of narrow) {
|
|
966
|
+
const value = next[relation];
|
|
967
|
+
if (value === void 0 || value === null) continue;
|
|
968
|
+
next[relation] = Array.isArray(value) ? value.map((el) => pickFields(el, leaves)) : pickFields(value, leaves);
|
|
969
|
+
}
|
|
970
|
+
return { ...rec, expand: next };
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
// src/query-params.ts
|
|
974
|
+
var MAX_PER_PAGE = 500;
|
|
975
|
+
function clampBatch(batch) {
|
|
976
|
+
if (!Number.isFinite(batch)) return MAX_PER_PAGE;
|
|
977
|
+
return Math.min(Math.max(1, Math.floor(batch)), MAX_PER_PAGE);
|
|
978
|
+
}
|
|
979
|
+
var TRANSPORT_KEYS = /* @__PURE__ */ new Set(["requestKey", "singleFlight", "fetch", "signal"]);
|
|
980
|
+
function stableStringify(value) {
|
|
981
|
+
const path = /* @__PURE__ */ new Set();
|
|
982
|
+
const sort = (v, topLevel) => {
|
|
983
|
+
if (Array.isArray(v)) return v.map((el) => sort(el, false));
|
|
984
|
+
if (v && typeof v === "object") {
|
|
985
|
+
const obj = v;
|
|
986
|
+
if (path.has(obj)) return "[Circular]";
|
|
987
|
+
path.add(obj);
|
|
988
|
+
const out = {};
|
|
989
|
+
for (const k of Object.keys(obj).sort()) {
|
|
990
|
+
if (topLevel && TRANSPORT_KEYS.has(k)) continue;
|
|
991
|
+
out[k] = sort(obj[k], false);
|
|
992
|
+
}
|
|
993
|
+
path.delete(obj);
|
|
994
|
+
return out;
|
|
995
|
+
}
|
|
996
|
+
return v;
|
|
997
|
+
};
|
|
998
|
+
try {
|
|
999
|
+
return JSON.stringify(sort(value, true));
|
|
1000
|
+
} catch {
|
|
1001
|
+
return String(value);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// src/paginate.ts
|
|
1006
|
+
var TRANSPORT_KEYS2 = /* @__PURE__ */ new Set([
|
|
1007
|
+
"requestKey",
|
|
1008
|
+
"autoCancel",
|
|
1009
|
+
"cancelKey",
|
|
1010
|
+
"singleFlight",
|
|
1011
|
+
"fetch",
|
|
1012
|
+
"headers",
|
|
1013
|
+
"signal",
|
|
1014
|
+
"params"
|
|
1015
|
+
]);
|
|
1016
|
+
function splitListOptions(options) {
|
|
1017
|
+
const {
|
|
1018
|
+
batch: requestedBatch = MAX_PER_PAGE,
|
|
1019
|
+
page: _page,
|
|
1020
|
+
perPage: _perPage,
|
|
1021
|
+
...rest
|
|
1022
|
+
} = options ?? {};
|
|
1023
|
+
const query = {};
|
|
1024
|
+
const transport = {};
|
|
1025
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
1026
|
+
if (TRANSPORT_KEYS2.has(key)) transport[key] = value;
|
|
1027
|
+
else query[key] = value;
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
query,
|
|
1031
|
+
transport,
|
|
1032
|
+
batch: clampBatch(requestedBatch)
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
function fullListKey(scope, query, requestKey) {
|
|
1036
|
+
if (typeof requestKey === "string") return requestKey;
|
|
1037
|
+
return `getFullList:${scope}:${stableStringify(query)}`;
|
|
1038
|
+
}
|
|
1039
|
+
async function paginateAll(batch, fetchPage) {
|
|
1040
|
+
const items = [];
|
|
1041
|
+
let page = 1;
|
|
1042
|
+
for (; ; ) {
|
|
1043
|
+
const res = await fetchPage(page, batch);
|
|
1044
|
+
if (!res || !res.items || res.items.length === 0) break;
|
|
1045
|
+
items.push(...res.items);
|
|
1046
|
+
if (page >= (res.totalPages ?? page)) break;
|
|
1047
|
+
page += 1;
|
|
1048
|
+
}
|
|
1049
|
+
return items;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// src/collection/query.ts
|
|
1053
|
+
function buildListQuery(ctx, page, perPage, queryParams) {
|
|
1054
|
+
if (Array.isArray(queryParams.sort)) {
|
|
1055
|
+
queryParams.sort = queryParams.sort.join(",");
|
|
1056
|
+
}
|
|
1057
|
+
if (Array.isArray(queryParams.expand)) {
|
|
1058
|
+
queryParams.expand = queryParams.expand.join(",");
|
|
1059
|
+
}
|
|
1060
|
+
if (typeof queryParams.filter === "function") {
|
|
1061
|
+
queryParams.filter = ctx.resolveFilter(queryParams.filter);
|
|
1062
|
+
}
|
|
1063
|
+
const expandInfo = normalizeExpand(
|
|
1064
|
+
typeof queryParams.expand === "string" ? queryParams.expand : void 0,
|
|
1065
|
+
ctx.expand
|
|
1066
|
+
);
|
|
1067
|
+
if (expandInfo.expand !== void 0) {
|
|
1068
|
+
queryParams.expand = expandInfo.expand;
|
|
1069
|
+
}
|
|
1070
|
+
const fields = mergeExpandFields(
|
|
1071
|
+
ctx.effectiveFields(queryParams.fields),
|
|
1072
|
+
expandInfo
|
|
1073
|
+
);
|
|
1074
|
+
if (fields !== void 0) {
|
|
1075
|
+
queryParams.fields = fields;
|
|
1076
|
+
}
|
|
1077
|
+
const qs = new URLSearchParams(
|
|
1078
|
+
Object.fromEntries(
|
|
1079
|
+
Object.entries({
|
|
1080
|
+
// `page`/`perPage` are spread LAST so the call's positional arguments
|
|
1081
|
+
// win: with the caller's keys last, `getList(2, 30, { page: 5 })`
|
|
1082
|
+
// silently requested page 5, and `getFullList({ perPage: 1000 })`
|
|
1083
|
+
// slipped a >500 `perPage` past `clampBatch`.
|
|
1084
|
+
...queryParams,
|
|
1085
|
+
page: String(page),
|
|
1086
|
+
perPage: String(perPage)
|
|
1087
|
+
}).map(([k, v]) => [k, String(v)])
|
|
1088
|
+
)
|
|
1089
|
+
).toString();
|
|
1090
|
+
return { qs, narrow: expandInfo.narrow };
|
|
1091
|
+
}
|
|
1092
|
+
function buildReadQuery(ctx, options) {
|
|
1093
|
+
const rawExpand = Array.isArray(options?.expand) ? options.expand.join(",") : typeof options?.expand === "string" ? options.expand : void 0;
|
|
1094
|
+
const expandInfo = normalizeExpand(rawExpand, ctx.expand);
|
|
1095
|
+
const fields = mergeExpandFields(
|
|
1096
|
+
ctx.effectiveFields(options?.fields),
|
|
1097
|
+
expandInfo
|
|
1098
|
+
);
|
|
1099
|
+
const qs = new URLSearchParams();
|
|
1100
|
+
if (fields !== void 0) qs.set("fields", fields);
|
|
1101
|
+
if (rawExpand !== void 0) qs.set("expand", expandInfo.expand ?? rawExpand);
|
|
1102
|
+
return { qs: qs.toString(), narrow: expandInfo.narrow };
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// src/collection/paths.ts
|
|
1106
|
+
function encodeId(id) {
|
|
1107
|
+
return encodeURIComponent(id);
|
|
1108
|
+
}
|
|
1109
|
+
function recordsPath(collectionName) {
|
|
1110
|
+
return "/collections/" + encodeId(collectionName) + "/records";
|
|
1111
|
+
}
|
|
1112
|
+
function collectionPath(collectionName) {
|
|
1113
|
+
return "/collections/" + encodeId(collectionName);
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
// src/collection/session.ts
|
|
1117
|
+
function rememberSession(ctx, data, model) {
|
|
1118
|
+
if (!data || !ctx.authStore) return;
|
|
1119
|
+
ctx.authStore.setCollectionName(ctx.collectionName);
|
|
1120
|
+
rememberAuthResponse(ctx.authStore, data, model);
|
|
1121
|
+
}
|
|
1122
|
+
function rememberSessionRecord(ctx, data) {
|
|
1123
|
+
const record = data?.["record"];
|
|
1124
|
+
const model = record && typeof record === "object" ? record : null;
|
|
1125
|
+
rememberSession(ctx, data, model);
|
|
1126
|
+
}
|
|
1127
|
+
async function postWithSession(ctx, path, body, options) {
|
|
1128
|
+
const data = await ctx.http.post(path, body, options);
|
|
1129
|
+
rememberSessionRecord(ctx, data);
|
|
1130
|
+
return data;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
718
1133
|
// src/oauth2.ts
|
|
719
1134
|
var DEFAULT_POPUP_WIDTH = 500;
|
|
720
1135
|
var DEFAULT_POPUP_HEIGHT = 700;
|
|
@@ -903,35 +1318,140 @@ function authorizeWithOAuth2Popup(opts) {
|
|
|
903
1318
|
});
|
|
904
1319
|
}
|
|
905
1320
|
|
|
906
|
-
// src/collection.ts
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1321
|
+
// src/collection/auth.ts
|
|
1322
|
+
function authWithPassword(ctx, identity, password, options) {
|
|
1323
|
+
return postWithSession(
|
|
1324
|
+
ctx,
|
|
1325
|
+
collectionPath(ctx.collectionName) + "/auth-with-password",
|
|
1326
|
+
{ identity, password },
|
|
1327
|
+
options
|
|
1328
|
+
);
|
|
911
1329
|
}
|
|
912
|
-
function
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
1330
|
+
function authRefresh(ctx, options) {
|
|
1331
|
+
return postWithSession(
|
|
1332
|
+
ctx,
|
|
1333
|
+
collectionPath(ctx.collectionName) + "/auth-refresh",
|
|
1334
|
+
{ refreshToken: ctx.authStore?.refreshToken },
|
|
1335
|
+
options
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
function listAuthMethods(ctx, options) {
|
|
1339
|
+
return ctx.http.get(
|
|
1340
|
+
collectionPath(ctx.collectionName) + "/auth-methods",
|
|
1341
|
+
options
|
|
1342
|
+
);
|
|
1343
|
+
}
|
|
1344
|
+
async function requestPasswordReset(ctx, email, options) {
|
|
1345
|
+
await ctx.http.post(
|
|
1346
|
+
collectionPath(ctx.collectionName) + "/request-password-reset",
|
|
1347
|
+
{ email },
|
|
1348
|
+
options
|
|
1349
|
+
);
|
|
1350
|
+
return true;
|
|
1351
|
+
}
|
|
1352
|
+
async function confirmPasswordReset(ctx, passwordResetToken, password, passwordConfirm, options) {
|
|
1353
|
+
await ctx.http.post(
|
|
1354
|
+
collectionPath(ctx.collectionName) + "/confirm-password-reset",
|
|
1355
|
+
{ token: passwordResetToken, password, passwordConfirm },
|
|
1356
|
+
options
|
|
1357
|
+
);
|
|
1358
|
+
return true;
|
|
1359
|
+
}
|
|
1360
|
+
async function requestVerification(ctx, email, options) {
|
|
1361
|
+
await ctx.http.post(
|
|
1362
|
+
collectionPath(ctx.collectionName) + "/request-verification",
|
|
1363
|
+
{ email },
|
|
1364
|
+
options
|
|
1365
|
+
);
|
|
1366
|
+
return true;
|
|
1367
|
+
}
|
|
1368
|
+
async function confirmVerification(ctx, verificationToken, options) {
|
|
1369
|
+
const data = await ctx.http.post(
|
|
1370
|
+
collectionPath(ctx.collectionName) + "/confirm-verification",
|
|
1371
|
+
{ token: verificationToken },
|
|
1372
|
+
options
|
|
1373
|
+
);
|
|
1374
|
+
return data?.record ?? null;
|
|
1375
|
+
}
|
|
1376
|
+
async function requestEmailChange(ctx, newEmail, options) {
|
|
1377
|
+
await ctx.http.post(
|
|
1378
|
+
collectionPath(ctx.collectionName) + "/request-email-change",
|
|
1379
|
+
{ newEmail },
|
|
1380
|
+
options
|
|
1381
|
+
);
|
|
1382
|
+
return true;
|
|
1383
|
+
}
|
|
1384
|
+
async function confirmEmailChange(ctx, emailChangeToken, password, options) {
|
|
1385
|
+
const data = await ctx.http.post(
|
|
1386
|
+
collectionPath(ctx.collectionName) + "/confirm-email-change",
|
|
1387
|
+
{ token: emailChangeToken, password },
|
|
1388
|
+
options
|
|
1389
|
+
);
|
|
1390
|
+
return data?.record ?? null;
|
|
1391
|
+
}
|
|
1392
|
+
async function authWithOAuth2(ctx, options) {
|
|
1393
|
+
const {
|
|
1394
|
+
provider,
|
|
1395
|
+
createData,
|
|
1396
|
+
urlCallback,
|
|
1397
|
+
popup,
|
|
1398
|
+
timeoutMs = DEFAULT_OAUTH2_TIMEOUT_MS
|
|
1399
|
+
} = options;
|
|
1400
|
+
const methods = await listAuthMethods(ctx);
|
|
1401
|
+
const providerInfo = methods?.oauth2?.providers?.find(
|
|
1402
|
+
(p) => p.name === provider
|
|
1403
|
+
);
|
|
1404
|
+
if (!providerInfo) {
|
|
1405
|
+
throw new ApiError(
|
|
1406
|
+
`OAuth2 provider "${provider}" is not enabled for this collection.`,
|
|
1407
|
+
methods ?? {},
|
|
1408
|
+
400,
|
|
1409
|
+
false,
|
|
1410
|
+
"oauth2_unknown_provider"
|
|
1411
|
+
);
|
|
1412
|
+
}
|
|
1413
|
+
const { code } = await authorizeWithOAuth2Popup({
|
|
1414
|
+
authURL: providerInfo.authURL,
|
|
1415
|
+
expectedOrigin: oauth2RedirectOrigin(ctx.http.baseUrl),
|
|
1416
|
+
urlCallback,
|
|
1417
|
+
popup,
|
|
1418
|
+
timeoutMs
|
|
1419
|
+
});
|
|
1420
|
+
return authWithOAuth2Code(ctx, {
|
|
1421
|
+
provider,
|
|
1422
|
+
code,
|
|
1423
|
+
codeVerifier: providerInfo.codeVerifier,
|
|
1424
|
+
createData
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
async function authWithOAuth2Code(ctx, options) {
|
|
1428
|
+
const { provider, code, codeVerifier, redirectUrl, createData } = options;
|
|
1429
|
+
const data = await ctx.http.post(
|
|
1430
|
+
collectionPath(ctx.collectionName) + "/auth-with-oauth2",
|
|
1431
|
+
{
|
|
1432
|
+
provider,
|
|
1433
|
+
code,
|
|
1434
|
+
codeVerifier,
|
|
1435
|
+
redirectUrl: redirectUrl ?? ctx.http.baseUrl + "/oauth2-redirect",
|
|
1436
|
+
createData: createData ?? {}
|
|
927
1437
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
1438
|
+
);
|
|
1439
|
+
if (!data) {
|
|
1440
|
+
throw new ApiError(
|
|
1441
|
+
"OAuth2 code exchange returned no data.",
|
|
1442
|
+
{},
|
|
1443
|
+
0,
|
|
1444
|
+
false,
|
|
1445
|
+
"oauth2_exchange_failed"
|
|
1446
|
+
);
|
|
934
1447
|
}
|
|
1448
|
+
rememberSessionRecord(ctx, data);
|
|
1449
|
+
return data;
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
// src/collection/realtime.ts
|
|
1453
|
+
function collectionTopic(collectionName) {
|
|
1454
|
+
return "collection:" + collectionName;
|
|
935
1455
|
}
|
|
936
1456
|
function normalizeAction(event, rawAction) {
|
|
937
1457
|
if (typeof rawAction === "string") {
|
|
@@ -945,15 +1465,49 @@ function normalizeAction(event, rawAction) {
|
|
|
945
1465
|
if (event === "delete" || event === "record_delete") return "delete";
|
|
946
1466
|
return "update";
|
|
947
1467
|
}
|
|
948
|
-
function
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
1468
|
+
function subscribeToCollection(ctx, topicOrCallback, maybeCallback, options) {
|
|
1469
|
+
const { realtime, collectionName } = ctx;
|
|
1470
|
+
if (!realtime) {
|
|
1471
|
+
console.warn("[lazypock] No realtime service configured.");
|
|
1472
|
+
return () => {
|
|
1473
|
+
};
|
|
954
1474
|
}
|
|
955
|
-
|
|
1475
|
+
let recordId;
|
|
1476
|
+
let callback;
|
|
1477
|
+
let joinPayload;
|
|
1478
|
+
if (typeof topicOrCallback === "function") {
|
|
1479
|
+
callback = topicOrCallback;
|
|
1480
|
+
if (typeof maybeCallback === "string") recordId = maybeCallback;
|
|
1481
|
+
} else {
|
|
1482
|
+
callback = maybeCallback;
|
|
1483
|
+
recordId = topicOrCallback === "*" ? void 0 : topicOrCallback;
|
|
1484
|
+
const { headers: _ignored, ...rest } = options ?? {};
|
|
1485
|
+
if (Object.keys(rest).length > 0) joinPayload = rest;
|
|
1486
|
+
}
|
|
1487
|
+
const topic = collectionTopic(collectionName) + (recordId ? ":" + recordId : "");
|
|
1488
|
+
const handler = (raw) => {
|
|
1489
|
+
const record = raw.payload?.["record"] ?? {};
|
|
1490
|
+
callback({
|
|
1491
|
+
action: normalizeAction(raw.event, raw.payload?.["action"]),
|
|
1492
|
+
record,
|
|
1493
|
+
topic: raw.topic
|
|
1494
|
+
});
|
|
1495
|
+
};
|
|
1496
|
+
realtime.ensureConnected();
|
|
1497
|
+
realtime.subscribe(topic, handler, joinPayload);
|
|
1498
|
+
return () => realtime.unsubscribe(topic, handler);
|
|
1499
|
+
}
|
|
1500
|
+
function unsubscribeFromCollection(ctx, recordId) {
|
|
1501
|
+
const { realtime, collectionName } = ctx;
|
|
1502
|
+
if (recordId === void 0) {
|
|
1503
|
+
realtime?.unsubscribeByPrefix(collectionTopic(collectionName));
|
|
1504
|
+
return;
|
|
1505
|
+
}
|
|
1506
|
+
const topic = collectionTopic(collectionName) + (recordId === "*" ? "" : ":" + recordId);
|
|
1507
|
+
realtime?.unsubscribe(topic);
|
|
956
1508
|
}
|
|
1509
|
+
|
|
1510
|
+
// src/collection/index.ts
|
|
957
1511
|
var CollectionService = class _CollectionService {
|
|
958
1512
|
/** @internal */
|
|
959
1513
|
constructor(http, collectionName, authStore, realtime, schema, schemaResolver) {
|
|
@@ -964,21 +1518,42 @@ var CollectionService = class _CollectionService {
|
|
|
964
1518
|
this.schema = schema;
|
|
965
1519
|
this.schemaResolver = schemaResolver;
|
|
966
1520
|
}
|
|
967
|
-
encodeId(id) {
|
|
968
|
-
return encodeURIComponent(id);
|
|
969
|
-
}
|
|
970
1521
|
/**
|
|
971
|
-
*
|
|
1522
|
+
* A copy of this service with the given overrides applied.
|
|
972
1523
|
*
|
|
973
|
-
*
|
|
974
|
-
*
|
|
1524
|
+
* Every field is listed exactly once, so a new service-level field can't be
|
|
1525
|
+
* silently dropped by `select()` or `withSchema()`.
|
|
975
1526
|
*/
|
|
1527
|
+
derive(patch = {}) {
|
|
1528
|
+
const derived = new _CollectionService(
|
|
1529
|
+
this.http,
|
|
1530
|
+
this.collectionName,
|
|
1531
|
+
this.authStore,
|
|
1532
|
+
this.realtime,
|
|
1533
|
+
patch.schema ?? this.schema,
|
|
1534
|
+
this.schemaResolver
|
|
1535
|
+
);
|
|
1536
|
+
derived.fieldsPreset = patch.fieldsPreset ?? this.fieldsPreset;
|
|
1537
|
+
return derived;
|
|
1538
|
+
}
|
|
1539
|
+
/** The records endpoint for this collection. */
|
|
976
1540
|
recordsPath() {
|
|
977
|
-
return
|
|
1541
|
+
return recordsPath(this.collectionName);
|
|
978
1542
|
}
|
|
979
1543
|
/** The collection's own endpoint, which is where its auth routes live. */
|
|
980
1544
|
collectionPath() {
|
|
981
|
-
return
|
|
1545
|
+
return collectionPath(this.collectionName);
|
|
1546
|
+
}
|
|
1547
|
+
encodeId(id) {
|
|
1548
|
+
return encodeId(id);
|
|
1549
|
+
}
|
|
1550
|
+
/** What the auth routes need: transport, collection name, session store. */
|
|
1551
|
+
sessionContext() {
|
|
1552
|
+
return {
|
|
1553
|
+
http: this.http,
|
|
1554
|
+
collectionName: this.collectionName,
|
|
1555
|
+
authStore: this.authStore
|
|
1556
|
+
};
|
|
982
1557
|
}
|
|
983
1558
|
// ── Field projection (`.select`) ──
|
|
984
1559
|
/**
|
|
@@ -993,10 +1568,10 @@ var CollectionService = class _CollectionService {
|
|
|
993
1568
|
* const t = await client.collection("posts").select("id", "title").getList();
|
|
994
1569
|
* // GET /api/posts?fields=id,title
|
|
995
1570
|
*
|
|
996
|
-
* // All visible fields (implicit system keys id/created/updated
|
|
1571
|
+
* // All visible fields (implicit system keys id/created/updated/... are
|
|
997
1572
|
* // kept; hidden fields are excluded via the projection)
|
|
998
1573
|
* const all = await client.collection("posts").select("*").getList();
|
|
999
|
-
* // GET /api/posts?fields=id,created,updated,collectionId,collectionName,title,published
|
|
1574
|
+
* // GET /api/posts?fields=id,created,updated,collectionId,collectionName,title,published,... (schema known)
|
|
1000
1575
|
* ```
|
|
1001
1576
|
*
|
|
1002
1577
|
* When no schema is known, the default (no `select()` call) sends no
|
|
@@ -1005,17 +1580,12 @@ var CollectionService = class _CollectionService {
|
|
|
1005
1580
|
*/
|
|
1006
1581
|
select(...fields) {
|
|
1007
1582
|
const preset = fields.length === 0 ? "*" : fields.join(",");
|
|
1008
|
-
const derived =
|
|
1009
|
-
this.http,
|
|
1010
|
-
this.collectionName,
|
|
1011
|
-
this.authStore,
|
|
1012
|
-
this.realtime,
|
|
1013
|
-
this.schema,
|
|
1014
|
-
this.schemaResolver
|
|
1015
|
-
);
|
|
1016
|
-
derived.fieldsPreset = preset;
|
|
1583
|
+
const derived = this.derive({ fieldsPreset: preset });
|
|
1017
1584
|
if (this.schema && preset !== "*") {
|
|
1018
|
-
const known = new Set(
|
|
1585
|
+
const known = /* @__PURE__ */ new Set([
|
|
1586
|
+
...SYSTEM_FIELDS,
|
|
1587
|
+
...this.schema.fields?.map((f) => f.name) ?? []
|
|
1588
|
+
]);
|
|
1019
1589
|
for (const f of fields) {
|
|
1020
1590
|
if (!known.has(f)) {
|
|
1021
1591
|
console.warn(
|
|
@@ -1034,14 +1604,7 @@ var CollectionService = class _CollectionService {
|
|
|
1034
1604
|
* system keys (`id`, `created`, `updated`, `collectionId`, `collectionName`)
|
|
1035
1605
|
*/
|
|
1036
1606
|
effectiveFields(optionsFields) {
|
|
1037
|
-
|
|
1038
|
-
if (this.fieldsPreset !== void 0) {
|
|
1039
|
-
if (this.fieldsPreset === "*") {
|
|
1040
|
-
return this.visibleFields();
|
|
1041
|
-
}
|
|
1042
|
-
return this.fieldsPreset;
|
|
1043
|
-
}
|
|
1044
|
-
return this.visibleFields();
|
|
1607
|
+
return effectiveFields(this.schema, this.fieldsPreset, optionsFields);
|
|
1045
1608
|
}
|
|
1046
1609
|
/**
|
|
1047
1610
|
* All non-hidden, non-password, non-autodate field names from the schema,
|
|
@@ -1058,21 +1621,23 @@ var CollectionService = class _CollectionService {
|
|
|
1058
1621
|
* default returns every non-password field, including the system keys).
|
|
1059
1622
|
*/
|
|
1060
1623
|
visibleFields() {
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1624
|
+
return visibleFields(this.schema);
|
|
1625
|
+
}
|
|
1626
|
+
/** Service state that query building needs. */
|
|
1627
|
+
queryContext() {
|
|
1628
|
+
return {
|
|
1629
|
+
expand: this.expandContext(),
|
|
1630
|
+
effectiveFields: (optionsFields) => this.effectiveFields(optionsFields),
|
|
1631
|
+
resolveFilter: (filter) => this.resolveFilter(filter)
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
/** Context for the schema-aware expand helpers. */
|
|
1635
|
+
expandContext() {
|
|
1636
|
+
return {
|
|
1637
|
+
collectionName: this.collectionName,
|
|
1638
|
+
schema: this.schema,
|
|
1639
|
+
resolveCollection: (key) => this.schemaResolver?.(key)
|
|
1640
|
+
};
|
|
1076
1641
|
}
|
|
1077
1642
|
// ── Typed filter builder ──
|
|
1078
1643
|
/**
|
|
@@ -1085,7 +1650,7 @@ var CollectionService = class _CollectionService {
|
|
|
1085
1650
|
* await posts.getList(1, 20, {
|
|
1086
1651
|
* filter: q("title").contains("x").and(q("published").eq(true)),
|
|
1087
1652
|
* });
|
|
1088
|
-
* // → filter=(title ~ '
|
|
1653
|
+
* // → filter=(title ~ 'x' && published = true)
|
|
1089
1654
|
*
|
|
1090
1655
|
* // PocketBase `?` array operators (multi-select / multi-relation):
|
|
1091
1656
|
* posts.getList(1, 20, { filter: q("tags").anyEq("news") });
|
|
@@ -1111,230 +1676,6 @@ var CollectionService = class _CollectionService {
|
|
|
1111
1676
|
}
|
|
1112
1677
|
return filter((field) => this.where(field));
|
|
1113
1678
|
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Resolve the leading relation chain of an expand token against the
|
|
1116
|
-
* (optionally schema-aware) collection graph.
|
|
1117
|
-
*
|
|
1118
|
-
* Walks `segments` from this collection: each segment that is a relation
|
|
1119
|
-
* advances to its target collection. Stops at the first non-relation
|
|
1120
|
-
* segment or when the target schema is unknown.
|
|
1121
|
-
*
|
|
1122
|
-
* @returns `depth` — how many leading segments are relations; and
|
|
1123
|
-
* `canInspect` — whether the schema at `depth` is known (so the next
|
|
1124
|
-
* segment can be classified as a field selection vs a nested relation).
|
|
1125
|
-
*/
|
|
1126
|
-
resolveRelationChain(segments) {
|
|
1127
|
-
let currentFields = this.schema?.fields;
|
|
1128
|
-
if (!currentFields) return { depth: 0, canInspect: false };
|
|
1129
|
-
for (let i = 0; i < segments.length; i++) {
|
|
1130
|
-
const field = currentFields.find((f) => f.name === segments[i]);
|
|
1131
|
-
if (!field || field.type !== "relation") {
|
|
1132
|
-
return { depth: i, canInspect: true };
|
|
1133
|
-
}
|
|
1134
|
-
if (i === segments.length - 1) {
|
|
1135
|
-
return { depth: segments.length, canInspect: true };
|
|
1136
|
-
}
|
|
1137
|
-
const target = this.resolveTargetCollection(field);
|
|
1138
|
-
if (!target?.fields) {
|
|
1139
|
-
return { depth: i + 1, canInspect: false };
|
|
1140
|
-
}
|
|
1141
|
-
currentFields = target.fields;
|
|
1142
|
-
}
|
|
1143
|
-
return { depth: segments.length, canInspect: true };
|
|
1144
|
-
}
|
|
1145
|
-
/** Follow a relation field to its target collection, if resolvable. */
|
|
1146
|
-
resolveTargetCollection(field) {
|
|
1147
|
-
const key = field.options?.collection ?? field.options?.collectionId ?? field.collectionId ?? void 0;
|
|
1148
|
-
if (!key || !this.schemaResolver) return void 0;
|
|
1149
|
-
return this.schemaResolver(String(key));
|
|
1150
|
-
}
|
|
1151
|
-
/** Warn once when an expand field is not a relation (schema known). */
|
|
1152
|
-
warnNonRelation(name) {
|
|
1153
|
-
console.warn(
|
|
1154
|
-
`[lazypock] expand("${name}"): field is not a relation on collection "${this.collectionName}"`
|
|
1155
|
-
);
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
* Validate and normalise a caller's `expand` string.
|
|
1159
|
-
*
|
|
1160
|
-
* PocketBase only expands *relations*: a dotted token like `author.user`
|
|
1161
|
-
* walks into the related record. Callers often use a dotted token to mean
|
|
1162
|
-
* "only these fields of the expanded record" (`owner.name,owner.email`),
|
|
1163
|
-
* which PocketBase silently drops. When the trailing segment is not a
|
|
1164
|
-
* relation — resolved from the schema graph — the token is rewritten as a
|
|
1165
|
-
* field selection:
|
|
1166
|
-
*
|
|
1167
|
-
* ```
|
|
1168
|
-
* expand: 'owner.name,owner.email'
|
|
1169
|
-
* → expand=owner&fields=<base>,expand.owner.name,expand.owner.email
|
|
1170
|
-
* ```
|
|
1171
|
-
*
|
|
1172
|
-
* Tokens whose whole path is relations keep their existing nested-expand
|
|
1173
|
-
* behaviour. Without a schema the two are indistinguishable, so a lone
|
|
1174
|
-
* dotted token stays a nested expand (with a warning) while two or more
|
|
1175
|
-
* dotted tokens sharing the same relation are treated as field selection.
|
|
1176
|
-
*/
|
|
1177
|
-
normalizeExpand(expand) {
|
|
1178
|
-
const narrow = /* @__PURE__ */ new Map();
|
|
1179
|
-
if (!expand) return { fieldSelectors: [], narrow };
|
|
1180
|
-
const tokens = expand.split(",").map((t) => t.trim()).filter(Boolean);
|
|
1181
|
-
if (tokens.length === 0) return { fieldSelectors: [], narrow };
|
|
1182
|
-
const relations = this.schema?.fields ? new Set(
|
|
1183
|
-
this.schema.fields.filter((f) => f.type === "relation").map((f) => f.name)
|
|
1184
|
-
) : void 0;
|
|
1185
|
-
const parsed = [];
|
|
1186
|
-
for (const token of tokens) {
|
|
1187
|
-
const segments = token.split(".").filter(Boolean);
|
|
1188
|
-
const base = segments[0] ?? token;
|
|
1189
|
-
if (segments.length <= 1) {
|
|
1190
|
-
if (relations && !relations.has(base)) {
|
|
1191
|
-
this.warnNonRelation(base);
|
|
1192
|
-
}
|
|
1193
|
-
parsed.push({ token, base, segments, kind: "plain", depth: 0 });
|
|
1194
|
-
continue;
|
|
1195
|
-
}
|
|
1196
|
-
if (!relations) {
|
|
1197
|
-
parsed.push({ token, base, segments, kind: "unknown", depth: 1 });
|
|
1198
|
-
continue;
|
|
1199
|
-
}
|
|
1200
|
-
const { depth, canInspect } = this.resolveRelationChain(segments);
|
|
1201
|
-
if (depth === 0) {
|
|
1202
|
-
this.warnNonRelation(base);
|
|
1203
|
-
parsed.push({ token, base, segments, kind: "plain", depth: 0 });
|
|
1204
|
-
} else if (depth === segments.length) {
|
|
1205
|
-
parsed.push({ token, base, segments, kind: "nested", depth });
|
|
1206
|
-
} else if (canInspect) {
|
|
1207
|
-
parsed.push({ token, base, segments, kind: "narrow", depth });
|
|
1208
|
-
} else {
|
|
1209
|
-
parsed.push({ token, base, segments, kind: "unknown", depth });
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
const byBase = /* @__PURE__ */ new Map();
|
|
1213
|
-
for (const t of parsed) {
|
|
1214
|
-
const arr = byBase.get(t.base);
|
|
1215
|
-
if (arr) arr.push(t);
|
|
1216
|
-
else byBase.set(t.base, [t]);
|
|
1217
|
-
}
|
|
1218
|
-
for (const [base, group] of byBase) {
|
|
1219
|
-
const unknowns = group.filter((t) => t.kind === "unknown");
|
|
1220
|
-
if (unknowns.length === 0) continue;
|
|
1221
|
-
const distinct = new Set(unknowns.map((t) => t.token));
|
|
1222
|
-
const hasNarrow = group.some((t) => t.kind === "narrow");
|
|
1223
|
-
if (!hasNarrow && distinct.size < 2) {
|
|
1224
|
-
for (const t of unknowns) {
|
|
1225
|
-
t.kind = "nested";
|
|
1226
|
-
console.warn(
|
|
1227
|
-
`[lazypock] expand("${t.token}"): treated as a nested relation. Pass types.schemas (or use fields=) to select fields of "${base}".`
|
|
1228
|
-
);
|
|
1229
|
-
}
|
|
1230
|
-
} else {
|
|
1231
|
-
for (const t of unknowns) t.kind = "narrow";
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
const expandParams = [];
|
|
1235
|
-
const expandSeen = /* @__PURE__ */ new Set();
|
|
1236
|
-
const fieldSelectors = [];
|
|
1237
|
-
const selectorSeen = /* @__PURE__ */ new Set();
|
|
1238
|
-
const pushParam = (p) => {
|
|
1239
|
-
if (!expandSeen.has(p)) {
|
|
1240
|
-
expandSeen.add(p);
|
|
1241
|
-
expandParams.push(p);
|
|
1242
|
-
}
|
|
1243
|
-
};
|
|
1244
|
-
const pushSelector = (s) => {
|
|
1245
|
-
if (!selectorSeen.has(s)) {
|
|
1246
|
-
selectorSeen.add(s);
|
|
1247
|
-
fieldSelectors.push(s);
|
|
1248
|
-
}
|
|
1249
|
-
};
|
|
1250
|
-
for (const t of parsed) {
|
|
1251
|
-
if (t.kind === "plain") {
|
|
1252
|
-
pushParam(t.token);
|
|
1253
|
-
pushSelector(`expand.${t.token}`);
|
|
1254
|
-
} else if (t.kind === "nested") {
|
|
1255
|
-
for (let i = 1; i <= t.segments.length; i++) {
|
|
1256
|
-
pushParam(t.segments.slice(0, i).join("."));
|
|
1257
|
-
}
|
|
1258
|
-
pushSelector(`expand.${t.segments[0]}`);
|
|
1259
|
-
} else {
|
|
1260
|
-
const depth = Math.max(1, t.depth);
|
|
1261
|
-
const relationPath = t.segments.slice(0, depth).join(".");
|
|
1262
|
-
pushParam(relationPath);
|
|
1263
|
-
pushSelector(`expand.${t.segments.slice(0, depth + 1).join(".")}`);
|
|
1264
|
-
if (t.segments.length === depth + 1) {
|
|
1265
|
-
const base = t.segments[0];
|
|
1266
|
-
const leaf = t.segments[depth];
|
|
1267
|
-
const leaves = narrow.get(base) ?? [];
|
|
1268
|
-
if (!leaves.includes(leaf)) leaves.push(leaf);
|
|
1269
|
-
narrow.set(base, leaves);
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
return {
|
|
1274
|
-
expand: expandParams.length > 0 ? expandParams.join(",") : void 0,
|
|
1275
|
-
fieldSelectors,
|
|
1276
|
-
narrow
|
|
1277
|
-
};
|
|
1278
|
-
}
|
|
1279
|
-
/**
|
|
1280
|
-
* Keep only the requested leaf fields on `record.expand.<relation>`.
|
|
1281
|
-
*
|
|
1282
|
-
* Mirrors PocketBase's server-side `fields=expand.<rel>.<field>` projection
|
|
1283
|
-
* for servers that don't support it (the LazyPock server), so field
|
|
1284
|
-
* selection behaves the same against both. A no-op when the server already
|
|
1285
|
-
* narrowed the record. Multi-relations are narrowed element-wise.
|
|
1286
|
-
*/
|
|
1287
|
-
applyExpandNarrowing(record, narrow) {
|
|
1288
|
-
if (narrow.size === 0) return record;
|
|
1289
|
-
const rec = record;
|
|
1290
|
-
const expand = rec.expand;
|
|
1291
|
-
if (!expand || typeof expand !== "object") return record;
|
|
1292
|
-
const next = { ...expand };
|
|
1293
|
-
for (const [relation, leaves] of narrow) {
|
|
1294
|
-
const value = next[relation];
|
|
1295
|
-
if (value === void 0 || value === null) continue;
|
|
1296
|
-
next[relation] = Array.isArray(value) ? value.map((el) => pickFields(el, leaves)) : pickFields(value, leaves);
|
|
1297
|
-
}
|
|
1298
|
-
return { ...rec, expand: next };
|
|
1299
|
-
}
|
|
1300
|
-
/**
|
|
1301
|
-
* Merge the expand-preserving selectors into the base field projection.
|
|
1302
|
-
*
|
|
1303
|
-
* Both servers are kept happy at once:
|
|
1304
|
-
* - PocketBase drops the whole `expand` payload unless an `expand.<key>`
|
|
1305
|
-
* entry is present in `fields`;
|
|
1306
|
-
* - the LazyPock server projects first (strict, top-level only) and then
|
|
1307
|
-
* expands, so the relation *field* itself must be present in `fields`.
|
|
1308
|
-
*
|
|
1309
|
-
* We therefore add both the relation field (`cate`) and the
|
|
1310
|
-
* `expand.<key>` entry. When no projection is active, no `fields` param is
|
|
1311
|
-
* sent at all — a value like `*` plus other tokens is not a wildcard on
|
|
1312
|
-
* the LazyPock server (it is a strict take, yielding empty records) and
|
|
1313
|
-
* the server keeps `expand` by default when `fields` is absent.
|
|
1314
|
-
*/
|
|
1315
|
-
mergeExpandFields(base, info) {
|
|
1316
|
-
if (info.fieldSelectors.length === 0) return base;
|
|
1317
|
-
if (base === void 0) return void 0;
|
|
1318
|
-
const parts = base.split(",").map((f) => f.trim()).filter(Boolean);
|
|
1319
|
-
const seen = new Set(parts);
|
|
1320
|
-
const push = (value) => {
|
|
1321
|
-
if (seen.has(value)) return;
|
|
1322
|
-
seen.add(value);
|
|
1323
|
-
parts.push(value);
|
|
1324
|
-
};
|
|
1325
|
-
const fullKeys = /* @__PURE__ */ new Set();
|
|
1326
|
-
for (const selector of info.fieldSelectors) {
|
|
1327
|
-
const seg = selector.split(".");
|
|
1328
|
-
if (seg.length === 2) fullKeys.add(seg[1]);
|
|
1329
|
-
}
|
|
1330
|
-
for (const selector of info.fieldSelectors) {
|
|
1331
|
-
const seg = selector.split(".");
|
|
1332
|
-
if (seg.length > 2 && fullKeys.has(seg[1])) continue;
|
|
1333
|
-
if (seg.length >= 2) push(seg[1]);
|
|
1334
|
-
push(selector);
|
|
1335
|
-
}
|
|
1336
|
-
return parts.join(",");
|
|
1337
|
-
}
|
|
1338
1679
|
/**
|
|
1339
1680
|
* Fetch a paginated list of records (PocketBase `getList`).
|
|
1340
1681
|
*
|
|
@@ -1343,50 +1684,13 @@ var CollectionService = class _CollectionService {
|
|
|
1343
1684
|
* @param options Query params (`filter`, `sort`, `expand`, `fields`) + request options.
|
|
1344
1685
|
*/
|
|
1345
1686
|
async getList(page = 1, perPage = 30, options) {
|
|
1346
|
-
const {
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
signal,
|
|
1353
|
-
singleFlight,
|
|
1354
|
-
params,
|
|
1355
|
-
...queryParams
|
|
1356
|
-
} = options ?? {};
|
|
1357
|
-
if (Array.isArray(queryParams.sort)) {
|
|
1358
|
-
queryParams.sort = queryParams.sort.join(",");
|
|
1359
|
-
}
|
|
1360
|
-
if (Array.isArray(queryParams.expand)) {
|
|
1361
|
-
queryParams.expand = queryParams.expand.join(",");
|
|
1362
|
-
}
|
|
1363
|
-
if (typeof queryParams.filter === "function") {
|
|
1364
|
-
queryParams.filter = this.resolveFilter(
|
|
1365
|
-
queryParams.filter
|
|
1366
|
-
);
|
|
1367
|
-
}
|
|
1368
|
-
const expandInfo = this.normalizeExpand(
|
|
1369
|
-
typeof queryParams.expand === "string" ? queryParams.expand : void 0
|
|
1370
|
-
);
|
|
1371
|
-
if (expandInfo.expand !== void 0) {
|
|
1372
|
-
queryParams.expand = expandInfo.expand;
|
|
1373
|
-
}
|
|
1374
|
-
const fields = this.mergeExpandFields(
|
|
1375
|
-
this.effectiveFields(queryParams.fields),
|
|
1376
|
-
expandInfo
|
|
1687
|
+
const { requestKey, autoCancel, cancelKey, fetch: fetch2, headers, signal, singleFlight, params, ...queryParams } = options ?? {};
|
|
1688
|
+
const { qs, narrow } = buildListQuery(
|
|
1689
|
+
this.queryContext(),
|
|
1690
|
+
page,
|
|
1691
|
+
perPage,
|
|
1692
|
+
queryParams
|
|
1377
1693
|
);
|
|
1378
|
-
if (fields !== void 0) {
|
|
1379
|
-
queryParams.fields = fields;
|
|
1380
|
-
}
|
|
1381
|
-
const qs = new URLSearchParams(
|
|
1382
|
-
Object.fromEntries(
|
|
1383
|
-
Object.entries({
|
|
1384
|
-
page: String(page),
|
|
1385
|
-
perPage: String(perPage),
|
|
1386
|
-
...queryParams
|
|
1387
|
-
}).map(([k, v]) => [k, String(v)])
|
|
1388
|
-
)
|
|
1389
|
-
).toString();
|
|
1390
1694
|
const result = await this.http.get(this.recordsPath() + "?" + qs, {
|
|
1391
1695
|
requestKey,
|
|
1392
1696
|
autoCancel,
|
|
@@ -1397,44 +1701,41 @@ var CollectionService = class _CollectionService {
|
|
|
1397
1701
|
singleFlight,
|
|
1398
1702
|
params
|
|
1399
1703
|
});
|
|
1400
|
-
if (result &&
|
|
1704
|
+
if (result && narrow.size > 0) {
|
|
1401
1705
|
result.items = result.items.map(
|
|
1402
|
-
(item) =>
|
|
1706
|
+
(item) => applyExpandNarrowing(item, narrow)
|
|
1403
1707
|
);
|
|
1404
1708
|
}
|
|
1405
1709
|
return result;
|
|
1406
1710
|
}
|
|
1407
1711
|
/**
|
|
1408
|
-
* Fetch all records at once (auto-paginates; `batch` defaults to and is
|
|
1712
|
+
* Fetch all records at once (auto-paginates; `batch` defaults to and is
|
|
1713
|
+
* capped at 500, the server max `perPage`). Mirrors PocketBase's
|
|
1409
1714
|
* `pb.collection(name).getFullList()`.
|
|
1410
1715
|
*
|
|
1411
1716
|
* @param options Query params (`sort`, `filter`, `batch`, etc.) + request options.
|
|
1412
1717
|
*/
|
|
1413
1718
|
async getFullList(options) {
|
|
1414
|
-
const {
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
const effectiveKey = typeof rest.requestKey === "string" ? rest.requestKey : `getFullList:${this.collectionName}:${stableStringify(rest)}`;
|
|
1420
|
-
const items = [];
|
|
1421
|
-
let page = 1;
|
|
1422
|
-
for (; ; ) {
|
|
1423
|
-
const res = await this.getList(
|
|
1424
|
-
page,
|
|
1425
|
-
batch,
|
|
1426
|
-
{
|
|
1427
|
-
...rest,
|
|
1428
|
-
requestKey: effectiveKey,
|
|
1429
|
-
singleFlight: true
|
|
1430
|
-
}
|
|
1431
|
-
);
|
|
1432
|
-
if (!res || !res.items || res.items.length === 0) break;
|
|
1433
|
-
items.push(...res.items);
|
|
1434
|
-
if (page >= (res.totalPages ?? page)) break;
|
|
1435
|
-
page += 1;
|
|
1719
|
+
const { query, transport, batch } = splitListOptions(
|
|
1720
|
+
options
|
|
1721
|
+
);
|
|
1722
|
+
if (typeof query.filter === "function") {
|
|
1723
|
+
query.filter = this.resolveFilter(query.filter);
|
|
1436
1724
|
}
|
|
1437
|
-
|
|
1725
|
+
const effectiveKey = fullListKey(
|
|
1726
|
+
this.collectionName,
|
|
1727
|
+
query,
|
|
1728
|
+
transport.requestKey
|
|
1729
|
+
);
|
|
1730
|
+
return paginateAll(
|
|
1731
|
+
batch,
|
|
1732
|
+
(page) => this.getList(page, batch, {
|
|
1733
|
+
...query,
|
|
1734
|
+
...transport,
|
|
1735
|
+
requestKey: effectiveKey,
|
|
1736
|
+
singleFlight: true
|
|
1737
|
+
})
|
|
1738
|
+
);
|
|
1438
1739
|
}
|
|
1439
1740
|
/**
|
|
1440
1741
|
* Fetch the first record matching a filter (PocketBase `getFirstListItem`).
|
|
@@ -1455,29 +1756,13 @@ var CollectionService = class _CollectionService {
|
|
|
1455
1756
|
* @param options Optional request options.
|
|
1456
1757
|
*/
|
|
1457
1758
|
async getOne(id, options) {
|
|
1458
|
-
|
|
1459
|
-
if (Array.isArray(options?.expand)) {
|
|
1460
|
-
rawExpand = options.expand.join(",");
|
|
1461
|
-
} else if (typeof options?.expand === "string") {
|
|
1462
|
-
rawExpand = options.expand;
|
|
1463
|
-
}
|
|
1464
|
-
const expandInfo = this.normalizeExpand(rawExpand);
|
|
1465
|
-
const fields = this.mergeExpandFields(
|
|
1466
|
-
this.effectiveFields(options?.fields),
|
|
1467
|
-
expandInfo
|
|
1468
|
-
);
|
|
1469
|
-
const qs = new URLSearchParams();
|
|
1470
|
-
if (fields !== void 0) qs.set("fields", fields);
|
|
1471
|
-
if (rawExpand !== void 0) {
|
|
1472
|
-
qs.set("expand", expandInfo.expand ?? rawExpand);
|
|
1473
|
-
}
|
|
1474
|
-
const qsStr = qs.toString();
|
|
1759
|
+
const { qs, narrow } = buildReadQuery(this.queryContext(), options);
|
|
1475
1760
|
const record = await this.http.get(
|
|
1476
|
-
this.recordsPath() + "/" + this.encodeId(id) + (
|
|
1761
|
+
this.recordsPath() + "/" + this.encodeId(id) + (qs ? "?" + qs : ""),
|
|
1477
1762
|
options
|
|
1478
1763
|
);
|
|
1479
|
-
if (record &&
|
|
1480
|
-
return
|
|
1764
|
+
if (record && narrow.size > 0) {
|
|
1765
|
+
return applyExpandNarrowing(record, narrow);
|
|
1481
1766
|
}
|
|
1482
1767
|
return record;
|
|
1483
1768
|
}
|
|
@@ -1519,13 +1804,12 @@ var CollectionService = class _CollectionService {
|
|
|
1519
1804
|
options
|
|
1520
1805
|
);
|
|
1521
1806
|
}
|
|
1522
|
-
// ── Expand / Relation Fields ──
|
|
1523
1807
|
/**
|
|
1524
1808
|
* Get a list of expandable (relation) fields for this collection.
|
|
1525
1809
|
* Useful for constructing `expand` query parameters.
|
|
1526
1810
|
*/
|
|
1527
1811
|
async expandFields(options) {
|
|
1528
|
-
const data = await this.http.get(
|
|
1812
|
+
const data = await this.http.get(this.collectionPath(), options);
|
|
1529
1813
|
if (!data?.fields) return null;
|
|
1530
1814
|
return data.fields.filter((f) => f.type === "relation" && f.options?.collection).map((f) => ({
|
|
1531
1815
|
field: f.name,
|
|
@@ -1558,47 +1842,15 @@ var CollectionService = class _CollectionService {
|
|
|
1558
1842
|
* excluded from responses, select/expand are validated.
|
|
1559
1843
|
*/
|
|
1560
1844
|
withSchema(schema) {
|
|
1561
|
-
|
|
1562
|
-
this.http,
|
|
1563
|
-
this.collectionName,
|
|
1564
|
-
this.authStore,
|
|
1565
|
-
this.realtime,
|
|
1566
|
-
schema,
|
|
1567
|
-
this.schemaResolver
|
|
1568
|
-
);
|
|
1569
|
-
derived.fieldsPreset = this.fieldsPreset;
|
|
1570
|
-
return derived;
|
|
1845
|
+
return this.derive({ schema });
|
|
1571
1846
|
}
|
|
1572
1847
|
subscribe(topicOrCallback, maybeCallback, options) {
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
let callback;
|
|
1580
|
-
let joinPayload;
|
|
1581
|
-
if (typeof topicOrCallback === "function") {
|
|
1582
|
-
callback = topicOrCallback;
|
|
1583
|
-
if (typeof maybeCallback === "string") recordId = maybeCallback;
|
|
1584
|
-
} else {
|
|
1585
|
-
callback = maybeCallback;
|
|
1586
|
-
recordId = topicOrCallback === "*" ? void 0 : topicOrCallback;
|
|
1587
|
-
const { headers: _ignored, ...rest } = options ?? {};
|
|
1588
|
-
if (Object.keys(rest).length > 0) joinPayload = rest;
|
|
1589
|
-
}
|
|
1590
|
-
const topic = "collection:" + this.collectionName + (recordId ? ":" + recordId : "");
|
|
1591
|
-
const handler = (raw) => {
|
|
1592
|
-
const record = raw.payload?.["record"] ?? {};
|
|
1593
|
-
callback({
|
|
1594
|
-
action: normalizeAction(raw.event, raw.payload?.["action"]),
|
|
1595
|
-
record,
|
|
1596
|
-
topic: raw.topic
|
|
1597
|
-
});
|
|
1598
|
-
};
|
|
1599
|
-
this.realtime.ensureConnected();
|
|
1600
|
-
this.realtime.subscribe(topic, handler, joinPayload);
|
|
1601
|
-
return () => this.realtime?.unsubscribe(topic, handler);
|
|
1848
|
+
return subscribeToCollection(
|
|
1849
|
+
{ realtime: this.realtime, collectionName: this.collectionName },
|
|
1850
|
+
topicOrCallback,
|
|
1851
|
+
maybeCallback,
|
|
1852
|
+
options
|
|
1853
|
+
);
|
|
1602
1854
|
}
|
|
1603
1855
|
/**
|
|
1604
1856
|
* Unsubscribe from realtime changes (PocketBase-compatible):
|
|
@@ -1607,66 +1859,33 @@ var CollectionService = class _CollectionService {
|
|
|
1607
1859
|
* - `unsubscribe('RECORD_ID')` — remove that record's subscriptions
|
|
1608
1860
|
*/
|
|
1609
1861
|
unsubscribe(recordId) {
|
|
1610
|
-
|
|
1611
|
-
this.realtime
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
return;
|
|
1615
|
-
}
|
|
1616
|
-
const topic = "collection:" + this.collectionName + (recordId === "*" ? "" : ":" + recordId);
|
|
1617
|
-
this.realtime?.unsubscribe(topic);
|
|
1862
|
+
unsubscribeFromCollection(
|
|
1863
|
+
{ realtime: this.realtime, collectionName: this.collectionName },
|
|
1864
|
+
recordId
|
|
1865
|
+
);
|
|
1618
1866
|
}
|
|
1619
1867
|
// ── Auth Collection Methods ──
|
|
1620
1868
|
/**
|
|
1621
1869
|
* Authenticate with email/password against this auth collection.
|
|
1622
1870
|
* Stores the returned token and user model in the auth store.
|
|
1623
1871
|
*/
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
this.collectionPath() + "/auth-with-password",
|
|
1627
|
-
{ identity, password },
|
|
1628
|
-
options
|
|
1629
|
-
);
|
|
1630
|
-
if (data && this.authStore) {
|
|
1631
|
-
this.authStore.setCollectionName(this.collectionName);
|
|
1632
|
-
rememberAuthResponse(
|
|
1633
|
-
this.authStore,
|
|
1634
|
-
data,
|
|
1635
|
-
data.record
|
|
1636
|
-
);
|
|
1637
|
-
}
|
|
1638
|
-
return data;
|
|
1872
|
+
authWithPassword(identity, password, options) {
|
|
1873
|
+
return authWithPassword(this.sessionContext(), identity, password, options);
|
|
1639
1874
|
}
|
|
1640
1875
|
/**
|
|
1641
1876
|
* Refresh the auth token for the currently authenticated user.
|
|
1642
1877
|
* Updates the stored token and user model.
|
|
1643
1878
|
*/
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
this.collectionPath() + "/auth-refresh",
|
|
1647
|
-
{ refreshToken: this.authStore?.refreshToken },
|
|
1648
|
-
options
|
|
1649
|
-
);
|
|
1650
|
-
if (data && this.authStore) {
|
|
1651
|
-
this.authStore.setCollectionName(this.collectionName);
|
|
1652
|
-
rememberAuthResponse(
|
|
1653
|
-
this.authStore,
|
|
1654
|
-
data,
|
|
1655
|
-
data.record
|
|
1656
|
-
);
|
|
1657
|
-
}
|
|
1658
|
-
return data;
|
|
1879
|
+
authRefresh(options) {
|
|
1880
|
+
return authRefresh(this.sessionContext(), options);
|
|
1659
1881
|
}
|
|
1660
1882
|
/**
|
|
1661
1883
|
* Get available auth methods for this collection (PocketBase `listAuthMethods`).
|
|
1662
1884
|
* Returns `password`, `oauth2.providers` (each with `name`, `authURL`,
|
|
1663
1885
|
* `state`, `codeVerifier`), and `mfa`.
|
|
1664
1886
|
*/
|
|
1665
|
-
|
|
1666
|
-
return this.
|
|
1667
|
-
this.collectionPath() + "/auth-methods",
|
|
1668
|
-
options
|
|
1669
|
-
);
|
|
1887
|
+
listAuthMethods(options) {
|
|
1888
|
+
return listAuthMethods(this.sessionContext(), options);
|
|
1670
1889
|
}
|
|
1671
1890
|
/**
|
|
1672
1891
|
* Get available auth methods for this collection.
|
|
@@ -1681,13 +1900,8 @@ var CollectionService = class _CollectionService {
|
|
|
1681
1900
|
* `requestPasswordReset`). The backend always answers 204 to avoid
|
|
1682
1901
|
* revealing whether the address exists.
|
|
1683
1902
|
*/
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
this.collectionPath() + "/request-password-reset",
|
|
1687
|
-
{ email },
|
|
1688
|
-
options
|
|
1689
|
-
);
|
|
1690
|
-
return true;
|
|
1903
|
+
requestPasswordReset(email, options) {
|
|
1904
|
+
return requestPasswordReset(this.sessionContext(), email, options);
|
|
1691
1905
|
}
|
|
1692
1906
|
/**
|
|
1693
1907
|
* Confirm a password reset (PocketBase `confirmPasswordReset`).
|
|
@@ -1696,63 +1910,49 @@ var CollectionService = class _CollectionService {
|
|
|
1696
1910
|
* @param password The new password (min 8 chars server-side).
|
|
1697
1911
|
* @param passwordConfirm Must equal `password`.
|
|
1698
1912
|
*/
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
this.
|
|
1702
|
-
|
|
1913
|
+
confirmPasswordReset(passwordResetToken, password, passwordConfirm, options) {
|
|
1914
|
+
return confirmPasswordReset(
|
|
1915
|
+
this.sessionContext(),
|
|
1916
|
+
passwordResetToken,
|
|
1917
|
+
password,
|
|
1918
|
+
passwordConfirm,
|
|
1703
1919
|
options
|
|
1704
1920
|
);
|
|
1705
|
-
return true;
|
|
1706
1921
|
}
|
|
1707
1922
|
/**
|
|
1708
1923
|
* Send a verification email to the given address (PocketBase
|
|
1709
1924
|
* `requestVerification`). The backend always answers 204 to avoid
|
|
1710
1925
|
* revealing whether the address exists.
|
|
1711
1926
|
*/
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
this.collectionPath() + "/request-verification",
|
|
1715
|
-
{ email },
|
|
1716
|
-
options
|
|
1717
|
-
);
|
|
1718
|
-
return true;
|
|
1927
|
+
requestVerification(email, options) {
|
|
1928
|
+
return requestVerification(this.sessionContext(), email, options);
|
|
1719
1929
|
}
|
|
1720
1930
|
/**
|
|
1721
1931
|
* Confirm an email verification (PocketBase `confirmVerification`).
|
|
1722
1932
|
* Resolves with the verified auth record.
|
|
1723
1933
|
*/
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
this.collectionPath() + "/confirm-verification",
|
|
1727
|
-
{ token: verificationToken },
|
|
1728
|
-
options
|
|
1729
|
-
);
|
|
1730
|
-
return data?.record ?? null;
|
|
1934
|
+
confirmVerification(verificationToken, options) {
|
|
1935
|
+
return confirmVerification(this.sessionContext(), verificationToken, options);
|
|
1731
1936
|
}
|
|
1732
1937
|
/**
|
|
1733
1938
|
* Send an email-change confirmation email to a new address (PocketBase
|
|
1734
1939
|
* `requestEmailChange`). Requires an authenticated auth-collection user.
|
|
1735
1940
|
*/
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
this.collectionPath() + "/request-email-change",
|
|
1739
|
-
{ newEmail },
|
|
1740
|
-
options
|
|
1741
|
-
);
|
|
1742
|
-
return true;
|
|
1941
|
+
requestEmailChange(newEmail, options) {
|
|
1942
|
+
return requestEmailChange(this.sessionContext(), newEmail, options);
|
|
1743
1943
|
}
|
|
1744
1944
|
/**
|
|
1745
1945
|
* Confirm an email change (PocketBase `confirmEmailChange`). Requires the
|
|
1746
1946
|
* currently authenticated user's password. Resolves with the updated auth
|
|
1747
1947
|
* record.
|
|
1748
1948
|
*/
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
this.
|
|
1752
|
-
|
|
1949
|
+
confirmEmailChange(emailChangeToken, password, options) {
|
|
1950
|
+
return confirmEmailChange(
|
|
1951
|
+
this.sessionContext(),
|
|
1952
|
+
emailChangeToken,
|
|
1953
|
+
password,
|
|
1753
1954
|
options
|
|
1754
1955
|
);
|
|
1755
|
-
return data?.record ?? null;
|
|
1756
1956
|
}
|
|
1757
1957
|
/**
|
|
1758
1958
|
* Sign in with an OAuth2 provider (PocketBase `authWithOAuth2` parity).
|
|
@@ -1769,40 +1969,8 @@ var CollectionService = class _CollectionService {
|
|
|
1769
1969
|
* `window.postMessage`. Use {@link listAuthMethods} + {@link authWithOAuth2Code}
|
|
1770
1970
|
* instead.
|
|
1771
1971
|
*/
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
provider,
|
|
1775
|
-
createData,
|
|
1776
|
-
urlCallback,
|
|
1777
|
-
popup,
|
|
1778
|
-
timeoutMs = DEFAULT_OAUTH2_TIMEOUT_MS
|
|
1779
|
-
} = options;
|
|
1780
|
-
const methods = await this.listAuthMethods();
|
|
1781
|
-
const providerInfo = methods?.oauth2?.providers?.find(
|
|
1782
|
-
(p) => p.name === provider
|
|
1783
|
-
);
|
|
1784
|
-
if (!providerInfo) {
|
|
1785
|
-
throw new ApiError(
|
|
1786
|
-
`OAuth2 provider "${provider}" is not enabled for this collection.`,
|
|
1787
|
-
methods ?? {},
|
|
1788
|
-
400,
|
|
1789
|
-
false,
|
|
1790
|
-
"oauth2_unknown_provider"
|
|
1791
|
-
);
|
|
1792
|
-
}
|
|
1793
|
-
const { code } = await authorizeWithOAuth2Popup({
|
|
1794
|
-
authURL: providerInfo.authURL,
|
|
1795
|
-
expectedOrigin: oauth2RedirectOrigin(this.http.baseUrl),
|
|
1796
|
-
urlCallback,
|
|
1797
|
-
popup,
|
|
1798
|
-
timeoutMs
|
|
1799
|
-
});
|
|
1800
|
-
return this.authWithOAuth2Code({
|
|
1801
|
-
provider,
|
|
1802
|
-
code,
|
|
1803
|
-
codeVerifier: providerInfo.codeVerifier,
|
|
1804
|
-
createData
|
|
1805
|
-
});
|
|
1972
|
+
authWithOAuth2(options) {
|
|
1973
|
+
return authWithOAuth2(this.sessionContext(), options);
|
|
1806
1974
|
}
|
|
1807
1975
|
/**
|
|
1808
1976
|
* Exchange an OAuth2 authorization code directly (PocketBase
|
|
@@ -1813,36 +1981,8 @@ var CollectionService = class _CollectionService {
|
|
|
1813
1981
|
* capture the `code` from your redirect/deep link, then call this to
|
|
1814
1982
|
* finish and populate the auth store.
|
|
1815
1983
|
*/
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
const data = await this.http.post(
|
|
1819
|
-
this.collectionPath() + "/auth-with-oauth2",
|
|
1820
|
-
{
|
|
1821
|
-
provider,
|
|
1822
|
-
code,
|
|
1823
|
-
codeVerifier,
|
|
1824
|
-
redirectUrl: redirectUrl ?? this.http.baseUrl + "/oauth2-redirect",
|
|
1825
|
-
createData: createData ?? {}
|
|
1826
|
-
}
|
|
1827
|
-
);
|
|
1828
|
-
if (!data) {
|
|
1829
|
-
throw new ApiError(
|
|
1830
|
-
"OAuth2 code exchange returned no data.",
|
|
1831
|
-
{},
|
|
1832
|
-
0,
|
|
1833
|
-
false,
|
|
1834
|
-
"oauth2_exchange_failed"
|
|
1835
|
-
);
|
|
1836
|
-
}
|
|
1837
|
-
if (this.authStore) {
|
|
1838
|
-
this.authStore.setCollectionName(this.collectionName);
|
|
1839
|
-
rememberAuthResponse(
|
|
1840
|
-
this.authStore,
|
|
1841
|
-
data,
|
|
1842
|
-
data.record
|
|
1843
|
-
);
|
|
1844
|
-
}
|
|
1845
|
-
return data;
|
|
1984
|
+
authWithOAuth2Code(options) {
|
|
1985
|
+
return authWithOAuth2Code(this.sessionContext(), options);
|
|
1846
1986
|
}
|
|
1847
1987
|
};
|
|
1848
1988
|
|
|
@@ -1909,6 +2049,7 @@ var RealtimeService = class {
|
|
|
1909
2049
|
ensureConnected() {
|
|
1910
2050
|
if (this.open || this.controller || !this.url) return;
|
|
1911
2051
|
if (this.serverTopics().length === 0) return;
|
|
2052
|
+
this.closed = false;
|
|
1912
2053
|
this.doConnect();
|
|
1913
2054
|
}
|
|
1914
2055
|
connect(opts) {
|
|
@@ -2109,6 +2250,10 @@ var RealtimeService = class {
|
|
|
2109
2250
|
function encodeKey(key) {
|
|
2110
2251
|
return key.split("/").map(encodeURIComponent).join("/");
|
|
2111
2252
|
}
|
|
2253
|
+
function uploadFileName(file, fallback = "file") {
|
|
2254
|
+
if (typeof File !== "undefined" && file instanceof File) return file.name;
|
|
2255
|
+
return fallback;
|
|
2256
|
+
}
|
|
2112
2257
|
function getFileUrl(baseUrl, key) {
|
|
2113
2258
|
return baseUrl.replace(/\/+$/, "") + "/files/" + encodeKey(key);
|
|
2114
2259
|
}
|
|
@@ -2155,7 +2300,7 @@ var FilesService = class {
|
|
|
2155
2300
|
throw new Error("FormData is not available in this environment");
|
|
2156
2301
|
}
|
|
2157
2302
|
const formData = new FormData();
|
|
2158
|
-
const name = filename || (file
|
|
2303
|
+
const name = filename || uploadFileName(file);
|
|
2159
2304
|
formData.append("file", file, name);
|
|
2160
2305
|
if (meta?.collectionName)
|
|
2161
2306
|
formData.append("collection_name", meta.collectionName);
|
|
@@ -2178,7 +2323,7 @@ var FilesService = class {
|
|
|
2178
2323
|
* `upload()` when the server has no direct-upload support configured.
|
|
2179
2324
|
*/
|
|
2180
2325
|
async uploadDirect(file, opts) {
|
|
2181
|
-
const filename = opts?.filename || (file
|
|
2326
|
+
const filename = opts?.filename || uploadFileName(file);
|
|
2182
2327
|
const mime = file.type || "application/octet-stream";
|
|
2183
2328
|
const presigned = await this.presign({
|
|
2184
2329
|
filename,
|
|
@@ -2305,42 +2450,15 @@ var CollectionsService = class {
|
|
|
2305
2450
|
*/
|
|
2306
2451
|
async getFullList(options) {
|
|
2307
2452
|
if (!this.http) return [];
|
|
2308
|
-
const {
|
|
2309
|
-
const
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
...queryParams
|
|
2318
|
-
} = rest;
|
|
2319
|
-
const effectiveKey = typeof reqKey === "string" ? reqKey : `getFullList:collections:${stableStringify(rest)}`;
|
|
2320
|
-
const items = [];
|
|
2321
|
-
let page = 1;
|
|
2322
|
-
for (; ; ) {
|
|
2323
|
-
const res = await this.getList(
|
|
2324
|
-
{
|
|
2325
|
-
...queryParams,
|
|
2326
|
-
...passthroughParams ?? {},
|
|
2327
|
-
page,
|
|
2328
|
-
perPage: batch
|
|
2329
|
-
},
|
|
2330
|
-
{
|
|
2331
|
-
requestKey: effectiveKey,
|
|
2332
|
-
singleFlight: true,
|
|
2333
|
-
...fetchFn ? { fetch: fetchFn } : {},
|
|
2334
|
-
...hdrs ? { headers: hdrs } : {},
|
|
2335
|
-
...sig ? { signal: sig } : {}
|
|
2336
|
-
}
|
|
2337
|
-
);
|
|
2338
|
-
if (!res || !res.items || res.items.length === 0) break;
|
|
2339
|
-
items.push(...res.items);
|
|
2340
|
-
if (page >= (res.totalPages ?? page)) break;
|
|
2341
|
-
page += 1;
|
|
2342
|
-
}
|
|
2343
|
-
return items;
|
|
2453
|
+
const { query, transport, batch } = splitListOptions(options);
|
|
2454
|
+
const requestKey = fullListKey("collections", query, transport.requestKey);
|
|
2455
|
+
return paginateAll(
|
|
2456
|
+
batch,
|
|
2457
|
+
(page, perPage) => this.getList(
|
|
2458
|
+
{ ...query, page, perPage },
|
|
2459
|
+
{ ...transport, requestKey, singleFlight: true }
|
|
2460
|
+
)
|
|
2461
|
+
);
|
|
2344
2462
|
}
|
|
2345
2463
|
/**
|
|
2346
2464
|
* Get a single collection by ID or name (admin).
|
|
@@ -2381,11 +2499,9 @@ var CollectionsService = class {
|
|
|
2381
2499
|
* @param options Optional request options.
|
|
2382
2500
|
*/
|
|
2383
2501
|
async delete(id, options) {
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
);
|
|
2388
|
-
return res == null ? false : true;
|
|
2502
|
+
if (!this.http) return false;
|
|
2503
|
+
await this.http.delete("/collections/" + encodeURIComponent(id), options);
|
|
2504
|
+
return true;
|
|
2389
2505
|
}
|
|
2390
2506
|
/**
|
|
2391
2507
|
* Subscribe to collection registry changes.
|
|
@@ -2425,6 +2541,147 @@ function normalizeAction2(event) {
|
|
|
2425
2541
|
return "update";
|
|
2426
2542
|
}
|
|
2427
2543
|
|
|
2544
|
+
// src/mail.ts
|
|
2545
|
+
function templatePath(name) {
|
|
2546
|
+
return "/mail/templates/" + encodeURIComponent(name.trim());
|
|
2547
|
+
}
|
|
2548
|
+
var MailService = class {
|
|
2549
|
+
constructor(http) {
|
|
2550
|
+
this.http = http;
|
|
2551
|
+
}
|
|
2552
|
+
/**
|
|
2553
|
+
* Queue one message for delivery.
|
|
2554
|
+
*
|
|
2555
|
+
* Either a `template` or a `subject` with a body. The type requires one of
|
|
2556
|
+
* them — a message with neither does not compile — while the server refuses a
|
|
2557
|
+
* message that carries both, and a template with a raw body.
|
|
2558
|
+
*
|
|
2559
|
+
* The address is shape-checked server-side; the SMTP server remains the
|
|
2560
|
+
* authority on whether it exists.
|
|
2561
|
+
*
|
|
2562
|
+
* @param message The template or raw message to send.
|
|
2563
|
+
* @param options Optional request options (signal, custom fetch).
|
|
2564
|
+
* @returns The queued job (`{ queued: true, id }`).
|
|
2565
|
+
*
|
|
2566
|
+
* @example
|
|
2567
|
+
* ```ts
|
|
2568
|
+
* // A template
|
|
2569
|
+
* await client.mail.send({ to: 'ada@example.com', template: 'receipt', assigns: { total: 42 } });
|
|
2570
|
+
*
|
|
2571
|
+
* // A raw message
|
|
2572
|
+
* await client.mail.send({ to: 'ada@example.com', subject: 'Hello', text: 'Hi there' });
|
|
2573
|
+
* ```
|
|
2574
|
+
*/
|
|
2575
|
+
async send(message, options) {
|
|
2576
|
+
const body = "template" in message ? {
|
|
2577
|
+
to: message.to,
|
|
2578
|
+
toName: message.toName,
|
|
2579
|
+
template: message.template,
|
|
2580
|
+
assigns: message.assigns
|
|
2581
|
+
} : {
|
|
2582
|
+
to: message.to,
|
|
2583
|
+
toName: message.toName,
|
|
2584
|
+
subject: message.subject,
|
|
2585
|
+
html: message.html,
|
|
2586
|
+
text: message.text
|
|
2587
|
+
};
|
|
2588
|
+
const data = await this.http.request("POST", "/mail", body, options);
|
|
2589
|
+
return data ?? { queued: false, id: "" };
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* List every template the server can send, with the body each part
|
|
2593
|
+
* currently resolves to and the layer it came from (superuser).
|
|
2594
|
+
*
|
|
2595
|
+
* Unpaginated: there are as many templates as the deployment defined, not
|
|
2596
|
+
* as many as it has sent.
|
|
2597
|
+
*/
|
|
2598
|
+
async listTemplates(options) {
|
|
2599
|
+
const data = await this.http.request(
|
|
2600
|
+
"GET",
|
|
2601
|
+
"/mail/templates",
|
|
2602
|
+
void 0,
|
|
2603
|
+
options
|
|
2604
|
+
);
|
|
2605
|
+
return data?.items ?? [];
|
|
2606
|
+
}
|
|
2607
|
+
/** Fetch one template by name (superuser). */
|
|
2608
|
+
async getTemplate(name, options) {
|
|
2609
|
+
const data = await this.http.request(
|
|
2610
|
+
"GET",
|
|
2611
|
+
templatePath(name),
|
|
2612
|
+
void 0,
|
|
2613
|
+
options
|
|
2614
|
+
);
|
|
2615
|
+
return data;
|
|
2616
|
+
}
|
|
2617
|
+
/**
|
|
2618
|
+
* Save all three parts of a template, replacing whatever was stored for them.
|
|
2619
|
+
*
|
|
2620
|
+
* All three at once rather than one at a time: a template is only allowed to
|
|
2621
|
+
* exist once it is complete, so a part-at-a-time write could leave a subject
|
|
2622
|
+
* saved against a body that does not parse. The server parses the bodies
|
|
2623
|
+
* before storing them, so a syntax error is a rejected save rather than a
|
|
2624
|
+
* queued email that fails where nobody is watching (superuser).
|
|
2625
|
+
*/
|
|
2626
|
+
async saveTemplate(name, parts, options) {
|
|
2627
|
+
const data = await this.http.put(
|
|
2628
|
+
templatePath(name),
|
|
2629
|
+
{ subject: parts.subject, html: parts.html, text: parts.text },
|
|
2630
|
+
options
|
|
2631
|
+
);
|
|
2632
|
+
return data;
|
|
2633
|
+
}
|
|
2634
|
+
/**
|
|
2635
|
+
* Remove the stored bodies, so the template falls back to its file or its
|
|
2636
|
+
* built-in (superuser).
|
|
2637
|
+
*
|
|
2638
|
+
* Deleting one that was never saved succeeds with `deleted: 0` — it is
|
|
2639
|
+
* already at its default — but an unknown *name* is a 404, so a typo cannot
|
|
2640
|
+
* look like a reset that worked.
|
|
2641
|
+
*/
|
|
2642
|
+
async deleteTemplate(name, options) {
|
|
2643
|
+
const data = await this.http.request(
|
|
2644
|
+
"DELETE",
|
|
2645
|
+
templatePath(name),
|
|
2646
|
+
void 0,
|
|
2647
|
+
options
|
|
2648
|
+
);
|
|
2649
|
+
return data ?? { name: name.trim(), deleted: 0 };
|
|
2650
|
+
}
|
|
2651
|
+
/**
|
|
2652
|
+
* Render a template the way a send would, without storing or sending
|
|
2653
|
+
* anything (superuser).
|
|
2654
|
+
*
|
|
2655
|
+
* Any of `subject`/`html`/`text` given in `draft` is rendered in place of the
|
|
2656
|
+
* stored part, which is what makes this useful while a body is still being
|
|
2657
|
+
* written. A draft over a name the server does not know is previewed as the
|
|
2658
|
+
* new template it is being written as, so such a draft has to arrive
|
|
2659
|
+
* complete (all three parts).
|
|
2660
|
+
*
|
|
2661
|
+
* Previews share one auto-cancellation key (`POST /mail/templates/<name>/preview`),
|
|
2662
|
+
* so a newer preview aborts an older one: the older promise rejects with an
|
|
2663
|
+
* `ApiError` whose `isAbort` is `true`. That is what an editor wants when a
|
|
2664
|
+
* keystroke supersedes the last render, but pass `{ requestKey: null }` to
|
|
2665
|
+
* keep every preview if you would rather have them all.
|
|
2666
|
+
*/
|
|
2667
|
+
async preview(name, draft, options) {
|
|
2668
|
+
const body = {
|
|
2669
|
+
assigns: draft?.assigns,
|
|
2670
|
+
toName: draft?.toName,
|
|
2671
|
+
subject: draft?.subject,
|
|
2672
|
+
html: draft?.html,
|
|
2673
|
+
text: draft?.text
|
|
2674
|
+
};
|
|
2675
|
+
const data = await this.http.request(
|
|
2676
|
+
"POST",
|
|
2677
|
+
templatePath(name) + "/preview",
|
|
2678
|
+
body,
|
|
2679
|
+
options
|
|
2680
|
+
);
|
|
2681
|
+
return data;
|
|
2682
|
+
}
|
|
2683
|
+
};
|
|
2684
|
+
|
|
2428
2685
|
// src/lazypock.ts
|
|
2429
2686
|
function createConnectionId() {
|
|
2430
2687
|
try {
|
|
@@ -2456,6 +2713,7 @@ var LazypockClient = class {
|
|
|
2456
2713
|
this.authStore.onChange(() => this.realtime.refresh());
|
|
2457
2714
|
this.authReady.then(() => this.realtime.refresh());
|
|
2458
2715
|
this.files = new FilesService(this.http);
|
|
2716
|
+
this.mail = new MailService(this.http);
|
|
2459
2717
|
this.collections = new CollectionsService(this.http, this.realtime);
|
|
2460
2718
|
if (options.types?.schemas) {
|
|
2461
2719
|
this.schemaByName = new Map(
|
|
@@ -2578,36 +2836,25 @@ var LazypockClient = class {
|
|
|
2578
2836
|
* @param collection Optional auth collection name.
|
|
2579
2837
|
*/
|
|
2580
2838
|
async login(email, password, collection) {
|
|
2581
|
-
let data;
|
|
2582
2839
|
if (collection) {
|
|
2583
|
-
|
|
2840
|
+
return this.collection(collection).authWithPassword(email, password);
|
|
2841
|
+
}
|
|
2842
|
+
let data = null;
|
|
2843
|
+
try {
|
|
2844
|
+
data = await this.http.post("/collections/_superusers/auth-with-password", {
|
|
2584
2845
|
identity: email,
|
|
2585
2846
|
password
|
|
2586
2847
|
});
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
data = await this.http.post("/collections/_superusers/auth-with-password", {
|
|
2598
|
-
identity: email,
|
|
2599
|
-
password
|
|
2600
|
-
});
|
|
2601
|
-
} catch {
|
|
2602
|
-
data = null;
|
|
2603
|
-
}
|
|
2604
|
-
if (!data) {
|
|
2605
|
-
data = await this.http.post("/superusers/login", { email, password });
|
|
2606
|
-
}
|
|
2607
|
-
if (data) {
|
|
2608
|
-
this.authStore.setCollectionName(null);
|
|
2609
|
-
rememberAuthResponse(this.authStore, data, null);
|
|
2610
|
-
}
|
|
2848
|
+
} catch (err) {
|
|
2849
|
+
if (!(err instanceof ApiError) || err.status !== 404) throw err;
|
|
2850
|
+
data = null;
|
|
2851
|
+
}
|
|
2852
|
+
if (!data) {
|
|
2853
|
+
data = await this.http.post("/superusers/login", { email, password });
|
|
2854
|
+
}
|
|
2855
|
+
if (data) {
|
|
2856
|
+
this.authStore.setCollectionName(null);
|
|
2857
|
+
rememberAuthResponse(this.authStore, data, null);
|
|
2611
2858
|
}
|
|
2612
2859
|
return data;
|
|
2613
2860
|
}
|
|
@@ -2619,7 +2866,7 @@ var LazypockClient = class {
|
|
|
2619
2866
|
async me(options) {
|
|
2620
2867
|
const data = await this.http.get("/me", options);
|
|
2621
2868
|
if (data) {
|
|
2622
|
-
this.authStore.
|
|
2869
|
+
this.authStore.setModel(data);
|
|
2623
2870
|
}
|
|
2624
2871
|
return data;
|
|
2625
2872
|
}
|
|
@@ -2632,17 +2879,8 @@ var LazypockClient = class {
|
|
|
2632
2879
|
* @param password Password.
|
|
2633
2880
|
* @param options Optional request options.
|
|
2634
2881
|
*/
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
"/collections/" + encodeURIComponent(collection) + "/auth-with-password",
|
|
2638
|
-
{ identity, password },
|
|
2639
|
-
options
|
|
2640
|
-
);
|
|
2641
|
-
if (data) {
|
|
2642
|
-
this.authStore.setCollectionName(collection);
|
|
2643
|
-
rememberAuthResponse(this.authStore, data, data.record);
|
|
2644
|
-
}
|
|
2645
|
-
return data;
|
|
2882
|
+
authWithPassword(collection, identity, password, options) {
|
|
2883
|
+
return this.collection(collection).authWithPassword(identity, password, options);
|
|
2646
2884
|
}
|
|
2647
2885
|
/**
|
|
2648
2886
|
* Refresh an auth collection token.
|
|
@@ -2651,17 +2889,8 @@ var LazypockClient = class {
|
|
|
2651
2889
|
* @param collection The auth collection name.
|
|
2652
2890
|
* @param options Optional request options.
|
|
2653
2891
|
*/
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
"/collections/" + encodeURIComponent(collection) + "/auth-refresh",
|
|
2657
|
-
{ refreshToken: this.authStore.refreshToken },
|
|
2658
|
-
options
|
|
2659
|
-
);
|
|
2660
|
-
if (data) {
|
|
2661
|
-
this.authStore.setCollectionName(collection);
|
|
2662
|
-
rememberAuthResponse(this.authStore, data, data.record);
|
|
2663
|
-
}
|
|
2664
|
-
return data;
|
|
2892
|
+
authRefresh(collection, options) {
|
|
2893
|
+
return this.collection(collection).authRefresh(options);
|
|
2665
2894
|
}
|
|
2666
2895
|
/** Clear the current auth state and remove persisted tokens. */
|
|
2667
2896
|
logout() {
|
|
@@ -2693,6 +2922,7 @@ export {
|
|
|
2693
2922
|
FilterExpr,
|
|
2694
2923
|
HttpClient,
|
|
2695
2924
|
LazypockClient,
|
|
2925
|
+
MailService,
|
|
2696
2926
|
RealtimeService,
|
|
2697
2927
|
TypedClient,
|
|
2698
2928
|
collectionTypeName,
|