You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below you’ll find instructions on how to install, set up, and use the client, along with a list of available operations.
3
+
Below you'll find instructions on how to install, set up, and use the client, along with a list of available operations.
4
4
5
5
## Features
6
6
@@ -26,7 +26,6 @@ yarn add @epcc-sdk/sdks-shopper
26
26
27
27
## Client Usage
28
28
29
-
30
29
Clients are responsible for sending the actual HTTP requests.
31
30
32
31
The Fetch client is built as a thin wrapper on top of Fetch API, extending its functionality. If you're already familiar with Fetch, configuring your client will feel like working directly with Fetch API.
@@ -38,23 +37,22 @@ You can configure the client in two ways:
38
37
39
38
**When using the operation function to make requests, by default the global client will be used unless another is provided.**
40
39
41
-
42
40
### 1. Configure the internal `client` instance directly
43
41
44
42
This is the simpler approach. You can call the setConfig() method at the beginning of your application or anytime you need to update the client configuration. You can pass any Fetch API configuration option to setConfig(), and even your own Fetch implementation.
45
43
46
44
```ts
47
-
import { client } from"@epcc-sdk/sdks-shopper";
45
+
import { client } from"@epcc-sdk/sdks-shopper"
48
46
49
47
client.setConfig({
50
-
// set default base url for requests
51
-
baseUrl: 'https://euwest.api.elasticpath.com',
48
+
// set default base url for requests
49
+
baseUrl: "https://euwest.api.elasticpath.com",
52
50
53
-
// set default headers for requests
54
-
headers: {
55
-
Authorization: 'Bearer YOUR_AUTH_TOKEN',
56
-
},
57
-
});
51
+
// set default headers for requests
52
+
headers: {
53
+
Authorization: "Bearer YOUR_AUTH_TOKEN",
54
+
},
55
+
})
58
56
```
59
57
60
58
The disadvantage of this approach is that your code may call the client instance before it's configured for the first time. Depending on your use case, you might need to use the second approach.
@@ -64,27 +62,27 @@ The disadvantage of this approach is that your code may call the client instance
64
62
This is useful when you want to use a different instance of the client for different parts of your application or when you want to use different configurations for different parts of your application.
* Set default headers only for requests made by this client.
75
-
*/
76
-
headers: {
77
-
"Custom-Header": 'My Value',
78
-
},
79
-
});
69
+
// set default base url for requests
70
+
baseUrl: "https://euwest.api.elasticpath.com",
71
+
/**
72
+
* Set default headers only for requests made by this client.
73
+
*/
74
+
headers: {
75
+
"Custom-Header": "My Value",
76
+
},
77
+
})
80
78
```
81
79
82
80
You can also pass this instance to any SDK function through the client option. This will override the default instance from `import { client } from "@epcc-sdk/sdks-shopper>".
83
81
84
82
```ts
85
83
const response =awaitgetByContextProduct({
86
-
client: myClient,
87
-
});
84
+
client: myClient,
85
+
})
88
86
```
89
87
90
88
### Direct configuration
@@ -93,44 +91,43 @@ Alternatively, you can pass the client configuration options to each SDK functio
Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application. They can be added with use and removed with eject. Below is an example request interceptor
If you need to access the compiled URL, you can use the buildUrl() method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.
154
-
155
-
```ts
156
-
typeFooData= {
157
-
path: {
158
-
fooId:number;
159
-
};
160
-
query?: {
161
-
bar?:string;
162
-
};
163
-
url:'/foo/{fooId}';
164
-
};
165
-
166
-
const url =client.buildUrl<FooData>({
167
-
path: {
168
-
fooId: 1,
169
-
},
170
-
query: {
171
-
bar: 'baz',
172
-
},
173
-
url: '/foo/{fooId}',
174
-
});
175
-
console.log(url); // prints '/foo/1?bar=baz'
176
-
```
177
-
178
-
179
-
---
180
-
181
-
182
-
183
-
184
-
185
-
## Operation Usage
186
-
The following examples demonstrate how to use the operation function to make requests.
The SDK provides a built-in local storage authentication interceptor that automatically handles token management:
@@ -242,6 +184,49 @@ This interceptor:
242
184
243
185
If you need to access the compiled URL, you can use the buildUrl() method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.
244
186
187
+
```ts
188
+
typeFooData= {
189
+
path: {
190
+
fooId:number
191
+
}
192
+
query?: {
193
+
bar?:string
194
+
}
195
+
url:"/foo/{fooId}"
196
+
}
197
+
198
+
const url =client.buildUrl<FooData>({
199
+
path: {
200
+
fooId: 1,
201
+
},
202
+
query: {
203
+
bar: "baz",
204
+
},
205
+
url: "/foo/{fooId}",
206
+
})
207
+
console.log(url) // prints '/foo/1?bar=baz'
208
+
```
209
+
210
+
## Operation Usage
211
+
212
+
The following examples demonstrate how to use the operation function to make requests.
If you need to access the compiled URL, you can use the buildUrl() method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.
73
-
74
-
## Utilities
75
-
76
-
The SDK provides a set of utility functions that simplify common tasks when building a storefront.
77
-
78
-
### Cart Initialization
79
-
80
-
The `initializeCart` utility creates or retrieves a cart ID for the current shopper session:
81
-
82
-
```ts
83
-
import { initializeCart } from "@epcc-sdk/sdks-shopper/utils"
84
-
85
-
// Initialize cart with default storage key
86
-
const cartId = await initializeCart()
87
-
88
-
// Or with a custom storage key
89
-
const cartId = await initializeCart({
90
-
storageKey: "my-custom-cart-key",
91
-
})
92
-
```
93
-
94
-
#### How it works:
95
-
96
-
1. Checks localStorage for an existing cart ID
97
-
2. If a cart ID exists, returns it immediately
98
-
3. If no cart ID exists, creates a new cart via the API
99
-
4. Stores the new cart ID in localStorage for future use
100
-
5. Returns the cart ID as a string
101
-
102
-
This utility is useful for implementing guest checkout flows and ensuring the shopper always has an active cart.
103
-
104
-
### Cart ID Retrieval
105
-
106
-
The `getCartId` utility provides a simple way to retrieve the current cart ID from localStorage:
107
-
108
-
```ts
109
-
import { getCartId } from "@epcc-sdk/sdks-shopper/utils"
110
-
111
-
// Get cart ID using default storage key
112
-
const cartId = getCartId()
113
-
114
-
// Or with a custom storage key
115
-
const cartId = getCartId({
116
-
storageKey: "my-custom-cart-key",
117
-
})
118
-
```
119
-
120
-
Use this utility when you need to quickly check if a cart exists or need to pass the current cart ID to operations.
0 commit comments