@fast-simon/bigcommerce_customer 1.0.33

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,22 @@
1
+ interface customer {
2
+ email: string;
3
+ group_id: string;
4
+ id: number;
5
+ }
6
+ export interface bigcommerceCustomerResponse {
7
+ application_id: string;
8
+ aud: string;
9
+ customer: customer;
10
+ exp: number;
11
+ iat: number;
12
+ iss: string;
13
+ operation: string;
14
+ store_hash: string;
15
+ sub: string;
16
+ version: number;
17
+ }
18
+ export declare type BigcommerceCustomerGroup = string | undefined;
19
+ export interface bigcommerceCustomerAPIResponse {
20
+ token: string;
21
+ }
22
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=bigcommerceCustomer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bigcommerceCustomer.js","sourceRoot":"","sources":["../../src/@types/bigcommerceCustomer.ts"],"names":[],"mappings":""}
package/lib/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { BigcommerceCustomer } from "./utils/bigcommerceCustomer";
2
+ export { BigcommerceCustomer, };
package/lib/main.js ADDED
@@ -0,0 +1,3 @@
1
+ import { BigcommerceCustomer } from "./utils/bigcommerceCustomer";
2
+ export { BigcommerceCustomer, };
3
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAElE,OAAO,EACH,mBAAmB,GACtB,CAAA"}
@@ -0,0 +1,23 @@
1
+ import { AxiosRequestConfig } from "axios";
2
+ interface Params {
3
+ [key: string]: any;
4
+ }
5
+ interface GetProps {
6
+ url: string;
7
+ params?: Params;
8
+ config?: AxiosRequestConfig;
9
+ fallbackURL?: string;
10
+ timeout?: number;
11
+ }
12
+ interface PostProps {
13
+ url: string;
14
+ data?: Params;
15
+ config?: AxiosRequestConfig;
16
+ }
17
+ export declare const API: {
18
+ get: <T>({ url, params, config }: GetProps) => Promise<T>;
19
+ post: <T_1>({ url, config, data }: PostProps) => Promise<any>;
20
+ getWithFallback: <T_2>({ url, params, config, fallbackURL, timeout }: GetProps) => Promise<any>;
21
+ };
22
+ export declare const parseUrl: (url: string) => string;
23
+ export {};
@@ -0,0 +1,44 @@
1
+ import axios from "axios";
2
+ export const API = {
3
+ get: async function ({ url, params, config }) {
4
+ const response = await axios.get(url, {
5
+ params,
6
+ ...config
7
+ });
8
+ return response.data;
9
+ },
10
+ post: async function ({ url, config, data }) {
11
+ const response = await axios.post(url, data, config);
12
+ return response.data;
13
+ },
14
+ getWithFallback: async function ({ url, params, config, fallbackURL, timeout }) {
15
+ try {
16
+ const response = await axios.get(url, {
17
+ params,
18
+ ...config,
19
+ timeout,
20
+ timeoutErrorMessage: 'Request timeout, using fallback endpoint instead'
21
+ });
22
+ return response.data;
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
+ }
35
+ }
36
+ };
37
+ export const parseUrl = (url) => {
38
+ const fixed = url.replace("https://", "").replace("http://", "");
39
+ if (fixed[fixed.length - 1] === "/") {
40
+ return fixed.slice(0, fixed.length - 1);
41
+ }
42
+ return fixed;
43
+ };
44
+ //# sourceMappingURL=API.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"API.js","sourceRoot":"","sources":["../../src/utils/API.ts"],"names":[],"mappings":"AAAA,OAAO,KAA0C,MAAM,OAAO,CAAC;AAoB/D,MAAM,CAAC,MAAM,GAAG,GAAG;IACf,GAAG,EAAE,KAAK,WAAc,EAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAW;QACnD,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAwB,GAAG,EAAE;YACzD,MAAM;YACN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AC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;KAC1C;IACD,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare const __ISP_BC_GROUP_KEY = "__ISP_BC_GROUP_KEY";
2
+ export declare class BigcommerceCustomer {
3
+ private static get;
4
+ static getBigcommerceCustomerGroupId(): Promise<string | undefined>;
5
+ }
@@ -0,0 +1,46 @@
1
+ export const __ISP_BC_GROUP_KEY = '__ISP_BC_GROUP_KEY';
2
+ import { API } from "./API";
3
+ const appClientId = "igimdk6jnrt5g5mbwui5gi7f7ke1w5p";
4
+ export class BigcommerceCustomer {
5
+ static get() {
6
+ return API.get({
7
+ url: `/customer/current.jwt?app_client_id=${appClientId}`,
8
+ });
9
+ }
10
+ static async getBigcommerceCustomerGroupId() {
11
+ const groupIdFromStorage = localStorage.getItem(__ISP_BC_GROUP_KEY);
12
+ if (groupIdFromStorage) {
13
+ return groupIdFromStorage;
14
+ }
15
+ else {
16
+ try {
17
+ const res = (await BigcommerceCustomer.get());
18
+ if (!res?.token)
19
+ return;
20
+ const currentCustomer = isp_parseJwt(res.token);
21
+ if (currentCustomer && currentCustomer.customer && currentCustomer.customer.group_id) {
22
+ const groupId = currentCustomer.customer.group_id;
23
+ if (!groupId)
24
+ return;
25
+ return groupId;
26
+ localStorage.setItem(__ISP_BC_GROUP_KEY, groupId);
27
+ }
28
+ }
29
+ catch (e) {
30
+ // no console.error as all non-logged in users will get an error
31
+ }
32
+ }
33
+ }
34
+ }
35
+ function isp_parseJwt(token) {
36
+ try {
37
+ const base64Url = token.split('.')[1];
38
+ const base64 = base64Url.replace('-', '+').replace('_', '/');
39
+ return JSON.parse(window.atob(base64));
40
+ }
41
+ catch (e) {
42
+ console.error(e);
43
+ return null;
44
+ }
45
+ }
46
+ //# sourceMappingURL=bigcommerceCustomer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bigcommerceCustomer.js","sourceRoot":"","s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AS,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;KAC1C;IAAC,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;KACf;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@fast-simon/bigcommerce_customer",
3
+ "files": [
4
+ "lib/**/*"
5
+ ],
6
+ "type": "module",
7
+ "types": "lib/main.d.ts",
8
+ "version": "1.0.33",
9
+ "scripts": {
10
+ "dev": "vite",
11
+ "build": "tsc && vite build",
12
+ "preview": "vite preview",
13
+ "prepublishOnly": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./build.ps1",
14
+ "tsc": "tsc"
15
+ },
16
+ "license": "ISC",
17
+ "author": {
18
+ "name": "Fast Simon inc",
19
+ "url": "",
20
+ "email": ""
21
+ },
22
+ "main": "lib/main.js",
23
+ "directories": {
24
+ "lib": "lib"
25
+ },
26
+ "description": "",
27
+ "devDependencies": {
28
+ "@fast-simon/types": "^1.0.9",
29
+ "typescript": "^4.5.5",
30
+ "vite": "^2.8.0"
31
+ },
32
+ "dependencies": {
33
+ "axios": "^0.24.0"
34
+ }
35
+ }