arky-sdk 0.7.65 → 0.7.67
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 +114 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -5
- package/dist/index.d.ts +60 -5
- package/dist/index.js +114 -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 });
|
|
@@ -1868,6 +1903,49 @@ var createTaxonomyApi = (apiConfig) => {
|
|
|
1868
1903
|
};
|
|
1869
1904
|
};
|
|
1870
1905
|
|
|
1906
|
+
// src/api/analytics.ts
|
|
1907
|
+
var createAnalyticsApi = (apiConfig) => {
|
|
1908
|
+
return {
|
|
1909
|
+
async getSummary(params, options) {
|
|
1910
|
+
return apiConfig.httpClient.get(
|
|
1911
|
+
`/v1/businesses/${apiConfig.businessId}/analytics/summary`,
|
|
1912
|
+
{
|
|
1913
|
+
...options,
|
|
1914
|
+
params: params || {}
|
|
1915
|
+
}
|
|
1916
|
+
);
|
|
1917
|
+
},
|
|
1918
|
+
async getSeries(params, options) {
|
|
1919
|
+
const { metrics, ...rest } = params;
|
|
1920
|
+
return apiConfig.httpClient.get(
|
|
1921
|
+
`/v1/businesses/${apiConfig.businessId}/analytics/series`,
|
|
1922
|
+
{
|
|
1923
|
+
...options,
|
|
1924
|
+
params: { ...rest, metrics: metrics.join(",") }
|
|
1925
|
+
}
|
|
1926
|
+
);
|
|
1927
|
+
},
|
|
1928
|
+
async getTopEntities(params, options) {
|
|
1929
|
+
return apiConfig.httpClient.get(
|
|
1930
|
+
`/v1/businesses/${apiConfig.businessId}/analytics/top`,
|
|
1931
|
+
{
|
|
1932
|
+
...options,
|
|
1933
|
+
params
|
|
1934
|
+
}
|
|
1935
|
+
);
|
|
1936
|
+
},
|
|
1937
|
+
async getStatusBreakdown(params, options) {
|
|
1938
|
+
return apiConfig.httpClient.get(
|
|
1939
|
+
`/v1/businesses/${apiConfig.businessId}/analytics/status`,
|
|
1940
|
+
{
|
|
1941
|
+
...options,
|
|
1942
|
+
params
|
|
1943
|
+
}
|
|
1944
|
+
);
|
|
1945
|
+
}
|
|
1946
|
+
};
|
|
1947
|
+
};
|
|
1948
|
+
|
|
1871
1949
|
// src/api/storefront.ts
|
|
1872
1950
|
function groupCartToItems(cart) {
|
|
1873
1951
|
const groups = /* @__PURE__ */ new Map();
|
|
@@ -2714,7 +2792,7 @@ function getFirstAvailableFCId(variant, quantity = 1) {
|
|
|
2714
2792
|
}
|
|
2715
2793
|
|
|
2716
2794
|
// src/index.ts
|
|
2717
|
-
var SDK_VERSION = "0.7.
|
|
2795
|
+
var SDK_VERSION = "0.7.67";
|
|
2718
2796
|
var SUPPORTED_FRAMEWORKS = [
|
|
2719
2797
|
"astro",
|
|
2720
2798
|
"react",
|
|
@@ -2764,15 +2842,19 @@ function createUtilitySurface(apiConfig) {
|
|
|
2764
2842
|
}
|
|
2765
2843
|
async function createAdmin(config) {
|
|
2766
2844
|
const locale = config.locale || "en";
|
|
2767
|
-
const
|
|
2845
|
+
const getToken = config.getToken || defaultGetToken;
|
|
2846
|
+
const setToken = config.setToken || defaultSetToken;
|
|
2847
|
+
const logout = config.logout || defaultLogout;
|
|
2848
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2849
|
+
const httpClient = createHttpClient({ ...config, getToken, setToken, logout });
|
|
2768
2850
|
const apiConfig = {
|
|
2769
2851
|
httpClient,
|
|
2770
2852
|
businessId: config.businessId,
|
|
2771
2853
|
baseUrl: config.baseUrl,
|
|
2772
2854
|
market: config.market,
|
|
2773
2855
|
locale,
|
|
2774
|
-
setToken
|
|
2775
|
-
getToken
|
|
2856
|
+
setToken,
|
|
2857
|
+
getToken
|
|
2776
2858
|
};
|
|
2777
2859
|
const accountApi = createAccountApi(apiConfig);
|
|
2778
2860
|
const authApi = createAuthApi(apiConfig);
|
|
@@ -2801,6 +2883,7 @@ async function createAdmin(config) {
|
|
|
2801
2883
|
const formApi = createFormApi(apiConfig);
|
|
2802
2884
|
const taxonomyApi = createTaxonomyApi(apiConfig);
|
|
2803
2885
|
const emailTemplateApi = createEmailTemplateApi(apiConfig);
|
|
2886
|
+
const analyticsApi = createAnalyticsApi(apiConfig);
|
|
2804
2887
|
const sdk = {
|
|
2805
2888
|
auth: authApi,
|
|
2806
2889
|
account: accountApi,
|
|
@@ -2947,7 +3030,11 @@ async function createAdmin(config) {
|
|
|
2947
3030
|
}
|
|
2948
3031
|
},
|
|
2949
3032
|
analytics: {
|
|
2950
|
-
track
|
|
3033
|
+
track,
|
|
3034
|
+
getSummary: analyticsApi.getSummary,
|
|
3035
|
+
getSeries: analyticsApi.getSeries,
|
|
3036
|
+
getTopEntities: analyticsApi.getTopEntities,
|
|
3037
|
+
getStatusBreakdown: analyticsApi.getStatusBreakdown
|
|
2951
3038
|
},
|
|
2952
3039
|
setBusinessId: (businessId) => {
|
|
2953
3040
|
apiConfig.businessId = businessId;
|
|
@@ -2961,9 +3048,9 @@ async function createAdmin(config) {
|
|
|
2961
3048
|
apiConfig.locale = locale2;
|
|
2962
3049
|
},
|
|
2963
3050
|
getLocale: () => apiConfig.locale,
|
|
2964
|
-
isAuthenticated
|
|
2965
|
-
logout
|
|
2966
|
-
setToken
|
|
3051
|
+
isAuthenticated,
|
|
3052
|
+
logout,
|
|
3053
|
+
setToken,
|
|
2967
3054
|
extractBlockValues,
|
|
2968
3055
|
utils: createUtilitySurface(apiConfig)
|
|
2969
3056
|
};
|
|
@@ -2971,8 +3058,15 @@ async function createAdmin(config) {
|
|
|
2971
3058
|
}
|
|
2972
3059
|
async function createStorefront(config) {
|
|
2973
3060
|
const locale = config.locale || "en";
|
|
3061
|
+
const getToken = config.getToken || defaultGetToken;
|
|
3062
|
+
const setToken = config.setToken || defaultSetToken;
|
|
3063
|
+
const logout = config.logout || defaultLogout;
|
|
3064
|
+
const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
|
|
2974
3065
|
const httpClient = createHttpClient({
|
|
2975
3066
|
...config,
|
|
3067
|
+
getToken,
|
|
3068
|
+
setToken,
|
|
3069
|
+
logout,
|
|
2976
3070
|
refreshPath: `/v1/storefront/${config.businessId}/customers/auth/refresh`
|
|
2977
3071
|
});
|
|
2978
3072
|
const apiConfig = {
|
|
@@ -2981,8 +3075,8 @@ async function createStorefront(config) {
|
|
|
2981
3075
|
baseUrl: config.baseUrl,
|
|
2982
3076
|
market: config.market,
|
|
2983
3077
|
locale,
|
|
2984
|
-
setToken
|
|
2985
|
-
getToken
|
|
3078
|
+
setToken,
|
|
3079
|
+
getToken
|
|
2986
3080
|
};
|
|
2987
3081
|
const storefrontApi = createStorefrontApi(apiConfig);
|
|
2988
3082
|
let sessionPromise = null;
|
|
@@ -3038,9 +3132,9 @@ async function createStorefront(config) {
|
|
|
3038
3132
|
apiConfig.locale = locale2;
|
|
3039
3133
|
},
|
|
3040
3134
|
getLocale: () => apiConfig.locale,
|
|
3041
|
-
isAuthenticated
|
|
3042
|
-
logout
|
|
3043
|
-
setToken
|
|
3135
|
+
isAuthenticated,
|
|
3136
|
+
logout,
|
|
3137
|
+
setToken,
|
|
3044
3138
|
extractBlockValues,
|
|
3045
3139
|
utils: createUtilitySurface(apiConfig)
|
|
3046
3140
|
};
|