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.d.cts
CHANGED
|
@@ -12,9 +12,9 @@ interface HttpClientConfig {
|
|
|
12
12
|
baseUrl: string;
|
|
13
13
|
businessId: string;
|
|
14
14
|
refreshPath?: string;
|
|
15
|
-
getToken
|
|
16
|
-
setToken
|
|
17
|
-
logout
|
|
15
|
+
getToken?: () => Promise<AuthTokens> | AuthTokens;
|
|
16
|
+
setToken?: (tokens: AuthTokens) => void;
|
|
17
|
+
logout?: () => void;
|
|
18
18
|
navigate?: (path: string) => void;
|
|
19
19
|
loginFallbackPath?: string;
|
|
20
20
|
isAuthenticated?: () => boolean;
|
|
@@ -148,7 +148,7 @@ declare global {
|
|
|
148
148
|
}
|
|
149
149
|
declare function track(eventName: string, params?: Record<string, any>): void;
|
|
150
150
|
|
|
151
|
-
declare const SDK_VERSION = "0.7.
|
|
151
|
+
declare const SDK_VERSION = "0.7.66";
|
|
152
152
|
declare const SUPPORTED_FRAMEWORKS: readonly ["astro", "react", "vue", "svelte", "vanilla"];
|
|
153
153
|
interface ApiConfig {
|
|
154
154
|
httpClient: any;
|
|
@@ -636,7 +636,6 @@ declare function createStorefront(config: HttpClientConfig & {
|
|
|
636
636
|
analytics: {
|
|
637
637
|
track: typeof track;
|
|
638
638
|
};
|
|
639
|
-
ensureSession: () => Promise<void>;
|
|
640
639
|
setBusinessId: (businessId: string) => void;
|
|
641
640
|
getBusinessId: () => string;
|
|
642
641
|
setMarket: (market: string) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,9 +12,9 @@ interface HttpClientConfig {
|
|
|
12
12
|
baseUrl: string;
|
|
13
13
|
businessId: string;
|
|
14
14
|
refreshPath?: string;
|
|
15
|
-
getToken
|
|
16
|
-
setToken
|
|
17
|
-
logout
|
|
15
|
+
getToken?: () => Promise<AuthTokens> | AuthTokens;
|
|
16
|
+
setToken?: (tokens: AuthTokens) => void;
|
|
17
|
+
logout?: () => void;
|
|
18
18
|
navigate?: (path: string) => void;
|
|
19
19
|
loginFallbackPath?: string;
|
|
20
20
|
isAuthenticated?: () => boolean;
|
|
@@ -148,7 +148,7 @@ declare global {
|
|
|
148
148
|
}
|
|
149
149
|
declare function track(eventName: string, params?: Record<string, any>): void;
|
|
150
150
|
|
|
151
|
-
declare const SDK_VERSION = "0.7.
|
|
151
|
+
declare const SDK_VERSION = "0.7.66";
|
|
152
152
|
declare const SUPPORTED_FRAMEWORKS: readonly ["astro", "react", "vue", "svelte", "vanilla"];
|
|
153
153
|
interface ApiConfig {
|
|
154
154
|
httpClient: any;
|
|
@@ -636,7 +636,6 @@ declare function createStorefront(config: HttpClientConfig & {
|
|
|
636
636
|
analytics: {
|
|
637
637
|
track: typeof track;
|
|
638
638
|
};
|
|
639
|
-
ensureSession: () => Promise<void>;
|
|
640
639
|
setBusinessId: (businessId: string) => void;
|
|
641
640
|
getBusinessId: () => string;
|
|
642
641
|
setMarket: (market: string) => void;
|
package/dist/index.js
CHANGED
|
@@ -43,7 +43,42 @@ function buildQueryString(params) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// src/services/createHttpClient.ts
|
|
46
|
+
var STORAGE_KEYS = {
|
|
47
|
+
accessToken: "arky_token",
|
|
48
|
+
refreshToken: "arky_refresh",
|
|
49
|
+
accessExpiresAt: "arky_expires_at"
|
|
50
|
+
};
|
|
51
|
+
function defaultGetToken() {
|
|
52
|
+
if (typeof window === "undefined") return { accessToken: "" };
|
|
53
|
+
return {
|
|
54
|
+
accessToken: localStorage.getItem(STORAGE_KEYS.accessToken) || "",
|
|
55
|
+
refreshToken: localStorage.getItem(STORAGE_KEYS.refreshToken) || "",
|
|
56
|
+
accessExpiresAt: parseInt(localStorage.getItem(STORAGE_KEYS.accessExpiresAt) || "0", 10)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function defaultSetToken(tokens) {
|
|
60
|
+
if (typeof window === "undefined") return;
|
|
61
|
+
if (tokens.accessToken) {
|
|
62
|
+
localStorage.setItem(STORAGE_KEYS.accessToken, tokens.accessToken);
|
|
63
|
+
localStorage.setItem(STORAGE_KEYS.refreshToken, tokens.refreshToken || "");
|
|
64
|
+
localStorage.setItem(STORAGE_KEYS.accessExpiresAt, (tokens.accessExpiresAt || 0).toString());
|
|
65
|
+
} else {
|
|
66
|
+
Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function defaultLogout() {
|
|
70
|
+
if (typeof window === "undefined") return;
|
|
71
|
+
Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
|
|
72
|
+
}
|
|
73
|
+
function defaultIsAuthenticated() {
|
|
74
|
+
if (typeof window === "undefined") return false;
|
|
75
|
+
const token = localStorage.getItem(STORAGE_KEYS.accessToken) || "";
|
|
76
|
+
return token.startsWith("customer_") || token.startsWith("account_");
|
|
77
|
+
}
|
|
46
78
|
function createHttpClient(cfg) {
|
|
79
|
+
const getToken = cfg.getToken || defaultGetToken;
|
|
80
|
+
const setToken = cfg.setToken || defaultSetToken;
|
|
81
|
+
const logout = cfg.logout || defaultLogout;
|
|
47
82
|
const refreshEndpoint = `${cfg.baseUrl}${cfg.refreshPath || "/v1/auth/refresh"}`;
|
|
48
83
|
let refreshPromise = null;
|
|
49
84
|
async function ensureFreshToken() {
|
|
@@ -51,9 +86,9 @@ function createHttpClient(cfg) {
|
|
|
51
86
|
return refreshPromise;
|
|
52
87
|
}
|
|
53
88
|
refreshPromise = (async () => {
|
|
54
|
-
const { refreshToken } = await
|
|
89
|
+
const { refreshToken } = await getToken();
|
|
55
90
|
if (!refreshToken) {
|
|
56
|
-
|
|
91
|
+
logout();
|
|
57
92
|
const err = new Error("No refresh token available");
|
|
58
93
|
err.name = "ApiError";
|
|
59
94
|
err.statusCode = 401;
|
|
@@ -65,14 +100,14 @@ function createHttpClient(cfg) {
|
|
|
65
100
|
body: JSON.stringify({ refreshToken })
|
|
66
101
|
});
|
|
67
102
|
if (!refRes.ok) {
|
|
68
|
-
|
|
103
|
+
logout();
|
|
69
104
|
const err = new Error("Token refresh failed");
|
|
70
105
|
err.name = "ApiError";
|
|
71
106
|
err.statusCode = 401;
|
|
72
107
|
throw err;
|
|
73
108
|
}
|
|
74
109
|
const data = await refRes.json();
|
|
75
|
-
|
|
110
|
+
setToken(data);
|
|
76
111
|
})().finally(() => {
|
|
77
112
|
refreshPromise = null;
|
|
78
113
|
});
|
|
@@ -87,11 +122,11 @@ function createHttpClient(cfg) {
|
|
|
87
122
|
"Content-Type": "application/json",
|
|
88
123
|
...options?.headers || {}
|
|
89
124
|
};
|
|
90
|
-
let { accessToken, accessExpiresAt } = await
|
|
125
|
+
let { accessToken, accessExpiresAt } = await getToken();
|
|
91
126
|
const nowSec = Date.now() / 1e3;
|
|
92
127
|
if (accessExpiresAt && nowSec > accessExpiresAt) {
|
|
93
128
|
await ensureFreshToken();
|
|
94
|
-
const tokens = await
|
|
129
|
+
const tokens = await getToken();
|
|
95
130
|
accessToken = tokens.accessToken;
|
|
96
131
|
}
|
|
97
132
|
if (accessToken) {
|
|
@@ -124,7 +159,7 @@ function createHttpClient(cfg) {
|
|
|
124
159
|
if (res.status === 401 && !options?.["_retried"]) {
|
|
125
160
|
try {
|
|
126
161
|
await ensureFreshToken();
|
|
127
|
-
const tokens = await
|
|
162
|
+
const tokens = await getToken();
|
|
128
163
|
headers["Authorization"] = `Bearer ${tokens.accessToken}`;
|
|
129
164
|
fetchOpts.headers = headers;
|
|
130
165
|
return request(method, path, body, { ...options, _retried: true });
|
|
@@ -2712,7 +2747,7 @@ function getFirstAvailableFCId(variant, quantity = 1) {
|
|
|
2712
2747
|
}
|
|
2713
2748
|
|
|
2714
2749
|
// src/index.ts
|
|
2715
|
-
var SDK_VERSION = "0.7.
|
|
2750
|
+
var SDK_VERSION = "0.7.66";
|
|
2716
2751
|
var SUPPORTED_FRAMEWORKS = [
|
|
2717
2752
|
"astro",
|
|
2718
2753
|
"react",
|
|
@@ -2762,15 +2797,19 @@ function createUtilitySurface(apiConfig) {
|
|
|
2762
2797
|
}
|
|
2763
2798
|
async function createAdmin(config) {
|
|
2764
2799
|
const locale = config.locale || "en";
|
|
2765
|
-
const
|
|
2800
|
+
const getToken = config.getToken || defaultGetToken;
|
|
2801
|
+
const setToken = config.setToken || defaultSetToken;
|
|
2802
|
+
const logout = config.logout || defaultLogout;
|
|
2803
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2804
|
+
const httpClient = createHttpClient({ ...config, getToken, setToken, logout });
|
|
2766
2805
|
const apiConfig = {
|
|
2767
2806
|
httpClient,
|
|
2768
2807
|
businessId: config.businessId,
|
|
2769
2808
|
baseUrl: config.baseUrl,
|
|
2770
2809
|
market: config.market,
|
|
2771
2810
|
locale,
|
|
2772
|
-
setToken
|
|
2773
|
-
getToken
|
|
2811
|
+
setToken,
|
|
2812
|
+
getToken
|
|
2774
2813
|
};
|
|
2775
2814
|
const accountApi = createAccountApi(apiConfig);
|
|
2776
2815
|
const authApi = createAuthApi(apiConfig);
|
|
@@ -2959,9 +2998,9 @@ async function createAdmin(config) {
|
|
|
2959
2998
|
apiConfig.locale = locale2;
|
|
2960
2999
|
},
|
|
2961
3000
|
getLocale: () => apiConfig.locale,
|
|
2962
|
-
isAuthenticated
|
|
2963
|
-
logout
|
|
2964
|
-
setToken
|
|
3001
|
+
isAuthenticated,
|
|
3002
|
+
logout,
|
|
3003
|
+
setToken,
|
|
2965
3004
|
extractBlockValues,
|
|
2966
3005
|
utils: createUtilitySurface(apiConfig)
|
|
2967
3006
|
};
|
|
@@ -2969,8 +3008,15 @@ async function createAdmin(config) {
|
|
|
2969
3008
|
}
|
|
2970
3009
|
async function createStorefront(config) {
|
|
2971
3010
|
const locale = config.locale || "en";
|
|
3011
|
+
const getToken = config.getToken || defaultGetToken;
|
|
3012
|
+
const setToken = config.setToken || defaultSetToken;
|
|
3013
|
+
const logout = config.logout || defaultLogout;
|
|
3014
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2972
3015
|
const httpClient = createHttpClient({
|
|
2973
3016
|
...config,
|
|
3017
|
+
getToken,
|
|
3018
|
+
setToken,
|
|
3019
|
+
logout,
|
|
2974
3020
|
refreshPath: `/v1/storefront/${config.businessId}/customers/auth/refresh`
|
|
2975
3021
|
});
|
|
2976
3022
|
const apiConfig = {
|
|
@@ -2979,8 +3025,8 @@ async function createStorefront(config) {
|
|
|
2979
3025
|
baseUrl: config.baseUrl,
|
|
2980
3026
|
market: config.market,
|
|
2981
3027
|
locale,
|
|
2982
|
-
setToken
|
|
2983
|
-
getToken
|
|
3028
|
+
setToken,
|
|
3029
|
+
getToken
|
|
2984
3030
|
};
|
|
2985
3031
|
const storefrontApi = createStorefrontApi(apiConfig);
|
|
2986
3032
|
let sessionPromise = null;
|
|
@@ -3024,7 +3070,6 @@ async function createStorefront(config) {
|
|
|
3024
3070
|
analytics: {
|
|
3025
3071
|
track
|
|
3026
3072
|
},
|
|
3027
|
-
ensureSession,
|
|
3028
3073
|
setBusinessId: (businessId) => {
|
|
3029
3074
|
apiConfig.businessId = businessId;
|
|
3030
3075
|
},
|
|
@@ -3037,9 +3082,9 @@ async function createStorefront(config) {
|
|
|
3037
3082
|
apiConfig.locale = locale2;
|
|
3038
3083
|
},
|
|
3039
3084
|
getLocale: () => apiConfig.locale,
|
|
3040
|
-
isAuthenticated
|
|
3041
|
-
logout
|
|
3042
|
-
setToken
|
|
3085
|
+
isAuthenticated,
|
|
3086
|
+
logout,
|
|
3087
|
+
setToken,
|
|
3043
3088
|
extractBlockValues,
|
|
3044
3089
|
utils: createUtilitySurface(apiConfig)
|
|
3045
3090
|
};
|