@fast-simon/bigcommerce_customer 1.0.43 → 1.0.44
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/lib/@types/bigcommerceCustomerTypes.js +2 -1
- package/lib/main.js +5 -2
- package/lib/utils/API.js +105 -32
- package/lib/utils/bigcommerceCustomer.js +94 -40
- package/package.json +1 -1
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/lib/main.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bigcommerceCustomer = void 0;
|
|
4
|
+
var bigcommerceCustomer_1 = require("./utils/bigcommerceCustomer");
|
|
5
|
+
Object.defineProperty(exports, "bigcommerceCustomer", { enumerable: true, get: function () { return bigcommerceCustomer_1.bigcommerceCustomer; } });
|
package/lib/utils/API.js
CHANGED
|
@@ -1,43 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (_) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.parseUrl = exports.API = void 0;
|
|
51
|
+
var axios_1 = require("axios");
|
|
52
|
+
exports.API = {
|
|
53
|
+
get: function (_a) {
|
|
54
|
+
var url = _a.url, params = _a.params, config = _a.config;
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
var response;
|
|
57
|
+
return __generator(this, function (_b) {
|
|
58
|
+
switch (_b.label) {
|
|
59
|
+
case 0: return [4 /*yield*/, axios_1.default.get(url, __assign({ params: params }, config))];
|
|
60
|
+
case 1:
|
|
61
|
+
response = _b.sent();
|
|
62
|
+
return [2 /*return*/, response.data];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
7
65
|
});
|
|
8
|
-
return response.data;
|
|
9
66
|
},
|
|
10
|
-
post:
|
|
11
|
-
|
|
12
|
-
return
|
|
67
|
+
post: function (_a) {
|
|
68
|
+
var url = _a.url, config = _a.config, data = _a.data;
|
|
69
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
70
|
+
var response;
|
|
71
|
+
return __generator(this, function (_b) {
|
|
72
|
+
switch (_b.label) {
|
|
73
|
+
case 0: return [4 /*yield*/, axios_1.default.post(url, data, config)];
|
|
74
|
+
case 1:
|
|
75
|
+
response = _b.sent();
|
|
76
|
+
return [2 /*return*/, response.data];
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
13
80
|
},
|
|
14
|
-
getWithFallback:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
81
|
+
getWithFallback: function (_a) {
|
|
82
|
+
var _b, _c;
|
|
83
|
+
var url = _a.url, params = _a.params, config = _a.config, fallbackURL = _a.fallbackURL, timeout = _a.timeout;
|
|
84
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
+
var response, e_1, response;
|
|
86
|
+
return __generator(this, function (_d) {
|
|
87
|
+
switch (_d.label) {
|
|
88
|
+
case 0:
|
|
89
|
+
_d.trys.push([0, 2, , 5]);
|
|
90
|
+
return [4 /*yield*/, axios_1.default.get(url, __assign(__assign({ params: params }, config), { timeout: timeout, timeoutErrorMessage: 'Request timeout, using fallback endpoint instead' }))];
|
|
91
|
+
case 1:
|
|
92
|
+
response = _d.sent();
|
|
93
|
+
return [2 /*return*/, response.data];
|
|
94
|
+
case 2:
|
|
95
|
+
e_1 = _d.sent();
|
|
96
|
+
console.error((_b = e_1) === null || _b === void 0 ? void 0 : _b.message);
|
|
97
|
+
if (!(fallbackURL != null)) return [3 /*break*/, 4];
|
|
98
|
+
return [4 /*yield*/, axios_1.default.get(fallbackURL, __assign({ params: params }, config))];
|
|
99
|
+
case 3:
|
|
100
|
+
response = _d.sent();
|
|
101
|
+
return [2 /*return*/, (_c = response) === null || _c === void 0 ? void 0 : _c.data];
|
|
102
|
+
case 4: return [2 /*return*/, null];
|
|
103
|
+
case 5: return [2 /*return*/];
|
|
104
|
+
}
|
|
21
105
|
});
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
console.error(e?.message);
|
|
26
|
-
if (fallbackURL != null) {
|
|
27
|
-
const response = await axios.get(fallbackURL, {
|
|
28
|
-
params,
|
|
29
|
-
...config
|
|
30
|
-
});
|
|
31
|
-
return response?.data;
|
|
32
|
-
}
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
106
|
+
});
|
|
35
107
|
}
|
|
36
108
|
};
|
|
37
|
-
|
|
38
|
-
|
|
109
|
+
var parseUrl = function (url) {
|
|
110
|
+
var fixed = url.replace("https://", "").replace("http://", "");
|
|
39
111
|
if (fixed[fixed.length - 1] === "/") {
|
|
40
112
|
return fixed.slice(0, fixed.length - 1);
|
|
41
113
|
}
|
|
42
114
|
return fixed;
|
|
43
115
|
};
|
|
116
|
+
exports.parseUrl = parseUrl;
|
|
@@ -1,51 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
catch (e) {
|
|
33
|
-
// no console.error as all non-logged in users will get an error
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
34
32
|
}
|
|
35
|
-
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.bigcommerceCustomer = exports.__ISP_BC_GROUP_KEY_SAVED_DATE = exports.__ISP_BC_GROUP_KEY_USER = exports.__ISP_BC_GROUP_KEY = void 0;
|
|
40
|
+
exports.__ISP_BC_GROUP_KEY = '__ISP_BC_GROUP_KEY';
|
|
41
|
+
exports.__ISP_BC_GROUP_KEY_USER = 'customerGroup';
|
|
42
|
+
exports.__ISP_BC_GROUP_KEY_SAVED_DATE = '__ISP_BC_GROUP_KEY_SAVED_DATE';
|
|
43
|
+
var API_1 = require("./API");
|
|
44
|
+
var appClientId = "igimdk6jnrt5g5mbwui5gi7f7ke1w5p";
|
|
45
|
+
var bigcommerceCustomer = /** @class */ (function () {
|
|
46
|
+
function bigcommerceCustomer() {
|
|
36
47
|
}
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
bigcommerceCustomer.get = function () {
|
|
49
|
+
return API_1.API.get({
|
|
50
|
+
url: "/customer/current.jwt?app_client_id=".concat(appClientId),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
bigcommerceCustomer.setBigcommerceCustomerGroupId = function () {
|
|
54
|
+
var _a;
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
var groupIdFromStorage, res, currentCustomer, groupId, e_1;
|
|
57
|
+
return __generator(this, function (_b) {
|
|
58
|
+
switch (_b.label) {
|
|
59
|
+
case 0:
|
|
60
|
+
groupIdFromStorage = (_a = localStorage.getItem(exports.__ISP_BC_GROUP_KEY)) !== null && _a !== void 0 ? _a : localStorage.getItem(exports.__ISP_BC_GROUP_KEY_USER);
|
|
61
|
+
if (!groupIdFromStorage) return [3 /*break*/, 1];
|
|
62
|
+
return [2 /*return*/, groupIdFromStorage];
|
|
63
|
+
case 1:
|
|
64
|
+
_b.trys.push([1, 3, , 4]);
|
|
65
|
+
return [4 /*yield*/, bigcommerceCustomer.get()];
|
|
66
|
+
case 2:
|
|
67
|
+
res = (_b.sent());
|
|
68
|
+
if (!(res === null || res === void 0 ? void 0 : res.token))
|
|
69
|
+
return [2 /*return*/];
|
|
70
|
+
currentCustomer = isp_parseJwt(res.token);
|
|
71
|
+
if (currentCustomer && currentCustomer.customer && currentCustomer.customer.group_id) {
|
|
72
|
+
groupId = currentCustomer.customer.group_id;
|
|
73
|
+
if (!groupId)
|
|
74
|
+
return [2 /*return*/];
|
|
75
|
+
localStorage.setItem(exports.__ISP_BC_GROUP_KEY, groupId);
|
|
76
|
+
localStorage.setItem(exports.__ISP_BC_GROUP_KEY_SAVED_DATE, Date.now().toString());
|
|
77
|
+
return [2 /*return*/, groupId];
|
|
78
|
+
}
|
|
79
|
+
return [3 /*break*/, 4];
|
|
80
|
+
case 3:
|
|
81
|
+
e_1 = _b.sent();
|
|
82
|
+
return [3 /*break*/, 4];
|
|
83
|
+
case 4: return [2 /*return*/];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
bigcommerceCustomer.getBigcommerceCustomerGroupId = function () {
|
|
89
|
+
var _a;
|
|
90
|
+
var groupIdFromStorage = (_a = localStorage.getItem(exports.__ISP_BC_GROUP_KEY)) !== null && _a !== void 0 ? _a : localStorage.getItem(exports.__ISP_BC_GROUP_KEY_USER);
|
|
39
91
|
if (groupIdFromStorage) {
|
|
40
92
|
return groupIdFromStorage;
|
|
41
93
|
}
|
|
42
94
|
return undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
95
|
+
};
|
|
96
|
+
return bigcommerceCustomer;
|
|
97
|
+
}());
|
|
98
|
+
exports.bigcommerceCustomer = bigcommerceCustomer;
|
|
45
99
|
function isp_parseJwt(token) {
|
|
46
100
|
try {
|
|
47
|
-
|
|
48
|
-
|
|
101
|
+
var base64Url = token.split('.')[1];
|
|
102
|
+
var base64 = base64Url.replace('-', '+').replace('_', '/');
|
|
49
103
|
return JSON.parse(window.atob(base64));
|
|
50
104
|
}
|
|
51
105
|
catch (e) {
|