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,6 +26,7 @@ yarn add @epcc-sdk/sdks-shopper
26
26
27
27
## Client Usage
28
28
29
+
29
30
Clients are responsible for sending the actual HTTP requests.
30
31
31
32
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.
@@ -37,22 +38,23 @@ You can configure the client in two ways:
37
38
38
39
**When using the operation function to make requests, by default the global client will be used unless another is provided.**
39
40
41
+
40
42
### 1. Configure the internal `client` instance directly
41
43
42
44
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.
43
45
44
46
```ts
45
-
import { client } from"@epcc-sdk/sdks-shopper"
47
+
import { client } from"@epcc-sdk/sdks-shopper";
46
48
47
49
client.setConfig({
48
-
// set default base url for requests
49
-
baseUrl: "https://euwest.api.elasticpath.com",
50
+
// set default base url for requests
51
+
baseUrl: 'https://euwest.api.elasticpath.com',
50
52
51
-
// set default headers for requests
52
-
headers: {
53
-
Authorization: "Bearer YOUR_AUTH_TOKEN",
54
-
},
55
-
})
53
+
// set default headers for requests
54
+
headers: {
55
+
Authorization: 'Bearer YOUR_AUTH_TOKEN',
56
+
},
57
+
});
56
58
```
57
59
58
60
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.
@@ -62,27 +64,27 @@ The disadvantage of this approach is that your code may call the client instance
62
64
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.
73
-
*/
74
-
headers: {
75
-
"Custom-Header": "My Value",
76
-
},
77
-
})
71
+
// set default base url for requests
72
+
baseUrl: "https://euwest.api.elasticpath.com",
73
+
/**
74
+
* Set default headers only for requests made by this client.
75
+
*/
76
+
headers: {
77
+
"Custom-Header": 'My Value',
78
+
},
79
+
});
78
80
```
79
81
80
82
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>".
81
83
82
84
```ts
83
85
const response =awaitgetByContextProduct({
84
-
client: myClient,
85
-
})
86
+
client: myClient,
87
+
});
86
88
```
87
89
88
90
### Direct configuration
@@ -91,43 +93,44 @@ 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.
186
154
187
155
```ts
188
156
typeFooData= {
189
157
path: {
190
-
fooId:number
191
-
}
158
+
fooId:number;
159
+
};
192
160
query?: {
193
-
bar?:string
194
-
}
195
-
url:"/foo/{fooId}"
196
-
}
161
+
bar?:string;
162
+
};
163
+
url:'/foo/{fooId}';
164
+
};
197
165
198
166
const url =client.buildUrl<FooData>({
199
167
path: {
200
168
fooId: 1,
201
169
},
202
170
query: {
203
-
bar: "baz",
171
+
bar: 'baz',
204
172
},
205
-
url: "/foo/{fooId}",
206
-
})
207
-
console.log(url) // prints '/foo/1?bar=baz'
173
+
url: '/foo/{fooId}',
174
+
});
175
+
console.log(url);// prints '/foo/1?bar=baz'
208
176
```
209
177
210
-
## Operation Usage
211
178
179
+
---
180
+
181
+
182
+
183
+
184
+
185
+
## Operation Usage
212
186
The following examples demonstrate how to use the operation function to make requests.
0 commit comments