@searchspring/snap-platforms 0.56.0 → 0.56.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.
@@ -0,0 +1,8 @@
1
+ import type { Product } from '@searchspring/snap-store-mobx';
2
+ type BigCommerceAddToCartConfig = {
3
+ redirect?: boolean | string;
4
+ idFieldName?: string;
5
+ };
6
+ export declare const addToCart: (items: Product[], config?: BigCommerceAddToCartConfig) => Promise<void>;
7
+ export {};
8
+ //# sourceMappingURL=addToCart.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addToCart.d.ts","sourceRoot":"","sources":["../../../../bigcommerce/src/addToCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAE7D,KAAK,0BAA0B,GAAG;IACjC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAWF,eAAO,MAAM,SAAS,UAAiB,OAAO,EAAE,WAAW,0BAA0B,kBAkDpF,CAAC"}
@@ -0,0 +1,81 @@
1
+ export const addToCart = async (items, config) => {
2
+ if (!items) {
3
+ console.error('Error: no products to add');
4
+ return;
5
+ }
6
+ const formData = {
7
+ line_items: [],
8
+ };
9
+ items.map((item) => {
10
+ let id = item?.display?.mappings?.core?.uid;
11
+ // try to find custom field in data
12
+ if (config?.idFieldName) {
13
+ let level = item;
14
+ config.idFieldName.split('.').map((field) => {
15
+ if (level[field]) {
16
+ level = level[field];
17
+ }
18
+ else {
19
+ console.error('Error: couldnt find column in item data. please check your idFieldName is correct in the config.');
20
+ return;
21
+ }
22
+ });
23
+ if (level && level !== item) {
24
+ id = level;
25
+ }
26
+ }
27
+ if (id && item.quantity) {
28
+ const obj = {
29
+ product_id: id,
30
+ quantity: item.quantity,
31
+ };
32
+ formData.line_items.push(obj);
33
+ }
34
+ });
35
+ // first check how many products we are adding
36
+ if (formData.line_items.length) {
37
+ for (let i = 0; i < formData.line_items.length; i++) {
38
+ await addSingleProductv1(formData.line_items[i]);
39
+ }
40
+ }
41
+ // do redirect (or not)
42
+ if (config?.redirect !== false) {
43
+ setTimeout(() => (window.location.href = typeof config?.redirect == 'string' ? config?.redirect : '/cart.php'));
44
+ }
45
+ };
46
+ const addSingleProductv1 = async (item) => {
47
+ if (!item) {
48
+ console.error('Error: no product to add');
49
+ return;
50
+ }
51
+ const endpoint = {
52
+ route: `/remote/v1/cart/add`,
53
+ method: 'POST',
54
+ accept: 'application/json',
55
+ content: 'application/json',
56
+ success: 200,
57
+ };
58
+ try {
59
+ const payload = JSON.stringify({
60
+ ...item,
61
+ action: 'add',
62
+ });
63
+ const init = {
64
+ method: endpoint.method,
65
+ credentials: 'same-origin',
66
+ headers: {
67
+ // note: no authorization
68
+ Accept: endpoint.accept,
69
+ 'Content-Type': endpoint.content,
70
+ },
71
+ body: payload,
72
+ };
73
+ const response = await fetch(endpoint.route, init);
74
+ if (response.status !== endpoint.success) {
75
+ throw new Error(`Error: addToCart responded with ${response.status}, ${response}`);
76
+ }
77
+ }
78
+ catch (err) {
79
+ console.error(err);
80
+ }
81
+ };
@@ -0,0 +1,2 @@
1
+ export * from './addToCart';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../bigcommerce/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './addToCart';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@searchspring/snap-platforms",
3
- "version": "0.56.0",
3
+ "version": "0.56.2",
4
4
  "description": "Snap Platforms Library",
5
5
  "author": "Searchspring",
6
6
  "license": "MIT",
@@ -9,8 +9,8 @@
9
9
  "access": "public"
10
10
  },
11
11
  "scripts": {
12
- "build": "rm -rf ./dist && rm -fr ./components/dist && tsc && tsc -p tsconfig.cjs.json",
13
- "build:docs": "typedoc --out docs src/index.ts",
12
+ "build": "rm -rf ./dist && tsc && tsc -p tsconfig.cjs.json",
13
+ "build:docs": "echo 'no docs for snap-platforms'",
14
14
  "dev": "tsc --watch",
15
15
  "format": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'",
16
16
  "lint": "eslint './**/*.{js,jsx,ts,tsx}'",
@@ -42,5 +42,5 @@
42
42
  "require": "./dist/cjs/bigcommerce/src/index.js"
43
43
  }
44
44
  },
45
- "gitHead": "3326303c739fd3bbb23e6477b275b89f7df3bb74"
45
+ "gitHead": "83b8de3b23a918e683c7f2434b032626af3faf7f"
46
46
  }