@commercengine/storefront-sdk 0.3.0 → 0.3.2
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 +90 -39
- package/dist/index.js +156 -127
- package/dist/lib/auth-utils.d.ts +16 -0
- package/dist/lib/auth-utils.js +38 -0
- package/dist/lib/auth.d.ts +105 -195
- package/dist/lib/auth.js +221 -653
- package/dist/lib/cart.d.ts +77 -80
- package/dist/lib/cart.js +183 -221
- package/dist/lib/catalog.d.ts +36 -69
- package/dist/lib/catalog.js +109 -118
- package/dist/lib/client.d.ts +37 -84
- package/dist/lib/client.js +148 -173
- package/dist/lib/customer.d.ts +87 -0
- package/dist/lib/customer.js +153 -0
- package/dist/lib/helper.d.ts +27 -0
- package/dist/lib/helper.js +40 -0
- package/dist/lib/jwt-utils.d.ts +75 -0
- package/dist/lib/jwt-utils.js +84 -0
- package/dist/lib/middleware.d.ts +83 -0
- package/dist/lib/middleware.js +248 -0
- package/dist/lib/order.d.ts +72 -0
- package/dist/lib/order.js +125 -0
- package/dist/lib/shipping.d.ts +14 -0
- package/dist/lib/shipping.js +17 -0
- package/dist/types/storefront-api-types.d.ts +359 -0
- package/dist/types/storefront-api-types.js +3 -0
- package/dist/types/storefront.d.ts +7976 -7369
- package/package.json +21 -12
package/dist/lib/catalog.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CatalogClient = void 0;
|
|
4
|
-
const client_1 = require("./client");
|
|
1
|
+
import { StorefrontAPIClient } from "./client";
|
|
5
2
|
/**
|
|
6
3
|
* Client for interacting with catalog endpoints
|
|
7
4
|
*/
|
|
8
|
-
class CatalogClient extends
|
|
5
|
+
export class CatalogClient extends StorefrontAPIClient {
|
|
9
6
|
/**
|
|
10
7
|
* List all products
|
|
11
8
|
*
|
|
@@ -13,15 +10,20 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
13
10
|
* @returns Promise with products and pagination info
|
|
14
11
|
*/
|
|
15
12
|
async listProducts(options) {
|
|
16
|
-
return this.executeRequest(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
return this.executeRequest(() => this.client.GET('/catalog/products', {
|
|
14
|
+
params: { query: options },
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* List all skus
|
|
19
|
+
*
|
|
20
|
+
* @param options - Optional query parameters
|
|
21
|
+
* @returns Promise with skus and pagination info
|
|
22
|
+
*/
|
|
23
|
+
async listSkus(options) {
|
|
24
|
+
return this.executeRequest(() => this.client.GET('/catalog/skus', {
|
|
25
|
+
params: { query: options },
|
|
26
|
+
}));
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Get details for a specific product
|
|
@@ -30,19 +32,13 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
30
32
|
* @param options - Optional query parameters
|
|
31
33
|
* @returns Promise with product details
|
|
32
34
|
*/
|
|
33
|
-
async getProductDetail(
|
|
34
|
-
return this.executeRequest(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
if (error) {
|
|
42
|
-
this.handleError(error);
|
|
43
|
-
}
|
|
44
|
-
return data?.content;
|
|
45
|
-
});
|
|
35
|
+
async getProductDetail(pathParams, queryParams) {
|
|
36
|
+
return this.executeRequest(() => this.client.GET('/catalog/products/{product_id}', {
|
|
37
|
+
params: {
|
|
38
|
+
path: pathParams,
|
|
39
|
+
query: queryParams,
|
|
40
|
+
},
|
|
41
|
+
}));
|
|
46
42
|
}
|
|
47
43
|
/**
|
|
48
44
|
* List variants for a specific product
|
|
@@ -51,19 +47,13 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
51
47
|
* @param options - Optional query parameters
|
|
52
48
|
* @returns Promise with variants
|
|
53
49
|
*/
|
|
54
|
-
async listProductVariants(
|
|
55
|
-
return this.executeRequest(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
});
|
|
62
|
-
if (error) {
|
|
63
|
-
this.handleError(error);
|
|
64
|
-
}
|
|
65
|
-
return data?.content;
|
|
66
|
-
});
|
|
50
|
+
async listProductVariants(pathParams, queryParams) {
|
|
51
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants", {
|
|
52
|
+
params: {
|
|
53
|
+
path: pathParams,
|
|
54
|
+
query: queryParams,
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
67
57
|
}
|
|
68
58
|
/**
|
|
69
59
|
* Get details for a specific variant
|
|
@@ -73,22 +63,13 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
73
63
|
* @param options - Optional query parameters
|
|
74
64
|
* @returns Promise with variant details
|
|
75
65
|
*/
|
|
76
|
-
async getVariantDetail(
|
|
77
|
-
return this.executeRequest(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
},
|
|
84
|
-
query: options,
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
if (error) {
|
|
88
|
-
this.handleError(error);
|
|
89
|
-
}
|
|
90
|
-
return data?.content;
|
|
91
|
-
});
|
|
66
|
+
async getVariantDetail(pathParams, queryParams) {
|
|
67
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants/{variant_id}", {
|
|
68
|
+
params: {
|
|
69
|
+
path: pathParams,
|
|
70
|
+
query: queryParams,
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
92
73
|
}
|
|
93
74
|
/**
|
|
94
75
|
* List all categories
|
|
@@ -97,15 +78,9 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
97
78
|
* @returns Promise with categories and pagination info
|
|
98
79
|
*/
|
|
99
80
|
async listCategories(options) {
|
|
100
|
-
return this.executeRequest(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
});
|
|
104
|
-
if (error) {
|
|
105
|
-
this.handleError(error);
|
|
106
|
-
}
|
|
107
|
-
return data?.content;
|
|
108
|
-
});
|
|
81
|
+
return this.executeRequest(() => this.client.GET("/catalog/categories", {
|
|
82
|
+
params: { query: options },
|
|
83
|
+
}));
|
|
109
84
|
}
|
|
110
85
|
/**
|
|
111
86
|
* List reviews for a specific product
|
|
@@ -114,19 +89,13 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
114
89
|
* @param options - Optional query parameters
|
|
115
90
|
* @returns Promise with reviews and pagination info
|
|
116
91
|
*/
|
|
117
|
-
async listProductReviews(
|
|
118
|
-
return this.executeRequest(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
if (error) {
|
|
126
|
-
this.handleError(error);
|
|
127
|
-
}
|
|
128
|
-
return data?.content;
|
|
129
|
-
});
|
|
92
|
+
async listProductReviews(pathParams, queryParams) {
|
|
93
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/reviews", {
|
|
94
|
+
params: {
|
|
95
|
+
path: pathParams,
|
|
96
|
+
query: queryParams,
|
|
97
|
+
},
|
|
98
|
+
}));
|
|
130
99
|
}
|
|
131
100
|
/**
|
|
132
101
|
* Create a review for a specific product
|
|
@@ -135,56 +104,78 @@ class CatalogClient extends client_1.StorefrontAPIClient {
|
|
|
135
104
|
* @param reviewData - The review data
|
|
136
105
|
* @returns Promise that resolves when the review is created
|
|
137
106
|
*/
|
|
138
|
-
async createProductReview(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
body
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
107
|
+
async createProductReview(pathParams, formData) {
|
|
108
|
+
return this.executeRequest(() => this.client.POST("/catalog/products/{product_id}/reviews", {
|
|
109
|
+
params: {
|
|
110
|
+
path: pathParams,
|
|
111
|
+
},
|
|
112
|
+
body: formData,
|
|
113
|
+
bodySerializer: (body) => {
|
|
114
|
+
const fd = new FormData();
|
|
115
|
+
for (const [key, value] of Object.entries(body)) {
|
|
116
|
+
if (value !== undefined && value !== null) {
|
|
117
|
+
// Handle File objects directly
|
|
118
|
+
if (value instanceof File || value instanceof Blob) {
|
|
119
|
+
fd.append(key, value);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
// Convert other values to string
|
|
123
|
+
fd.append(key, String(value));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return fd;
|
|
151
128
|
}
|
|
152
|
-
});
|
|
129
|
+
}));
|
|
153
130
|
}
|
|
154
131
|
/**
|
|
155
132
|
* Search for products
|
|
156
133
|
*
|
|
157
134
|
* @param searchData - The search parameters
|
|
158
|
-
* @returns Promise with search results
|
|
135
|
+
* @returns Promise with search results, facet distribution, facet stats, and pagination
|
|
159
136
|
*/
|
|
160
137
|
async searchProducts(searchData) {
|
|
161
|
-
return this.executeRequest(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
});
|
|
165
|
-
if (error) {
|
|
166
|
-
this.handleError(error);
|
|
167
|
-
}
|
|
168
|
-
return data?.content;
|
|
169
|
-
});
|
|
138
|
+
return this.executeRequest(() => this.client.POST("/catalog/products/search", {
|
|
139
|
+
body: searchData,
|
|
140
|
+
}));
|
|
170
141
|
}
|
|
171
142
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @
|
|
143
|
+
* Retrieve cross-sell recommendations for a specific product
|
|
144
|
+
*
|
|
145
|
+
* @param options - Optional query parameters
|
|
146
|
+
* @returns Promise with cross-sell recommendations with pagination
|
|
175
147
|
*/
|
|
176
|
-
async
|
|
177
|
-
return this.executeRequest(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
148
|
+
async listCrossSellProducts(options) {
|
|
149
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/cross-sell", {
|
|
150
|
+
params: {
|
|
151
|
+
query: options,
|
|
152
|
+
},
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Retrieve up-sell recommendations for a specific product
|
|
157
|
+
*
|
|
158
|
+
* @param options - Optional query parameters
|
|
159
|
+
* @returns Promise with up-sell recommendations with pagination
|
|
160
|
+
*/
|
|
161
|
+
async listUpSellProducts(options) {
|
|
162
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/up-sell", {
|
|
163
|
+
params: {
|
|
164
|
+
query: options,
|
|
165
|
+
},
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Retrieve related products for a specific product
|
|
170
|
+
*
|
|
171
|
+
* @param options - Optional query parameters
|
|
172
|
+
* @returns Promise with related products with pagination
|
|
173
|
+
*/
|
|
174
|
+
async listSimilarProducts(options) {
|
|
175
|
+
return this.executeRequest(() => this.client.GET("/catalog/products/similar", {
|
|
176
|
+
params: {
|
|
177
|
+
query: options,
|
|
178
|
+
},
|
|
179
|
+
}));
|
|
188
180
|
}
|
|
189
181
|
}
|
|
190
|
-
exports.CatalogClient = CatalogClient;
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import createClient from "openapi-fetch";
|
|
2
|
+
import type { paths } from "../types/storefront";
|
|
3
|
+
import { ApiErrorResponse, ApiResult } from "../types/storefront-api-types";
|
|
4
|
+
import type { StorefrontSDKOptions } from "../index";
|
|
2
5
|
/**
|
|
3
6
|
* Available API environments
|
|
4
7
|
*/
|
|
@@ -12,76 +15,23 @@ export declare enum Environment {
|
|
|
12
15
|
*/
|
|
13
16
|
Production = "production"
|
|
14
17
|
}
|
|
15
|
-
import type { paths } from "../types/storefront";
|
|
16
|
-
/**
|
|
17
|
-
* Configuration options for the StorefrontAPI client
|
|
18
|
-
*/
|
|
19
|
-
export interface StorefrontAPIConfig {
|
|
20
|
-
/**
|
|
21
|
-
* The store ID to use for API requests
|
|
22
|
-
*/
|
|
23
|
-
storeId: string;
|
|
24
|
-
/**
|
|
25
|
-
* The environment to use (staging or production)
|
|
26
|
-
*/
|
|
27
|
-
environment?: Environment;
|
|
28
|
-
/**
|
|
29
|
-
* Custom base URL (overrides environment-based URL if provided)
|
|
30
|
-
*/
|
|
31
|
-
baseUrl?: string;
|
|
32
|
-
/**
|
|
33
|
-
* Optional authentication token
|
|
34
|
-
*/
|
|
35
|
-
token?: string;
|
|
36
|
-
/**
|
|
37
|
-
* X-Api-Key for anonymous authentication endpoints
|
|
38
|
-
* Required for initial authentication
|
|
39
|
-
*/
|
|
40
|
-
apiKey?: string;
|
|
41
|
-
/**
|
|
42
|
-
* Optional timeout in milliseconds
|
|
43
|
-
*/
|
|
44
|
-
timeout?: number;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Error response from the API
|
|
48
|
-
*/
|
|
49
|
-
export type ApiErrorResponse = {
|
|
50
|
-
message?: string;
|
|
51
|
-
success: boolean;
|
|
52
|
-
code?: string;
|
|
53
|
-
error?: Record<string, any>;
|
|
54
|
-
};
|
|
55
18
|
/**
|
|
56
19
|
* Base API client for Storefront API
|
|
57
20
|
*/
|
|
58
21
|
export declare class StorefrontAPIClient {
|
|
59
22
|
protected client: ReturnType<typeof createClient<paths>>;
|
|
60
|
-
protected config:
|
|
61
|
-
private headers;
|
|
23
|
+
protected config: StorefrontSDKOptions;
|
|
62
24
|
private readonly baseUrl;
|
|
63
|
-
private isRefreshing;
|
|
64
|
-
private static sharedConfigs;
|
|
65
25
|
/**
|
|
66
26
|
* Create a new StorefrontAPIClient
|
|
67
27
|
*
|
|
68
28
|
* @param config - Configuration for the API client
|
|
69
|
-
*
|
|
70
|
-
* @remarks
|
|
71
|
-
* This client implements a token refresh mechanism that will:
|
|
72
|
-
* 1. Automatically retry requests that fail with a 401 error
|
|
73
|
-
* 2. Refresh the token before retrying the request
|
|
74
|
-
* 3. If the token refresh fails, the original error will be thrown
|
|
75
|
-
*
|
|
76
|
-
* This behavior is inherited by all client classes that extend StorefrontAPIClient.
|
|
77
|
-
*
|
|
78
|
-
* When using the AuthClient, tokens can be stored persistently in:
|
|
79
|
-
* - Memory (default) - Tokens are lost when the page refreshes or app restarts
|
|
80
|
-
* - Browser localStorage - Tokens persist across page refreshes
|
|
81
|
-
* - Cookies - Tokens persist across page refreshes and can be read server-side
|
|
82
|
-
* - Next.js cookies API - For server-side components
|
|
83
29
|
*/
|
|
84
|
-
constructor(config:
|
|
30
|
+
constructor(config: StorefrontSDKOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Set up timeout middleware
|
|
33
|
+
*/
|
|
34
|
+
private setupTimeoutMiddleware;
|
|
85
35
|
/**
|
|
86
36
|
* Constructs the base URL from the configuration
|
|
87
37
|
*
|
|
@@ -97,20 +47,31 @@ export declare class StorefrontAPIClient {
|
|
|
97
47
|
getBaseUrl(): string;
|
|
98
48
|
/**
|
|
99
49
|
* Get the authorization header value
|
|
50
|
+
* If using token storage, gets the current token from storage
|
|
51
|
+
* Otherwise returns the manual token
|
|
100
52
|
*
|
|
101
53
|
* @returns The Authorization header value or empty string if no token is set
|
|
102
54
|
*/
|
|
103
|
-
getAuthorizationHeader(): string
|
|
55
|
+
getAuthorizationHeader(): Promise<string>;
|
|
104
56
|
/**
|
|
105
|
-
* Set
|
|
57
|
+
* Set authentication tokens
|
|
106
58
|
*
|
|
107
|
-
* @param
|
|
59
|
+
* @param accessToken - The access token (required)
|
|
60
|
+
* @param refreshToken - The refresh token (optional)
|
|
61
|
+
*
|
|
62
|
+
* Behavior:
|
|
63
|
+
* - If tokenStorage is provided: Stores tokens for automatic management
|
|
64
|
+
* - If tokenStorage is not provided: Only stores access token for manual management
|
|
108
65
|
*/
|
|
109
|
-
|
|
66
|
+
setTokens(accessToken: string, refreshToken?: string): Promise<void>;
|
|
110
67
|
/**
|
|
111
|
-
* Clear
|
|
68
|
+
* Clear all authentication tokens
|
|
69
|
+
*
|
|
70
|
+
* Behavior:
|
|
71
|
+
* - If tokenStorage is provided: Clears both access and refresh tokens from storage
|
|
72
|
+
* - If tokenStorage is not provided: Clears the manual access token
|
|
112
73
|
*/
|
|
113
|
-
|
|
74
|
+
clearTokens(): Promise<void>;
|
|
114
75
|
/**
|
|
115
76
|
* Set the X-Api-Key header
|
|
116
77
|
*
|
|
@@ -122,26 +83,18 @@ export declare class StorefrontAPIClient {
|
|
|
122
83
|
*/
|
|
123
84
|
clearApiKey(): void;
|
|
124
85
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @param error - Error object from OpenAPI-Fetch
|
|
128
|
-
* @throws Rethrows the error with additional context
|
|
129
|
-
*/
|
|
130
|
-
protected handleError(error: any): Promise<never>;
|
|
131
|
-
/**
|
|
132
|
-
* Attempt to refresh the token
|
|
133
|
-
* This is a placeholder method that will be overridden by AuthClient
|
|
134
|
-
*
|
|
135
|
-
* @returns Promise that resolves to true if token was refreshed, false otherwise
|
|
136
|
-
*/
|
|
137
|
-
protected attemptTokenRefresh(): Promise<boolean>;
|
|
138
|
-
/**
|
|
139
|
-
* Execute a request with automatic token refresh handling
|
|
140
|
-
* For AuthClient, this will attempt to refresh the token and retry once on 401 errors
|
|
141
|
-
* Base implementation just executes the request
|
|
86
|
+
* Execute a request and handle the response
|
|
142
87
|
*
|
|
143
|
-
* @param
|
|
88
|
+
* @param apiCall - Function that executes the API request
|
|
144
89
|
* @returns Promise with the API response
|
|
145
90
|
*/
|
|
146
|
-
protected executeRequest<T>(
|
|
91
|
+
protected executeRequest<T>(apiCall: () => Promise<{
|
|
92
|
+
data?: {
|
|
93
|
+
message?: string;
|
|
94
|
+
success?: boolean;
|
|
95
|
+
content?: T;
|
|
96
|
+
};
|
|
97
|
+
error?: ApiErrorResponse;
|
|
98
|
+
response: Response;
|
|
99
|
+
}>): Promise<ApiResult<T>>;
|
|
147
100
|
}
|