@commercengine/storefront-sdk 0.3.5 → 0.3.7
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.d.ts +4 -3
- package/dist/index.js +7 -5
- package/dist/lib/middleware.d.ts +46 -0
- package/dist/lib/middleware.js +88 -3
- package/dist/types/storefront.d.ts +11 -11
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { OrderClient } from "./lib/order";
|
|
|
6
6
|
import { ShippingClient } from "./lib/shipping";
|
|
7
7
|
import { HelpersClient } from "./lib/helper";
|
|
8
8
|
import { CustomerClient } from "./lib/customer";
|
|
9
|
-
import { TokenStorage, MemoryTokenStorage, BrowserTokenStorage } from "./lib/middleware";
|
|
9
|
+
import { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage } from "./lib/middleware";
|
|
10
10
|
import { type UserInfo } from "./lib/jwt-utils";
|
|
11
11
|
/**
|
|
12
12
|
* SDK initialization options
|
|
@@ -168,9 +168,10 @@ export declare class StorefrontSDK {
|
|
|
168
168
|
getCustomerGroupId(): Promise<string | null>;
|
|
169
169
|
}
|
|
170
170
|
export default StorefrontSDK;
|
|
171
|
-
export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient };
|
|
171
|
+
export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, };
|
|
172
172
|
export { Environment };
|
|
173
|
-
export { TokenStorage, MemoryTokenStorage, BrowserTokenStorage };
|
|
173
|
+
export { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, };
|
|
174
|
+
export type { CookieTokenStorageOptions } from "./lib/middleware";
|
|
174
175
|
export type { UserInfo } from "./lib/jwt-utils";
|
|
175
176
|
export type { components, operations, paths } from "./types/storefront";
|
|
176
177
|
export type * from "./types/storefront-api-types";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StorefrontAPIClient, Environment
|
|
1
|
+
import { StorefrontAPIClient, Environment } from "./lib/client";
|
|
2
2
|
import { CatalogClient } from "./lib/catalog";
|
|
3
3
|
import { CartClient } from "./lib/cart";
|
|
4
4
|
import { AuthClient } from "./lib/auth";
|
|
@@ -6,7 +6,7 @@ import { OrderClient } from "./lib/order";
|
|
|
6
6
|
import { ShippingClient } from "./lib/shipping";
|
|
7
7
|
import { HelpersClient } from "./lib/helper";
|
|
8
8
|
import { CustomerClient } from "./lib/customer";
|
|
9
|
-
import { MemoryTokenStorage, BrowserTokenStorage, } from "./lib/middleware";
|
|
9
|
+
import { MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, } from "./lib/middleware";
|
|
10
10
|
import { extractUserInfoFromToken, getUserIdFromToken, isUserLoggedIn, isUserAnonymous, } from "./lib/jwt-utils";
|
|
11
11
|
/**
|
|
12
12
|
* Main SDK class for the Storefront API
|
|
@@ -132,7 +132,9 @@ export class StorefrontSDK {
|
|
|
132
132
|
* Get the current access token if using token storage
|
|
133
133
|
*/
|
|
134
134
|
async getAccessToken() {
|
|
135
|
-
return await this.auth
|
|
135
|
+
return await this.auth
|
|
136
|
+
.getAuthorizationHeader()
|
|
137
|
+
.then((header) => header.startsWith("Bearer ") ? header.substring(7) : null);
|
|
136
138
|
}
|
|
137
139
|
/**
|
|
138
140
|
* Get user information from the current access token
|
|
@@ -200,8 +202,8 @@ export class StorefrontSDK {
|
|
|
200
202
|
// Export the main SDK class
|
|
201
203
|
export default StorefrontSDK;
|
|
202
204
|
// Export individual clients for advanced usage
|
|
203
|
-
export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient };
|
|
205
|
+
export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient, };
|
|
204
206
|
// Export environment enum
|
|
205
207
|
export { Environment };
|
|
206
208
|
// Export token storage types
|
|
207
|
-
export { MemoryTokenStorage, BrowserTokenStorage };
|
|
209
|
+
export { MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, };
|
package/dist/lib/middleware.d.ts
CHANGED
|
@@ -34,6 +34,52 @@ export declare class BrowserTokenStorage implements TokenStorage {
|
|
|
34
34
|
setRefreshToken(token: string): Promise<void>;
|
|
35
35
|
clearTokens(): Promise<void>;
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Cookie-based token storage implementation
|
|
39
|
+
*/
|
|
40
|
+
export declare class CookieTokenStorage implements TokenStorage {
|
|
41
|
+
private accessTokenKey;
|
|
42
|
+
private refreshTokenKey;
|
|
43
|
+
private options;
|
|
44
|
+
constructor(options?: CookieTokenStorageOptions);
|
|
45
|
+
getAccessToken(): Promise<string | null>;
|
|
46
|
+
setAccessToken(token: string): Promise<void>;
|
|
47
|
+
getRefreshToken(): Promise<string | null>;
|
|
48
|
+
setRefreshToken(token: string): Promise<void>;
|
|
49
|
+
clearTokens(): Promise<void>;
|
|
50
|
+
private getCookie;
|
|
51
|
+
private setCookie;
|
|
52
|
+
private deleteCookie;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Configuration options for CookieTokenStorage
|
|
56
|
+
*/
|
|
57
|
+
export interface CookieTokenStorageOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Prefix for cookie names (default: "storefront_")
|
|
60
|
+
*/
|
|
61
|
+
prefix?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Maximum age of cookies in seconds (default: 7 days)
|
|
64
|
+
*/
|
|
65
|
+
maxAge?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Cookie path (default: "/")
|
|
68
|
+
*/
|
|
69
|
+
path?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Cookie domain (default: current domain)
|
|
72
|
+
*/
|
|
73
|
+
domain?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Whether cookies should be secure (default: auto-detect based on protocol)
|
|
76
|
+
*/
|
|
77
|
+
secure?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* SameSite cookie attribute (default: "Lax")
|
|
80
|
+
*/
|
|
81
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
82
|
+
}
|
|
37
83
|
/**
|
|
38
84
|
* Configuration for the auth middleware
|
|
39
85
|
*/
|
package/dist/lib/middleware.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isTokenExpired } from "./jwt-utils";
|
|
2
|
-
import { getPathnameFromUrl, isAnonymousAuthEndpoint, isTokenReturningEndpoint, isLogoutEndpoint } from "./auth-utils";
|
|
2
|
+
import { getPathnameFromUrl, isAnonymousAuthEndpoint, isTokenReturningEndpoint, isLogoutEndpoint, } from "./auth-utils";
|
|
3
3
|
/**
|
|
4
4
|
* Simple in-memory token storage implementation
|
|
5
5
|
*/
|
|
@@ -60,6 +60,90 @@ export class BrowserTokenStorage {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Cookie-based token storage implementation
|
|
65
|
+
*/
|
|
66
|
+
export class CookieTokenStorage {
|
|
67
|
+
accessTokenKey;
|
|
68
|
+
refreshTokenKey;
|
|
69
|
+
options;
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
const prefix = options.prefix || "storefront_";
|
|
72
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
73
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
74
|
+
this.options = {
|
|
75
|
+
maxAge: options.maxAge || 7 * 24 * 60 * 60, // 7 days default
|
|
76
|
+
path: options.path || "/",
|
|
77
|
+
domain: options.domain,
|
|
78
|
+
secure: options.secure ??
|
|
79
|
+
(typeof window !== "undefined" &&
|
|
80
|
+
window.location?.protocol === "https:"),
|
|
81
|
+
sameSite: options.sameSite || "Lax",
|
|
82
|
+
httpOnly: false, // Must be false for client-side access
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
async getAccessToken() {
|
|
86
|
+
return this.getCookie(this.accessTokenKey);
|
|
87
|
+
}
|
|
88
|
+
async setAccessToken(token) {
|
|
89
|
+
this.setCookie(this.accessTokenKey, token);
|
|
90
|
+
}
|
|
91
|
+
async getRefreshToken() {
|
|
92
|
+
return this.getCookie(this.refreshTokenKey);
|
|
93
|
+
}
|
|
94
|
+
async setRefreshToken(token) {
|
|
95
|
+
this.setCookie(this.refreshTokenKey, token);
|
|
96
|
+
}
|
|
97
|
+
async clearTokens() {
|
|
98
|
+
this.deleteCookie(this.accessTokenKey);
|
|
99
|
+
this.deleteCookie(this.refreshTokenKey);
|
|
100
|
+
}
|
|
101
|
+
getCookie(name) {
|
|
102
|
+
if (typeof document === "undefined")
|
|
103
|
+
return null;
|
|
104
|
+
const value = `; ${document.cookie}`;
|
|
105
|
+
const parts = value.split(`; ${name}=`);
|
|
106
|
+
if (parts.length === 2) {
|
|
107
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
108
|
+
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
setCookie(name, value) {
|
|
113
|
+
if (typeof document === "undefined")
|
|
114
|
+
return;
|
|
115
|
+
const encodedValue = encodeURIComponent(value);
|
|
116
|
+
let cookieString = `${name}=${encodedValue}`;
|
|
117
|
+
if (this.options.maxAge) {
|
|
118
|
+
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
119
|
+
}
|
|
120
|
+
if (this.options.path) {
|
|
121
|
+
cookieString += `; Path=${this.options.path}`;
|
|
122
|
+
}
|
|
123
|
+
if (this.options.domain) {
|
|
124
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
125
|
+
}
|
|
126
|
+
if (this.options.secure) {
|
|
127
|
+
cookieString += `; Secure`;
|
|
128
|
+
}
|
|
129
|
+
if (this.options.sameSite) {
|
|
130
|
+
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
131
|
+
}
|
|
132
|
+
document.cookie = cookieString;
|
|
133
|
+
}
|
|
134
|
+
deleteCookie(name) {
|
|
135
|
+
if (typeof document === "undefined")
|
|
136
|
+
return;
|
|
137
|
+
let cookieString = `${name}=; Max-Age=0`;
|
|
138
|
+
if (this.options.path) {
|
|
139
|
+
cookieString += `; Path=${this.options.path}`;
|
|
140
|
+
}
|
|
141
|
+
if (this.options.domain) {
|
|
142
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
143
|
+
}
|
|
144
|
+
document.cookie = cookieString;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
63
147
|
/**
|
|
64
148
|
* Create authentication middleware for openapi-fetch
|
|
65
149
|
*/
|
|
@@ -115,7 +199,7 @@ export function createAuthMiddleware(config) {
|
|
|
115
199
|
headers: {
|
|
116
200
|
"Content-Type": "application/json",
|
|
117
201
|
...(config.apiKey && { "X-Api-Key": config.apiKey }),
|
|
118
|
-
|
|
202
|
+
Authorization: `Bearer ${currentAccessToken}`, // For user_id continuity
|
|
119
203
|
},
|
|
120
204
|
});
|
|
121
205
|
if (!response.ok) {
|
|
@@ -184,7 +268,8 @@ export function createAuthMiddleware(config) {
|
|
|
184
268
|
const pathname = getPathnameFromUrl(request.url);
|
|
185
269
|
// Handle successful responses that return tokens
|
|
186
270
|
if (response.ok) {
|
|
187
|
-
if (isTokenReturningEndpoint(pathname) ||
|
|
271
|
+
if (isTokenReturningEndpoint(pathname) ||
|
|
272
|
+
isAnonymousAuthEndpoint(pathname)) {
|
|
188
273
|
try {
|
|
189
274
|
const data = await response.clone().json();
|
|
190
275
|
const content = data.content;
|
|
@@ -1836,6 +1836,8 @@ export interface components {
|
|
|
1836
1836
|
readonly active: boolean;
|
|
1837
1837
|
/** @description Indicates whether the product has stock available */
|
|
1838
1838
|
readonly stock_available: boolean;
|
|
1839
|
+
/** @description Indicates whether the item is being fulfilled as a backorder. When true, the item is not currently in stock and will ship later once inventory is available. This may result in the order being split into multiple shipments, with delays for the backordered portion. */
|
|
1840
|
+
backorder?: boolean;
|
|
1839
1841
|
/** @description Indicates whether this item is associated with any active (product-specific) coupons */
|
|
1840
1842
|
readonly on_offer: boolean;
|
|
1841
1843
|
/** @description Indicates whether the product has any subscription plans avaialble */
|
|
@@ -2145,6 +2147,8 @@ export interface components {
|
|
|
2145
2147
|
short_description: string | null;
|
|
2146
2148
|
readonly active: boolean;
|
|
2147
2149
|
readonly stock_available: boolean;
|
|
2150
|
+
/** @description Indicates whether the item is being fulfilled as a backorder. When true, the item is not currently in stock and will ship later once inventory is available. This may result in the order being split into multiple shipments, with delays for the backordered portion. */
|
|
2151
|
+
backorder?: boolean;
|
|
2148
2152
|
/** @description Indicates whether this item is associated with any active (product-specific) coupons */
|
|
2149
2153
|
readonly on_offer: boolean;
|
|
2150
2154
|
readonly on_promotion: boolean;
|
|
@@ -2213,6 +2217,8 @@ export interface components {
|
|
|
2213
2217
|
short_description: string | null;
|
|
2214
2218
|
active: boolean;
|
|
2215
2219
|
stock_available: boolean;
|
|
2220
|
+
/** @description Indicates whether the item is being fulfilled as a backorder. When true, the item is not currently in stock and will ship later once inventory is available. This may result in the order being split into multiple shipments, with delays for the backordered portion. */
|
|
2221
|
+
backorder?: boolean;
|
|
2216
2222
|
/** @description Indicates whether this item is associated with any active (product-specific) coupons */
|
|
2217
2223
|
on_offer: boolean;
|
|
2218
2224
|
on_subscription: boolean;
|
|
@@ -2227,6 +2233,7 @@ export interface components {
|
|
|
2227
2233
|
promotion: components["schemas"]["ProductPromotion"];
|
|
2228
2234
|
subscription: components["schemas"]["ProductSubscription"][];
|
|
2229
2235
|
associated_options: components["schemas"]["AssociatedOption"] | null;
|
|
2236
|
+
shipping?: components["schemas"]["ProductShipping"];
|
|
2230
2237
|
};
|
|
2231
2238
|
/** @description pagination metadata structure */
|
|
2232
2239
|
Pagination: {
|
|
@@ -2266,17 +2273,10 @@ export interface components {
|
|
|
2266
2273
|
};
|
|
2267
2274
|
/** ProductShipping */
|
|
2268
2275
|
ProductShipping: {
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
/** @default false */
|
|
2274
|
-
ships_internationally: boolean;
|
|
2275
|
-
mid_code: string | null;
|
|
2276
|
-
/** @default India */
|
|
2277
|
-
country_of_origin: string | null;
|
|
2278
|
-
tax_type: string | null;
|
|
2279
|
-
tax_rate: number | null;
|
|
2276
|
+
handling_charges_including_tax: number;
|
|
2277
|
+
handling_charges_excluding_tax: number;
|
|
2278
|
+
tax_type: string;
|
|
2279
|
+
tax_rate: number;
|
|
2280
2280
|
} | null;
|
|
2281
2281
|
/** Seo */
|
|
2282
2282
|
Seo: {
|