@helpdice/sdk 0.2.6 → 0.2.9
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/dist/cjs/account/endpoint.js +8 -0
- package/dist/cjs/account/index.js +17 -0
- package/dist/cjs/account/types.js +2 -0
- package/dist/cjs/account/update_profile.js +17 -0
- package/dist/cjs/accounting/createInvoice.js +17 -0
- package/dist/{accounting → cjs/accounting}/endpoint.js +3 -2
- package/dist/cjs/accounting/index.js +25 -0
- package/dist/cjs/accounting/submitInvoice.js +17 -0
- package/dist/cjs/accounting/types.js +2 -0
- package/dist/cjs/accounting/updateInvoice.js +17 -0
- package/dist/cjs/apis/cities.js +17 -0
- package/dist/cjs/apis/countries.js +17 -0
- package/dist/cjs/apis/index.js +14 -0
- package/dist/cjs/apis/states.js +17 -0
- package/dist/{assets → cjs/assets}/endpoint.js +3 -2
- package/dist/cjs/assets/index.js +11 -0
- package/dist/cjs/auth/authWithKey.js +17 -0
- package/dist/cjs/auth/authWithToken.js +17 -0
- package/dist/{auth → cjs/auth}/endpoint.js +3 -2
- package/dist/cjs/auth/index.js +94 -0
- package/dist/cjs/auth/logout.js +22 -0
- package/dist/cjs/auth/register.js +17 -0
- package/dist/cjs/auth/resetPassword.js +17 -0
- package/dist/{auth → cjs/auth}/signInWithGoogle.js +14 -10
- package/dist/cjs/auth/signin.js +17 -0
- package/dist/cjs/auth/signup.js +17 -0
- package/dist/cjs/auth/types.js +2 -0
- package/dist/{config → cjs/config}/axios.js +42 -30
- package/dist/cjs/config/constants.js +12 -0
- package/dist/{config → cjs/config}/fetch.js +5 -4
- package/dist/{config → cjs/config}/handler.js +3 -2
- package/dist/cjs/config/index.js +37 -0
- package/dist/{config → cjs/config}/local-storage.js +1 -2
- package/dist/cjs/config/route.js +47 -0
- package/dist/{config → cjs/config}/validate.js +6 -5
- package/dist/{content → cjs/content}/endpoint.js +3 -2
- package/dist/cjs/content/index.js +29 -0
- package/dist/cjs/hooks/index.js +10 -0
- package/dist/{hooks → cjs/hooks}/useAuth.js +2 -3
- package/dist/{hooks → cjs/hooks}/usePrivate.js +2 -3
- package/dist/cjs/hooks/useUser.js +15 -0
- package/dist/cjs/index.js +59 -0
- package/dist/{inventory → cjs/inventory}/endpoint.js +3 -2
- package/dist/cjs/inventory/index.js +26 -0
- package/dist/cjs/linkie/endpoint.js +8 -0
- package/dist/cjs/linkie/index.js +20 -0
- package/dist/cjs/linkie/shortUrl.js +17 -0
- package/dist/cjs/linkie/types.js +2 -0
- package/dist/cjs/order/create.js +17 -0
- package/dist/cjs/order/endpoint.js +8 -0
- package/dist/cjs/order/index.js +10 -0
- package/dist/cjs/order/types.js +2 -0
- package/dist/{payment → cjs/payment}/components/ProcessPayment.js +3 -4
- package/dist/cjs/payment/endpoint.js +9 -0
- package/dist/cjs/payment/index.js +12 -0
- package/dist/{payment → cjs/payment}/start.js +11 -7
- package/dist/cjs/payment/types.js +2 -0
- package/dist/cjs/payment/verify.js +17 -0
- package/dist/cjs/sales/endpoint.js +8 -0
- package/dist/cjs/sales/index.js +7 -0
- package/dist/cjs/sales/lead.js +22 -0
- package/dist/{sales → cjs/sales}/types.js +7 -5
- package/dist/{support → cjs/support}/endpoint.js +3 -2
- package/dist/cjs/support/index.js +15 -0
- package/dist/cjs/support/newsletter.js +17 -0
- package/dist/cjs/support/ticket.js +17 -0
- package/dist/cjs/tools/endpoint.js +8 -0
- package/dist/{tools → cjs/tools}/exchangeRates.js +10 -6
- package/dist/cjs/tools/index.js +10 -0
- package/dist/cjs/types.js +9 -0
- package/dist/cjs/utils/auth.js +23 -0
- package/dist/{utils → cjs/utils}/handler.js +3 -2
- package/dist/{utils → cjs/utils}/localStorage.js +28 -18
- package/dist/{utils → cjs/utils}/share.js +4 -2
- package/dist/index.js +985 -23
- package/dist/index.js.map +1 -1
- package/dist/{account → types/account}/index.d.ts +3 -2
- package/dist/{account → types/account}/update_profile.d.ts +2 -2
- package/dist/{accounting → types/accounting}/createInvoice.d.ts +2 -2
- package/dist/{accounting → types/accounting}/index.d.ts +7 -6
- package/dist/{accounting → types/accounting}/submitInvoice.d.ts +2 -2
- package/dist/{accounting → types/accounting}/types.d.ts +1 -1
- package/dist/{accounting → types/accounting}/updateInvoice.d.ts +2 -2
- package/dist/types/apis/cities.d.ts +11 -0
- package/dist/types/apis/countries.d.ts +11 -0
- package/dist/types/apis/index.d.ts +30 -0
- package/dist/types/apis/states.d.ts +11 -0
- package/dist/{auth → types/auth}/authWithKey.d.ts +1 -1
- package/dist/{auth → types/auth}/authWithToken.d.ts +1 -1
- package/dist/{auth → types/auth}/index.d.ts +13 -13
- package/dist/{auth → types/auth}/register.d.ts +1 -1
- package/dist/{auth → types/auth}/resetPassword.d.ts +1 -1
- package/dist/{auth → types/auth}/signin.d.ts +1 -1
- package/dist/{auth → types/auth}/signup.d.ts +1 -1
- package/dist/{config → types/config}/axios.d.ts +1 -1
- package/dist/{config → types/config}/index.d.ts +2 -1
- package/dist/types/config/local-storage.d.ts +0 -0
- package/dist/{config → types/config}/validate.d.ts +1 -1
- package/dist/types/hooks/index.d.ts +1 -0
- package/dist/types/hooks/useAuth.d.ts +0 -0
- package/dist/types/hooks/usePrivate.d.ts +0 -0
- package/dist/types/index.d.ts +20 -0
- package/dist/{linkie → types/linkie}/index.d.ts +2 -2
- package/dist/{linkie → types/linkie}/shortUrl.d.ts +2 -2
- package/dist/{order → types/order}/create.d.ts +1 -1
- package/dist/{order → types/order}/index.d.ts +1 -1
- package/dist/types/payment/components/ProcessPayment.d.ts +0 -0
- package/dist/{payment → types/payment}/index.d.ts +3 -3
- package/dist/{payment → types/payment}/verify.d.ts +2 -2
- package/dist/types/sales/index.d.ts +7 -0
- package/dist/{sales → types/sales}/lead.d.ts +1 -1
- package/dist/{sales → types/sales}/types.d.ts +1 -1
- package/dist/{support → types/support}/endpoint.d.ts +1 -0
- package/dist/types/support/feedback.d.ts +10 -0
- package/dist/{support → types/support}/index.d.ts +9 -0
- package/package.json +11 -4
- package/dist/account/endpoint.js +0 -7
- package/dist/account/endpoint.js.map +0 -1
- package/dist/account/index.js +0 -13
- package/dist/account/index.js.map +0 -1
- package/dist/account/types.js +0 -2
- package/dist/account/types.js.map +0 -1
- package/dist/account/update_profile.js +0 -13
- package/dist/account/update_profile.js.map +0 -1
- package/dist/account/updateprofile.d.ts +0 -12
- package/dist/account/updateprofile.js +0 -13
- package/dist/account/updateprofile.js.map +0 -1
- package/dist/accounting/createInvoice.js +0 -13
- package/dist/accounting/createInvoice.js.map +0 -1
- package/dist/accounting/endpoint.js.map +0 -1
- package/dist/accounting/index.js +0 -21
- package/dist/accounting/index.js.map +0 -1
- package/dist/accounting/invoice.d.ts +0 -12
- package/dist/accounting/invoice.js +0 -13
- package/dist/accounting/invoice.js.map +0 -1
- package/dist/accounting/submitInvoice.js +0 -13
- package/dist/accounting/submitInvoice.js.map +0 -1
- package/dist/accounting/types.js +0 -2
- package/dist/accounting/types.js.map +0 -1
- package/dist/accounting/updateInvoice.js +0 -13
- package/dist/accounting/updateInvoice.js.map +0 -1
- package/dist/assets/endpoint.js.map +0 -1
- package/dist/assets/index.js +0 -10
- package/dist/assets/index.js.map +0 -1
- package/dist/auth/authWithKey.js +0 -13
- package/dist/auth/authWithKey.js.map +0 -1
- package/dist/auth/authWithToken.js +0 -13
- package/dist/auth/authWithToken.js.map +0 -1
- package/dist/auth/endpoint.js.map +0 -1
- package/dist/auth/index.js +0 -90
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/logout.js +0 -18
- package/dist/auth/logout.js.map +0 -1
- package/dist/auth/register.js +0 -13
- package/dist/auth/register.js.map +0 -1
- package/dist/auth/resetPassword.js +0 -13
- package/dist/auth/resetPassword.js.map +0 -1
- package/dist/auth/signInWithGoogle.js.map +0 -1
- package/dist/auth/signin.js +0 -13
- package/dist/auth/signin.js.map +0 -1
- package/dist/auth/signup.js +0 -13
- package/dist/auth/signup.js.map +0 -1
- package/dist/auth/types.js +0 -2
- package/dist/auth/types.js.map +0 -1
- package/dist/auth/verifyToken.d.ts +0 -11
- package/dist/auth/verifyToken.js +0 -13
- package/dist/auth/verifyToken.js.map +0 -1
- package/dist/config/Env.d.ts +0 -40
- package/dist/config/Env.js +0 -52
- package/dist/config/Env.js.map +0 -1
- package/dist/config/axios.js.map +0 -1
- package/dist/config/constants.js +0 -10
- package/dist/config/constants.js.map +0 -1
- package/dist/config/fetch.js.map +0 -1
- package/dist/config/firebase.d.ts +0 -3
- package/dist/config/firebase.js +0 -16
- package/dist/config/firebase.js.map +0 -1
- package/dist/config/handler.js.map +0 -1
- package/dist/config/index.js +0 -26
- package/dist/config/index.js.map +0 -1
- package/dist/config/local-storage.d.ts +0 -1
- package/dist/config/local-storage.js.map +0 -1
- package/dist/config/route.js +0 -42
- package/dist/config/route.js.map +0 -1
- package/dist/config/validate.js.map +0 -1
- package/dist/content/endpoint.js.map +0 -1
- package/dist/content/index.js +0 -28
- package/dist/content/index.js.map +0 -1
- package/dist/hooks/index.d.ts +0 -1
- package/dist/hooks/index.js +0 -4
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/useAuth.d.ts +0 -1
- package/dist/hooks/useAuth.js.map +0 -1
- package/dist/hooks/usePrivate.d.ts +0 -1
- package/dist/hooks/usePrivate.js.map +0 -1
- package/dist/hooks/useUser.js +0 -11
- package/dist/hooks/useUser.js.map +0 -1
- package/dist/index.d.ts +0 -17
- package/dist/inventory/endpoint.js.map +0 -1
- package/dist/inventory/index.js +0 -25
- package/dist/inventory/index.js.map +0 -1
- package/dist/linkie/endpoint.js +0 -7
- package/dist/linkie/endpoint.js.map +0 -1
- package/dist/linkie/index.js +0 -16
- package/dist/linkie/index.js.map +0 -1
- package/dist/linkie/shortUrl.js +0 -13
- package/dist/linkie/shortUrl.js.map +0 -1
- package/dist/linkie/types.js +0 -2
- package/dist/linkie/types.js.map +0 -1
- package/dist/order/create.js +0 -13
- package/dist/order/create.js.map +0 -1
- package/dist/order/endpoint.js +0 -7
- package/dist/order/endpoint.js.map +0 -1
- package/dist/order/index.js +0 -6
- package/dist/order/index.js.map +0 -1
- package/dist/order/types.js +0 -2
- package/dist/order/types.js.map +0 -1
- package/dist/package.json +0 -48
- package/dist/payment/components/ProcessPayment.d.ts +0 -1
- package/dist/payment/components/ProcessPayment.js.map +0 -1
- package/dist/payment/endpoint.js +0 -8
- package/dist/payment/endpoint.js.map +0 -1
- package/dist/payment/index.js +0 -8
- package/dist/payment/index.js.map +0 -1
- package/dist/payment/start.js.map +0 -1
- package/dist/payment/types.js +0 -2
- package/dist/payment/types.js.map +0 -1
- package/dist/payment/verify.js +0 -13
- package/dist/payment/verify.js.map +0 -1
- package/dist/sales/endpoint.js +0 -7
- package/dist/sales/endpoint.js.map +0 -1
- package/dist/sales/index.d.ts +0 -5
- package/dist/sales/index.js +0 -6
- package/dist/sales/index.js.map +0 -1
- package/dist/sales/lead.js +0 -17
- package/dist/sales/lead.js.map +0 -1
- package/dist/sales/types.js.map +0 -1
- package/dist/support/endpoint.js.map +0 -1
- package/dist/support/index.js +0 -11
- package/dist/support/index.js.map +0 -1
- package/dist/support/newsletter.js +0 -13
- package/dist/support/newsletter.js.map +0 -1
- package/dist/support/ticket.js +0 -13
- package/dist/support/ticket.js.map +0 -1
- package/dist/tools/endpoint.js +0 -7
- package/dist/tools/endpoint.js.map +0 -1
- package/dist/tools/exchangeRates.js.map +0 -1
- package/dist/tools/index.js +0 -6
- package/dist/tools/index.js.map +0 -1
- package/dist/types.js +0 -7
- package/dist/types.js.map +0 -1
- package/dist/utils/auth.js +0 -16
- package/dist/utils/auth.js.map +0 -1
- package/dist/utils/exchangeRates.d.ts +0 -1
- package/dist/utils/exchangeRates.js +0 -27
- package/dist/utils/exchangeRates.js.map +0 -1
- package/dist/utils/handler.js.map +0 -1
- package/dist/utils/localStorage.js.map +0 -1
- package/dist/utils/share.js.map +0 -1
- /package/dist/{account → types/account}/endpoint.d.ts +0 -0
- /package/dist/{account → types/account}/types.d.ts +0 -0
- /package/dist/{accounting → types/accounting}/endpoint.d.ts +0 -0
- /package/dist/{assets → types/assets}/endpoint.d.ts +0 -0
- /package/dist/{assets → types/assets}/index.d.ts +0 -0
- /package/dist/{auth → types/auth}/endpoint.d.ts +0 -0
- /package/dist/{auth → types/auth}/logout.d.ts +0 -0
- /package/dist/{auth → types/auth}/signInWithGoogle.d.ts +0 -0
- /package/dist/{auth → types/auth}/types.d.ts +0 -0
- /package/dist/{config → types/config}/constants.d.ts +0 -0
- /package/dist/{config → types/config}/fetch.d.ts +0 -0
- /package/dist/{config → types/config}/handler.d.ts +0 -0
- /package/dist/{config → types/config}/route.d.ts +0 -0
- /package/dist/{content → types/content}/endpoint.d.ts +0 -0
- /package/dist/{content → types/content}/index.d.ts +0 -0
- /package/dist/{hooks → types/hooks}/useUser.d.ts +0 -0
- /package/dist/{inventory → types/inventory}/endpoint.d.ts +0 -0
- /package/dist/{inventory → types/inventory}/index.d.ts +0 -0
- /package/dist/{linkie → types/linkie}/endpoint.d.ts +0 -0
- /package/dist/{linkie → types/linkie}/types.d.ts +0 -0
- /package/dist/{order → types/order}/endpoint.d.ts +0 -0
- /package/dist/{order → types/order}/types.d.ts +0 -0
- /package/dist/{payment → types/payment}/endpoint.d.ts +0 -0
- /package/dist/{payment → types/payment}/start.d.ts +0 -0
- /package/dist/{payment → types/payment}/types.d.ts +0 -0
- /package/dist/{sales → types/sales}/endpoint.d.ts +0 -0
- /package/dist/{support → types/support}/newsletter.d.ts +0 -0
- /package/dist/{support → types/support}/ticket.d.ts +0 -0
- /package/dist/{tools → types/tools}/endpoint.d.ts +0 -0
- /package/dist/{tools → types/tools}/exchangeRates.d.ts +0 -0
- /package/dist/{tools → types/tools}/index.d.ts +0 -0
- /package/dist/{types.d.ts → types/types.d.ts} +0 -0
- /package/dist/{utils → types/utils}/auth.d.ts +0 -0
- /package/dist/{utils → types/utils}/handler.d.ts +0 -0
- /package/dist/{utils → types/utils}/localStorage.d.ts +0 -0
- /package/dist/{utils → types/utils}/share.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1,23 +1,985 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import Cookies from 'js-cookie';
|
|
2
|
+
import { jwtDecode } from 'jwt-decode';
|
|
3
|
+
import firebase from 'firebase/compat/app';
|
|
4
|
+
import CryptoJS from 'crypto-js';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import { GoogleAuthProvider, getAuth, signInWithPopup } from 'firebase/auth';
|
|
7
|
+
|
|
8
|
+
// validateConfig
|
|
9
|
+
function validateConfig(requiredKeys) {
|
|
10
|
+
const config = getConfig();
|
|
11
|
+
const missingKeys = requiredKeys.filter((key) => {
|
|
12
|
+
return !config[key]; // null, undefined, empty string → treated as missing
|
|
13
|
+
});
|
|
14
|
+
if (missingKeys.length > 0) {
|
|
15
|
+
throw new Error(`SDK configuration error: Missing required config value(s): ${missingKeys.join(', ')}\n` +
|
|
16
|
+
'👉 Please ensure these keys are set using setConfig({ ... }) before making requests.');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let config = {
|
|
21
|
+
tokenKey: '',
|
|
22
|
+
apiKey: null,
|
|
23
|
+
apiUrl: 'https://api.helpdice.com',
|
|
24
|
+
appUrl: '',
|
|
25
|
+
apisUrl: 'https://apis.helpdice.com',
|
|
26
|
+
environment: 'production',
|
|
27
|
+
firebase: undefined
|
|
28
|
+
};
|
|
29
|
+
function initializeSDK(newConfig) {
|
|
30
|
+
config = { ...config, ...newConfig };
|
|
31
|
+
validateConfig(['apiKey', 'tokenKey']);
|
|
32
|
+
}
|
|
33
|
+
function getFirebase() {
|
|
34
|
+
validateConfig(['firebase']);
|
|
35
|
+
const appConfig = getConfig();
|
|
36
|
+
if (appConfig.firebase) {
|
|
37
|
+
// if (firebase?.default != undefined) {
|
|
38
|
+
// return firebase.default.initializeApp(appConfig.firebase)
|
|
39
|
+
// }
|
|
40
|
+
return firebase.initializeApp(appConfig.firebase);
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`SDK configuration error: Firebase is not configured properly`);
|
|
43
|
+
}
|
|
44
|
+
function getConfig() {
|
|
45
|
+
return config;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const getCookie = (key = getConfig().tokenKey) => {
|
|
49
|
+
// console.log(Cookies.get(key));
|
|
50
|
+
if (key)
|
|
51
|
+
return Cookies.get(key);
|
|
52
|
+
return "";
|
|
53
|
+
};
|
|
54
|
+
const isAuth = () => !!getCookie();
|
|
55
|
+
// export const getServerSideToken = (ctx: NextPageContext) => {
|
|
56
|
+
// const { token } = parseCookies(ctx);
|
|
57
|
+
// return token;
|
|
58
|
+
// };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Function to set encryption key
|
|
62
|
+
* @param {string} key - encryption key name
|
|
63
|
+
* @param {string} value string value to be set as encryption key
|
|
64
|
+
*/
|
|
65
|
+
function setEncryptionKey(key, value) {
|
|
66
|
+
if (value) {
|
|
67
|
+
const sha256Key = CryptoJS.SHA256(key).toString();
|
|
68
|
+
// console.log('setting key: ', key, value);
|
|
69
|
+
const sha256Data = CryptoJS.SHA256(value).toString();
|
|
70
|
+
localStorage.setItem(sha256Key, JSON.stringify(sha256Data));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
console.error("No data provided");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* function to get the encryption key
|
|
78
|
+
* @param {string} - name of the key to get encryption key for
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
// eslint-disable-next-line consistent-return
|
|
82
|
+
function getEncryptionKey(key) {
|
|
83
|
+
try {
|
|
84
|
+
const shaKey = CryptoJS.SHA256(key).toString();
|
|
85
|
+
// console.log('getting key: ', key, localStorage.getItem(shaKey));
|
|
86
|
+
return localStorage.getItem(shaKey);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
console.error("something went wrong man hoho");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Funtion to store data in local storage
|
|
94
|
+
* Pass second argument as true if setting data for employee side
|
|
95
|
+
* @param {string} key key for the value to be stored
|
|
96
|
+
* @param {*} data actual data to be stored with the provided key
|
|
97
|
+
* @param {boolean} [useEmployeeKey] - flag to decide which encyption key to use, (employee's or admin's)
|
|
98
|
+
*/
|
|
99
|
+
function setDataInStorage(key, data) {
|
|
100
|
+
// Encrypt key
|
|
101
|
+
const shaKey = CryptoJS.SHA256(key).toString();
|
|
102
|
+
const encKeyName = "account_key";
|
|
103
|
+
const encryptionKey = getEncryptionKey(encKeyName);
|
|
104
|
+
// console.log('setting data: ', encryption_key, key, shaKey, data);
|
|
105
|
+
if (encryptionKey) {
|
|
106
|
+
// Encrypt data
|
|
107
|
+
const dataCipher = CryptoJS.AES.encrypt(JSON.stringify(data), encryptionKey).toString();
|
|
108
|
+
localStorage.setItem(shaKey, JSON.stringify(dataCipher));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Function to get data from local storage
|
|
113
|
+
* Pass second argument as true if getting data for employee side
|
|
114
|
+
* @param {string} key - key of the item to fetch from local storage
|
|
115
|
+
* @param {boolean} [useEmployeeKey] - flag to decide which encyption key to use, (employee's or admin's)
|
|
116
|
+
* @returns {*} value associated with the key
|
|
117
|
+
*/
|
|
118
|
+
// eslint-disable-next-line consistent-return
|
|
119
|
+
function getDataFromStorage(key) {
|
|
120
|
+
try {
|
|
121
|
+
const shaKey = CryptoJS.SHA256(key).toString();
|
|
122
|
+
const encKeyName = "account_key";
|
|
123
|
+
const encryptionKey = getEncryptionKey(encKeyName);
|
|
124
|
+
// console.log(
|
|
125
|
+
// 'from getting data: ',
|
|
126
|
+
// key,
|
|
127
|
+
// 'encryption-key',
|
|
128
|
+
// typeof encryption_key,
|
|
129
|
+
// encryption_key
|
|
130
|
+
// );
|
|
131
|
+
if (encryptionKey && shaKey) {
|
|
132
|
+
const ciphertext = JSON.parse(String(localStorage.getItem(shaKey)));
|
|
133
|
+
if (ciphertext) {
|
|
134
|
+
// Decrypt
|
|
135
|
+
const bytes = CryptoJS.AES.decrypt(ciphertext, encryptionKey);
|
|
136
|
+
const originalData = bytes.toString(CryptoJS.enc.Utf8);
|
|
137
|
+
// console.log('getting data: ', key, shaKey, originalData);
|
|
138
|
+
return JSON.parse(originalData);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
// console.error('Error getting data from storage boi boi');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Function to clear items from local storage
|
|
148
|
+
* @param {(string|string[])} keys - key to remove or array of keys
|
|
149
|
+
*/
|
|
150
|
+
// export function clearStorage(keys: string[]) {
|
|
151
|
+
// try {
|
|
152
|
+
// if (Array.isArray(keys)) {
|
|
153
|
+
// keys.forEach((key) => {
|
|
154
|
+
// const shaKey = CryptoJS.SHA256(key).toString();
|
|
155
|
+
// localStorage.removeItem(shaKey);
|
|
156
|
+
// });
|
|
157
|
+
// } else if (typeof keys === "string") {
|
|
158
|
+
// const shaKey = CryptoJS.SHA256(keys).toString();
|
|
159
|
+
// localStorage.removeItem(shaKey);
|
|
160
|
+
// }
|
|
161
|
+
// } catch (err) {
|
|
162
|
+
// console.error("Error clearing storage", err);
|
|
163
|
+
// }
|
|
164
|
+
// }
|
|
165
|
+
function clearStorage(keys) {
|
|
166
|
+
try {
|
|
167
|
+
if (Array.isArray(keys)) {
|
|
168
|
+
for (const key of keys) {
|
|
169
|
+
const shaKey = CryptoJS.SHA256(key).toString();
|
|
170
|
+
localStorage.removeItem(shaKey);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else if (typeof keys === "string") {
|
|
174
|
+
const shaKey = CryptoJS.SHA256(keys).toString();
|
|
175
|
+
localStorage.removeItem(shaKey);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
console.error("Error clearing storage", err);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const TOKEN_KEY = "access-token";
|
|
184
|
+
/* eslint-enable */
|
|
185
|
+
|
|
186
|
+
const http = axios.create({
|
|
187
|
+
withCredentials: true,
|
|
188
|
+
headers: {
|
|
189
|
+
"Access-Control-Allow-Origin": "*",
|
|
190
|
+
"Content-Type": "application/json",
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
http.interceptors.request.use((config) => {
|
|
194
|
+
validateConfig(['apiKey', 'apiUrl']);
|
|
195
|
+
const { apiUrl } = getConfig();
|
|
196
|
+
config.baseURL = apiUrl;
|
|
197
|
+
if (config.headers) {
|
|
198
|
+
config.headers["Content-Type"] = "application/json";
|
|
199
|
+
config.headers.Authorization = `Bearer ${getCookie()}` || "";
|
|
200
|
+
config.headers["HB-API-KEY"] =
|
|
201
|
+
`Bearer ${getConfig().apiKey}` || "";
|
|
202
|
+
}
|
|
203
|
+
return config;
|
|
204
|
+
}, Promise.reject);
|
|
205
|
+
const post = (endpoint, data, config) => new Promise((resolve, reject) => http
|
|
206
|
+
.post(endpoint, data, config)
|
|
207
|
+
.then(resolve)
|
|
208
|
+
.catch((e) => reject(e.response?.data)));
|
|
209
|
+
const put = (endpoint, data, config) => new Promise((resolve, reject) => http
|
|
210
|
+
.put(endpoint, data, config)
|
|
211
|
+
.then(resolve)
|
|
212
|
+
.catch((e) => reject(e.response?.data)));
|
|
213
|
+
const get = (endpoint, config) => {
|
|
214
|
+
// console.log('Get Request Config :', config);
|
|
215
|
+
return new Promise((resolve, reject) => http
|
|
216
|
+
.get(endpoint, config)
|
|
217
|
+
.then(resolve)
|
|
218
|
+
.catch((e) => reject(e.response?.data)));
|
|
219
|
+
};
|
|
220
|
+
/* ======================================================================================================= */
|
|
221
|
+
/* ------------------------------------ For Linkie App --------------------------------------------------- */
|
|
222
|
+
/* ======================================================================================================= */
|
|
223
|
+
const chttp = axios.create({
|
|
224
|
+
withCredentials: true,
|
|
225
|
+
headers: {
|
|
226
|
+
"Access-Control-Allow-Origin": "*",
|
|
227
|
+
"Content-Type": "application/json",
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
chttp.interceptors.request.use((config) => {
|
|
231
|
+
validateConfig(['apiKey', 'tokenKey', 'linkieUrl']);
|
|
232
|
+
const { linkieUrl } = getConfig();
|
|
233
|
+
config.baseURL = linkieUrl;
|
|
234
|
+
if (config.headers) {
|
|
235
|
+
config.headers["Content-Type"] = "application/json";
|
|
236
|
+
config.headers.Authorization = localStorage.getItem(TOKEN_KEY) || "";
|
|
237
|
+
config.headers["HB-API-KEY"] =
|
|
238
|
+
`Bearer ${getConfig().apiKey}` || "";
|
|
239
|
+
}
|
|
240
|
+
return config;
|
|
241
|
+
}, Promise.reject);
|
|
242
|
+
// chttp.interceptors.response.use(
|
|
243
|
+
// (response) => response,
|
|
244
|
+
// async (error) => {
|
|
245
|
+
// if (error.code === "ERR_NETWORK") {
|
|
246
|
+
// return Promise.reject(error);
|
|
247
|
+
// }
|
|
248
|
+
// const refreshToken = localStorage.getItem(REFRESH_TOKEN_KEY);
|
|
249
|
+
// if (!refreshToken) {
|
|
250
|
+
// return Promise.reject(error);
|
|
251
|
+
// }
|
|
252
|
+
// // const originalRequest = error.config;
|
|
253
|
+
// // if (error.response.status === 401 && !originalRequest._retry) {
|
|
254
|
+
// // originalRequest._retry = true;
|
|
255
|
+
// // if (http.defaults.headers) {
|
|
256
|
+
// // http.defaults.headers.Authorization = `Bearer ${refreshToken}`;
|
|
257
|
+
// // }
|
|
258
|
+
// // try {
|
|
259
|
+
// // const res = await post('auth/token');
|
|
260
|
+
// // const { access_token, threshold } = res.data;
|
|
261
|
+
// // // handleItem(TOKEN_KEY, access_token);
|
|
262
|
+
// // // handleItem(TOKEN_THRESHOLD_KEY, setThreshold(threshold));
|
|
263
|
+
// // if (http.defaults.headers) {
|
|
264
|
+
// // http.defaults.headers.Authorization = access_token;
|
|
265
|
+
// // }
|
|
266
|
+
// // return await http(originalRequest);
|
|
267
|
+
// // } catch (reason) {
|
|
268
|
+
// // return Promise.reject(reason);
|
|
269
|
+
// // }
|
|
270
|
+
// // }
|
|
271
|
+
// return Promise.reject(error);
|
|
272
|
+
// },
|
|
273
|
+
// );
|
|
274
|
+
const linkiePost = (endpoint, data, config) => new Promise((resolve, reject) => chttp
|
|
275
|
+
.post(endpoint, data, config)
|
|
276
|
+
.then(resolve)
|
|
277
|
+
.catch((e) => reject(e.response?.data)));
|
|
278
|
+
|
|
279
|
+
// The fetchDataWithCallbacks function that accepts a Promise and a callbacks object
|
|
280
|
+
async function handler$1(promise, callbacks) {
|
|
281
|
+
// Destructure callbacks from the object
|
|
282
|
+
const { onFetching, onSuccess, onError, onSettled } = callbacks ?? {};
|
|
283
|
+
// Call the onFetching callback before making the request
|
|
284
|
+
if (onFetching)
|
|
285
|
+
onFetching();
|
|
286
|
+
// Handle the Promise passed as an argument
|
|
287
|
+
promise
|
|
288
|
+
.then((response) => {
|
|
289
|
+
// Call the onSuccess callback if the Promise resolves successfully
|
|
290
|
+
if (onSuccess)
|
|
291
|
+
onSuccess({
|
|
292
|
+
data: response.data,
|
|
293
|
+
status: response.status,
|
|
294
|
+
statusText: response.statusText,
|
|
295
|
+
});
|
|
296
|
+
})
|
|
297
|
+
.catch((error) => {
|
|
298
|
+
// Call the onError callback if the Promise is rejected
|
|
299
|
+
if (onError)
|
|
300
|
+
onError(error);
|
|
301
|
+
})
|
|
302
|
+
.finally(() => {
|
|
303
|
+
// Call the onSettled callback after the Promise is settled
|
|
304
|
+
if (onSettled)
|
|
305
|
+
onSettled();
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const account = {
|
|
310
|
+
v1: {
|
|
311
|
+
profile: "profile",
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const accounting = {
|
|
316
|
+
v1: {
|
|
317
|
+
item: "item",
|
|
318
|
+
items: "items",
|
|
319
|
+
invoice: "invoice",
|
|
320
|
+
submit_invoice: "invoice/submit",
|
|
321
|
+
invoices: "invoices",
|
|
322
|
+
item_categories: "item/category",
|
|
323
|
+
item_brands: "item/brand",
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const assets = {
|
|
328
|
+
v1: {
|
|
329
|
+
imagePlaceholder: "images/image-placeholder.webp",
|
|
330
|
+
avatar: "images/avatar.webp",
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const auth = {
|
|
335
|
+
v1: {
|
|
336
|
+
signin: "signin",
|
|
337
|
+
register: "signup",
|
|
338
|
+
signup: "register",
|
|
339
|
+
logout: "logout",
|
|
340
|
+
auth_with_token: "authenticate",
|
|
341
|
+
auth_with_key: "verify",
|
|
342
|
+
reset_password: "recovery-password",
|
|
343
|
+
change_password: "change-password"
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const cms = {
|
|
348
|
+
v1: {
|
|
349
|
+
article: "article",
|
|
350
|
+
articles: "articles",
|
|
351
|
+
articleCategories: "article/categories",
|
|
352
|
+
mcq: "mcq",
|
|
353
|
+
mcqs: "mcqs",
|
|
354
|
+
all_mcqs: "mcqs/all",
|
|
355
|
+
mcqCategories: "mcq/categories",
|
|
356
|
+
qna: "qna",
|
|
357
|
+
qnas: "qnas",
|
|
358
|
+
qnaCategories: "qna/categories",
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const inventory = {
|
|
363
|
+
v1: {
|
|
364
|
+
store: "store",
|
|
365
|
+
store_products: "store/products",
|
|
366
|
+
},
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const linkie = {
|
|
370
|
+
v1: {
|
|
371
|
+
shorten: "shorten",
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const payment = {
|
|
376
|
+
v1: {
|
|
377
|
+
create: "",
|
|
378
|
+
verify: "verify",
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const sales = {
|
|
383
|
+
v1: {
|
|
384
|
+
addLead: "lead"
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const support = {
|
|
389
|
+
v1: {
|
|
390
|
+
ticket: "ticket",
|
|
391
|
+
tickets: "tickets",
|
|
392
|
+
newsletter: "newsletter",
|
|
393
|
+
feedback: "feedback"
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const tools = {
|
|
398
|
+
v1: {
|
|
399
|
+
exchangeRates: "exchange-rates",
|
|
400
|
+
},
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const urls = {
|
|
404
|
+
auth,
|
|
405
|
+
cms,
|
|
406
|
+
account,
|
|
407
|
+
accounting,
|
|
408
|
+
support,
|
|
409
|
+
assets,
|
|
410
|
+
payment,
|
|
411
|
+
inventory,
|
|
412
|
+
tools,
|
|
413
|
+
sales,
|
|
414
|
+
linkie
|
|
415
|
+
};
|
|
416
|
+
// Function to get the URL dynamically based on category, version, and action
|
|
417
|
+
function getUrl(category, version, action, omitCategory = false) {
|
|
418
|
+
const categoryData = urls[category]; // Access the correct category dynamically
|
|
419
|
+
if (categoryData) {
|
|
420
|
+
const versionData = categoryData[version]; // Access the correct version dynamically
|
|
421
|
+
// console.log(versionData?.[action]);
|
|
422
|
+
if (versionData !== undefined) {
|
|
423
|
+
console.log(versionData[action]);
|
|
424
|
+
if (omitCategory) {
|
|
425
|
+
return `/api/${version}/${versionData[action]}`;
|
|
426
|
+
}
|
|
427
|
+
return `/api/${version}/${category}/${versionData[action]}`; // Construct the URL
|
|
428
|
+
}
|
|
429
|
+
throw new Error(`Invalid action: ${action} for version: ${version} in category: ${category}`);
|
|
430
|
+
}
|
|
431
|
+
throw new Error(`Invalid category: ${category}`);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const logout = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
435
|
+
return handler$1(post(getUrl("auth", "v1", "logout"), { ...data, fcm_token: getDataFromStorage("fcm_token") }, config), {
|
|
436
|
+
onFetching: () => onFetching?.(),
|
|
437
|
+
onError: (error) => onError?.(error),
|
|
438
|
+
onSuccess: (data) => {
|
|
439
|
+
clearStorage(["accountAccessToken", "accountInfo", "account_key"]);
|
|
440
|
+
onSuccess?.(data);
|
|
441
|
+
window.location.href = "/";
|
|
442
|
+
},
|
|
443
|
+
onSettled: () => onSettled?.(),
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const register = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
448
|
+
return handler$1(post(getUrl("auth", "v1", "register"), data, config), {
|
|
449
|
+
onFetching: () => onFetching?.(),
|
|
450
|
+
onError: (error) => onError?.(error),
|
|
451
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
452
|
+
onSettled: () => onSettled?.(),
|
|
453
|
+
});
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const signup = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
457
|
+
return handler$1(post(getUrl("auth", "v1", "signup"), data, config), {
|
|
458
|
+
onFetching: () => onFetching?.(),
|
|
459
|
+
onError: (error) => onError?.(error),
|
|
460
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
461
|
+
onSettled: () => onSettled?.(),
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// import signin from "./signin";
|
|
466
|
+
// Function to handle Google Sign-In
|
|
467
|
+
async function signInWithGoogle({ onSuccess, onError, onFetching, onSettled, }) {
|
|
468
|
+
try {
|
|
469
|
+
const provider = new GoogleAuthProvider();
|
|
470
|
+
const auth = getAuth(getFirebase());
|
|
471
|
+
signInWithPopup(auth, provider)
|
|
472
|
+
.then((result) => {
|
|
473
|
+
// This gives you a Google Access Token. You can use it to access the Google API.
|
|
474
|
+
const credential = GoogleAuthProvider.credentialFromResult(result);
|
|
475
|
+
// const token = credential?.accessToken;
|
|
476
|
+
// The signed-in user info.
|
|
477
|
+
const user = result.user;
|
|
478
|
+
// console.log(user);
|
|
479
|
+
return signup({
|
|
480
|
+
provider: "Google",
|
|
481
|
+
uid: user.uid,
|
|
482
|
+
name: String(user.displayName),
|
|
483
|
+
email: String(user.email),
|
|
484
|
+
username: String(user.email),
|
|
485
|
+
password: user.uid.substring(0, user.uid.length / 2),
|
|
486
|
+
// token: token,
|
|
487
|
+
today: new Date(),
|
|
488
|
+
}, {
|
|
489
|
+
onFetching: () => onFetching?.(),
|
|
490
|
+
onError: (error) => onError?.(error),
|
|
491
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
492
|
+
onSettled: () => onSettled?.(),
|
|
493
|
+
});
|
|
494
|
+
})
|
|
495
|
+
.catch((error) => {
|
|
496
|
+
onError?.(error);
|
|
497
|
+
return Promise.reject("Something Went Wrong");
|
|
498
|
+
// Handle Errors here.
|
|
499
|
+
// const errorCode = error.code;
|
|
500
|
+
// const errorMessage = error.message;
|
|
501
|
+
// The email of the user's account used.
|
|
502
|
+
// const email = error.customData.email;
|
|
503
|
+
// The AuthCredential type that was used.
|
|
504
|
+
// const credential = GoogleAuthProvider.credentialFromError(error);
|
|
505
|
+
// ...
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
catch (err) {
|
|
509
|
+
onError?.(err);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const signin = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
514
|
+
return handler$1(post(getUrl("auth", "v1", "signin"), data, config), {
|
|
515
|
+
onFetching: () => onFetching?.(),
|
|
516
|
+
onError: (error) => onError?.(error),
|
|
517
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
518
|
+
onSettled: () => onSettled?.(),
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const authWithToken = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
523
|
+
return handler$1(post(getUrl("auth", "v1", "auth_with_token"), data, config), {
|
|
524
|
+
onFetching: () => onFetching?.(),
|
|
525
|
+
onError: (error) => onError?.(error),
|
|
526
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
527
|
+
onSettled: () => onSettled?.(),
|
|
528
|
+
});
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const authWithKey = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
532
|
+
return handler$1(post(getUrl("auth", "v1", "auth_with_key"), data, config), {
|
|
533
|
+
onFetching: () => onFetching?.(),
|
|
534
|
+
onError: (error) => onError?.(error),
|
|
535
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
536
|
+
onSettled: () => onSettled?.(),
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
const resetPassword = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
541
|
+
return handler$1(post(getUrl("auth", "v1", "reset_password"), data, config), {
|
|
542
|
+
onFetching: () => onFetching?.(),
|
|
543
|
+
onError: (error) => onError?.(error),
|
|
544
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
545
|
+
onSettled: () => onSettled?.(),
|
|
546
|
+
});
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const Auth = {
|
|
550
|
+
signIn: signin,
|
|
551
|
+
signUp: signup,
|
|
552
|
+
register,
|
|
553
|
+
resetPassword,
|
|
554
|
+
signInWithGoogle,
|
|
555
|
+
saveTokenToCookie: (token) => {
|
|
556
|
+
validateConfig(['tokenKey']);
|
|
557
|
+
const { tokenKey } = getConfig();
|
|
558
|
+
if (tokenKey) {
|
|
559
|
+
Cookies.set(tokenKey, token, { expires: 900000 });
|
|
560
|
+
}
|
|
561
|
+
window.location.href = "/";
|
|
562
|
+
},
|
|
563
|
+
saveTokenToStorage: (token) => {
|
|
564
|
+
const accountInfo = jwtDecode(token);
|
|
565
|
+
if (accountInfo) {
|
|
566
|
+
setEncryptionKey("account_key", accountInfo._id + accountInfo.createdAt);
|
|
567
|
+
setDataInStorage("accountAccessToken", token);
|
|
568
|
+
setDataInStorage("accountInfo", accountInfo);
|
|
569
|
+
}
|
|
570
|
+
},
|
|
571
|
+
getTokenFromStorage: () => {
|
|
572
|
+
return getDataFromStorage("accountAccessToken");
|
|
573
|
+
},
|
|
574
|
+
getUserFromStrorage: () => {
|
|
575
|
+
return getDataFromStorage("accountInfo");
|
|
576
|
+
},
|
|
577
|
+
/**
|
|
578
|
+
* Action to update stored admin info in the redux
|
|
579
|
+
* @param {string | object} key key or an object with key: value of the fields to be updated
|
|
580
|
+
* @param {any} value value of the field to be updated (only if the key passed is of type string)
|
|
581
|
+
*/
|
|
582
|
+
updateUserInfo: (key, value) => () => {
|
|
583
|
+
console.log("typeof key: ", typeof key, key);
|
|
584
|
+
try {
|
|
585
|
+
// console.log(key);
|
|
586
|
+
const accountInfo = getDataFromStorage("accountInfo");
|
|
587
|
+
if (typeof key === "object") {
|
|
588
|
+
let updatedInfo = { ...accountInfo };
|
|
589
|
+
// for..of
|
|
590
|
+
for (const [ky, value] of Object.entries(key)) {
|
|
591
|
+
updatedInfo = { ...updatedInfo, [ky]: value };
|
|
592
|
+
}
|
|
593
|
+
// console.log(updatedInfo);
|
|
594
|
+
setDataInStorage("accountInfo", updatedInfo);
|
|
595
|
+
}
|
|
596
|
+
else if (typeof key === "string") {
|
|
597
|
+
if (value) {
|
|
598
|
+
setDataInStorage("accountInfo", { ...accountInfo, [key]: value });
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
console.error("Value is not provided for the given key to update admin info");
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
console.error("update logo action ", error);
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
isAuthenticated: (cookie) => {
|
|
610
|
+
if (cookie) {
|
|
611
|
+
return isAuth();
|
|
612
|
+
}
|
|
613
|
+
return getDataFromStorage("accountAccessToken") !== undefined;
|
|
614
|
+
},
|
|
615
|
+
// getServerToken: (ctx: NextPageContext) => getServerSideToken(ctx),
|
|
616
|
+
logout,
|
|
617
|
+
Server: {
|
|
618
|
+
authWithToken,
|
|
619
|
+
authWithKey
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
const create = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
624
|
+
return handler$1(post(getUrl("order", "v1", "create"), data, config), {
|
|
625
|
+
onFetching: () => onFetching?.(),
|
|
626
|
+
onError: (error) => onError?.(error),
|
|
627
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
628
|
+
onSettled: () => onSettled?.(),
|
|
629
|
+
});
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
const Order = {
|
|
633
|
+
create,
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
const handler = ({ promise, onLoading = () => { }, onSuccess = () => { }, onError = () => { }, onSettled = () => { }, onProgress = () => { }, }) => {
|
|
637
|
+
// Call onLoading at the start to show loading state
|
|
638
|
+
onLoading(true);
|
|
639
|
+
// Handling the passed promise
|
|
640
|
+
promise
|
|
641
|
+
.then((response) => {
|
|
642
|
+
// Show success state
|
|
643
|
+
console.log("Request Success:", response);
|
|
644
|
+
// Execute success callback if provided
|
|
645
|
+
if (onSuccess) {
|
|
646
|
+
onSuccess(response.data);
|
|
647
|
+
}
|
|
648
|
+
})
|
|
649
|
+
.catch((error) => {
|
|
650
|
+
// Show error state
|
|
651
|
+
console.error("Request Error:", error);
|
|
652
|
+
// Execute error callback if provided
|
|
653
|
+
if (onError) {
|
|
654
|
+
onError(error);
|
|
655
|
+
}
|
|
656
|
+
})
|
|
657
|
+
.finally(() => {
|
|
658
|
+
// Execute settled callback (on success or error)
|
|
659
|
+
if (onSettled) {
|
|
660
|
+
onSettled();
|
|
661
|
+
}
|
|
662
|
+
// Call onLoading to hide loading indicator after completion
|
|
663
|
+
onLoading(false);
|
|
664
|
+
// Hide loading indicator
|
|
665
|
+
console.log("Request Completed");
|
|
666
|
+
});
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
function start(data, { config = null, onLoading = () => { }, onSuccess = () => { }, onError = () => { }, onSettled = () => { }, } = {}) {
|
|
670
|
+
return handler({
|
|
671
|
+
promise: post(getUrl("payment", "v1", "create"), {
|
|
672
|
+
...data,
|
|
673
|
+
method: {
|
|
674
|
+
label: data.paymentService,
|
|
675
|
+
currency: data.currency,
|
|
676
|
+
},
|
|
677
|
+
}, config || undefined),
|
|
678
|
+
onLoading,
|
|
679
|
+
onError: (error) => onError(error),
|
|
680
|
+
onSuccess: (responseData) => {
|
|
681
|
+
if (responseData?.method === "Razor Pay") {
|
|
682
|
+
const options = responseData.options;
|
|
683
|
+
options.handler = async (response) => {
|
|
684
|
+
// console.log("Payment Success:", response);
|
|
685
|
+
onSuccess(response);
|
|
686
|
+
// Validation call (if needed)
|
|
687
|
+
};
|
|
688
|
+
const rzp1 = new window.Razorpay(options);
|
|
689
|
+
rzp1.on("payment.failed", (response) => {
|
|
690
|
+
onError(response);
|
|
691
|
+
});
|
|
692
|
+
rzp1.open();
|
|
693
|
+
}
|
|
694
|
+
},
|
|
695
|
+
onSettled,
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const verify = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
700
|
+
return handler$1(post(getUrl("payment", "v1", "verify"), data, config), {
|
|
701
|
+
onFetching: () => onFetching?.(),
|
|
702
|
+
onError: (error) => onError?.(error),
|
|
703
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
704
|
+
onSettled: () => onSettled?.(),
|
|
705
|
+
});
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
const Payment = {
|
|
709
|
+
start,
|
|
710
|
+
verify,
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
const Content = {
|
|
714
|
+
article: (slug) => get(getUrl("cms", "v1", "article"), {
|
|
715
|
+
params: {
|
|
716
|
+
slug,
|
|
717
|
+
},
|
|
718
|
+
}),
|
|
719
|
+
articles: (config) => get(getUrl("cms", "v1", "articles"), config),
|
|
720
|
+
articleCategories: (config) => get(getUrl("cms", "v1", "articleCategories"), config),
|
|
721
|
+
mcq: (slug) => get(getUrl("cms", "v1", "mcq"), {
|
|
722
|
+
params: {
|
|
723
|
+
slug,
|
|
724
|
+
},
|
|
725
|
+
}),
|
|
726
|
+
mcqs: (config) => get(getUrl("cms", "v1", "mcqs"), config),
|
|
727
|
+
allMcqs: (config) => get(getUrl("cms", "v1", "all_mcqs"), config),
|
|
728
|
+
mcqsCategories: (config) => get(getUrl("cms", "v1", "mcqCategories"), config),
|
|
729
|
+
qna: (slug) => get(getUrl("cms", "v1", "qna"), {
|
|
730
|
+
params: {
|
|
731
|
+
slug,
|
|
732
|
+
},
|
|
733
|
+
}),
|
|
734
|
+
qnas: (config) => get(getUrl("cms", "v1", "qnas"), config),
|
|
735
|
+
qnasCategories: (config) => get(getUrl("cms", "v1", "qnaCategories"), config),
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
const createInvoice = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
739
|
+
return handler$1(post(getUrl("accounting", "v1", "invoice"), data, config), {
|
|
740
|
+
onFetching: () => onFetching?.(),
|
|
741
|
+
onError: (error) => onError?.(error),
|
|
742
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
743
|
+
onSettled: () => onSettled?.(),
|
|
744
|
+
});
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
const submitInvoice = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
748
|
+
return handler$1(post(getUrl("accounting", "v1", "submit_invoice"), data, config), {
|
|
749
|
+
onFetching: () => onFetching?.(),
|
|
750
|
+
onError: (error) => onError?.(error),
|
|
751
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
752
|
+
onSettled: () => onSettled?.(),
|
|
753
|
+
});
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
const updateInvoice = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
757
|
+
return handler$1(put(getUrl("accounting", "v1", "invoice"), data, config), {
|
|
758
|
+
onFetching: () => onFetching?.(),
|
|
759
|
+
onError: (error) => onError?.(error),
|
|
760
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
761
|
+
onSettled: () => onSettled?.(),
|
|
762
|
+
});
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
const Accounting = {
|
|
766
|
+
item: (slug) => get(getUrl("accounting", "v1", "item"), {
|
|
767
|
+
params: {
|
|
768
|
+
slug,
|
|
769
|
+
},
|
|
770
|
+
}),
|
|
771
|
+
items: (config) => get(getUrl("accounting", "v1", "items"), config),
|
|
772
|
+
createInvoice,
|
|
773
|
+
updateInvoice,
|
|
774
|
+
invoices: (config) => get(getUrl("accounting", "v1", "invoices"), config),
|
|
775
|
+
submitInvoice,
|
|
776
|
+
itemCategories: () => get(getUrl("accounting", "v1", "item_categories")),
|
|
777
|
+
itemBrands: () => get(getUrl("accounting", "v1", "item_brands")),
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
const feedback = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
781
|
+
return handler$1(post(getUrl("support", "v1", "feedback"), data, config), {
|
|
782
|
+
onFetching: () => onFetching?.(),
|
|
783
|
+
onError: (error) => onError?.(error),
|
|
784
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
785
|
+
onSettled: () => onSettled?.(),
|
|
786
|
+
});
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
const newsletter = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
790
|
+
return handler$1(post(getUrl("support", "v1", "newsletter"), data, config), {
|
|
791
|
+
onFetching: () => onFetching?.(),
|
|
792
|
+
onError: (error) => onError?.(error),
|
|
793
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
794
|
+
onSettled: () => onSettled?.(),
|
|
795
|
+
});
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
const ticket = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
799
|
+
return handler$1(post(getUrl("support", "v1", "ticket"), data, config), {
|
|
800
|
+
onFetching: () => onFetching?.(),
|
|
801
|
+
onError: (error) => onError?.(error),
|
|
802
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
803
|
+
onSettled: () => onSettled?.(),
|
|
804
|
+
});
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
const Support = {
|
|
808
|
+
ticket,
|
|
809
|
+
newsletter,
|
|
810
|
+
tickets: (config) => get(getUrl("support", "v1", "tickets"), config),
|
|
811
|
+
feedback
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
const Assets = {
|
|
815
|
+
images: {
|
|
816
|
+
imagePlaceholder: getConfig().apiUrl + getUrl("assets", "v1", "imagePlaceholder"),
|
|
817
|
+
avatar: getConfig().apiUrl + getUrl("assets", "v1", "avatar"),
|
|
818
|
+
},
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
const updateProfile = (data, { config, onFetching, onSuccess, onError, onSettled }) => {
|
|
822
|
+
return handler$1(put(getUrl("account", "v1", "profile"), data, config), {
|
|
823
|
+
onFetching: () => onFetching?.(),
|
|
824
|
+
onError: (error) => onError?.(error),
|
|
825
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
826
|
+
onSettled: () => onSettled?.(),
|
|
827
|
+
});
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
const Account = {
|
|
831
|
+
profile: (id) => get(getUrl("account", "v1", "profile"), {
|
|
832
|
+
params: {
|
|
833
|
+
id,
|
|
834
|
+
},
|
|
835
|
+
}),
|
|
836
|
+
updateProfile,
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
function shareUrl(title, url) {
|
|
840
|
+
// URL encoding to ensure special characters are handled properly
|
|
841
|
+
const encodedTitle = encodeURIComponent(title);
|
|
842
|
+
const encodedUrl = encodeURIComponent(url);
|
|
843
|
+
const target = "_blank";
|
|
844
|
+
const dimension = "width=600,height=400";
|
|
845
|
+
// Social media share URLs
|
|
846
|
+
const shareLinks = {
|
|
847
|
+
facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`,
|
|
848
|
+
twitter: `https://twitter.com/intent/tweet?text=${encodedTitle}&url=${encodedUrl}`,
|
|
849
|
+
linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}`,
|
|
850
|
+
whatsapp: `https://api.whatsapp.com/send?text=${encodedTitle} ${encodedUrl}`,
|
|
851
|
+
pinterest: `https://www.pinterest.com/pin/create/button/?url=${encodedUrl}&description=${encodedTitle}`,
|
|
852
|
+
};
|
|
853
|
+
return {
|
|
854
|
+
facebook: () => window.open(shareLinks.facebook, target, dimension),
|
|
855
|
+
twitter: () => window.open(shareLinks.twitter, target, dimension),
|
|
856
|
+
linkedin: () => window.open(shareLinks.linkedin, target, dimension),
|
|
857
|
+
whatsapp: () => window.open(shareLinks.whatsapp, target, dimension),
|
|
858
|
+
pinterest: () => window.open(shareLinks.pinterest, target, dimension),
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function addLead(data, { config = null, onLoading = () => { }, onSuccess = () => { }, onError = () => { }, onSettled = () => { }, } = {}) {
|
|
863
|
+
return handler({
|
|
864
|
+
promise: post(getUrl("sales", "v1", "addLead"), {
|
|
865
|
+
...data
|
|
866
|
+
}, config || undefined),
|
|
867
|
+
onLoading,
|
|
868
|
+
onError: (error) => onError(error),
|
|
869
|
+
onSuccess: (responseData) => {
|
|
870
|
+
onSuccess(responseData);
|
|
871
|
+
},
|
|
872
|
+
onSettled,
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
const Sales = {
|
|
877
|
+
addLead,
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const Inventory = {
|
|
881
|
+
getStoreById: (id) => get(getUrl("inventory", "v1", "store"), {
|
|
882
|
+
params: {
|
|
883
|
+
id,
|
|
884
|
+
},
|
|
885
|
+
}),
|
|
886
|
+
getStoreBySlug: (slug) => get(getUrl("inventory", "v1", "store"), {
|
|
887
|
+
params: {
|
|
888
|
+
slug,
|
|
889
|
+
},
|
|
890
|
+
}),
|
|
891
|
+
getStoreProducts: (slug, category, brand, price, search) => get(getUrl("inventory", "v1", "store_products"), {
|
|
892
|
+
params: {
|
|
893
|
+
slug,
|
|
894
|
+
category,
|
|
895
|
+
brand,
|
|
896
|
+
price,
|
|
897
|
+
search,
|
|
898
|
+
},
|
|
899
|
+
}),
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
const exchangeRates = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
903
|
+
return handler$1(get(getUrl("tools", "v1", "exchangeRates"), config), {
|
|
904
|
+
onFetching: () => onFetching?.(),
|
|
905
|
+
onError: (error) => onError?.(error),
|
|
906
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
907
|
+
onSettled: () => onSettled?.(),
|
|
908
|
+
});
|
|
909
|
+
};
|
|
910
|
+
// export async function convertCurrency(amount: number, fromCurrency?: string, toCurrency?: string) {
|
|
911
|
+
// try {
|
|
912
|
+
// const rates = await getExchangeRates(fromCurrency);
|
|
913
|
+
// if (!rates[toCurrency]) {
|
|
914
|
+
// throw new Error(`Currency ${toCurrency} is not available.`);
|
|
915
|
+
// }
|
|
916
|
+
// const convertedAmount = (amount * rates[toCurrency]).toFixed(2);
|
|
917
|
+
// return convertedAmount;
|
|
918
|
+
// } catch (error) {
|
|
919
|
+
// console.error('Error converting currency:', error);
|
|
920
|
+
// throw error;
|
|
921
|
+
// }
|
|
922
|
+
// }
|
|
923
|
+
|
|
924
|
+
const Tools = {
|
|
925
|
+
exchangeRates,
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
const shortUrl = (data, { config, onFetching, onSuccess, onError, onSettled, }) => {
|
|
929
|
+
return handler$1(linkiePost(getUrl("linkie", "v1", "shorten", true), data, config), {
|
|
930
|
+
onFetching: () => onFetching?.(),
|
|
931
|
+
onError: (error) => onError?.(error),
|
|
932
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
933
|
+
onSettled: () => onSettled?.(),
|
|
934
|
+
});
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
// import cookie from "js-cookie";
|
|
938
|
+
// import { jwtDecode } from "jwt-decode";
|
|
939
|
+
// import type { NextPageContext } from "next";
|
|
940
|
+
// import { getConfig } from "../config/index";
|
|
941
|
+
// import { isAuth } from "../utils/auth";
|
|
942
|
+
// import {
|
|
943
|
+
// getDataFromStorage,
|
|
944
|
+
// setDataInStorage,
|
|
945
|
+
// setEncryptionKey,
|
|
946
|
+
// } from "../utils/localStorage";
|
|
947
|
+
const Linkie = {
|
|
948
|
+
shortUrl
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
const getCities = ({ config, onFetching, onSuccess, onError, onSettled }) => {
|
|
952
|
+
return handler$1(get(`${getConfig().apisUrl}/api/countries/v1/city`, config), {
|
|
953
|
+
onFetching: () => onFetching?.(),
|
|
954
|
+
onError: (error) => onError?.(error),
|
|
955
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
956
|
+
onSettled: () => onSettled?.(),
|
|
957
|
+
});
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
const getCountries = ({ config, onFetching, onSuccess, onError, onSettled }) => {
|
|
961
|
+
return handler$1(get(`${getConfig().apisUrl}/api/countries/v1`, config), {
|
|
962
|
+
onFetching: () => onFetching?.(),
|
|
963
|
+
onError: (error) => onError?.(error),
|
|
964
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
965
|
+
onSettled: () => onSettled?.(),
|
|
966
|
+
});
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
const getStates = ({ config, onFetching, onSuccess, onError, onSettled }) => {
|
|
970
|
+
return handler$1(get(`${getConfig().apisUrl}/api/countries/v1/state`, config), {
|
|
971
|
+
onFetching: () => onFetching?.(),
|
|
972
|
+
onError: (error) => onError?.(error),
|
|
973
|
+
onSuccess: (data) => onSuccess?.(data),
|
|
974
|
+
onSettled: () => onSettled?.(),
|
|
975
|
+
});
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
const Apis = {
|
|
979
|
+
getCountries,
|
|
980
|
+
getStates,
|
|
981
|
+
getCities
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
export { Account, Accounting, Apis, Assets, Auth, Content, Inventory, Linkie, Order, Payment, Sales, Support, Tools, getConfig, getCookie, getUrl, initializeSDK, shareUrl };
|
|
985
|
+
//# sourceMappingURL=index.js.map
|