@elasticpath/js-sdk 0.0.0-semantic-release

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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +347 -0
  3. package/package.json +117 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Elastic Path Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,347 @@
1
+ <img src="https://www.elasticpath.com/themes/custom/bootstrap_sass/logo.svg" alt="" width="400" />
2
+
3
+ # Elastic Path Commerce Cloud JavaScript SDK
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@elasticpath/sdk.svg)](https://www.npmjs.com/package/@elasticpath/sdk)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/elasticpath/js-sdk/issues)
8
+ [![follow on Twitter](https://img.shields.io/twitter/follow/elasticpath?style=social&logo=twitter)](https://twitter.com/intent/follow?screen_name=elasticpath)
9
+
10
+ > A simple to use API interface to help get you off the ground quickly and efficiently with your Elastic Path Commerce Cloud JavaScript apps.
11
+
12
+ 📚 [API reference](https://documentation.elasticpath.com/commerce-cloud/docs/developer/get-started/sdk.html#officially-supported-sdk) &mdash; 📚 [Elastic Path Commerce Cloud](https://www.elasticpath.com)
13
+
14
+ ## 🛠 Installation
15
+
16
+ Install the package from [npm](https://www.npmjs.com/package/@elasticpath/sdk) and import in your project.
17
+
18
+ ```bash
19
+ npm install --save @elasticpath/sdk
20
+ ```
21
+
22
+ ## ⛽️ Usage
23
+
24
+ To get started, instantiate a new ElasticPath client with your store credentials.
25
+
26
+ > **Note:** This requires an [Elastic Path Commerce Cloud](https://www.elasticpath.com) account.
27
+
28
+ ```js
29
+ // JavaScript
30
+ import { gateway as ElasticPathGateway } from '@elasticpath/sdk'
31
+
32
+ const ElasticPath = ElasticPathGateway({
33
+ client_id: 'XXX'
34
+ })
35
+
36
+ // Node.js
37
+ const ElasticPathGateway = require('@elasticpath/sdk').gateway
38
+
39
+ const ElasticPath = ElasticPathGateway({
40
+ client_id: 'XXX',
41
+ client_secret: 'XXX'
42
+ })
43
+ ```
44
+
45
+ Alternatively you can include the `UMD` bundle via [UNPKG](https://unpkg.com) like so:
46
+
47
+ ``` html
48
+ <script src="https://unpkg.com/@elasticpath/sdk"></script>
49
+
50
+ <script>
51
+ const ElasticPath = elasticpath.gateway({
52
+ client_id: 'XXX'
53
+ });
54
+ </script>
55
+ ```
56
+
57
+ > **Note:** If you're using [webpack](https://webpack.github.io), you'll need to add the following to your projects configuration file.
58
+
59
+ ```js
60
+ node: {
61
+ fs: 'empty'
62
+ }
63
+ ```
64
+
65
+ You can now authenticate with the ElasticPath service 🎉
66
+
67
+ ```js
68
+ ElasticPath.Authenticate().then(response => {
69
+ console.log('authenticated', response)
70
+ })
71
+ ```
72
+
73
+ Check out the [API reference](https://elasticpath.dev/docs/commerce-cloud) to learn more about authenticating and the available endpoints.
74
+
75
+ ### Custom Host
76
+
77
+ If you're an enterprise customer with your own infrastructure, you'll need to specify your API URL when instantiating:
78
+
79
+ ```js
80
+ const ElasticPath = ElasticPathGateway({
81
+ client_id: 'XXX',
82
+ host: 'api.yourdomain.com'
83
+ })
84
+ ```
85
+
86
+ ### Custom Storage
87
+
88
+ By default the Elastic Path Commerce Cloud SDK persists data to `window.localStorage` in the browser and `node-localstorage` in Node. If this doesn't suit your needs you can override the default storage with a `MemoryStorageFactory` which will persist data for the life cycle of the JavaScript VM:
89
+
90
+ ```js
91
+ import { gateway as ElasticPathGateway, MemoryStorageFactory } from '@elasticpath/sdk'
92
+
93
+ const ElasticPath = ElasticPathGateway({
94
+ client_id: 'XXX',
95
+ storage: new MemoryStorageFactory()
96
+ })
97
+ ```
98
+
99
+ Or alternatively, create your own storage factory by passing in an object which implements the following interface:
100
+
101
+ ```js
102
+ interface StorageFactory {
103
+ set(key: string, value: string): void;
104
+ get(key: string): string | null;
105
+ delete(key: string): void;
106
+ }
107
+ ```
108
+
109
+ ### Multiple Gateways
110
+
111
+ You can support multiple gateways with a `name` property when initializing the gateway.
112
+
113
+ `name` should be unique to avoid sharing storage keys with the other gateways of the same name.
114
+
115
+ ```js
116
+ import { gateway as EPCCGateway } from "@elasticpath/sdk"
117
+
118
+ const gatewayOne = EPCCGateway({
119
+ name: "my-first-gateway",
120
+ client_id: 'XXX'
121
+ })
122
+
123
+ const gatewayTwo = EPCCGateway({
124
+ name: "my-second-gateway",
125
+ client_id: 'XXX'
126
+ })
127
+ ```
128
+
129
+ Storage keys used for storage solutions are prefixed with the name provided and end with the relevant feature e.g.
130
+ `my-first-gateway_ep_cart`, `my-first-gateway_ep_credentials` and `my-first-gateway_ep_currency`.
131
+
132
+ If no name property is provided to the EPCCGateway function, the legacy naming is maintained:
133
+ `mcart`, `epCredentials` and `mcurrency`
134
+
135
+ ### Included Headers
136
+
137
+ There are currently several optional headers you can pass into the configuration, which include `application`, `language` and `currency`.
138
+
139
+ You can pass them into the config used by the gateway like this:
140
+
141
+ ``` TypeScript
142
+ // JavaScript
143
+ import { gateway as ElasticPathGateway } from '@elasticpath/sdk'
144
+ // const ElasticPathGateway = require('@elasticpath/sdk').gateway -> for Node
145
+
146
+ const ElasticPath = ElasticPathGateway({
147
+ client_id: 'XXX',
148
+ client_secret: 'XXX'
149
+ currency: 'YEN',
150
+ language: 'en',
151
+ application: 'my-app'
152
+ })
153
+ ```
154
+
155
+ ### Retries
156
+
157
+ In case the server responds with status 429 - "Too Many Requests" SDK will wait for some time and retry the same API request up to a given number of times.
158
+ You can fine tune this logic through following config parameters:
159
+
160
+ ``` TypeScript
161
+ const ElasticPath = ElasticPathGateway({
162
+ client_id: 'XXX',
163
+ client_secret: 'XXX',
164
+ retryDelay: 1000,
165
+ retryJitter: 500,
166
+ fetchMaxAttempts: 4
167
+ })
168
+
169
+ ```
170
+
171
+ In case of a 429 response SDK will wait for `retryDelay` milliseconds (default 1000) before attempting to make the same call.
172
+ If the server responds with 429 again it will wait for 2 * `retryDelay` ms, then 3 * `retryDelay` ms and so on.
173
+ On top of that the random value between 0 and `retryJitter` (default 500) will be added to each wait.
174
+ This would repeat up to `fetchMaxAttempts` (default 4) times.
175
+
176
+ ### Throttling (Rate Limiting)
177
+
178
+ SDK supports throttling through use of `throttled-queue` library.
179
+ Unlike the throttle functions of popular libraries, `throttled-queue` will not prevent any executions.
180
+ Instead, every execution is placed into a queue, which will be drained at the desired rate limit.
181
+ You can control throttling through following parameters:
182
+
183
+ ``` TypeScript
184
+ const ElasticPath = ElasticPathGateway({
185
+ client_id: 'XXX',
186
+ client_secret: 'XXX',
187
+ throttleEnabled: true,
188
+ throttleLimit: 3,
189
+ throttleInterval: 125
190
+ })
191
+
192
+ ```
193
+
194
+ This feature is disabled by default and to enable it you need to set `throttleEnabled` to true.
195
+ Once enabled you can use `throttleLimit` (default 3) and `throttleInterval` (default 125) to define what is the maximum number of calls per interval.
196
+ For example setting `throttleLimit = 5, throttleInterval = 1000` means maximum of 5 calls per second.
197
+
198
+ ### Handling File Upload
199
+
200
+ Files can be uploaded to the EPCC file service with the `ElasticPath.Files.Create` method. You should pass a `FormData` object as described in the [documentation](https://documentation.elasticpath.com/commerce-cloud/docs/api/advanced/files/create-a-file.html#post-create-a-file 'documentation').
201
+
202
+ In a Node.js environment, where you may be using an alternative `FormData` implementation, you can include a second parameter to represent the `Content-Type` header for the request. This must be `multipart/form-data` and must include a `boundary`. For example, using the `form-data` [package](https://www.npmjs.com/package/form-data 'package'):
203
+
204
+ ``` TypeScript
205
+ const FormData = require('form-data')
206
+ const formData = new FormData()
207
+ formData.append('file', buffer)
208
+
209
+ const contentType = formData.getHeaders()['content-type']
210
+
211
+ ElasticPath.Files.Create(formData, contentType)
212
+ ```
213
+
214
+ #### Referencing a file stored elsewhere
215
+
216
+ If you want to create a file by simply [referencing](https://documentation.elasticpath.com/commerce-cloud/docs/api/advanced/files/create-a-file.html#post-create-a-file 'referencing') a file stored elsewhere, you can use this helper method:
217
+
218
+ ``` TypeScript
219
+ ElasticPath.Files.Link('https://cdn.external-host.com/files/filename.png')
220
+ ```
221
+
222
+ Just pass the URL to the `Link` method and creation will be handled for you.
223
+
224
+ ### TypeScript Support
225
+
226
+ The Elastic Path Commerce Cloud JavaScript SDK is fully supported in Typescript.
227
+
228
+ Imported module will contain all interfaces needed to consume backend services. i.e:
229
+
230
+ ```TypeScript
231
+ import * as elasticpath from '@elasticpath/sdk';
232
+
233
+ const product: elasticpath.ProductBase = {...}
234
+ ```
235
+
236
+ If you do not want to use the namespace, you can extend the interfaces and define them yourself, like so:
237
+
238
+ ```TypeScript
239
+ // You can name the interface anything you like
240
+ interface Product extends product.ProductBase {
241
+ }
242
+
243
+ const product: Product = {...}
244
+ ```
245
+
246
+ Here is an example of a simple product creation:
247
+
248
+ ```TypeScript
249
+ import { ElasticPath, gateway, ProductBase, Resource } from '@elasticpath/sdk';
250
+
251
+ async function main() {
252
+ const g: ElasticPath = gateway({client_id, client_secret});
253
+ const auth = await g.Authenticate();
254
+
255
+ const newProduct: ProductBase = {
256
+ type: "product",
257
+ name: "My Product",
258
+ slug: "my-prod",
259
+ sku: "my-prod",
260
+ manage_stock: false,
261
+ description: "Some description",
262
+ status: "draft",
263
+ commodity_type: "physical",
264
+ price: [
265
+ {
266
+ amount: 5499,
267
+ currency: "USD",
268
+ includes_tax: true
269
+ }
270
+ ]
271
+ };
272
+
273
+ const nP: Resource<Product> = await g.Products.Create(newProduct);
274
+ }
275
+ ```
276
+
277
+ You can also extend any base interface compatible with flows to create any custom interfaces that you might be using by re-declaring `@elasticpath/sdk` module. Following example adds several properties to `ProductsBase` interface that correspond to flows added to the backend.
278
+
279
+ In your project add a definition file (with a `.d.ts` extension) with a following code:
280
+
281
+ ```TypeScript
282
+ import * as elasticpath from '@elasticpath/sdk';
283
+
284
+ declare module '@elasticpath/sdk' {
285
+
286
+ interface Weight {
287
+ g: number;
288
+ kg: number;
289
+ lb: number;
290
+ oz: number;
291
+ }
292
+
293
+ interface ProductBase {
294
+ background_color: string;
295
+ background_colour: string | null;
296
+ bulb: string;
297
+ bulb_qty: string;
298
+ finish: string;
299
+ material: string;
300
+ max_watt: string;
301
+ new: string | null;
302
+ on_sale: string | null;
303
+ weight: Weight;
304
+ }
305
+
306
+ }
307
+ ```
308
+
309
+ This will affect base interface and all other Product interfaces that inherit from base interface so added properties will be present when creating, updating, fetching products.
310
+
311
+ ## ❤️ Contributing
312
+
313
+ We love community contributions. Here's a quick guide if you want to submit a pull request:
314
+
315
+ 1. Fork the repository
316
+ 2. Add a test for your change (it should fail)
317
+ 3. Make the tests pass
318
+ 4. Commit your changes (see note below)
319
+ 5. Submit your PR with a brief description explaining your changes
320
+
321
+ > **Note:** Commits should adhere to the [Angular commit conventions](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#-git-commit-guidelines).
322
+
323
+ Make sure you have [Prettier](https://prettier.io) installed for your editor with ESLint integration enabled.
324
+
325
+ ## ⚡️ Development
326
+
327
+ The SDK is built with [ES6 modules](https://strongloop.com/strongblog/an-introduction-to-javascript-es6-modules/) that are bundled using [Rollup](http://rollupjs.org).
328
+
329
+ If you want to roll your own bundle, or make changes to any of the modules in `src`, then you'll need to install the package dependencies and run rollup while watching for changes.
330
+
331
+ ```
332
+ npm install
333
+ npm start
334
+ ```
335
+
336
+ To run test
337
+
338
+ ```
339
+ npm test
340
+ ```
341
+
342
+ You can learn more about the Rollup API and configuration [here](https://github.com/rollup/rollup/wiki).
343
+
344
+ ## Terms And Conditions
345
+
346
+ - Any changes to this project must be reviewed and approved by the repository owner.
347
+ - For more information about the license, see [MIT License](https://github.com/elasticpath/js-sdk/blob/main/LICENSE).
package/package.json ADDED
@@ -0,0 +1,117 @@
1
+ {
2
+ "name": "@elasticpath/js-sdk",
3
+ "description": "SDK for the Elastic Path eCommerce API",
4
+ "version": "0.0.0-semantic-release",
5
+ "homepage": "https://github.com/elasticpath/js-sdk",
6
+ "author": "Elastic Path (https://elasticpath.com/)",
7
+ "files": [
8
+ "dist/**"
9
+ ],
10
+ "scripts": {
11
+ "commit": "cz",
12
+ "rollup": "rollup -c",
13
+ "start": "SERVE=true rollup -c -w",
14
+ "test": "NODE_ENV=test mocha --require @babel/register -r ts-node/register test/tests.ts",
15
+ "test-watch": "nodemon -w 'src/**' -w 'test/**' -e ts --exec 'NODE_ENV=test mocha --require @babel/register -r ts-node/register test/tests.ts'",
16
+ "test-output": "NODE_ENV=test mocha --require @babel/register -r ts-node/register --reporter mocha-junit-reporter test/tests.ts",
17
+ "playground": "ts-node playground/index.ts",
18
+ "lint": "eslint src"
19
+ },
20
+ "main": "dist/index.cjs.js",
21
+ "module": "dist/index.esm.js",
22
+ "jsnext:main": "dist/index.esm.js",
23
+ "cjs:main": "dist/index.cjs.js",
24
+ "browser": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git://github.com/elasticpath/js-sdk.git"
29
+ },
30
+ "release": {
31
+ "branches": [
32
+ "main",
33
+ "beta"
34
+ ],
35
+ "prepare": [
36
+ "@semantic-release/npm",
37
+ {
38
+ "path": "@semantic-release/exec",
39
+ "cmd": "./preRelease.sh ${nextRelease.version}"
40
+ }
41
+ ]
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/elasticpath/js-sdk/issues"
45
+ },
46
+ "devDependencies": {
47
+ "@babel/core": "7.21.3",
48
+ "@babel/eslint-parser": "7.21.3",
49
+ "@babel/preset-env": "7.20.2",
50
+ "@babel/register": "7.21.0",
51
+ "@rollup/plugin-babel": "5.3.1",
52
+ "@rollup/plugin-commonjs": "22.0.2",
53
+ "@rollup/plugin-json": "4.1.0",
54
+ "@rollup/plugin-node-resolve": "13.3.0",
55
+ "@semantic-release/exec": "^6.0.3",
56
+ "@sinonjs/fake-timers": "^11.1.0",
57
+ "@types/chai": "4.3.4",
58
+ "@types/mocha": "9.1.1",
59
+ "@types/node": "18.0.6",
60
+ "@types/sinonjs__fake-timers": "^8.1.2",
61
+ "babel-plugin-rewire": "^1.2.0",
62
+ "chai": "4.3.7",
63
+ "commitizen": "4.2.5",
64
+ "core-js": "3.29.1",
65
+ "cz-conventional-changelog": "2.1.0",
66
+ "eslint": "8.37.0",
67
+ "eslint-config-airbnb-base": "15.0.0",
68
+ "eslint-config-prettier": "8.5.0",
69
+ "eslint-plugin-import": "2.26.0",
70
+ "lint-staged": "7.3.0",
71
+ "mocha": "10.0.0",
72
+ "mocha-junit-reporter": "1.18.0",
73
+ "nock": "13.2.9",
74
+ "nodemon": "2.0.22",
75
+ "prettier": "2.7.1",
76
+ "rewire": "6.0.0",
77
+ "rimraf": "2.6.2",
78
+ "rollup": "2.79.1",
79
+ "rollup-plugin-dts": "4.2.3",
80
+ "rollup-plugin-filesize": "9.1.2",
81
+ "rollup-plugin-ignore": "1.0.10",
82
+ "rollup-plugin-livereload": "2.0.5",
83
+ "rollup-plugin-serve": "2.0.2",
84
+ "rollup-plugin-sourcemaps": "0.6.3",
85
+ "rollup-plugin-uglify": "6.0.4",
86
+ "ts-node": "10.9.1",
87
+ "typescript": "4.9.5"
88
+ },
89
+ "dependencies": {
90
+ "cross-fetch": "^3.1.5",
91
+ "es6-promise": "^4.0.5",
92
+ "form-data": "^4.0.0",
93
+ "inflected": "^2.0.1",
94
+ "js-cookie": "^3.0.1",
95
+ "node-localstorage": "^2.1.6",
96
+ "throttled-queue": "^2.1.4"
97
+ },
98
+ "config": {
99
+ "commitizen": {
100
+ "path": "cz-conventional-changelog"
101
+ }
102
+ },
103
+ "directories": {
104
+ "test": "test"
105
+ },
106
+ "license": "MIT",
107
+ "publishConfig": {
108
+ "access": "public"
109
+ },
110
+ "lint-staged": {
111
+ "{src,test}/**/*.{js,ts,json,md}": [
112
+ "eslint --fix src",
113
+ "prettier --write",
114
+ "git add"
115
+ ]
116
+ }
117
+ }