arky-sdk 0.7.64 → 0.7.66
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 +65 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -5
- package/dist/index.d.ts +4 -5
- package/dist/index.js +65 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -45,7 +45,42 @@ function buildQueryString(params) {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
// src/services/createHttpClient.ts
|
|
48
|
+
var STORAGE_KEYS = {
|
|
49
|
+
accessToken: "arky_token",
|
|
50
|
+
refreshToken: "arky_refresh",
|
|
51
|
+
accessExpiresAt: "arky_expires_at"
|
|
52
|
+
};
|
|
53
|
+
function defaultGetToken() {
|
|
54
|
+
if (typeof window === "undefined") return { accessToken: "" };
|
|
55
|
+
return {
|
|
56
|
+
accessToken: localStorage.getItem(STORAGE_KEYS.accessToken) || "",
|
|
57
|
+
refreshToken: localStorage.getItem(STORAGE_KEYS.refreshToken) || "",
|
|
58
|
+
accessExpiresAt: parseInt(localStorage.getItem(STORAGE_KEYS.accessExpiresAt) || "0", 10)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function defaultSetToken(tokens) {
|
|
62
|
+
if (typeof window === "undefined") return;
|
|
63
|
+
if (tokens.accessToken) {
|
|
64
|
+
localStorage.setItem(STORAGE_KEYS.accessToken, tokens.accessToken);
|
|
65
|
+
localStorage.setItem(STORAGE_KEYS.refreshToken, tokens.refreshToken || "");
|
|
66
|
+
localStorage.setItem(STORAGE_KEYS.accessExpiresAt, (tokens.accessExpiresAt || 0).toString());
|
|
67
|
+
} else {
|
|
68
|
+
Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function defaultLogout() {
|
|
72
|
+
if (typeof window === "undefined") return;
|
|
73
|
+
Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
|
|
74
|
+
}
|
|
75
|
+
function defaultIsAuthenticated() {
|
|
76
|
+
if (typeof window === "undefined") return false;
|
|
77
|
+
const token = localStorage.getItem(STORAGE_KEYS.accessToken) || "";
|
|
78
|
+
return token.startsWith("customer_") || token.startsWith("account_");
|
|
79
|
+
}
|
|
48
80
|
function createHttpClient(cfg) {
|
|
81
|
+
const getToken = cfg.getToken || defaultGetToken;
|
|
82
|
+
const setToken = cfg.setToken || defaultSetToken;
|
|
83
|
+
const logout = cfg.logout || defaultLogout;
|
|
49
84
|
const refreshEndpoint = `${cfg.baseUrl}${cfg.refreshPath || "/v1/auth/refresh"}`;
|
|
50
85
|
let refreshPromise = null;
|
|
51
86
|
async function ensureFreshToken() {
|
|
@@ -53,9 +88,9 @@ function createHttpClient(cfg) {
|
|
|
53
88
|
return refreshPromise;
|
|
54
89
|
}
|
|
55
90
|
refreshPromise = (async () => {
|
|
56
|
-
const { refreshToken } = await
|
|
91
|
+
const { refreshToken } = await getToken();
|
|
57
92
|
if (!refreshToken) {
|
|
58
|
-
|
|
93
|
+
logout();
|
|
59
94
|
const err = new Error("No refresh token available");
|
|
60
95
|
err.name = "ApiError";
|
|
61
96
|
err.statusCode = 401;
|
|
@@ -67,14 +102,14 @@ function createHttpClient(cfg) {
|
|
|
67
102
|
body: JSON.stringify({ refreshToken })
|
|
68
103
|
});
|
|
69
104
|
if (!refRes.ok) {
|
|
70
|
-
|
|
105
|
+
logout();
|
|
71
106
|
const err = new Error("Token refresh failed");
|
|
72
107
|
err.name = "ApiError";
|
|
73
108
|
err.statusCode = 401;
|
|
74
109
|
throw err;
|
|
75
110
|
}
|
|
76
111
|
const data = await refRes.json();
|
|
77
|
-
|
|
112
|
+
setToken(data);
|
|
78
113
|
})().finally(() => {
|
|
79
114
|
refreshPromise = null;
|
|
80
115
|
});
|
|
@@ -89,11 +124,11 @@ function createHttpClient(cfg) {
|
|
|
89
124
|
"Content-Type": "application/json",
|
|
90
125
|
...options?.headers || {}
|
|
91
126
|
};
|
|
92
|
-
let { accessToken, accessExpiresAt } = await
|
|
127
|
+
let { accessToken, accessExpiresAt } = await getToken();
|
|
93
128
|
const nowSec = Date.now() / 1e3;
|
|
94
129
|
if (accessExpiresAt && nowSec > accessExpiresAt) {
|
|
95
130
|
await ensureFreshToken();
|
|
96
|
-
const tokens = await
|
|
131
|
+
const tokens = await getToken();
|
|
97
132
|
accessToken = tokens.accessToken;
|
|
98
133
|
}
|
|
99
134
|
if (accessToken) {
|
|
@@ -126,7 +161,7 @@ function createHttpClient(cfg) {
|
|
|
126
161
|
if (res.status === 401 && !options?.["_retried"]) {
|
|
127
162
|
try {
|
|
128
163
|
await ensureFreshToken();
|
|
129
|
-
const tokens = await
|
|
164
|
+
const tokens = await getToken();
|
|
130
165
|
headers["Authorization"] = `Bearer ${tokens.accessToken}`;
|
|
131
166
|
fetchOpts.headers = headers;
|
|
132
167
|
return request(method, path, body, { ...options, _retried: true });
|
|
@@ -2714,7 +2749,7 @@ function getFirstAvailableFCId(variant, quantity = 1) {
|
|
|
2714
2749
|
}
|
|
2715
2750
|
|
|
2716
2751
|
// src/index.ts
|
|
2717
|
-
var SDK_VERSION = "0.7.
|
|
2752
|
+
var SDK_VERSION = "0.7.66";
|
|
2718
2753
|
var SUPPORTED_FRAMEWORKS = [
|
|
2719
2754
|
"astro",
|
|
2720
2755
|
"react",
|
|
@@ -2764,15 +2799,19 @@ function createUtilitySurface(apiConfig) {
|
|
|
2764
2799
|
}
|
|
2765
2800
|
async function createAdmin(config) {
|
|
2766
2801
|
const locale = config.locale || "en";
|
|
2767
|
-
const
|
|
2802
|
+
const getToken = config.getToken || defaultGetToken;
|
|
2803
|
+
const setToken = config.setToken || defaultSetToken;
|
|
2804
|
+
const logout = config.logout || defaultLogout;
|
|
2805
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2806
|
+
const httpClient = createHttpClient({ ...config, getToken, setToken, logout });
|
|
2768
2807
|
const apiConfig = {
|
|
2769
2808
|
httpClient,
|
|
2770
2809
|
businessId: config.businessId,
|
|
2771
2810
|
baseUrl: config.baseUrl,
|
|
2772
2811
|
market: config.market,
|
|
2773
2812
|
locale,
|
|
2774
|
-
setToken
|
|
2775
|
-
getToken
|
|
2813
|
+
setToken,
|
|
2814
|
+
getToken
|
|
2776
2815
|
};
|
|
2777
2816
|
const accountApi = createAccountApi(apiConfig);
|
|
2778
2817
|
const authApi = createAuthApi(apiConfig);
|
|
@@ -2961,9 +3000,9 @@ async function createAdmin(config) {
|
|
|
2961
3000
|
apiConfig.locale = locale2;
|
|
2962
3001
|
},
|
|
2963
3002
|
getLocale: () => apiConfig.locale,
|
|
2964
|
-
isAuthenticated
|
|
2965
|
-
logout
|
|
2966
|
-
setToken
|
|
3003
|
+
isAuthenticated,
|
|
3004
|
+
logout,
|
|
3005
|
+
setToken,
|
|
2967
3006
|
extractBlockValues,
|
|
2968
3007
|
utils: createUtilitySurface(apiConfig)
|
|
2969
3008
|
};
|
|
@@ -2971,8 +3010,15 @@ async function createAdmin(config) {
|
|
|
2971
3010
|
}
|
|
2972
3011
|
async function createStorefront(config) {
|
|
2973
3012
|
const locale = config.locale || "en";
|
|
3013
|
+
const getToken = config.getToken || defaultGetToken;
|
|
3014
|
+
const setToken = config.setToken || defaultSetToken;
|
|
3015
|
+
const logout = config.logout || defaultLogout;
|
|
3016
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2974
3017
|
const httpClient = createHttpClient({
|
|
2975
3018
|
...config,
|
|
3019
|
+
getToken,
|
|
3020
|
+
setToken,
|
|
3021
|
+
logout,
|
|
2976
3022
|
refreshPath: `/v1/storefront/${config.businessId}/customers/auth/refresh`
|
|
2977
3023
|
});
|
|
2978
3024
|
const apiConfig = {
|
|
@@ -2981,8 +3027,8 @@ async function createStorefront(config) {
|
|
|
2981
3027
|
baseUrl: config.baseUrl,
|
|
2982
3028
|
market: config.market,
|
|
2983
3029
|
locale,
|
|
2984
|
-
setToken
|
|
2985
|
-
getToken
|
|
3030
|
+
setToken,
|
|
3031
|
+
getToken
|
|
2986
3032
|
};
|
|
2987
3033
|
const storefrontApi = createStorefrontApi(apiConfig);
|
|
2988
3034
|
let sessionPromise = null;
|
|
@@ -3026,7 +3072,6 @@ async function createStorefront(config) {
|
|
|
3026
3072
|
analytics: {
|
|
3027
3073
|
track
|
|
3028
3074
|
},
|
|
3029
|
-
ensureSession,
|
|
3030
3075
|
setBusinessId: (businessId) => {
|
|
3031
3076
|
apiConfig.businessId = businessId;
|
|
3032
3077
|
},
|
|
@@ -3039,9 +3084,9 @@ async function createStorefront(config) {
|
|
|
3039
3084
|
apiConfig.locale = locale2;
|
|
3040
3085
|
},
|
|
3041
3086
|
getLocale: () => apiConfig.locale,
|
|
3042
|
-
isAuthenticated
|
|
3043
|
-
logout
|
|
3044
|
-
setToken
|
|
3087
|
+
isAuthenticated,
|
|
3088
|
+
logout,
|
|
3089
|
+
setToken,
|
|
3045
3090
|
extractBlockValues,
|
|
3046
3091
|
utils: createUtilitySurface(apiConfig)
|
|
3047
3092
|
};
|