@ztimson/momentum 0.44.0 → 0.45.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/dist/actions.d.ts +2 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/ai.d.ts.map +1 -1
- package/dist/analytics.d.ts.map +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/core.d.ts +5 -5
- package/dist/core.d.ts.map +1 -1
- package/dist/data.d.ts.map +1 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/groups.d.ts +4 -2
- package/dist/groups.d.ts.map +1 -1
- package/dist/index.cjs +294 -210
- package/dist/index.mjs +295 -211
- package/dist/logger.d.ts.map +1 -1
- package/dist/momentum.d.ts.map +1 -1
- package/dist/pathed-events.d.ts +70 -48
- package/dist/pathed-events.d.ts.map +1 -1
- package/dist/payments.d.ts.map +1 -1
- package/dist/pdf.d.ts.map +1 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/static.d.ts.map +1 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/users.d.ts +2 -2
- package/dist/users.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -740,94 +740,177 @@ L.MemoryStorage = At;
|
|
|
740
740
|
};
|
|
741
741
|
Object.defineProperty(r, "__esModule", { value: true }), e(S, r), e(L, r);
|
|
742
742
|
})(xt);
|
|
743
|
-
function
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
743
|
+
function PE(str, ...args) {
|
|
744
|
+
const combined = [];
|
|
745
|
+
for (let i = 0; i < str.length || i < args.length; i++) {
|
|
746
|
+
if (str[i]) combined.push(str[i]);
|
|
747
|
+
if (args[i]) combined.push(args[i]);
|
|
748
|
+
}
|
|
749
|
+
return new PathEvent(combined.join(""));
|
|
750
|
+
}
|
|
751
|
+
function PES(str, ...args) {
|
|
752
|
+
const combined = [];
|
|
753
|
+
for (let i = 0; i < str.length || i < args.length; i++) {
|
|
754
|
+
if (str[i]) combined.push(str[i]);
|
|
755
|
+
if (args[i]) combined.push(args[i]);
|
|
756
|
+
}
|
|
757
|
+
let colon = false;
|
|
758
|
+
const [paths, methods] = combined.filter((p2) => p2 != null).flatMap((p2) => {
|
|
759
|
+
var _a;
|
|
760
|
+
return (_a = p2 == null ? void 0 : p2.toString()) == null ? void 0 : _a.split(":");
|
|
748
761
|
}).reduce((acc, p2) => {
|
|
749
|
-
if (p2.
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
if (p2.all || p2.update) acc.update = true;
|
|
756
|
-
if (p2.all || p2.delete) acc.delete = true;
|
|
757
|
-
if (p2.all || p2.execute) acc.execute = true;
|
|
758
|
-
acc.methods = [...acc.methods, ...p2.methods];
|
|
762
|
+
if (p2.endsWith(":")) {
|
|
763
|
+
colon = true;
|
|
764
|
+
if (p2.length > 1) acc[0].push(p2.slice(0, -1));
|
|
765
|
+
} else {
|
|
766
|
+
acc[colon ? 1 : 0].push(p2);
|
|
767
|
+
}
|
|
759
768
|
return acc;
|
|
760
|
-
},
|
|
761
|
-
|
|
762
|
-
if (combined.none) combined.methods = ["n"];
|
|
763
|
-
combined.methods = new B(combined.methods);
|
|
764
|
-
combined.full = pathedEvent(combined.fullPath, ...combined.methods);
|
|
765
|
-
return combined;
|
|
766
|
-
}
|
|
767
|
-
function hasPath(target, ...anyPath) {
|
|
768
|
-
const parsedRequired = anyPath.map(parsePathedEvent);
|
|
769
|
-
const parsedTarget = ft(target).map(parsePathedEvent);
|
|
770
|
-
return !!parsedRequired.find((r) => {
|
|
771
|
-
if (r.all) return true;
|
|
772
|
-
const filtered = parsedTarget.filter((p2) => r.fullPath.startsWith(p2.fullPath));
|
|
773
|
-
if (!filtered.length) return false;
|
|
774
|
-
const combined = combinePathedEvents(...filtered);
|
|
775
|
-
return !combined.none && (combined.all || new B(combined.methods).intersection(new B(r.methods)).length);
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
function hasPathFatal(target, ...paths) {
|
|
779
|
-
if (!hasPath(target, ...paths)) throw new Error(`Missing permission: ${paths.join(", ")}`);
|
|
780
|
-
}
|
|
781
|
-
function pathedEvent(path, ...methods) {
|
|
782
|
-
var _a;
|
|
783
|
-
let p2 = ft(path).map((p22) => p22 == null ? void 0 : p22.toString()).filter((p22) => !!p22).map((p22) => p22 == null ? void 0 : p22.replaceAll(/(^\/|\/$)/g, "")).join("/");
|
|
784
|
-
if (methods.length) p2 += `:${methods.join("")}`;
|
|
785
|
-
return (_a = p2 == null ? void 0 : p2.replaceAll("//", "/")) == null ? void 0 : _a.trim();
|
|
769
|
+
}, [[], []]);
|
|
770
|
+
return PathEvent.toString(paths, methods);
|
|
786
771
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
772
|
+
class PathEvent {
|
|
773
|
+
constructor(pathedEvent) {
|
|
774
|
+
/** First directory in path */
|
|
775
|
+
__publicField(this, "module");
|
|
776
|
+
/** Entire path, including the module & name */
|
|
777
|
+
__publicField(this, "fullPath");
|
|
778
|
+
/** Path including the name, excluding the module */
|
|
779
|
+
__publicField(this, "path");
|
|
780
|
+
/** Last sagment of path */
|
|
781
|
+
__publicField(this, "name");
|
|
782
|
+
/** List of methods */
|
|
783
|
+
__publicField(this, "methods");
|
|
784
|
+
/** All/Wildcard specified */
|
|
785
|
+
__publicField(this, "all");
|
|
786
|
+
/** None specified */
|
|
787
|
+
__publicField(this, "none");
|
|
788
|
+
/** Create method specified */
|
|
789
|
+
__publicField(this, "create");
|
|
790
|
+
/** Read method specified */
|
|
791
|
+
__publicField(this, "read");
|
|
792
|
+
/** Update method specified */
|
|
793
|
+
__publicField(this, "update");
|
|
794
|
+
/** Delete method specified */
|
|
795
|
+
__publicField(this, "delete");
|
|
796
|
+
var _a;
|
|
797
|
+
if (typeof pathedEvent == "object") return Object.assign(this, pathedEvent);
|
|
798
|
+
let [p2, scope, method] = pathedEvent.split(":");
|
|
799
|
+
if (!method) method = scope || "*";
|
|
800
|
+
if (p2 == "*" || !p2 && method == "*") {
|
|
801
|
+
p2 = "";
|
|
802
|
+
method = "*";
|
|
803
|
+
}
|
|
804
|
+
let temp = p2.split("/").filter((p22) => !!p22);
|
|
805
|
+
this.module = ((_a = temp.splice(0, 1)[0]) == null ? void 0 : _a.toLowerCase()) || "";
|
|
806
|
+
this.fullPath = p2;
|
|
807
|
+
this.path = temp.join("/");
|
|
808
|
+
this.name = temp.pop() || "";
|
|
809
|
+
this.methods = method.split("");
|
|
810
|
+
this.all = method == null ? void 0 : method.includes("*");
|
|
811
|
+
this.none = method == null ? void 0 : method.includes("n");
|
|
812
|
+
this.create = !(method == null ? void 0 : method.includes("n")) && ((method == null ? void 0 : method.includes("*")) || (method == null ? void 0 : method.includes("w")) || (method == null ? void 0 : method.includes("c")));
|
|
813
|
+
this.read = !(method == null ? void 0 : method.includes("n")) && ((method == null ? void 0 : method.includes("*")) || (method == null ? void 0 : method.includes("r")));
|
|
814
|
+
this.update = !(method == null ? void 0 : method.includes("n")) && ((method == null ? void 0 : method.includes("*")) || (method == null ? void 0 : method.includes("w")) || (method == null ? void 0 : method.includes("u")));
|
|
815
|
+
this.delete = !(method == null ? void 0 : method.includes("n")) && ((method == null ? void 0 : method.includes("*")) || (method == null ? void 0 : method.includes("w")) || (method == null ? void 0 : method.includes("d")));
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Combine multiple pathed events into one parsed object. Longest path takes precedent, but all subsequent methods are
|
|
819
|
+
* combined until a "none" is reached
|
|
820
|
+
*
|
|
821
|
+
* @param {string | PathEvent} paths PathedEvents as strings or pre-parsed
|
|
822
|
+
* @return {PathEvent} Final combined permission
|
|
823
|
+
*/
|
|
824
|
+
static combine(paths) {
|
|
825
|
+
let hitNone = false;
|
|
826
|
+
const combined = paths.map((p2) => new PathEvent(p2)).toSorted((p1, p2) => {
|
|
827
|
+
const l1 = p1.fullPath.length, l2 = p2.fullPath.length;
|
|
828
|
+
return l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
|
|
829
|
+
}).reduce((acc, p2) => {
|
|
830
|
+
if (p2.none) hitNone = true;
|
|
831
|
+
if (!acc) return p2;
|
|
832
|
+
if (hitNone) return acc;
|
|
833
|
+
if (p2.all) acc.all = true;
|
|
834
|
+
if (p2.all || p2.create) acc.create = true;
|
|
835
|
+
if (p2.all || p2.read) acc.read = true;
|
|
836
|
+
if (p2.all || p2.update) acc.update = true;
|
|
837
|
+
if (p2.all || p2.delete) acc.delete = true;
|
|
838
|
+
acc.methods = [...acc.methods, ...p2.methods];
|
|
839
|
+
return acc;
|
|
840
|
+
}, null);
|
|
841
|
+
if (combined.all) combined.methods = ["*"];
|
|
842
|
+
if (combined.none) combined.methods = ["n"];
|
|
843
|
+
combined.methods = new B(combined.methods);
|
|
844
|
+
combined.raw = PES`${combined.fullPath}:${combined.methods}`;
|
|
845
|
+
return combined;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Squash 2 sets of paths & return true if any overlap is found
|
|
849
|
+
*
|
|
850
|
+
* @param {string | PathEvent | (string | PathEvent)[]} target Array of PathedEvents as strings or pre-parsed
|
|
851
|
+
* @param has
|
|
852
|
+
* @return {boolean} Whether there is any overlap
|
|
853
|
+
*/
|
|
854
|
+
static has(target, ...has) {
|
|
855
|
+
const parsedRequired = ft(has).map((pe) => new PathEvent(pe));
|
|
856
|
+
const parsedTarget = ft(target).map((pe) => new PathEvent(pe));
|
|
857
|
+
return !!parsedRequired.find((r) => {
|
|
858
|
+
if (!r.fullPath && r.all) return true;
|
|
859
|
+
const filtered = parsedTarget.filter((p2) => r.fullPath.startsWith(p2.fullPath));
|
|
860
|
+
if (!filtered.length) return false;
|
|
861
|
+
const combined = PathEvent.combine(filtered);
|
|
862
|
+
return !combined.none && (combined.all || new B(combined.methods).intersection(new B(r.methods)).length);
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Same as `has` but raises an error if there is no overlap
|
|
867
|
+
*
|
|
868
|
+
* @param {string | string[]} target Array of PathedEvents as strings or pre-parsed
|
|
869
|
+
* @param has
|
|
870
|
+
*/
|
|
871
|
+
static hasFatal(target, ...has) {
|
|
872
|
+
if (!PathEvent.has(target, ...has)) throw new Error(`Missing permission: ${ft(has).join(", ")}`);
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Create pathed event string from its components
|
|
876
|
+
*
|
|
877
|
+
* @param {string | string[]} path Event path
|
|
878
|
+
* @param {Method} methods Event method
|
|
879
|
+
* @return {string} String representation of PathedEvent
|
|
880
|
+
*/
|
|
881
|
+
static toString(path, methods) {
|
|
882
|
+
let p2 = ft(path).filter((p22) => p22 != null).join("/");
|
|
883
|
+
if (methods.length) p2 += `:${ft(methods).map((m) => m.toLowerCase()).join("")}`;
|
|
884
|
+
return p2 == null ? void 0 : p2.trim().replaceAll(/\/{2,}/g, "/").replaceAll(/(^\/|\/$)/g, "");
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Create pathed event string from its components
|
|
888
|
+
*
|
|
889
|
+
* @return {string} String representation of PathedEvent
|
|
890
|
+
*/
|
|
891
|
+
toString() {
|
|
892
|
+
return PathEvent.toString(this.fullPath, this.methods);
|
|
893
|
+
}
|
|
811
894
|
}
|
|
812
895
|
class PathedEventEmitter {
|
|
813
896
|
constructor() {
|
|
814
897
|
__publicField(this, "listeners", []);
|
|
815
898
|
}
|
|
816
899
|
emit(event, ...args) {
|
|
817
|
-
const parsed =
|
|
818
|
-
this.listeners.filter((l) =>
|
|
900
|
+
const parsed = new PathEvent(event);
|
|
901
|
+
this.listeners.filter((l) => PathEvent.has(l[0], event)).forEach((l) => l[1](parsed, ...args));
|
|
819
902
|
}
|
|
820
903
|
off(listener) {
|
|
821
904
|
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
822
905
|
}
|
|
823
906
|
on(event, listener) {
|
|
824
|
-
ft(event).forEach((e) => this.listeners.push([
|
|
907
|
+
ft(event).forEach((e) => this.listeners.push([new PathEvent(e), listener]));
|
|
825
908
|
return () => this.off(listener);
|
|
826
909
|
}
|
|
827
910
|
once(event, listener) {
|
|
828
911
|
return new Promise((res) => {
|
|
829
912
|
const unsubscribe = this.on(event, (event2, ...args) => {
|
|
830
|
-
res(args);
|
|
913
|
+
res(args.length < 2 ? args[0] : args);
|
|
831
914
|
if (listener) listener(event2, ...args);
|
|
832
915
|
unsubscribe();
|
|
833
916
|
});
|
|
@@ -859,11 +942,11 @@ class Api extends F {
|
|
|
859
942
|
if (token == this._token) return;
|
|
860
943
|
this._token = token;
|
|
861
944
|
this.headers["Authorization"] = token ? `Bearer ${token}` : null;
|
|
862
|
-
this.emit(
|
|
945
|
+
this.emit(PES`api/token:${token ? "u" : "d"}`, token);
|
|
863
946
|
}
|
|
864
947
|
healthcheck() {
|
|
865
948
|
return this.request({ url: "/api/healthcheck" }).then((resp) => {
|
|
866
|
-
this.emit(
|
|
949
|
+
this.emit(PES`api/healthcheck:r`, resp);
|
|
867
950
|
return resp;
|
|
868
951
|
});
|
|
869
952
|
}
|
|
@@ -871,15 +954,15 @@ class Api extends F {
|
|
|
871
954
|
const key = Lt(options);
|
|
872
955
|
const method = options.method == "GET" ? "r" : options.method == "POST" ? "c" : options.method == "DELETE" ? "d" : "u";
|
|
873
956
|
if (this.pending[key] != null) return this.pending[key];
|
|
874
|
-
this.pending[key] = super.request(options).then((
|
|
875
|
-
this.emit(
|
|
876
|
-
return
|
|
957
|
+
this.pending[key] = super.request(options).then((response) => {
|
|
958
|
+
this.emit(PES`api/response:${method}`, { request: options, response });
|
|
959
|
+
return response.data;
|
|
877
960
|
}).catch((err) => {
|
|
878
961
|
const e = (err == null ? void 0 : err.data) || err;
|
|
879
|
-
this.emit(
|
|
962
|
+
this.emit(PES`api/error:${method}`, { request: options, error: e });
|
|
880
963
|
throw e;
|
|
881
964
|
}).finally(() => delete this.pending[key]);
|
|
882
|
-
this.emit(
|
|
965
|
+
this.emit(PES`api/request:${method}`, { request: options, response: this.pending[key] });
|
|
883
966
|
return this.pending[key];
|
|
884
967
|
}
|
|
885
968
|
}
|
|
@@ -902,15 +985,15 @@ class Actions extends PathedEventEmitter {
|
|
|
902
985
|
}
|
|
903
986
|
delete(id) {
|
|
904
987
|
if (!id) throw new Error("Cannot delete action, missing ID");
|
|
905
|
-
return this.api.request({ url: `/api/` +
|
|
988
|
+
return this.api.request({ url: `/api/` + PES`actions/${id}`, method: "DELETE" }).then(() => {
|
|
906
989
|
this.cache.delete(id);
|
|
907
|
-
this.emit(
|
|
990
|
+
this.emit(PES`actions/${id}:d`, id);
|
|
908
991
|
});
|
|
909
992
|
}
|
|
910
993
|
all() {
|
|
911
|
-
return this.api.request({ url: `/api
|
|
994
|
+
return this.api.request({ url: `/api/` + PES`actions` }).then((resp) => {
|
|
912
995
|
this.cache.addAll(resp);
|
|
913
|
-
this.emit(
|
|
996
|
+
this.emit(PES`actions:r`, resp || []);
|
|
914
997
|
return resp;
|
|
915
998
|
});
|
|
916
999
|
}
|
|
@@ -918,35 +1001,29 @@ class Actions extends PathedEventEmitter {
|
|
|
918
1001
|
if (!id) throw new Error("Cannot read action, missing ID");
|
|
919
1002
|
const cached = this.cache.get(id);
|
|
920
1003
|
if (!reload && cached) return Promise.resolve(cached);
|
|
921
|
-
return this.api.request({ url: `/api/` +
|
|
1004
|
+
return this.api.request({ url: `/api/` + PES`actions/${id}` }).then((action) => {
|
|
922
1005
|
if (action) this.cache.add(action);
|
|
923
|
-
this.emit(
|
|
1006
|
+
this.emit(PES`actions/${id}:r`, action);
|
|
924
1007
|
return action;
|
|
925
1008
|
});
|
|
926
1009
|
}
|
|
927
1010
|
run(path, opts = {}) {
|
|
928
1011
|
if (!path) throw new Error("Cannot run action, missing path");
|
|
929
|
-
return this.api.request({ url: `/api/` +
|
|
930
|
-
this.emit(pathedEvent(["actions/run", path], "x"), resp);
|
|
931
|
-
return resp;
|
|
932
|
-
});
|
|
1012
|
+
return this.api.request({ url: `/api/` + PES`actions/run/${path}`, ...opts });
|
|
933
1013
|
}
|
|
934
1014
|
runById(action, opts = {}) {
|
|
935
1015
|
const id = typeof action == "string" ? action : action == null ? void 0 : action._id;
|
|
936
1016
|
if (!id) throw new Error("Cannot run action, missing ID");
|
|
937
|
-
return this.api.request({ url: "/api/" +
|
|
938
|
-
this.emit(pathedEvent(["actions/run-by-id", id], "x"), resp);
|
|
939
|
-
return resp;
|
|
940
|
-
});
|
|
1017
|
+
return this.api.request({ url: "/api/" + PES`actions/run-by-id/${id}`, method: "POST", ...opts });
|
|
941
1018
|
}
|
|
942
1019
|
update(action) {
|
|
943
1020
|
return this.api.request({
|
|
944
|
-
url: `/api/` +
|
|
1021
|
+
url: `/api/` + PES`actions/${action._id}`,
|
|
945
1022
|
method: "POST",
|
|
946
1023
|
body: action
|
|
947
1024
|
}).then((action2) => {
|
|
948
1025
|
if (action2) this.cache.add(action2);
|
|
949
|
-
this.emit(
|
|
1026
|
+
this.emit(PES`actions/${action2._id}:u`, action2);
|
|
950
1027
|
return action2;
|
|
951
1028
|
});
|
|
952
1029
|
}
|
|
@@ -959,16 +1036,16 @@ class Ai extends PathedEventEmitter {
|
|
|
959
1036
|
}
|
|
960
1037
|
ask(question, context) {
|
|
961
1038
|
if (!question) throw new Error("Cannot ask AI, missing question");
|
|
962
|
-
return this.api.request({ url: `/api
|
|
1039
|
+
return this.api.request({ url: `/api/` + PES`ai`, method: "POST", body: {
|
|
963
1040
|
question,
|
|
964
1041
|
context
|
|
965
|
-
} }).then((
|
|
966
|
-
this.emit(
|
|
967
|
-
return
|
|
1042
|
+
} }).then((response) => {
|
|
1043
|
+
this.emit(PES`ai:c`, { question, context, response });
|
|
1044
|
+
return response;
|
|
968
1045
|
});
|
|
969
1046
|
}
|
|
970
1047
|
clear() {
|
|
971
|
-
return this.api.request({ url: "/api/
|
|
1048
|
+
return this.api.request({ url: "/api/" + PES`ai`, method: "DELETE" }).then(() => this.emit(PES`ai:d`, this.api.token));
|
|
972
1049
|
}
|
|
973
1050
|
}
|
|
974
1051
|
class Analytics extends PathedEventEmitter {
|
|
@@ -979,8 +1056,8 @@ class Analytics extends PathedEventEmitter {
|
|
|
979
1056
|
}
|
|
980
1057
|
ipTrace(ip) {
|
|
981
1058
|
if (!ip) throw new Error("Cannot trace, missing IP");
|
|
982
|
-
return this.api.request({ url: `/api
|
|
983
|
-
this.emit(
|
|
1059
|
+
return this.api.request({ url: `/api/` + PES`analytics/trace/${ip}` }).then((resp) => {
|
|
1060
|
+
this.emit(PES`analytics/trace/${ip}:r`, resp);
|
|
984
1061
|
return resp;
|
|
985
1062
|
});
|
|
986
1063
|
}
|
|
@@ -1026,11 +1103,12 @@ class Auth extends PathedEventEmitter {
|
|
|
1026
1103
|
"/api/auth/totp"
|
|
1027
1104
|
];
|
|
1028
1105
|
if (resp.status == 401 && !blacklist.find((url) => resp.url.includes(url)))
|
|
1029
|
-
this.emit(
|
|
1106
|
+
this.emit(PES`auth/session-expired:d`, this.api.token);
|
|
1030
1107
|
next();
|
|
1031
1108
|
});
|
|
1032
1109
|
this.api.on("api/token", (event, token) => {
|
|
1033
1110
|
var _a;
|
|
1111
|
+
console.log("loading user...");
|
|
1034
1112
|
if ((_a = this.opts) == null ? void 0 : _a.persist) {
|
|
1035
1113
|
if (token) localStorage.setItem(this.storageKey, token);
|
|
1036
1114
|
else localStorage.removeItem(this.storageKey);
|
|
@@ -1053,13 +1131,13 @@ class Auth extends PathedEventEmitter {
|
|
|
1053
1131
|
set user(user) {
|
|
1054
1132
|
if (!A(this.user, user)) {
|
|
1055
1133
|
this._user = user ? user : null;
|
|
1056
|
-
this.emit(
|
|
1134
|
+
this.emit(PES`auth/user:u`, this._user);
|
|
1057
1135
|
}
|
|
1058
1136
|
}
|
|
1059
1137
|
knownHost(host = location.origin) {
|
|
1060
1138
|
if (host.startsWith("/")) return Promise.resolve();
|
|
1061
|
-
return this.api.request({ url: `/api/auth/known-host?host=${encodeURI(new URL(host).origin)}` }).then(() => this.emit(
|
|
1062
|
-
this.emit(
|
|
1139
|
+
return this.api.request({ url: `/api/auth/known-host?host=${encodeURI(new URL(host).origin)}` }).then(() => this.emit(PES`auth/known-host/${host}:r`, true)).catch((err) => {
|
|
1140
|
+
this.emit(PES`auth/known-host/${host}:r`, false);
|
|
1063
1141
|
throw err;
|
|
1064
1142
|
});
|
|
1065
1143
|
}
|
|
@@ -1077,7 +1155,7 @@ class Auth extends PathedEventEmitter {
|
|
|
1077
1155
|
}).then(async (resp) => {
|
|
1078
1156
|
this.api.token = (resp == null ? void 0 : resp.token) || null;
|
|
1079
1157
|
const user = await this.once("auth/user");
|
|
1080
|
-
this.emit(
|
|
1158
|
+
this.emit(PES`auth/login/${username}:u`, user);
|
|
1081
1159
|
return user;
|
|
1082
1160
|
});
|
|
1083
1161
|
}
|
|
@@ -1098,16 +1176,16 @@ class Auth extends PathedEventEmitter {
|
|
|
1098
1176
|
});
|
|
1099
1177
|
}
|
|
1100
1178
|
logout() {
|
|
1179
|
+
this.emit(PES`auth/logout:d`, this.user);
|
|
1101
1180
|
this.api.token = null;
|
|
1102
1181
|
this.user = null;
|
|
1103
|
-
this.emit(pathedEvent("auth/logout", "d"));
|
|
1104
1182
|
}
|
|
1105
1183
|
async register(u) {
|
|
1106
1184
|
var _a;
|
|
1107
1185
|
if (!u.username || !u.password) throw new Error("Cannot register user, missing username or password");
|
|
1108
1186
|
const user = await this.api.request({ url: "/api/auth/register", body: { ...u } });
|
|
1109
1187
|
if ((_a = user == null ? void 0 : user.image) == null ? void 0 : _a.startsWith("/")) user.image = `${this.api.url}${user.image}?token=${this.api.token}`;
|
|
1110
|
-
this.emit(
|
|
1188
|
+
this.emit(PES`auth/register:c`, user);
|
|
1111
1189
|
return user;
|
|
1112
1190
|
}
|
|
1113
1191
|
reset(emailOrPass, token) {
|
|
@@ -1120,7 +1198,7 @@ class Auth extends PathedEventEmitter {
|
|
|
1120
1198
|
password: token ? emailOrPass : void 0
|
|
1121
1199
|
}
|
|
1122
1200
|
}).then(() => {
|
|
1123
|
-
this.emit(
|
|
1201
|
+
this.emit(PES`auth/reset:${token ? "u" : "c"}`, token || emailOrPass);
|
|
1124
1202
|
});
|
|
1125
1203
|
}
|
|
1126
1204
|
async session(token, set = false) {
|
|
@@ -1129,12 +1207,12 @@ class Auth extends PathedEventEmitter {
|
|
|
1129
1207
|
url: "/api/auth/session",
|
|
1130
1208
|
headers: token ? { "Authorization": `Bearer ${token}` } : void 0
|
|
1131
1209
|
});
|
|
1132
|
-
this.emit(
|
|
1210
|
+
this.emit(PES`auth/session:r`, session);
|
|
1133
1211
|
if (set) {
|
|
1134
1212
|
this.api.token = token;
|
|
1135
1213
|
if (session == null ? void 0 : session.user) session.user.image = `${this.api.url}${session.user.image}?token=${this.api.token}`;
|
|
1136
1214
|
this.user = (session == null ? void 0 : session.user) || null;
|
|
1137
|
-
if (session) this.emit(
|
|
1215
|
+
if (session) this.emit(PES`auth/login:c`, session.user);
|
|
1138
1216
|
}
|
|
1139
1217
|
return session;
|
|
1140
1218
|
}
|
|
@@ -1144,7 +1222,7 @@ class Auth extends PathedEventEmitter {
|
|
|
1144
1222
|
url: "/api/auth/password",
|
|
1145
1223
|
body: { username, password, oldPassword }
|
|
1146
1224
|
}).then((resp) => {
|
|
1147
|
-
this.emit(
|
|
1225
|
+
this.emit(PES`auth/reset:u`, resp == null ? void 0 : resp.token);
|
|
1148
1226
|
if (resp == null ? void 0 : resp.token) this.api.token = resp.token;
|
|
1149
1227
|
});
|
|
1150
1228
|
}
|
|
@@ -1250,7 +1328,7 @@ class Client extends PathedEventEmitter {
|
|
|
1250
1328
|
if (!dismissed && !this.pwa && this.mobile) this.pwaPrompt();
|
|
1251
1329
|
}, 500);
|
|
1252
1330
|
}
|
|
1253
|
-
this.emit(
|
|
1331
|
+
this.emit(PES`client/inject:c`, this.platform);
|
|
1254
1332
|
}
|
|
1255
1333
|
pwaPrompt(platform) {
|
|
1256
1334
|
const url = this.settings.api.url;
|
|
@@ -1367,13 +1445,13 @@ class Client extends PathedEventEmitter {
|
|
|
1367
1445
|
setTimeout(() => {
|
|
1368
1446
|
prompt.remove();
|
|
1369
1447
|
backdrop.remove();
|
|
1370
|
-
this.emit(
|
|
1448
|
+
this.emit(PES`client/pwa:d`, platform);
|
|
1371
1449
|
}, 500);
|
|
1372
1450
|
};
|
|
1373
1451
|
prompt.append(close);
|
|
1374
1452
|
backdrop.append(prompt);
|
|
1375
1453
|
document.body.append(backdrop);
|
|
1376
|
-
this.emit(
|
|
1454
|
+
this.emit(PES`client/pwa:c`, platform);
|
|
1377
1455
|
}
|
|
1378
1456
|
}
|
|
1379
1457
|
class Data extends PathedEventEmitter {
|
|
@@ -1385,18 +1463,18 @@ class Data extends PathedEventEmitter {
|
|
|
1385
1463
|
create(collection, document2) {
|
|
1386
1464
|
if (!collection || !document2) throw new Error("Cannot create document, missing collection or document");
|
|
1387
1465
|
return this.api.request({
|
|
1388
|
-
url: `/api/` +
|
|
1466
|
+
url: `/api/` + PES`data/${collection}`,
|
|
1389
1467
|
method: "POST",
|
|
1390
1468
|
body: document2
|
|
1391
1469
|
}).then((resp) => {
|
|
1392
|
-
this.emit(
|
|
1470
|
+
this.emit(PES`data/${collection}:c`, resp);
|
|
1393
1471
|
return resp;
|
|
1394
1472
|
});
|
|
1395
1473
|
}
|
|
1396
1474
|
read(collection, id) {
|
|
1397
1475
|
if (!collection) throw new Error("Cannot read documents, missing collection");
|
|
1398
|
-
return this.api.request({ url: `/api/` +
|
|
1399
|
-
this.emit(
|
|
1476
|
+
return this.api.request({ url: `/api/` + PES`data/${collection}/${id}` }).then((resp) => {
|
|
1477
|
+
this.emit(PES`data/${collection}/${id}:r`, collection, resp);
|
|
1400
1478
|
return resp;
|
|
1401
1479
|
});
|
|
1402
1480
|
}
|
|
@@ -1404,43 +1482,43 @@ class Data extends PathedEventEmitter {
|
|
|
1404
1482
|
if (!collection || !document2) throw new Error("Cannot update document, missing collection or document");
|
|
1405
1483
|
if (!document2._id) return this.create(collection, document2);
|
|
1406
1484
|
return this.api.request({
|
|
1407
|
-
url: `/api/` +
|
|
1485
|
+
url: `/api/` + PES`data/${collection}/${document2._id}`,
|
|
1408
1486
|
method: append ? "PATCH" : "PUT",
|
|
1409
1487
|
body: document2
|
|
1410
1488
|
}).then((resp) => {
|
|
1411
|
-
this.emit(
|
|
1489
|
+
this.emit(PES`data/${collection}/${document2._id}:u`, resp);
|
|
1412
1490
|
return resp;
|
|
1413
1491
|
});
|
|
1414
1492
|
}
|
|
1415
1493
|
delete(collection, id) {
|
|
1416
1494
|
if (!collection || !id) throw new Error("Cannot delete document, missing collection or ID");
|
|
1417
1495
|
return this.api.request({
|
|
1418
|
-
url: `/api/` +
|
|
1496
|
+
url: `/api/` + PES`data/${collection}/${id}`,
|
|
1419
1497
|
method: "DELETE"
|
|
1420
|
-
}).then(() => this.emit(
|
|
1498
|
+
}).then(() => this.emit(PES`data/${collection}/${id}:d`, id));
|
|
1421
1499
|
}
|
|
1422
1500
|
raw(collection, query) {
|
|
1423
1501
|
if (!collection || !query) throw new Error("Cannot execute raw query, missing collection or query");
|
|
1424
1502
|
const mode = query.operand.startsWith("find") ? "r" : query.operand == "insert" ? "c" : query.operand.startsWith("delete") ? "d" : "u";
|
|
1425
|
-
return this.api.request({ url: `/api/` +
|
|
1426
|
-
this.emit(
|
|
1503
|
+
return this.api.request({ url: `/api/` + PES`data/${collection}` + "?raw", body: query }).then((resp) => {
|
|
1504
|
+
this.emit(PES`data/${collection}:${mode}`, resp);
|
|
1427
1505
|
return resp;
|
|
1428
1506
|
});
|
|
1429
1507
|
}
|
|
1430
1508
|
deleteSchema(path) {
|
|
1431
1509
|
if (!path) throw new Error("Cannot delete schema, missing collection path");
|
|
1432
|
-
return this.api.request({ url: `/api/` +
|
|
1510
|
+
return this.api.request({ url: `/api/` + PES`schema/${path}`, method: "DELETE" }).then(() => this.emit(PES`schema/${path}:d`, path));
|
|
1433
1511
|
}
|
|
1434
1512
|
getSchema(pathOrTree) {
|
|
1435
|
-
return this.api.request({ url: "/api/" +
|
|
1436
|
-
this.emit(
|
|
1513
|
+
return this.api.request({ url: "/api/" + PES`schema/${typeof pathOrTree == "string" ? pathOrTree : ""}` + (pathOrTree === true ? `?tree=${pathOrTree}` : "") }).then((resp) => {
|
|
1514
|
+
this.emit(PES`schema/${typeof pathOrTree == "string" ? pathOrTree : ""}:r`, resp);
|
|
1437
1515
|
return resp;
|
|
1438
1516
|
});
|
|
1439
1517
|
}
|
|
1440
1518
|
setSchema(schema) {
|
|
1441
1519
|
if (!schema.path) throw new Error("Cannot update schema, missing collection path");
|
|
1442
|
-
return this.api.request({ url: "/api/" +
|
|
1443
|
-
this.emit(
|
|
1520
|
+
return this.api.request({ url: "/api/" + PES`schema/${schema.path}`, body: schema }).then((resp) => {
|
|
1521
|
+
this.emit(PES`schema/${schema.path}:${schema._id ? "u" : "c"}`, resp);
|
|
1444
1522
|
return resp;
|
|
1445
1523
|
});
|
|
1446
1524
|
}
|
|
@@ -1454,10 +1532,10 @@ class Email extends PathedEventEmitter {
|
|
|
1454
1532
|
send(email) {
|
|
1455
1533
|
var _a;
|
|
1456
1534
|
if (!email.to && !email.bcc || !email.body) throw new Error("Cannot send email, missing address or body");
|
|
1457
|
-
return this.api.request({ url: "/api/" +
|
|
1535
|
+
return this.api.request({ url: "/api/" + PES`email/${(_a = email.body) == null ? void 0 : _a.template}`, body: email }).then((response) => {
|
|
1458
1536
|
var _a2;
|
|
1459
|
-
this.emit(
|
|
1460
|
-
return
|
|
1537
|
+
this.emit(PES`email/${(_a2 = email.body) == null ? void 0 : _a2.template}:c`, { email, response });
|
|
1538
|
+
return response;
|
|
1461
1539
|
});
|
|
1462
1540
|
}
|
|
1463
1541
|
}
|
|
@@ -1465,49 +1543,59 @@ class Groups extends PathedEventEmitter {
|
|
|
1465
1543
|
constructor(api) {
|
|
1466
1544
|
super();
|
|
1467
1545
|
__publicField(this, "api");
|
|
1546
|
+
__publicField(this, "cache", new Dt("name"));
|
|
1468
1547
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1469
1548
|
}
|
|
1470
|
-
all() {
|
|
1471
|
-
|
|
1472
|
-
|
|
1549
|
+
async all(reload) {
|
|
1550
|
+
if (!reload && this.cache.complete) return this.cache.all();
|
|
1551
|
+
return this.api.request({ url: `/api/` + PES`groups` }).then((resp) => {
|
|
1552
|
+
this.cache.addAll(resp);
|
|
1553
|
+
this.emit(PES`groups:r`, resp || []);
|
|
1473
1554
|
return resp;
|
|
1474
1555
|
});
|
|
1475
1556
|
}
|
|
1476
1557
|
create(group) {
|
|
1477
1558
|
if (!group.name) throw new Error("Cannot create group, missing name");
|
|
1478
1559
|
return this.api.request({
|
|
1479
|
-
url: `/api
|
|
1560
|
+
url: `/api/` + PES`groups/${group.name}`,
|
|
1480
1561
|
method: "POST",
|
|
1481
1562
|
body: group
|
|
1482
1563
|
}).then((resp) => {
|
|
1483
|
-
this.
|
|
1564
|
+
this.cache.add(resp);
|
|
1565
|
+
this.emit(PES`groups/${group.name}:c`, resp);
|
|
1484
1566
|
return resp;
|
|
1485
1567
|
});
|
|
1486
1568
|
}
|
|
1487
|
-
read(name) {
|
|
1569
|
+
async read(name, reload) {
|
|
1488
1570
|
if (!name) throw new Error("Cannot read group, missing name");
|
|
1489
|
-
|
|
1490
|
-
|
|
1571
|
+
if (!reload && this.cache.get(name)) return this.cache.get(name);
|
|
1572
|
+
return this.api.request({ url: `/api/` + PES`groups/${name}` }).then((resp) => {
|
|
1573
|
+
this.cache.add(resp);
|
|
1574
|
+
this.emit(PES`groups/${name}:r`, resp);
|
|
1491
1575
|
return resp;
|
|
1492
1576
|
});
|
|
1493
1577
|
}
|
|
1494
1578
|
update(group) {
|
|
1495
1579
|
if (!group.name) throw new Error("Cannot update group, missing name");
|
|
1496
1580
|
return this.api.request({
|
|
1497
|
-
url: `/api/` +
|
|
1581
|
+
url: `/api/` + PES`groups/${group.name}`,
|
|
1498
1582
|
method: "PATCH",
|
|
1499
1583
|
body: group
|
|
1500
1584
|
}).then((resp) => {
|
|
1501
|
-
this.
|
|
1585
|
+
this.cache.add(resp);
|
|
1586
|
+
this.emit(PES`groups/${group.name}:u`, resp);
|
|
1502
1587
|
return resp;
|
|
1503
1588
|
});
|
|
1504
1589
|
}
|
|
1505
1590
|
delete(name) {
|
|
1506
1591
|
if (!name) throw new Error("Cannot delete group, missing name");
|
|
1507
1592
|
return this.api.request({
|
|
1508
|
-
url: `/api/` +
|
|
1593
|
+
url: `/api/` + PES`groups/${name}`,
|
|
1509
1594
|
method: "DELETE"
|
|
1510
|
-
}).then(() =>
|
|
1595
|
+
}).then(() => {
|
|
1596
|
+
this.cache.delete(name);
|
|
1597
|
+
this.emit(PES`groups/${name}:d`);
|
|
1598
|
+
});
|
|
1511
1599
|
}
|
|
1512
1600
|
}
|
|
1513
1601
|
class Logger extends PathedEventEmitter {
|
|
@@ -1546,11 +1634,11 @@ ${log}`;
|
|
|
1546
1634
|
};
|
|
1547
1635
|
}
|
|
1548
1636
|
createLog(log, namespace = this.namespace) {
|
|
1549
|
-
return this.api.request({ url: `/api/` +
|
|
1637
|
+
return this.api.request({ url: `/api/` + PES`logs/${namespace}`, body: log }).then(() => this.emit(PES`logs/${namespace}:c`, log)).catch(() => {
|
|
1550
1638
|
});
|
|
1551
1639
|
}
|
|
1552
1640
|
clearLogs(namespace = this.namespace) {
|
|
1553
|
-
return this.api.request({ url: `/api/` +
|
|
1641
|
+
return this.api.request({ url: `/api/` + PES`logs/${namespace}`, method: "DELETE" }).then(() => this.emit(PES`logs/${namespace}:d`));
|
|
1554
1642
|
}
|
|
1555
1643
|
clearServerLogs() {
|
|
1556
1644
|
return this.clearLogs("server");
|
|
@@ -1558,8 +1646,8 @@ ${log}`;
|
|
|
1558
1646
|
getLogs(length, page, namespace = this.namespace) {
|
|
1559
1647
|
if (!namespace) throw new Error("Cannot get logs, missing namespace");
|
|
1560
1648
|
const query = [length ? `length=${length}` : void 0, page ? `page=${page}` : void 0].filter((v) => !!v).join("&");
|
|
1561
|
-
return this.api.request({ url: `/api/` +
|
|
1562
|
-
this.emit(
|
|
1649
|
+
return this.api.request({ url: `/api/` + PES`logs/${namespace}` + (query ? `?${query}` : "") }).then((logs) => {
|
|
1650
|
+
this.emit(PES`logs/${namespace}:r`, logs);
|
|
1563
1651
|
return logs;
|
|
1564
1652
|
});
|
|
1565
1653
|
}
|
|
@@ -1605,7 +1693,7 @@ class Payments extends PathedEventEmitter {
|
|
|
1605
1693
|
amount,
|
|
1606
1694
|
custom
|
|
1607
1695
|
} });
|
|
1608
|
-
this.emit(
|
|
1696
|
+
this.emit(PES`payments:c`, request.data.clientSecret);
|
|
1609
1697
|
return request.data.clientSecret;
|
|
1610
1698
|
}
|
|
1611
1699
|
async createForm(element, amount, custom) {
|
|
@@ -1619,8 +1707,8 @@ class Payments extends PathedEventEmitter {
|
|
|
1619
1707
|
});
|
|
1620
1708
|
}
|
|
1621
1709
|
async history(username) {
|
|
1622
|
-
const history = await this.api.request({ url: `/api/` +
|
|
1623
|
-
this.emit(
|
|
1710
|
+
const history = await this.api.request({ url: `/api/` + PES`payments/${username}` });
|
|
1711
|
+
this.emit(PES`payments/${username}:r`, history);
|
|
1624
1712
|
return history;
|
|
1625
1713
|
}
|
|
1626
1714
|
}
|
|
@@ -1631,7 +1719,7 @@ class Pdf extends PathedEventEmitter {
|
|
|
1631
1719
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1632
1720
|
}
|
|
1633
1721
|
createPdf(body, options) {
|
|
1634
|
-
return this.api.request({ url: `/api
|
|
1722
|
+
return this.api.request({ url: `/api/` + PES`pdf`, body: { ...body, options }, decode: false }).then(async (resp) => {
|
|
1635
1723
|
const blob = await resp.blob();
|
|
1636
1724
|
if (options == null ? void 0 : options.download) {
|
|
1637
1725
|
let filename = (options == null ? void 0 : options.filename) || qt();
|
|
@@ -1640,7 +1728,7 @@ class Pdf extends PathedEventEmitter {
|
|
|
1640
1728
|
dt(url, filename);
|
|
1641
1729
|
URL.revokeObjectURL(url);
|
|
1642
1730
|
}
|
|
1643
|
-
this.emit(
|
|
1731
|
+
this.emit(PES`pdf:c`, blob);
|
|
1644
1732
|
return blob;
|
|
1645
1733
|
});
|
|
1646
1734
|
}
|
|
@@ -1712,54 +1800,54 @@ class Storage extends PathedEventEmitter {
|
|
|
1712
1800
|
}
|
|
1713
1801
|
copy(source, destination) {
|
|
1714
1802
|
if (!source || !destination) throw new Error("Cannot copy file or folder, missing source or destination");
|
|
1715
|
-
return this.api.request({ url: "/api/" +
|
|
1716
|
-
this.emit(
|
|
1717
|
-
return
|
|
1803
|
+
return this.api.request({ url: "/api/" + PES`storage/${destination}`, body: { from: source } }).then((response) => {
|
|
1804
|
+
this.emit(PES`storage/${destination}:c`, response);
|
|
1805
|
+
return response;
|
|
1718
1806
|
});
|
|
1719
1807
|
}
|
|
1720
1808
|
delete(path) {
|
|
1721
1809
|
if (!path) throw new Error("Cannot delete file or folder, missing path");
|
|
1722
|
-
return this.api.request({ url: "/api/" +
|
|
1723
|
-
this.emit(
|
|
1810
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}`, method: "DELETE" }).then(() => {
|
|
1811
|
+
this.emit(PES`storage/${path}:d`, path);
|
|
1724
1812
|
});
|
|
1725
1813
|
}
|
|
1726
1814
|
download(path, opts = {}) {
|
|
1727
1815
|
if (!path) throw new Error("Cannot download file, missing path");
|
|
1728
|
-
return this.api.request({ ...opts, url: "/api/" +
|
|
1816
|
+
return this.api.request({ ...opts, url: "/api/" + PES`storage/${path}`, decode: false }).then(async (response) => {
|
|
1729
1817
|
const blob = await response.blob();
|
|
1730
1818
|
const name = opts.downloadAs || path.split("/").pop();
|
|
1731
|
-
this.emit(
|
|
1819
|
+
this.emit(PES`storage/${path}:r`, blob);
|
|
1732
1820
|
Gt(blob, name);
|
|
1733
1821
|
return response;
|
|
1734
1822
|
});
|
|
1735
1823
|
}
|
|
1736
1824
|
list(path) {
|
|
1737
1825
|
if (!path) path = "/";
|
|
1738
|
-
return this.api.request({ url: "/api/" +
|
|
1739
|
-
this.emit(
|
|
1826
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}` + "?list" }).then((resp) => {
|
|
1827
|
+
this.emit(PES`storage/${path}:r`, resp);
|
|
1740
1828
|
return resp;
|
|
1741
1829
|
});
|
|
1742
1830
|
}
|
|
1743
1831
|
open(path, target = "_blank") {
|
|
1744
1832
|
if (!path) throw new Error("Cannot download file, missing path");
|
|
1745
|
-
const link = `${this.api.url}/api/${
|
|
1833
|
+
const link = `${this.api.url}/api/` + PES`storage/${path}` + (this.api.token ? `?token=${this.api.token}` : "");
|
|
1746
1834
|
if (!target) return link;
|
|
1747
|
-
this.emit(
|
|
1835
|
+
this.emit(PES`storage/${path}:r`, path);
|
|
1748
1836
|
return window.open(link, target);
|
|
1749
1837
|
}
|
|
1750
1838
|
mkdir(path) {
|
|
1751
1839
|
if (!path) throw new Error("Cannot make directory, missing path");
|
|
1752
|
-
return this.api.request({ url: "/api/" +
|
|
1753
|
-
this.emit(
|
|
1840
|
+
return this.api.request({ url: "/api/" + PES`storage/${path}`, body: { directory: true } }).then((resp) => {
|
|
1841
|
+
this.emit(PES`storage/${path}:c`, resp);
|
|
1754
1842
|
return resp;
|
|
1755
1843
|
});
|
|
1756
1844
|
}
|
|
1757
1845
|
move(source, destination) {
|
|
1758
1846
|
if (!source || !destination) throw new Error("Cannot move file or folder, missing source or destination");
|
|
1759
1847
|
if (source == destination) return this.list(destination);
|
|
1760
|
-
return this.api.request({ url: "/api/" +
|
|
1761
|
-
this.emit(
|
|
1762
|
-
return
|
|
1848
|
+
return this.api.request({ url: "/api/" + PES`storage/${source}`, method: "PATCH", body: { move: destination } }).then((response) => {
|
|
1849
|
+
this.emit(PES`storage/${source}:u`, response);
|
|
1850
|
+
return response;
|
|
1763
1851
|
});
|
|
1764
1852
|
}
|
|
1765
1853
|
upload(files, opts) {
|
|
@@ -1768,13 +1856,13 @@ class Storage extends PathedEventEmitter {
|
|
|
1768
1856
|
if (!files || Array.isArray(files) && !files.length) return [];
|
|
1769
1857
|
const path = (opts && typeof opts == "object" ? opts == null ? void 0 : opts.path : opts) || "/";
|
|
1770
1858
|
return Ft({
|
|
1771
|
-
url: `${this.api.url}/api/${
|
|
1859
|
+
url: `${this.api.url}/api/` + PES`storage/${path}`,
|
|
1772
1860
|
files: ft(files),
|
|
1773
1861
|
headers: this.api.headers
|
|
1774
1862
|
}).onProgress((p2) => {
|
|
1775
1863
|
prog(p2);
|
|
1776
1864
|
}).then((resp) => {
|
|
1777
|
-
this.emit(
|
|
1865
|
+
this.emit(PES`storage/${path}:c`, resp);
|
|
1778
1866
|
res(resp);
|
|
1779
1867
|
}).catch((err) => rej(err));
|
|
1780
1868
|
});
|
|
@@ -1787,44 +1875,44 @@ class Users extends PathedEventEmitter {
|
|
|
1787
1875
|
__publicField(this, "cache", new Dt("username"));
|
|
1788
1876
|
this.api = typeof api == "string" ? new Api(api) : api;
|
|
1789
1877
|
}
|
|
1790
|
-
|
|
1791
|
-
if (!username) throw new Error("Cannot delete user, missing username");
|
|
1792
|
-
return this.api.request({
|
|
1793
|
-
url: "/api/" + pathedEvent(["users", username]),
|
|
1794
|
-
method: "DELETE"
|
|
1795
|
-
}).then(() => {
|
|
1796
|
-
this.cache.delete(username);
|
|
1797
|
-
this.emit(pathedEvent(["users", username], "d"), username);
|
|
1798
|
-
});
|
|
1799
|
-
}
|
|
1800
|
-
async all(reload = false) {
|
|
1878
|
+
async all(reload) {
|
|
1801
1879
|
if (!reload && this.cache.complete) return this.cache.all();
|
|
1802
|
-
return this.api.request({ url: "/api/" +
|
|
1880
|
+
return this.api.request({ url: "/api/" + PES`users` }).then((resp) => {
|
|
1803
1881
|
resp == null ? void 0 : resp.forEach((r) => {
|
|
1804
1882
|
r.image = this.api.url + r.image + `?token=${this.api.token}`;
|
|
1805
1883
|
return r;
|
|
1806
1884
|
});
|
|
1807
1885
|
this.cache.addAll(resp);
|
|
1808
|
-
this.emit(
|
|
1886
|
+
this.emit(PES`users:r`, resp || []);
|
|
1809
1887
|
return resp;
|
|
1810
1888
|
});
|
|
1811
1889
|
}
|
|
1812
|
-
|
|
1890
|
+
delete(username) {
|
|
1891
|
+
if (!username) throw new Error("Cannot delete user, missing username");
|
|
1892
|
+
return this.api.request({
|
|
1893
|
+
url: "/api/" + PES`users/${username}`,
|
|
1894
|
+
method: "DELETE"
|
|
1895
|
+
}).then(() => {
|
|
1896
|
+
this.cache.delete(username);
|
|
1897
|
+
this.emit(PES`users/${username}:d`, username);
|
|
1898
|
+
});
|
|
1899
|
+
}
|
|
1900
|
+
async read(username, reload) {
|
|
1813
1901
|
if (!username) throw new Error("Cannot read user, missing username");
|
|
1814
1902
|
if (!reload && this.cache.get(username)) return this.cache.get(username);
|
|
1815
|
-
return this.api.request({ url: "/api/" +
|
|
1903
|
+
return this.api.request({ url: "/api/" + PES`users/${username}` }).then((resp) => {
|
|
1816
1904
|
if (resp) {
|
|
1817
1905
|
resp.image = this.api.url + resp.image + `?token=${this.api.token}`;
|
|
1818
1906
|
this.cache.add(resp);
|
|
1819
1907
|
}
|
|
1820
|
-
this.emit(
|
|
1908
|
+
this.emit(PES`users/${username}:r`, resp);
|
|
1821
1909
|
return resp;
|
|
1822
1910
|
});
|
|
1823
1911
|
}
|
|
1824
1912
|
update(user) {
|
|
1825
1913
|
if (!user.username) throw new Error("Cannot update user, missing username");
|
|
1826
1914
|
return this.api.request({
|
|
1827
|
-
url: `/api/` +
|
|
1915
|
+
url: `/api/` + PES`users/${user.username}`,
|
|
1828
1916
|
method: "PATCH",
|
|
1829
1917
|
body: user
|
|
1830
1918
|
}).then((resp) => {
|
|
@@ -1832,19 +1920,16 @@ class Users extends PathedEventEmitter {
|
|
|
1832
1920
|
resp.image = this.api.url + resp.image + `?token=${this.api.token}`;
|
|
1833
1921
|
this.cache.add(resp);
|
|
1834
1922
|
}
|
|
1835
|
-
this.emit(
|
|
1923
|
+
this.emit(PES`users/${user.username}:${resp._id ? "u" : "c"}`, resp);
|
|
1836
1924
|
return resp;
|
|
1837
1925
|
});
|
|
1838
1926
|
}
|
|
1839
1927
|
uploadImage(username, file) {
|
|
1840
1928
|
if (!username || !file) throw new Error("Cannot update user image, missing username or file");
|
|
1841
1929
|
return Ft({
|
|
1842
|
-
url: this.api.url + `/api/` +
|
|
1930
|
+
url: this.api.url + `/api/` + PES`users/${username}/image`,
|
|
1843
1931
|
files: [file],
|
|
1844
1932
|
headers: this.api.headers
|
|
1845
|
-
}).then((resp) => {
|
|
1846
|
-
this.emit(pathedEvent(["users", username, "image"], "u"), username, file);
|
|
1847
|
-
return resp;
|
|
1848
1933
|
});
|
|
1849
1934
|
}
|
|
1850
1935
|
}
|
|
@@ -1857,33 +1942,33 @@ class Settings extends PathedEventEmitter {
|
|
|
1857
1942
|
}
|
|
1858
1943
|
async all(detailed = false, reload) {
|
|
1859
1944
|
if (!reload && !detailed && this.cache.complete) return this.cache;
|
|
1860
|
-
return this.api.request({ url: `/api
|
|
1945
|
+
return this.api.request({ url: `/api/` + PES`settings` + (detailed ? "?detailed" : "") }).then((resp) => {
|
|
1861
1946
|
if (resp) Object.keys(resp).forEach((key) => this.cache.set(key, detailed ? resp[key].value : resp[key]));
|
|
1862
|
-
this.emit(
|
|
1947
|
+
this.emit(PES`settings:r`, resp || []);
|
|
1863
1948
|
return resp;
|
|
1864
1949
|
});
|
|
1865
1950
|
}
|
|
1866
1951
|
delete(key) {
|
|
1867
1952
|
if (!key) throw new Error("Cannot delete setting, missing key");
|
|
1868
|
-
return this.api.request({ url: `/api/` +
|
|
1953
|
+
return this.api.request({ url: `/api/` + PES`settings/${key}`, method: "DELETE" }).then(() => {
|
|
1869
1954
|
this.cache.delete(key);
|
|
1870
|
-
this.emit(
|
|
1955
|
+
this.emit(PES`settings/${key}:d`, key);
|
|
1871
1956
|
});
|
|
1872
1957
|
}
|
|
1873
1958
|
read(key, reload = false) {
|
|
1874
1959
|
if (!key) throw new Error("Cannot read setting, missing key");
|
|
1875
1960
|
if (!reload && this.cache.get(key)) return this.cache.get(key);
|
|
1876
|
-
return this.api.request({ url: `/api/` +
|
|
1961
|
+
return this.api.request({ url: `/api/` + PES`settings/${key}` }).then((variable) => {
|
|
1877
1962
|
if (variable) this.cache.set(variable.key, variable.value);
|
|
1878
|
-
this.emit(
|
|
1963
|
+
this.emit(PES`settings/${key}:r`, variable);
|
|
1879
1964
|
return variable;
|
|
1880
1965
|
});
|
|
1881
1966
|
}
|
|
1882
1967
|
update(variable) {
|
|
1883
1968
|
if (!variable.key) throw new Error("Cannot update setting, missing key");
|
|
1884
|
-
return this.api.request({ url: `/api/` +
|
|
1969
|
+
return this.api.request({ url: `/api/` + PES`settings/${variable.key}`, body: variable }).then((variable2) => {
|
|
1885
1970
|
if (variable2) this.cache.set(variable2.key, variable2.value);
|
|
1886
|
-
this.emit(`/api/` +
|
|
1971
|
+
this.emit(`/api/` + PES`settings/${variable2.key}:${variable2._id ? "u" : "c"}`, variable2);
|
|
1887
1972
|
return variable2;
|
|
1888
1973
|
});
|
|
1889
1974
|
}
|
|
@@ -1896,21 +1981,21 @@ class Static extends PathedEventEmitter {
|
|
|
1896
1981
|
}
|
|
1897
1982
|
delete(path) {
|
|
1898
1983
|
if (!path) throw new Error("Cannot delete static asset, missing path");
|
|
1899
|
-
return this.api.request({ url: `/api/` +
|
|
1900
|
-
this.emit(
|
|
1984
|
+
return this.api.request({ url: `/api/` + PES`static/${path}`, method: "DELETE" }).then(() => {
|
|
1985
|
+
this.emit(PES`static/${path}:d`, path);
|
|
1901
1986
|
});
|
|
1902
1987
|
}
|
|
1903
1988
|
upload(files, path = "/") {
|
|
1904
1989
|
if (!files) throw new Error("Cannot upload static assets, missing file");
|
|
1905
1990
|
return new E(async (res, rej, prog) => {
|
|
1906
1991
|
return Ft({
|
|
1907
|
-
url: this.api.url + "/api/" +
|
|
1992
|
+
url: this.api.url + "/api/" + PES`static/${path}`,
|
|
1908
1993
|
files: ft(files),
|
|
1909
1994
|
headers: this.api.headers
|
|
1910
1995
|
}).onProgress((p2) => {
|
|
1911
1996
|
prog(p2);
|
|
1912
1997
|
}).then((resp) => {
|
|
1913
|
-
this.emit(
|
|
1998
|
+
this.emit(PES`static/${path}:c`, resp);
|
|
1914
1999
|
res(resp);
|
|
1915
2000
|
}).catch((err) => rej(err));
|
|
1916
2001
|
});
|
|
@@ -1974,7 +2059,8 @@ class Momentum extends PathedEventEmitter {
|
|
|
1974
2059
|
this.relayEvents(this.users);
|
|
1975
2060
|
this.users.on(`*`, () => {
|
|
1976
2061
|
if (!this.auth.user) return;
|
|
1977
|
-
|
|
2062
|
+
const cached = this.users.cache.get(this.auth.user.username);
|
|
2063
|
+
if (cached) this.auth.user = cached;
|
|
1978
2064
|
});
|
|
1979
2065
|
}
|
|
1980
2066
|
}
|
|
@@ -1991,6 +2077,9 @@ export {
|
|
|
1991
2077
|
Groups,
|
|
1992
2078
|
Logger,
|
|
1993
2079
|
Momentum,
|
|
2080
|
+
PE,
|
|
2081
|
+
PES,
|
|
2082
|
+
PathEvent,
|
|
1994
2083
|
PathedEventEmitter,
|
|
1995
2084
|
Payments,
|
|
1996
2085
|
Pdf,
|
|
@@ -1999,10 +2088,5 @@ export {
|
|
|
1999
2088
|
Static,
|
|
2000
2089
|
Storage,
|
|
2001
2090
|
Totp,
|
|
2002
|
-
Users
|
|
2003
|
-
combinePathedEvents,
|
|
2004
|
-
hasPath,
|
|
2005
|
-
hasPathFatal,
|
|
2006
|
-
parsePathedEvent,
|
|
2007
|
-
pathedEvent
|
|
2091
|
+
Users
|
|
2008
2092
|
};
|