@commercelayer/provisioning-sdk 1.0.0-beta.1 → 1.0.0-beta.3
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/README.md +107 -126
- package/lib/cjs/client.d.ts +6 -3
- package/lib/cjs/client.js +31 -6
- package/lib/cjs/commercelayer.js +1 -4
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/resource.js +1 -1
- package/lib/cjs/util.d.ts +1 -1
- package/lib/cjs/util.js +1 -0
- package/lib/esm/client.d.ts +6 -3
- package/lib/esm/client.js +31 -6
- package/lib/esm/commercelayer.js +1 -4
- package/lib/esm/index.js +1 -1
- package/lib/esm/resource.js +1 -1
- package/lib/esm/util.d.ts +1 -1
- package/lib/esm/util.js +27 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
# Commerce Layer Provisioning SDK
|
|
2
2
|
|
|
3
|
-
[](https://npmjs.org/package/@commercelayer/sdk)
|
|
4
|
-
[](https://npmjs.org/package/@commercelayer/sdk)
|
|
5
|
-
[](https://github.com/commercelayer/commercelayer-sdk/blob/master/package.json)
|
|
3
|
+
[](https://npmjs.org/package/@commercelayer/provisioning-sdk)
|
|
4
|
+
[](https://npmjs.org/package/@commercelayer/provisioning-sdk)
|
|
5
|
+
[](https://github.com/commercelayer/commercelayer-sdk/blob/master/package.json)
|
|
6
6
|
[](https://github.com/semantic-release/semantic-release)
|
|
7
7
|
[](https://github.com/commercelayer/commercelayer-sdk/actions/workflows/semantic-release.yml)
|
|
8
8
|
[](https://github.com/commercelayer/commercelayer-cli/actions/workflows/codeql-analysis.yml)
|
|
9
9
|
[](https://www.typescriptlang.org/)
|
|
10
10
|
|
|
11
|
-
A JavaScript Library wrapper that makes it quick and easy to interact with the [Commerce Layer API](https://docs.commercelayer.io/
|
|
12
|
-
|
|
13
|
-
## What is Commerce Layer?
|
|
14
|
-
|
|
15
|
-
[Commerce Layer](https://commercelayer.io) is a multi-market commerce API and order management system that lets you add global shopping capabilities to any website, mobile app, chatbot, wearable, voice, or IoT device, with ease. Compose your stack with the best-of-breed tools you already mastered and love. Make any experience shoppable, anywhere, through a blazing-fast, enterprise-grade, and secure API.
|
|
11
|
+
A JavaScript Library wrapper that makes it quick and easy to interact with the [Commerce Layer Provisioning API](https://docs.commercelayer.io/provisioning).
|
|
16
12
|
|
|
17
13
|
## Table of contents
|
|
18
14
|
|
|
@@ -31,221 +27,214 @@ A JavaScript Library wrapper that makes it quick and easy to interact with the [
|
|
|
31
27
|
|
|
32
28
|
## Getting started
|
|
33
29
|
|
|
34
|
-
To get started with Commerce Layer
|
|
35
|
-
|
|
36
|
-
> If you want, you can also read [this tutorial](https://commercelayer.io/blog/getting-started-with-commerce-layer-javascript-sdk) from Commerce Layer's blog.
|
|
30
|
+
To get started with Commerce Layer Provisioning SDK you need to install it, get the credentials that will allow you to perform your API calls, and import the SDK into your application's code. The sections below explain how to achieve this.
|
|
37
31
|
|
|
38
32
|
### Installation
|
|
39
33
|
|
|
40
|
-
Commerce Layer
|
|
34
|
+
Commerce Layer Provisioning SDK is available as an [npm](https://www.npmjs.com/package/@commercelayer/provisioning-sdk) and [yarn](https://yarnpkg.com/package/@commercelayer/provisioning-sdk) package that you can install with the command below:
|
|
41
35
|
|
|
42
36
|
```shell
|
|
43
|
-
npm install @commercelayer/sdk
|
|
37
|
+
npm install @commercelayer/provisioning-sdk
|
|
44
38
|
|
|
45
39
|
// or
|
|
46
40
|
|
|
47
|
-
yarn add @commercelayer/sdk
|
|
41
|
+
yarn add @commercelayer/provisioning-sdk
|
|
48
42
|
```
|
|
49
43
|
|
|
50
44
|
### Authentication
|
|
51
45
|
|
|
52
|
-
All requests to Commerce Layer API must be authenticated with an [OAuth2](https://oauth.net/2) bearer token. Hence, before starting to use this SDK you need to get a valid access token. Kindly check [our documentation](https://docs.commercelayer.io/
|
|
46
|
+
All requests to Commerce Layer API must be authenticated with an [OAuth2](https://oauth.net/2) bearer token. Hence, before starting to use this SDK you need to get a valid access token. Kindly check [our documentation](https://docs.commercelayer.io/provisioning/authentication) for more information about the available authorization flows.
|
|
53
47
|
|
|
54
|
-
> Feel free to use [Commerce Layer
|
|
48
|
+
> Feel free to use [Commerce Layer Provisioning Auth](https://github.com/commercelayer/commercelayer-js-auth), a JavaScript library that helps you wrap our authentication API.
|
|
55
49
|
|
|
56
50
|
### Import
|
|
57
51
|
|
|
58
52
|
You can use the ES6 default import with the SDK like so:
|
|
59
53
|
|
|
60
54
|
```javascript
|
|
61
|
-
import
|
|
55
|
+
import CommerceLayerProvisioning from '@commercelayer/provisioning-sdk'
|
|
62
56
|
|
|
63
|
-
const
|
|
64
|
-
organization: 'your-organization-slug',
|
|
57
|
+
const clp = CommerceLayerProvisioning({
|
|
65
58
|
accessToken: 'your-access-token'
|
|
66
59
|
})
|
|
67
60
|
```
|
|
68
61
|
|
|
69
62
|
## SDK usage
|
|
70
63
|
|
|
71
|
-
The JavaScript SDK is a wrapper around Commerce Layer API which means you would still be making API requests but with a different syntax. For now, we don't have comprehensive SDK documentation for every single resource our API supports
|
|
64
|
+
The JavaScript SDK is a wrapper around Commerce Layer Provisioning API which means you would still be making API requests but with a different syntax. For now, we don't have comprehensive SDK documentation for every single resource our API supports, hence you will need to rely on our comprehensive [Provisioning API Reference](https://docs.commercelayer.io/provisioning/v/api-reference-p) as you go about using this SDK. So for example, if you want to create a role, take a look at the [Role object](https://docs.commercelayer.io/provisioning/v/api-reference-p/role/object) or the [Create a role](https://docs.commercelayer.io/provisioning/v/api-reference-p/roles/create) documentation to see the required attributes and/or relationships. The same goes for every other supported resource.
|
|
72
65
|
|
|
73
|
-
|
|
66
|
+
The code snippets below show how to use the SDK when performing the standard CRUD operations provided by our REST API. Kindly check our [Provisioning API reference](https://docs.commercelayer.io/provisioning/v/api-reference-p) for the complete list of available **resources** and their **attributes**.
|
|
74
67
|
|
|
75
68
|
### Create
|
|
76
69
|
|
|
77
70
|
<details>
|
|
78
|
-
<summary>How to create
|
|
71
|
+
<summary>How to create a Role</summary>
|
|
79
72
|
<br />
|
|
80
73
|
|
|
81
74
|
```javascript
|
|
82
|
-
// Select the
|
|
83
|
-
const
|
|
75
|
+
// Select the organization (it's a required relationship for the SKU resource)
|
|
76
|
+
const organizations = await clp.organizations.list({ filters: { name_eq: 'Test Org' } })
|
|
84
77
|
|
|
85
78
|
const attributes = {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
description: "A very beautiful and cozy mens t-shirt",
|
|
89
|
-
weight: "500",
|
|
90
|
-
unit_of_weight: "gr"
|
|
91
|
-
shipping_category: cl.shipping_categories.relationship(shippingCategories[0].id), // assigns the relationship
|
|
79
|
+
name: 'Test Role',
|
|
80
|
+
organization: clp.organizations.relationship(organizations.first().id), // assigns the relationship
|
|
92
81
|
}
|
|
93
82
|
|
|
94
|
-
const
|
|
83
|
+
const newRole = await clp.roles.create(attributes)
|
|
95
84
|
```
|
|
96
85
|
|
|
97
|
-
ℹ️ Check our API reference for more information on how to [create
|
|
86
|
+
ℹ️ Check our API reference for more information on how to [create a Role](https://docs.commercelayer.io/provisioning/v/api-reference-p/roles/create).
|
|
98
87
|
</details>
|
|
99
88
|
|
|
100
89
|
### Retrieve / List
|
|
101
90
|
|
|
102
91
|
<details>
|
|
103
|
-
<summary>How to fetch a single
|
|
92
|
+
<summary>How to fetch a single organization</summary>
|
|
104
93
|
<br />
|
|
105
94
|
|
|
106
95
|
```javascript
|
|
107
|
-
// Fetch the
|
|
108
|
-
const
|
|
96
|
+
// Fetch the organization by ID
|
|
97
|
+
const org = await clp.organizations.retrieve('BxAkSVqKEn')
|
|
109
98
|
|
|
110
|
-
// Fetch all
|
|
111
|
-
const
|
|
99
|
+
// Fetch all organizations and filter by name
|
|
100
|
+
const orgs = await clp.organizations.list({ filters: { name_start: 'TestOrg_' } })
|
|
112
101
|
|
|
113
|
-
// Fetch the first
|
|
114
|
-
const
|
|
102
|
+
// Fetch the first organization of the list
|
|
103
|
+
const org = (await clp.organizations.list()).first()
|
|
115
104
|
|
|
116
|
-
// Fetch the last
|
|
117
|
-
const
|
|
105
|
+
// Fetch the last organization of the list
|
|
106
|
+
const org = (await clp.organizations.list()).last()
|
|
118
107
|
```
|
|
119
108
|
|
|
120
|
-
ℹ️ Check our API reference for more information on how to [retrieve an
|
|
109
|
+
ℹ️ Check our API reference for more information on how to [retrieve an organization](https://docs.commercelayer.io/provisioning/v/api-reference-p/organizations/retrieve).
|
|
121
110
|
</details>
|
|
122
111
|
|
|
123
112
|
<details>
|
|
124
|
-
<summary>How to fetch a collection of
|
|
113
|
+
<summary>How to fetch a collection of organizations</summary>
|
|
125
114
|
<br />
|
|
126
115
|
|
|
127
116
|
```javascript
|
|
128
|
-
// Fetch all the
|
|
129
|
-
const
|
|
117
|
+
// Fetch all the organizations
|
|
118
|
+
const orgs = await clp.organizations.list()
|
|
130
119
|
```
|
|
131
120
|
|
|
132
121
|
When fetching a collection of resources you can leverage the `meta` attribute to get its `meta` information like so:
|
|
133
122
|
|
|
134
123
|
```javascript
|
|
135
|
-
const
|
|
136
|
-
const meta =
|
|
124
|
+
const orgs = await clp.organizations.list()
|
|
125
|
+
const meta = orgs.meta
|
|
137
126
|
```
|
|
138
127
|
|
|
139
|
-
ℹ️ Check our API reference for more information on how to [list all SKUs](https://docs.commercelayer.io/
|
|
128
|
+
ℹ️ Check our API reference for more information on how to [list all SKUs](https://docs.commercelayer.io/provisioning/v/api-reference-p/organizations/list).
|
|
140
129
|
</details>
|
|
141
130
|
|
|
142
131
|
<details>
|
|
143
|
-
<summary>How to fetch a collection of
|
|
132
|
+
<summary>How to fetch a collection of organizations and sort the results</summary>
|
|
144
133
|
<br />
|
|
145
134
|
|
|
146
135
|
```javascript
|
|
147
136
|
// Sort the results by creation date in ascending order (default)
|
|
148
|
-
const
|
|
137
|
+
const orgs = await clp.organizations.list({ sort: { created_at: 'asc' } })
|
|
149
138
|
|
|
150
139
|
// Sort the results by creation date in descending order
|
|
151
|
-
const
|
|
140
|
+
const orgs = await clp.organizations.list({ sort: { created_at: 'desc' } })
|
|
152
141
|
```
|
|
153
142
|
|
|
154
|
-
ℹ️ Check our API reference for more information on how to [sort results](https://docs.commercelayer.io/
|
|
143
|
+
ℹ️ Check our API reference for more information on how to [sort results](https://docs.commercelayer.io/provisioning/sorting-results).
|
|
155
144
|
</details>
|
|
156
145
|
|
|
157
146
|
<details>
|
|
158
|
-
<summary>How to fetch a collection of
|
|
147
|
+
<summary>How to fetch a collection of Memberships and include associations</summary>
|
|
159
148
|
<br />
|
|
160
149
|
|
|
161
150
|
```javascript
|
|
162
|
-
// Include an association (
|
|
163
|
-
const
|
|
151
|
+
// Include an association (organization)
|
|
152
|
+
const mships = await clp.memberships.list({ include: [ 'organization' ] })
|
|
164
153
|
|
|
165
|
-
// Include an association (stock
|
|
166
|
-
const
|
|
154
|
+
// Include an association (stock role)
|
|
155
|
+
const mships = await clp.memberships.list({ include: [ 'role' ] })
|
|
167
156
|
```
|
|
168
157
|
|
|
169
|
-
ℹ️ Check our API reference for more information on how to [include associations](https://docs.commercelayer.io/
|
|
158
|
+
ℹ️ Check our API reference for more information on how to [include associations](https://docs.commercelayer.io/provisioning/including-associations).
|
|
170
159
|
</details>
|
|
171
160
|
|
|
172
161
|
<details>
|
|
173
|
-
<summary>How to fetch a collection of
|
|
162
|
+
<summary>How to fetch a collection of Permissions and return specific fields (sparse fieldsets)</summary>
|
|
174
163
|
<br />
|
|
175
164
|
|
|
176
165
|
```javascript
|
|
177
166
|
// Request the API to return only specific fields
|
|
178
|
-
const
|
|
167
|
+
const perms = await clp.permissions.list({ fields: { permissions: [ 'can_create', 'can_update' ] } })
|
|
179
168
|
|
|
180
169
|
// Request the API to return only specific fields of the included resource
|
|
181
|
-
const
|
|
170
|
+
const mships = await clp.memberships.list({ include: [ 'organization' ], fields: { organizations: [ 'name' ] } })
|
|
182
171
|
```
|
|
183
172
|
|
|
184
|
-
ℹ️ Check our API reference for more information on how to [use sparse fieldsets](https://docs.commercelayer.io/
|
|
173
|
+
ℹ️ Check our API reference for more information on how to [use sparse fieldsets](https://docs.commercelayer.io/provisioning/sparse-fieldsets).
|
|
185
174
|
</details>
|
|
186
175
|
|
|
187
176
|
<details>
|
|
188
|
-
<summary>How to fetch a collection of
|
|
177
|
+
<summary>How to fetch a collection of organizations and filter data</summary>
|
|
189
178
|
<br />
|
|
190
179
|
|
|
191
180
|
```javascript
|
|
192
|
-
// Filter all the
|
|
193
|
-
const
|
|
181
|
+
// Filter all the organizations fetching only the ones whose name starts with the string "ORG"
|
|
182
|
+
const orgs = await clp.organizations .list({ filters: { name_start: 'ORG' } })
|
|
194
183
|
|
|
195
|
-
// Filter all the
|
|
196
|
-
const
|
|
184
|
+
// Filter all the organizations fetching only the ones whose name ends with the string "Brand"
|
|
185
|
+
const orgs = await clp.organizations.list({ filters: { name_end: 'Brand' } })
|
|
197
186
|
|
|
198
|
-
// Filter all the
|
|
199
|
-
const
|
|
187
|
+
// Filter all the organizations fetching only the ones whose name contains the string "Test"
|
|
188
|
+
const orgs = await clp.organizations.list({ filters: { name_cont: 'Test' } })
|
|
200
189
|
|
|
201
|
-
// Filter all the
|
|
190
|
+
// Filter all the organizations fetching only the ones created between two specific dates
|
|
202
191
|
// (filters combined according to the AND logic)
|
|
203
|
-
const
|
|
192
|
+
const orgs = await clp.organizations.list({ filters: { created_at_gt: '2018-01-01', created_at_lt: '2018-01-31'} })
|
|
204
193
|
|
|
205
|
-
// Filters all the
|
|
194
|
+
// Filters all the organizations fetching only the ones created or updated after a specific date
|
|
206
195
|
// (attributes combined according to the OR logic)
|
|
207
|
-
const
|
|
196
|
+
const orgs = await clp.organizations.list({ filters: { updated_at_or_created_at_gt: '2019-10-10' } })
|
|
208
197
|
|
|
209
|
-
// Filters all the
|
|
210
|
-
// and whose
|
|
211
|
-
const
|
|
198
|
+
// Filters all the Roles fetching only the ones whose name contains the string "Admin"
|
|
199
|
+
// and whose organization name starts with the string "ORG"
|
|
200
|
+
const roles = await clp.roles.list({ filters: { name_cont: 'Admin', organization_name_start: 'ORG'} })
|
|
212
201
|
```
|
|
213
202
|
|
|
214
|
-
ℹ️ Check our API reference for more information on how to [filter data](https://docs.commercelayer.io/
|
|
203
|
+
ℹ️ Check our API reference for more information on how to [filter data](https://docs.commercelayer.io/provisioning/filtering-data).
|
|
215
204
|
</details>
|
|
216
205
|
|
|
217
206
|
<details>
|
|
218
|
-
<summary>How to paginate a collection of
|
|
207
|
+
<summary>How to paginate a collection of organizations</summary>
|
|
219
208
|
<br />
|
|
220
209
|
|
|
221
210
|
When you fetch a collection of resources, you get paginated results. You can request specific pages or items in a page like so:
|
|
222
211
|
|
|
223
212
|
```javascript
|
|
224
|
-
// Fetch the
|
|
225
|
-
const
|
|
213
|
+
// Fetch the organizations, setting the page number to 3 and the page size to 5
|
|
214
|
+
const skorgsus = await clp.organizations.list({ pageNumber: 3, pageSize: 5 })
|
|
226
215
|
|
|
227
|
-
// Get the total number of
|
|
228
|
-
const
|
|
216
|
+
// Get the total number of organizations in the collection
|
|
217
|
+
const orgCount = orgs.meta.recordCount
|
|
229
218
|
|
|
230
219
|
// Get the total number of pages
|
|
231
|
-
const pageCount =
|
|
220
|
+
const pageCount = orgs.meta.pageCount
|
|
232
221
|
```
|
|
233
222
|
|
|
234
223
|
> PS: the default page number is **1**, the default page size is **10**, and the maximum page size allowed is **25**.
|
|
235
224
|
|
|
236
|
-
ℹ️ Check our API reference for more information on how [pagination](https://docs.commercelayer.io/
|
|
225
|
+
ℹ️ Check our API reference for more information on how [pagination](https://docs.commercelayer.io/provisioning/pagination) works.
|
|
237
226
|
</details>
|
|
238
227
|
|
|
239
228
|
<details>
|
|
240
|
-
<summary>How to iterate through a collection of
|
|
229
|
+
<summary>How to iterate through a collection of Organizations</summary>
|
|
241
230
|
<br />
|
|
242
231
|
|
|
243
232
|
To execute a function for every item of a collection, use the `map()` method like so:
|
|
244
233
|
|
|
245
234
|
```javascript
|
|
246
|
-
// Fetch the whole list of
|
|
247
|
-
const
|
|
248
|
-
|
|
235
|
+
// Fetch the whole list of organizations (1st page) and print their ids and names to console
|
|
236
|
+
const orgs = await clp.organizations.list()
|
|
237
|
+
orgs.map(o => console.log('ID: ' + o.id + ' - Name: ' + o.name))
|
|
249
238
|
```
|
|
250
239
|
|
|
251
240
|
</details>
|
|
@@ -265,19 +254,19 @@ Many resources have relationships with other resources and instead of including
|
|
|
265
254
|
|
|
266
255
|
```javascript
|
|
267
256
|
// Fetch 1-to-1 related resource: billing address of an order
|
|
268
|
-
const
|
|
257
|
+
const org = clp.memberships.organization('xYZkjABcde')
|
|
269
258
|
|
|
270
259
|
// Fetch 1-to-N related resources: orders associated to a customer
|
|
271
|
-
const
|
|
260
|
+
const perms = clp.roles.permissions('XyzKjAbCDe', { fields: ['can_create', 'can_update'] })
|
|
272
261
|
```
|
|
273
262
|
|
|
274
263
|
In general:
|
|
275
264
|
|
|
276
|
-
- An API endpoint like `/api/
|
|
277
|
-
- 1-to-1 relationship API endpoints like `/api/
|
|
278
|
-
- 1-to-N relationship API endpoints like `/api/
|
|
265
|
+
- An API endpoint like `/api/organizations` or `/api/organization/<organizationId>` translates to `clp.organizations` or `clp.organizations('<organizationId>')` with the SDK.
|
|
266
|
+
- 1-to-1 relationship API endpoints like `/api/roles/<roleId>/organization` translates to `clp.roles('<roleId>', { include: ['organization'] }}` with the SDK.
|
|
267
|
+
- 1-to-N relationship API endpoints like `/api/roles/<roleId>?include=versions` or `/api/roles/<roleId>/permissions` translates to `clp.roles.retrieve('<roleId>', { include: ['versions'] })` or `clp.roles.permissions('<roleId>')` with the SDK.
|
|
279
268
|
|
|
280
|
-
ℹ️ Check our API reference for more information on how to [fetch relationships](https://docs.commercelayer.io/
|
|
269
|
+
ℹ️ Check our API reference for more information on how to [fetch relationships](https://docs.commercelayer.io/provisioning/fetching-relationships).
|
|
281
270
|
</details>
|
|
282
271
|
|
|
283
272
|
<details>
|
|
@@ -290,8 +279,8 @@ function passing a filter to get as result the total number of
|
|
|
290
279
|
resources.
|
|
291
280
|
|
|
292
281
|
```javascript
|
|
293
|
-
// Get the total number of
|
|
294
|
-
const
|
|
282
|
+
// Get the total number of sales_channel credentials
|
|
283
|
+
const credentials = clp.api_credentials.count({ filters: { kind_eq: 'sales_channel' } })
|
|
295
284
|
|
|
296
285
|
```
|
|
297
286
|
|
|
@@ -300,59 +289,59 @@ const placedOrders = cl.orders.count({ filters: { status_eq: 'placed' } })
|
|
|
300
289
|
### Update
|
|
301
290
|
|
|
302
291
|
<details>
|
|
303
|
-
<summary>How to update an existing
|
|
292
|
+
<summary>How to update an existing organization</summary>
|
|
304
293
|
<br />
|
|
305
294
|
|
|
306
295
|
```javascript
|
|
307
|
-
const
|
|
296
|
+
const org = {
|
|
308
297
|
id: 'xYZkjABcde',
|
|
309
|
-
|
|
310
|
-
|
|
298
|
+
reference: '<new-reference>',
|
|
299
|
+
reference_origin: '<new-reference-origin>'
|
|
311
300
|
}
|
|
312
301
|
|
|
313
|
-
|
|
302
|
+
clp.organizations.update(org) // updates the SKU on the server
|
|
314
303
|
```
|
|
315
304
|
|
|
316
|
-
ℹ️ Check our API reference for more information on how to [update an
|
|
305
|
+
ℹ️ Check our API reference for more information on how to [update an organization](https://docs.commercelayer.io/provisioning/v/api-reference-p/organizations/update).
|
|
317
306
|
</details>
|
|
318
307
|
|
|
319
308
|
### Delete
|
|
320
309
|
|
|
321
310
|
<details>
|
|
322
|
-
<summary>How to delete an existing
|
|
311
|
+
<summary>How to delete an existing membership</summary>
|
|
323
312
|
<br />
|
|
324
313
|
|
|
325
314
|
```javascript
|
|
326
|
-
|
|
315
|
+
clp.memberships.delete('xYZkjABcde') // persisted deletion
|
|
327
316
|
```
|
|
328
317
|
|
|
329
|
-
ℹ️ Check our API reference for more information on how to [delete
|
|
318
|
+
ℹ️ Check our API reference for more information on how to [delete a membership](https://docs.commercelayer.io/provisioning/v/api-reference-p/memberships/delete).
|
|
330
319
|
</details>
|
|
331
320
|
|
|
332
321
|
## Overriding credentials
|
|
333
322
|
|
|
334
|
-
If needed, Commerce Layer
|
|
323
|
+
If needed, Commerce Layer Provisioning SDK lets you change the client configuration and set it at a request level. To do that, just use the `config()` method or pass the `options` parameter and authenticate the API call with the desired credentials:
|
|
335
324
|
|
|
336
325
|
```javascript
|
|
337
326
|
// Permanently change configuration at client level
|
|
338
|
-
|
|
339
|
-
const
|
|
327
|
+
clp.config({ accessToken: 'your-access-token' })
|
|
328
|
+
const roles = await clp.roles.list()
|
|
340
329
|
|
|
341
330
|
or
|
|
342
331
|
|
|
343
332
|
// Use configuration at request level
|
|
344
|
-
|
|
333
|
+
clp.roles.list({}, { accessToken: 'your-access-token' })
|
|
345
334
|
```
|
|
346
335
|
|
|
347
336
|
## Handling validation errors
|
|
348
337
|
|
|
349
|
-
Commerce Layer API returns specific errors (with extra information) on each attribute of a single resource. You can inspect them to properly handle validation errors (if any). To do that, use the `errors` attribute of the catched error:
|
|
338
|
+
Commerce Layer Provisioning API returns specific errors (with extra information) on each attribute of a single resource. You can inspect them to properly handle validation errors (if any). To do that, use the `errors` attribute of the catched error:
|
|
350
339
|
|
|
351
340
|
```javascript
|
|
352
341
|
// Log error messages to console:
|
|
353
|
-
const attributes = {
|
|
342
|
+
const attributes = { name: '' }
|
|
354
343
|
|
|
355
|
-
const
|
|
344
|
+
const newRole = await clp.roles.create(attributes).catch(error => console.log(error.errors))
|
|
356
345
|
|
|
357
346
|
// Logged errors
|
|
358
347
|
/*
|
|
@@ -365,19 +354,11 @@ Commerce Layer API returns specific errors (with extra information) on each attr
|
|
|
365
354
|
status: '422',
|
|
366
355
|
meta: { error: 'blank' }
|
|
367
356
|
},
|
|
368
|
-
{
|
|
369
|
-
title: 'has already been taken',
|
|
370
|
-
detail: 'code - has already been taken',
|
|
371
|
-
code: 'VALIDATION_ERROR',
|
|
372
|
-
source: { pointer: '/data/attributes/code' },
|
|
373
|
-
status: '422',
|
|
374
|
-
meta: { error: 'taken', value: 'TSHIRTMM000000FFFFFFXL' }
|
|
375
|
-
},
|
|
376
357
|
{
|
|
377
358
|
title: "can't be blank",
|
|
378
|
-
detail: "
|
|
359
|
+
detail: "organization - can't be blank",
|
|
379
360
|
code: 'VALIDATION_ERROR',
|
|
380
|
-
source: { pointer: '/data/relationships/
|
|
361
|
+
source: { pointer: '/data/relationships/organization' },
|
|
381
362
|
status: '422',
|
|
382
363
|
meta: { error: 'blank' }
|
|
383
364
|
}
|
|
@@ -386,16 +367,16 @@ Commerce Layer API returns specific errors (with extra information) on each attr
|
|
|
386
367
|
|
|
387
368
|
```
|
|
388
369
|
|
|
389
|
-
ℹ️ Check our API reference for more information about the [errors](https://docs.commercelayer.io/
|
|
370
|
+
ℹ️ Check our API reference for more information about the [errors](https://docs.commercelayer.io/provisioning/handling-errors) returned by the API.
|
|
390
371
|
|
|
391
372
|
## Contributors guide
|
|
392
373
|
|
|
393
|
-
1. Fork [this repository](https://github.com/commercelayer/
|
|
374
|
+
1. Fork [this repository](https://github.com/commercelayer/provisioning-sdk) (learn how to do this [here](https://help.github.com/articles/fork-a-repo)).
|
|
394
375
|
|
|
395
376
|
2. Clone the forked repository like so:
|
|
396
377
|
|
|
397
378
|
```shell
|
|
398
|
-
git clone https://github.com/<your username>/
|
|
379
|
+
git clone https://github.com/<your username>/provisioning-sdk.git && cd provisioning-sdk
|
|
399
380
|
```
|
|
400
381
|
|
|
401
382
|
3. Make your changes and create a pull request ([learn how to do this](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request)).
|
|
@@ -406,7 +387,7 @@ Commerce Layer API returns specific errors (with extra information) on each attr
|
|
|
406
387
|
|
|
407
388
|
1. Join [Commerce Layer's Slack community](https://slack.commercelayer.app).
|
|
408
389
|
|
|
409
|
-
2. Create an [issue](https://github.com/commercelayer/
|
|
390
|
+
2. Create an [issue](https://github.com/commercelayer/provisioning-sdk/issues) in this repository.
|
|
410
391
|
|
|
411
392
|
3. Ping us [on Twitter](https://twitter.com/commercelayer).
|
|
412
393
|
|
package/lib/cjs/client.d.ts
CHANGED
|
@@ -16,13 +16,15 @@ type RequestConfig = {
|
|
|
16
16
|
proxy?: ProxyConfig;
|
|
17
17
|
headers?: RequestHeaders;
|
|
18
18
|
};
|
|
19
|
+
type RequestConfigExtra = {
|
|
20
|
+
adapter?: Adapter;
|
|
21
|
+
userAgent?: string;
|
|
22
|
+
};
|
|
19
23
|
type ApiConfig = {
|
|
20
24
|
domain?: string;
|
|
21
25
|
accessToken: string;
|
|
22
26
|
};
|
|
23
|
-
type ApiClientInitConfig = ApiConfig & RequestConfig &
|
|
24
|
-
adapter?: Adapter;
|
|
25
|
-
};
|
|
27
|
+
type ApiClientInitConfig = ApiConfig & RequestConfig & RequestConfigExtra;
|
|
26
28
|
type ApiClientConfig = Partial<ApiClientInitConfig>;
|
|
27
29
|
declare class ApiClient {
|
|
28
30
|
#private;
|
|
@@ -31,6 +33,7 @@ declare class ApiClient {
|
|
|
31
33
|
interceptors: InterceptorManager;
|
|
32
34
|
private constructor();
|
|
33
35
|
config(config: ApiClientConfig): ApiClient;
|
|
36
|
+
userAgent(userAgent: string): ApiClient;
|
|
34
37
|
adapter(adapter: Adapter): ApiClient;
|
|
35
38
|
request(method: Method, path: string, body?: any, options?: ApiClientConfig): Promise<any>;
|
|
36
39
|
private customHeaders;
|
package/lib/cjs/client.js
CHANGED
|
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
const axios_1 = __importDefault(require("axios"));
|
|
19
19
|
const error_1 = require("./error");
|
|
20
20
|
const config_1 = __importDefault(require("./config"));
|
|
21
|
+
// import { packageInfo } from './util'
|
|
21
22
|
const debug_1 = __importDefault(require("./debug"));
|
|
22
23
|
const debug = (0, debug_1.default)('client');
|
|
23
24
|
const baseURL = (domain) => {
|
|
@@ -40,10 +41,15 @@ class ApiClient {
|
|
|
40
41
|
timeout: options.timeout || config_1.default.client.timeout,
|
|
41
42
|
proxy: options.proxy,
|
|
42
43
|
httpAgent: options.httpAgent,
|
|
43
|
-
httpsAgent: options.httpsAgent
|
|
44
|
+
httpsAgent: options.httpsAgent
|
|
44
45
|
};
|
|
45
46
|
// Set custom headers
|
|
46
47
|
const customHeaders = this.customHeaders(options.headers);
|
|
48
|
+
// Set User-Agent
|
|
49
|
+
// const userAgentData = packageInfo(['version', 'dependencies.axios'], { nestedName: true })
|
|
50
|
+
let userAgent = options.userAgent || `SDK-provisioning axios/${axios_1.default.VERSION}`;
|
|
51
|
+
if (!userAgent.includes('axios/'))
|
|
52
|
+
userAgent += ` axios/${axios_1.default.VERSION}`;
|
|
47
53
|
const axiosOptions = {
|
|
48
54
|
baseURL: this.baseUrl,
|
|
49
55
|
timeout: config_1.default.client.timeout,
|
|
@@ -51,7 +57,8 @@ class ApiClient {
|
|
|
51
57
|
...customHeaders,
|
|
52
58
|
'Accept': 'application/vnd.api+json',
|
|
53
59
|
'Content-Type': 'application/vnd.api+json',
|
|
54
|
-
'Authorization': 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f")
|
|
60
|
+
'Authorization': 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f"),
|
|
61
|
+
'User-Agent': userAgent
|
|
55
62
|
},
|
|
56
63
|
...axiosConfig
|
|
57
64
|
};
|
|
@@ -73,6 +80,10 @@ class ApiClient {
|
|
|
73
80
|
def.httpAgent = config.httpAgent;
|
|
74
81
|
if (config.httpsAgent)
|
|
75
82
|
def.httpsAgent = config.httpsAgent;
|
|
83
|
+
if (config.adapter)
|
|
84
|
+
this.adapter(config.adapter);
|
|
85
|
+
if (config.userAgent)
|
|
86
|
+
this.userAgent(config.userAgent);
|
|
76
87
|
// API Client config
|
|
77
88
|
if (config.accessToken) {
|
|
78
89
|
__classPrivateFieldSet(this, _ApiClient_accessToken, config.accessToken, "f");
|
|
@@ -80,8 +91,18 @@ class ApiClient {
|
|
|
80
91
|
}
|
|
81
92
|
if (config.headers)
|
|
82
93
|
def.headers.common = this.customHeaders(config.headers);
|
|
83
|
-
|
|
84
|
-
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
userAgent(userAgent) {
|
|
97
|
+
if (userAgent) {
|
|
98
|
+
let ua = userAgent;
|
|
99
|
+
if (!ua.includes('axios/')) {
|
|
100
|
+
// const axiosVer = packageInfo(['dependencies.axios'], { nestedName: true })
|
|
101
|
+
if (axios_1.default.VERSION)
|
|
102
|
+
ua += ` axios/${axios_1.default.VERSION}`;
|
|
103
|
+
}
|
|
104
|
+
__classPrivateFieldGet(this, _ApiClient_client, "f").defaults.headers['User-Agent'] = ua;
|
|
105
|
+
}
|
|
85
106
|
return this;
|
|
86
107
|
}
|
|
87
108
|
adapter(adapter) {
|
|
@@ -91,10 +112,14 @@ class ApiClient {
|
|
|
91
112
|
}
|
|
92
113
|
async request(method, path, body, options) {
|
|
93
114
|
debug('request %s %s, %O, %O', method, path, body || {}, options || {});
|
|
115
|
+
// Ignored params alerts (in debug mode)
|
|
116
|
+
if (options === null || options === void 0 ? void 0 : options.adapter)
|
|
117
|
+
debug('Adapter ignored in request config');
|
|
118
|
+
if (options === null || options === void 0 ? void 0 : options.userAgent)
|
|
119
|
+
debug('User-Agent header ignored in request config');
|
|
94
120
|
const data = body ? { data: body } : undefined;
|
|
95
121
|
const url = path;
|
|
96
122
|
// Runtime request parameters
|
|
97
|
-
// const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : undefined
|
|
98
123
|
const accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
|
|
99
124
|
const headers = this.customHeaders(options === null || options === void 0 ? void 0 : options.headers);
|
|
100
125
|
if (accessToken)
|
|
@@ -111,7 +136,7 @@ class ApiClient {
|
|
|
111
136
|
const customHeaders = {};
|
|
112
137
|
if (headers) {
|
|
113
138
|
for (const [name, value] of Object.entries(headers))
|
|
114
|
-
if (!['accept', 'content-type', 'authorization'].includes(name.toLowerCase()))
|
|
139
|
+
if (!['accept', 'content-type', 'authorization', 'user-agent'].includes(name.toLowerCase()))
|
|
115
140
|
customHeaders[name] = value;
|
|
116
141
|
}
|
|
117
142
|
return customHeaders;
|
package/lib/cjs/commercelayer.js
CHANGED
|
@@ -68,22 +68,19 @@ class CommerceLayerProvisioningClient {
|
|
|
68
68
|
// ##__CL_RESOURCES_INIT_STOP__##
|
|
69
69
|
}
|
|
70
70
|
// get environment(): ApiMode { return this.#environment }
|
|
71
|
-
localConfig(config
|
|
71
|
+
localConfig(config) {
|
|
72
72
|
}
|
|
73
73
|
config(config) {
|
|
74
74
|
debug('config %o', config);
|
|
75
75
|
// CommerceLayer config
|
|
76
76
|
this.localConfig(config);
|
|
77
77
|
// ResourceAdapter config
|
|
78
|
-
// To rebuild baseUrl in client in case only the domain is defined
|
|
79
|
-
// if (!config.organization) config.organization = this.currentOrganization
|
|
80
78
|
__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").config(config);
|
|
81
79
|
return this;
|
|
82
80
|
}
|
|
83
81
|
resources() {
|
|
84
82
|
return static_1.CommerceLayerProvisioningStatic.resources();
|
|
85
83
|
}
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
87
84
|
isApiError(error) {
|
|
88
85
|
return static_1.CommerceLayerProvisioningStatic.isApiError(error);
|
|
89
86
|
}
|