@moonbase.sh/storefront-api 0.3.19 → 0.3.21
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/index.cjs +55 -32
- package/dist/index.d.cts +1415 -1402
- package/dist/index.d.ts +1415 -1402
- package/dist/index.js +55 -32
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -333,7 +333,7 @@ var schemas_exports = {};
|
|
|
333
333
|
__export(schemas_exports, {
|
|
334
334
|
addressSchema: () => addressSchema,
|
|
335
335
|
communicationPreferencesSchema: () => communicationPreferencesSchema,
|
|
336
|
-
|
|
336
|
+
identitySchema: () => identitySchema,
|
|
337
337
|
userAccountConfirmedSchema: () => userAccountConfirmedSchema,
|
|
338
338
|
userSchema: () => userSchema
|
|
339
339
|
});
|
|
@@ -362,7 +362,7 @@ var userSchema = z7.object({
|
|
|
362
362
|
hasProducts: z7.boolean().nullish(),
|
|
363
363
|
hasSubscriptions: z7.boolean().nullish()
|
|
364
364
|
});
|
|
365
|
-
var
|
|
365
|
+
var identitySchema = userSchema.and(z7.object({
|
|
366
366
|
accessToken: z7.string(),
|
|
367
367
|
refreshToken: z7.string()
|
|
368
368
|
}));
|
|
@@ -399,7 +399,7 @@ var IdentityEndpoints = class {
|
|
|
399
399
|
await handleResponseProblem(response);
|
|
400
400
|
try {
|
|
401
401
|
const data = await response.json();
|
|
402
|
-
const user =
|
|
402
|
+
const user = identitySchema.parse(data);
|
|
403
403
|
this.tokenStore.setUser(user);
|
|
404
404
|
return userSchema.parse(user);
|
|
405
405
|
} catch (err) {
|
|
@@ -408,7 +408,7 @@ var IdentityEndpoints = class {
|
|
|
408
408
|
}
|
|
409
409
|
}
|
|
410
410
|
async signUp(name, email, password, address, acceptedPrivacyPolicy, acceptedTermsAndConditions, communicationOptIn) {
|
|
411
|
-
const response = await this.api.fetch(`/api/customer/identity/sign-up?scheme=JWT&communicationOptIn=${communicationOptIn ? "true" : "false"}`,
|
|
411
|
+
const response = await this.api.fetch(`/api/customer/identity/sign-up?scheme=JWT&communicationOptIn=${communicationOptIn ? "true" : "false"}`, identitySchema, {
|
|
412
412
|
method: "POST",
|
|
413
413
|
body: {
|
|
414
414
|
name,
|
|
@@ -854,7 +854,7 @@ var MoonbaseApi = class {
|
|
|
854
854
|
this.tokenStore = tokenStore;
|
|
855
855
|
}
|
|
856
856
|
async authenticatedFetch(path, schema, options) {
|
|
857
|
-
if (!this.tokenStore.
|
|
857
|
+
if (!this.tokenStore.user)
|
|
858
858
|
throw new NotAuthenticatedError();
|
|
859
859
|
return await this.fetch(path, schema, options);
|
|
860
860
|
}
|
|
@@ -909,7 +909,7 @@ var MoonbaseApi = class {
|
|
|
909
909
|
}
|
|
910
910
|
async reportParsingProblem(path, err, body) {
|
|
911
911
|
try {
|
|
912
|
-
await fetch2(this.baseUrl
|
|
912
|
+
await fetch2(`${this.baseUrl}/api/customer/insights/error`, {
|
|
913
913
|
mode: "cors",
|
|
914
914
|
method: "POST",
|
|
915
915
|
headers: {
|
|
@@ -931,7 +931,7 @@ var MoonbaseApi = class {
|
|
|
931
931
|
}
|
|
932
932
|
async reportRequestProblem(path, request, response, err) {
|
|
933
933
|
try {
|
|
934
|
-
await fetch2(this.baseUrl
|
|
934
|
+
await fetch2(`${this.baseUrl}/api/customer/insights/warn`, {
|
|
935
935
|
mode: "cors",
|
|
936
936
|
method: "POST",
|
|
937
937
|
headers: {
|
|
@@ -959,29 +959,56 @@ var MoonbaseApi = class {
|
|
|
959
959
|
|
|
960
960
|
// src/utils/tokenStore.ts
|
|
961
961
|
import fetch3 from "cross-fetch";
|
|
962
|
+
|
|
963
|
+
// src/utils/store.ts
|
|
964
|
+
var LocalStorageStore = class {
|
|
965
|
+
get(key) {
|
|
966
|
+
const item = localStorage.getItem(key);
|
|
967
|
+
if (item) {
|
|
968
|
+
return JSON.parse(item);
|
|
969
|
+
}
|
|
970
|
+
return null;
|
|
971
|
+
}
|
|
972
|
+
set(key, item) {
|
|
973
|
+
localStorage.setItem(key, JSON.stringify(item));
|
|
974
|
+
}
|
|
975
|
+
remove(key) {
|
|
976
|
+
localStorage.removeItem(key);
|
|
977
|
+
}
|
|
978
|
+
listen(key, callback) {
|
|
979
|
+
if (typeof window !== "undefined") {
|
|
980
|
+
window.addEventListener("storage", (event) => {
|
|
981
|
+
if (event.key === key) {
|
|
982
|
+
callback(event.newValue ? JSON.parse(event.newValue) : null);
|
|
983
|
+
}
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
// src/utils/tokenStore.ts
|
|
962
990
|
var _TokenStore = class _TokenStore {
|
|
963
991
|
constructor(configuration) {
|
|
964
992
|
this.configuration = configuration;
|
|
965
993
|
this.tokens = null;
|
|
966
994
|
this.refreshTimeoutId = null;
|
|
967
995
|
this.refreshPromise = null;
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
996
|
+
var _a;
|
|
997
|
+
this.store = (_a = configuration.store) != null ? _a : new LocalStorageStore();
|
|
998
|
+
const storedToken = this.store.get(_TokenStore.storageKey);
|
|
999
|
+
if (storedToken) {
|
|
1000
|
+
this.tokens = {
|
|
1001
|
+
...storedToken,
|
|
1002
|
+
expiresAt: new Date(storedToken.expiresAt)
|
|
1003
|
+
};
|
|
975
1004
|
}
|
|
1005
|
+
this.store.listen(_TokenStore.storageKey, (updatedTokens) => this.handleStorageUpdate(updatedTokens));
|
|
976
1006
|
}
|
|
977
1007
|
get user() {
|
|
978
1008
|
if (this.tokens)
|
|
979
1009
|
return this.tokens;
|
|
980
1010
|
return null;
|
|
981
1011
|
}
|
|
982
|
-
get hasAccessToken() {
|
|
983
|
-
return !!this.tokens;
|
|
984
|
-
}
|
|
985
1012
|
async getAccessToken() {
|
|
986
1013
|
var _a, _b, _c;
|
|
987
1014
|
if (this.isExpired) {
|
|
@@ -998,8 +1025,8 @@ var _TokenStore = class _TokenStore {
|
|
|
998
1025
|
setUser(user) {
|
|
999
1026
|
if (user === null) {
|
|
1000
1027
|
this.tokens = null;
|
|
1001
|
-
if (typeof window !== "undefined"
|
|
1002
|
-
|
|
1028
|
+
if (typeof window !== "undefined")
|
|
1029
|
+
this.store.remove(_TokenStore.storageKey);
|
|
1003
1030
|
if (this.refreshTimeoutId != null)
|
|
1004
1031
|
window.clearTimeout(this.refreshTimeoutId);
|
|
1005
1032
|
return null;
|
|
@@ -1009,8 +1036,8 @@ var _TokenStore = class _TokenStore {
|
|
|
1009
1036
|
// Hardcoded 15 minutes now, might want to check the JWT tho
|
|
1010
1037
|
expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
|
|
1011
1038
|
};
|
|
1012
|
-
if (typeof window !== "undefined"
|
|
1013
|
-
|
|
1039
|
+
if (typeof window !== "undefined")
|
|
1040
|
+
this.store.set(_TokenStore.storageKey, this.tokens);
|
|
1014
1041
|
if (this.refreshTimeoutId != null)
|
|
1015
1042
|
window.clearTimeout(this.refreshTimeoutId);
|
|
1016
1043
|
this.refreshTimeoutId = window.setTimeout(() => {
|
|
@@ -1040,19 +1067,15 @@ var _TokenStore = class _TokenStore {
|
|
|
1040
1067
|
}
|
|
1041
1068
|
throw new MoonbaseError("Unexpected result", `Could not refresh access token, status code ${response.status}`, response.status);
|
|
1042
1069
|
}
|
|
1043
|
-
const result =
|
|
1070
|
+
const result = identitySchema.parse(await response.json());
|
|
1044
1071
|
return this.setUser(result);
|
|
1045
1072
|
}
|
|
1046
|
-
handleStorageUpdate(
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
if (this.tokens) {
|
|
1053
|
-
this.tokens.expiresAt = new Date(this.tokens.expiresAt);
|
|
1054
|
-
}
|
|
1055
|
-
break;
|
|
1073
|
+
handleStorageUpdate(updatedTokens) {
|
|
1074
|
+
this.tokens = updatedTokens;
|
|
1075
|
+
if (this.refreshTimeoutId != null)
|
|
1076
|
+
window.clearTimeout(this.refreshTimeoutId);
|
|
1077
|
+
if (this.tokens) {
|
|
1078
|
+
this.tokens.expiresAt = new Date(this.tokens.expiresAt);
|
|
1056
1079
|
}
|
|
1057
1080
|
}
|
|
1058
1081
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moonbase.sh/storefront-api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.21",
|
|
5
5
|
"description": "Package to let you build storefronts with Moonbase.sh as payment and delivery provider",
|
|
6
6
|
"author": "Tobias Lønnerød Madsen <m@dsen.tv>",
|
|
7
7
|
"license": "MIT",
|