@acoustte-digital-services/digitalstore-controls-dev 0.8.1-dev.20260326104921 → 0.8.1-dev.20260326105727
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/LinkNodeButton-KBSXOEHS.mjs +169 -0
- package/dist/chunk-3IDT4246.mjs +400 -0
- package/dist/index.js +640 -565
- package/dist/index.mjs +352 -901
- package/package.json +1 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
Button_default,
|
|
4
|
+
ServiceClient_default
|
|
5
|
+
} from "./chunk-3IDT4246.mjs";
|
|
6
|
+
|
|
7
|
+
// src/components/pageRenderingEngine/nodes/LinkNodeButton.tsx
|
|
8
|
+
import { useCallback, useState } from "react";
|
|
9
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
|
+
var LinkNodeButton = (props) => {
|
|
11
|
+
const { node, dataitem, children, linkText, linkType, linkUrl } = props;
|
|
12
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
13
|
+
const [error, setError] = useState(null);
|
|
14
|
+
const extractFieldNames = useCallback((template) => {
|
|
15
|
+
if (!template) return [];
|
|
16
|
+
const regex = /\{(\{\})?([a-zA-Z_$][a-zA-Z0-9_$]*)(?:\}\})?\}/g;
|
|
17
|
+
const matches = Array.from(template.matchAll(regex));
|
|
18
|
+
const fieldNames = matches.map((match) => match[2] || match[1]).filter((name, index, self) => self.indexOf(name) === index);
|
|
19
|
+
return fieldNames;
|
|
20
|
+
}, []);
|
|
21
|
+
const replaceTemplateVariables = useCallback((template, responseData) => {
|
|
22
|
+
if (!template) return template;
|
|
23
|
+
let result = template;
|
|
24
|
+
const fieldNames = extractFieldNames(template);
|
|
25
|
+
if (responseData) {
|
|
26
|
+
fieldNames.forEach((fieldName) => {
|
|
27
|
+
const value = getNestedValue(responseData, fieldName);
|
|
28
|
+
if (value !== void 0) {
|
|
29
|
+
const regex1 = new RegExp(`\\{${fieldName}\\}`, "g");
|
|
30
|
+
const regex2 = new RegExp(`\\{\\{${fieldName}\\}\\}`, "g");
|
|
31
|
+
result = result.replace(regex1, String(value));
|
|
32
|
+
result = result.replace(regex2, String(value));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (props.routeParameters) {
|
|
37
|
+
Object.entries(props.routeParameters).forEach(([key, value]) => {
|
|
38
|
+
const regex = new RegExp(`\\{\\{${key}\\}\\}`, "g");
|
|
39
|
+
result = result.replace(regex, String(value));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (dataitem) {
|
|
43
|
+
Object.entries(dataitem).forEach(([key, value]) => {
|
|
44
|
+
const regex = new RegExp(`\\{\\{${key}\\}\\}`, "g");
|
|
45
|
+
result = result.replace(regex, String(value));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}, [props.routeParameters, dataitem, extractFieldNames]);
|
|
50
|
+
const getNestedValue = useCallback((obj, path) => {
|
|
51
|
+
if (!obj || !path) return void 0;
|
|
52
|
+
if (obj[path] !== void 0) {
|
|
53
|
+
return obj[path];
|
|
54
|
+
}
|
|
55
|
+
const keys = path.split(".");
|
|
56
|
+
let current = obj;
|
|
57
|
+
for (const key of keys) {
|
|
58
|
+
if (current[key] === void 0) {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
current = current[key];
|
|
62
|
+
}
|
|
63
|
+
return current;
|
|
64
|
+
}, []);
|
|
65
|
+
const onClick = useCallback(async (e) => {
|
|
66
|
+
if (!node.postUrl) {
|
|
67
|
+
setError("No POST URL configured for this button");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
setIsLoading(true);
|
|
71
|
+
setError(null);
|
|
72
|
+
try {
|
|
73
|
+
const resolvedPostUrl = replaceTemplateVariables(node.postUrl);
|
|
74
|
+
let parsedPayload = {};
|
|
75
|
+
if (node.payload) {
|
|
76
|
+
try {
|
|
77
|
+
const payloadStr = replaceTemplateVariables(node.payload);
|
|
78
|
+
parsedPayload = JSON.parse(payloadStr);
|
|
79
|
+
console.log("Parsed payload:", parsedPayload);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.error("Failed to parse payload JSON:", err);
|
|
82
|
+
parsedPayload = { error: "Invalid payload JSON" };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const serviceClient = new ServiceClient_default(props.apiBaseUrl, props.session);
|
|
86
|
+
const response = await serviceClient.post(resolvedPostUrl, parsedPayload);
|
|
87
|
+
console.log("API Response:", response);
|
|
88
|
+
if (response && !response.isSuccessful) {
|
|
89
|
+
const errorMessage = response.message || "API request failed";
|
|
90
|
+
setError(errorMessage);
|
|
91
|
+
setIsLoading(false);
|
|
92
|
+
return { isSuccessful: false, message: errorMessage };
|
|
93
|
+
}
|
|
94
|
+
if (response && node.redirectUrl) {
|
|
95
|
+
const fieldNames = extractFieldNames(node.redirectUrl);
|
|
96
|
+
console.log("Field names in redirect URL:", fieldNames);
|
|
97
|
+
const fieldValueMap = {};
|
|
98
|
+
fieldNames.forEach((fieldName) => {
|
|
99
|
+
const value = getNestedValue(response, fieldName);
|
|
100
|
+
if (value !== void 0) {
|
|
101
|
+
fieldValueMap[fieldName] = String(value);
|
|
102
|
+
} else {
|
|
103
|
+
const resultValue = getNestedValue(response, `result.${fieldName}`);
|
|
104
|
+
if (resultValue !== void 0) {
|
|
105
|
+
fieldValueMap[fieldName] = String(resultValue);
|
|
106
|
+
} else {
|
|
107
|
+
const dataValue = getNestedValue(response, `data.${fieldName}`);
|
|
108
|
+
if (dataValue !== void 0) {
|
|
109
|
+
fieldValueMap[fieldName] = String(dataValue);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
console.log("Field value map:", fieldValueMap);
|
|
115
|
+
const missingFields = fieldNames.filter((fieldName) => !fieldValueMap[fieldName]);
|
|
116
|
+
if (missingFields.length > 0) {
|
|
117
|
+
console.warn(`Missing field values for: ${missingFields.join(", ")}`);
|
|
118
|
+
}
|
|
119
|
+
let resolvedRedirectUrl = node.redirectUrl;
|
|
120
|
+
Object.entries(fieldValueMap).forEach(([fieldName, value]) => {
|
|
121
|
+
const regex1 = new RegExp(`\\{${fieldName}\\}`, "g");
|
|
122
|
+
const regex2 = new RegExp(`\\{\\{${fieldName}\\}\\}`, "g");
|
|
123
|
+
resolvedRedirectUrl = resolvedRedirectUrl.replace(regex1, value);
|
|
124
|
+
resolvedRedirectUrl = resolvedRedirectUrl.replace(regex2, value);
|
|
125
|
+
});
|
|
126
|
+
resolvedRedirectUrl = replaceTemplateVariables(resolvedRedirectUrl, response);
|
|
127
|
+
console.log("Final redirect URL:", resolvedRedirectUrl);
|
|
128
|
+
if (resolvedRedirectUrl && !resolvedRedirectUrl.includes("{")) {
|
|
129
|
+
window.location.href = resolvedRedirectUrl;
|
|
130
|
+
}
|
|
131
|
+
} else if (response && !response.result && response.message) {
|
|
132
|
+
setError(response.message);
|
|
133
|
+
throw new Error(response.message);
|
|
134
|
+
} else if (!response) {
|
|
135
|
+
setError("No response from server");
|
|
136
|
+
}
|
|
137
|
+
setIsLoading(false);
|
|
138
|
+
return { isSuccessful: true, response };
|
|
139
|
+
} catch (err) {
|
|
140
|
+
console.error("Button API call failed:", err);
|
|
141
|
+
setError(err.message || "An unexpected error occurred");
|
|
142
|
+
setIsLoading(false);
|
|
143
|
+
return { isSuccessful: false, message: err.message };
|
|
144
|
+
}
|
|
145
|
+
}, [node.postUrl, node.payload, node.redirectUrl, replaceTemplateVariables, extractFieldNames, getNestedValue, props.apiBaseUrl, props.session]);
|
|
146
|
+
const renderButtonContent = () => {
|
|
147
|
+
if (children) {
|
|
148
|
+
return children;
|
|
149
|
+
}
|
|
150
|
+
if (linkText) {
|
|
151
|
+
return /* @__PURE__ */ jsx("span", { children: linkText });
|
|
152
|
+
}
|
|
153
|
+
return node.title || "Button";
|
|
154
|
+
};
|
|
155
|
+
return /* @__PURE__ */ jsx("div", { className: "link-button-wrapper", children: /* @__PURE__ */ jsx(
|
|
156
|
+
Button_default,
|
|
157
|
+
{
|
|
158
|
+
ButtonType: linkType,
|
|
159
|
+
onClick,
|
|
160
|
+
disabled: isLoading,
|
|
161
|
+
className: "w-full",
|
|
162
|
+
children: renderButtonContent()
|
|
163
|
+
}
|
|
164
|
+
) });
|
|
165
|
+
};
|
|
166
|
+
var LinkNodeButton_default = LinkNodeButton;
|
|
167
|
+
export {
|
|
168
|
+
LinkNodeButton_default as default
|
|
169
|
+
};
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// src/components/Button.tsx
|
|
2
|
+
import React3, { useState as useState2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/components/ToastService.tsx
|
|
5
|
+
var ToastService = class _ToastService {
|
|
6
|
+
static initialize(showToast, closeToast) {
|
|
7
|
+
_ToastService.showToast = showToast;
|
|
8
|
+
_ToastService.closeToast = closeToast;
|
|
9
|
+
}
|
|
10
|
+
static showError(message) {
|
|
11
|
+
if (_ToastService.showToast) {
|
|
12
|
+
_ToastService.showToast(message, "error");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
static showInfo(message) {
|
|
16
|
+
if (_ToastService.showToast) {
|
|
17
|
+
_ToastService.showToast(message, "info");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
static close() {
|
|
21
|
+
if (_ToastService.closeToast) {
|
|
22
|
+
_ToastService.closeToast();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var ToastService_default = ToastService;
|
|
27
|
+
|
|
28
|
+
// src/components/StyleTypes.tsx
|
|
29
|
+
var buttonClasses = /* @__PURE__ */ new Map([
|
|
30
|
+
["Primary" /* Solid */, "btn-solid"],
|
|
31
|
+
["PrimaryHollow" /* Hollow */, "btn-hollow"],
|
|
32
|
+
["Link" /* Link */, "btn-link"]
|
|
33
|
+
]);
|
|
34
|
+
var progressClasses = /* @__PURE__ */ new Map([
|
|
35
|
+
["Primary" /* Solid */, ""],
|
|
36
|
+
["PrimaryHollow" /* Hollow */, ""],
|
|
37
|
+
["Link" /* Link */, ""]
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
// src/components/Confirm.tsx
|
|
41
|
+
import { useState } from "react";
|
|
42
|
+
|
|
43
|
+
// src/components/ClientButton.tsx
|
|
44
|
+
import React from "react";
|
|
45
|
+
import { jsx } from "react/jsx-runtime";
|
|
46
|
+
var ClientButton = (props) => {
|
|
47
|
+
const execute = async (event) => {
|
|
48
|
+
if (props.onClick !== void 0) {
|
|
49
|
+
props.onClick();
|
|
50
|
+
} else {
|
|
51
|
+
ToastService_default.showError("No action defined.");
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
let buttonClass = props.ButtonType ? buttonClasses.get(props.ButtonType) : buttonClasses.get("Primary" /* Solid */);
|
|
55
|
+
return /* @__PURE__ */ jsx(React.Fragment, { children: /* @__PURE__ */ jsx(
|
|
56
|
+
"button",
|
|
57
|
+
{
|
|
58
|
+
type: "button",
|
|
59
|
+
onClick: execute,
|
|
60
|
+
className: buttonClass + " " + props.className,
|
|
61
|
+
children: props.children
|
|
62
|
+
}
|
|
63
|
+
) });
|
|
64
|
+
};
|
|
65
|
+
var ClientButton_default = ClientButton;
|
|
66
|
+
|
|
67
|
+
// src/components/Confirm.tsx
|
|
68
|
+
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
69
|
+
var Confirm = ({ message, onConfirm, onCancel }) => {
|
|
70
|
+
const [showModal, setShowModal] = useState(true);
|
|
71
|
+
const handleConfirmAction = () => {
|
|
72
|
+
setShowModal(false);
|
|
73
|
+
if (onConfirm) {
|
|
74
|
+
onConfirm();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const handleCancelAction = () => {
|
|
78
|
+
setShowModal(false);
|
|
79
|
+
if (onCancel) {
|
|
80
|
+
onCancel();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
return /* @__PURE__ */ jsx2(Fragment, { children: showModal && /* @__PURE__ */ jsxs("div", { className: "fixed inset-0 flex items-center justify-center z-50", children: [
|
|
84
|
+
/* @__PURE__ */ jsx2("div", { className: "absolute inset-0 bg-black opacity-70" }),
|
|
85
|
+
/* @__PURE__ */ jsxs("div", { className: "bg-white rounded-md p-6 w-2/6 shadow border z-50", children: [
|
|
86
|
+
/* @__PURE__ */ jsx2("p", { className: "text-xl font-medium mb-4", children: "Confirmation" }),
|
|
87
|
+
/* @__PURE__ */ jsx2("p", { className: "mb-4", children: message }),
|
|
88
|
+
/* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-8", children: [
|
|
89
|
+
/* @__PURE__ */ jsx2(
|
|
90
|
+
ClientButton_default,
|
|
91
|
+
{
|
|
92
|
+
onClick: handleCancelAction,
|
|
93
|
+
ButtonType: "PrimaryHollow" /* Hollow */,
|
|
94
|
+
children: "Cancel"
|
|
95
|
+
}
|
|
96
|
+
),
|
|
97
|
+
/* @__PURE__ */ jsx2(
|
|
98
|
+
ClientButton_default,
|
|
99
|
+
{
|
|
100
|
+
onClick: handleConfirmAction,
|
|
101
|
+
children: "Confirm"
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
] })
|
|
105
|
+
] })
|
|
106
|
+
] }) });
|
|
107
|
+
};
|
|
108
|
+
var Confirm_default = Confirm;
|
|
109
|
+
{
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/components/Button.tsx
|
|
113
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
114
|
+
var Button = (props) => {
|
|
115
|
+
const [inProgress, setInProgress] = useState2(false);
|
|
116
|
+
const [isActionPerformed, setIsActionPerformed] = useState2(false);
|
|
117
|
+
const [responseMessage, setResponseMessage] = useState2(null);
|
|
118
|
+
const [showModal, setShowModal] = useState2(null);
|
|
119
|
+
const execute = async (event) => {
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
event.stopPropagation();
|
|
122
|
+
if (props.confirm) {
|
|
123
|
+
const confirmed = await showConfirmation("Are you sure you want to delete this item?");
|
|
124
|
+
setShowModal(null);
|
|
125
|
+
if (!confirmed) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (props.oneTimeAction && isActionPerformed) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
setInProgress(true);
|
|
133
|
+
let isValid = true;
|
|
134
|
+
if (props.onValidate !== void 0) {
|
|
135
|
+
isValid = await props.onValidate();
|
|
136
|
+
if (!isValid) {
|
|
137
|
+
setInProgress(false);
|
|
138
|
+
ToastService_default.showError("There are errors in the form. Please fix them before proceeding.");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (props.onClick !== void 0) {
|
|
143
|
+
let response = await props.onClick();
|
|
144
|
+
if (response.isSuccessful) {
|
|
145
|
+
setIsActionPerformed(true);
|
|
146
|
+
setResponseMessage(response.message);
|
|
147
|
+
if (props.showToast) {
|
|
148
|
+
ToastService_default.showInfo(response.message || "");
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
ToastService_default.showError(response.message || "");
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
ToastService_default.showError("No action defined.");
|
|
155
|
+
}
|
|
156
|
+
setInProgress(false);
|
|
157
|
+
};
|
|
158
|
+
const showConfirmation = (message) => {
|
|
159
|
+
return new Promise((resolve) => {
|
|
160
|
+
const onConfirm = () => resolve(true);
|
|
161
|
+
const onCancel = () => resolve(false);
|
|
162
|
+
setShowModal(/* @__PURE__ */ jsx3(Confirm_default, { message: props.confirmationMessage, onConfirm, onCancel }));
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
let buttonClass = props.ButtonType ? buttonClasses.get(props.ButtonType) : buttonClasses.get("Primary" /* Solid */);
|
|
166
|
+
let progressClass = props.ButtonType ? progressClasses.get(props.ButtonType) : progressClasses.get("Primary" /* Solid */);
|
|
167
|
+
const isDisabled = inProgress || isActionPerformed && props.oneTimeAction;
|
|
168
|
+
return /* @__PURE__ */ jsxs2(React3.Fragment, { children: [
|
|
169
|
+
/* @__PURE__ */ jsxs2(
|
|
170
|
+
"button",
|
|
171
|
+
{
|
|
172
|
+
type: "submit",
|
|
173
|
+
onClick: execute,
|
|
174
|
+
disabled: props.disabled,
|
|
175
|
+
title: isDisabled ? "The button is disabled to prevent any action" : "",
|
|
176
|
+
className: buttonClass + " relative " + props.className,
|
|
177
|
+
children: [
|
|
178
|
+
isActionPerformed && props.oneTimeAction && responseMessage ? responseMessage : props.children,
|
|
179
|
+
inProgress && /* @__PURE__ */ jsx3(React3.Fragment, { children: props.hideProgressIndicator === true ? /* @__PURE__ */ jsx3("div", { className: "absolute bottom-0 left-0 h-0.5 bg-gray-400 rounded animate-progress" }) : /* @__PURE__ */ jsxs2("svg", { className: "animate-spin ml-2 mr-3 h-5 w-5 " + progressClass, xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", children: [
|
|
180
|
+
/* @__PURE__ */ jsx3("circle", { className: "opacity-25", cx: "12", cy: "12", r: "10", stroke: "currentColor", strokeWidth: "4" }),
|
|
181
|
+
/* @__PURE__ */ jsx3("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" })
|
|
182
|
+
] }) })
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
),
|
|
186
|
+
showModal
|
|
187
|
+
] });
|
|
188
|
+
};
|
|
189
|
+
var Button_default = Button;
|
|
190
|
+
|
|
191
|
+
// src/clients/CacheManage.tsx
|
|
192
|
+
import NodeCache from "node-cache";
|
|
193
|
+
var CacheManager = class _CacheManager {
|
|
194
|
+
constructor() {
|
|
195
|
+
this.maxCacheSize = 1e3;
|
|
196
|
+
this.cache = new NodeCache({ stdTTL: 0, checkperiod: 300 });
|
|
197
|
+
}
|
|
198
|
+
static getInstance() {
|
|
199
|
+
if (!_CacheManager.instance) {
|
|
200
|
+
_CacheManager.instance = new _CacheManager();
|
|
201
|
+
}
|
|
202
|
+
return _CacheManager.instance;
|
|
203
|
+
}
|
|
204
|
+
get(key) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
set(key, data, ttl) {
|
|
208
|
+
}
|
|
209
|
+
clear() {
|
|
210
|
+
this.cache.flushAll();
|
|
211
|
+
}
|
|
212
|
+
size() {
|
|
213
|
+
return this.cache.keys().length;
|
|
214
|
+
}
|
|
215
|
+
destroy() {
|
|
216
|
+
this.cache.close();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/clients/ServiceClient.tsx
|
|
221
|
+
var ServerApiError = class extends Error {
|
|
222
|
+
constructor(data, status) {
|
|
223
|
+
super(data.message || "---");
|
|
224
|
+
this.status = status;
|
|
225
|
+
this.data = data;
|
|
226
|
+
this.data.isSuccessful = false;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var ServiceClient = class {
|
|
230
|
+
constructor(apiBaseUrl, session) {
|
|
231
|
+
this.cacheManager = CacheManager.getInstance();
|
|
232
|
+
this.baseUrl = apiBaseUrl;
|
|
233
|
+
this.session = session;
|
|
234
|
+
}
|
|
235
|
+
buildFullPath(path, params) {
|
|
236
|
+
let updatedPath = path;
|
|
237
|
+
if (params) {
|
|
238
|
+
Object.keys(params).forEach((key) => {
|
|
239
|
+
updatedPath = updatedPath.replace(
|
|
240
|
+
`{${key}}`,
|
|
241
|
+
String(params[key])
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return this.baseUrl + updatedPath;
|
|
246
|
+
}
|
|
247
|
+
getConfig() {
|
|
248
|
+
const config = { headers: {} };
|
|
249
|
+
if (this.session) {
|
|
250
|
+
if (this.session.oAuthToken) {
|
|
251
|
+
config.headers["Authorization"] = "Bearer " + this.session.oAuthToken;
|
|
252
|
+
}
|
|
253
|
+
config.headers["cid"] = this.session.cid || "";
|
|
254
|
+
config.headers["UserCurrencyCode"] = this.session.userCurrencyCode || "INR";
|
|
255
|
+
config.headers["MarketCode"] = this.session?.marketCode || "IND";
|
|
256
|
+
}
|
|
257
|
+
return config;
|
|
258
|
+
}
|
|
259
|
+
handleFetchError(error) {
|
|
260
|
+
console.log(error);
|
|
261
|
+
const serverApiError = error;
|
|
262
|
+
if (serverApiError) {
|
|
263
|
+
return serverApiError.data;
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
message: "There is some error. Please try after sometime.",
|
|
267
|
+
isSuccessful: false
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async fetchJsonWithCache(fullPath, config) {
|
|
271
|
+
const cacheKey = fullPath + "--" + (this.session?.marketCode || "IND");
|
|
272
|
+
const cachedData = this.cacheManager.get(cacheKey);
|
|
273
|
+
if (cachedData) {
|
|
274
|
+
return cachedData;
|
|
275
|
+
}
|
|
276
|
+
console.log("*****************CALLING API:", cacheKey, (/* @__PURE__ */ new Date()).toISOString());
|
|
277
|
+
const response = await fetch(fullPath, { headers: config.headers });
|
|
278
|
+
if (!response.ok) {
|
|
279
|
+
const apiErrorData = await response.json();
|
|
280
|
+
throw new ServerApiError(apiErrorData, response.status);
|
|
281
|
+
}
|
|
282
|
+
const cacheControl = response.headers.get("Cache-Control");
|
|
283
|
+
let revalidate = null;
|
|
284
|
+
if (cacheControl) {
|
|
285
|
+
const maxAgeMatch = cacheControl.match(/max-age=(\d+)/);
|
|
286
|
+
if (maxAgeMatch && maxAgeMatch[1]) {
|
|
287
|
+
const maxAge = parseInt(maxAgeMatch[1], 10);
|
|
288
|
+
revalidate = maxAge * 1e3;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const data = await response.json();
|
|
292
|
+
data.isSuccessful = true;
|
|
293
|
+
if (revalidate !== null && revalidate > 0) {
|
|
294
|
+
console.log("revalidate............I am caching:" + revalidate);
|
|
295
|
+
this.cacheManager.set(cacheKey, data, revalidate);
|
|
296
|
+
}
|
|
297
|
+
return data;
|
|
298
|
+
}
|
|
299
|
+
// private async refreshToken(): Promise<void> {
|
|
300
|
+
// console.log("*******************calling refresh token***********************");
|
|
301
|
+
// try {
|
|
302
|
+
// const response = await fetch(this.baseUrl + "/auth/storefront/login/refreshToken", {
|
|
303
|
+
// method: 'POST',
|
|
304
|
+
// headers: {
|
|
305
|
+
// 'Content-Type': 'application/json'
|
|
306
|
+
// },
|
|
307
|
+
// body: JSON.stringify({ refreshToken: this.session.refreshToken })
|
|
308
|
+
// });
|
|
309
|
+
// if (!response.ok) {
|
|
310
|
+
// throw new Error("Failed to refresh token");
|
|
311
|
+
// }
|
|
312
|
+
// const responseData = await response.json();
|
|
313
|
+
// this.session.oAuthToken = responseData.result.accessToken;
|
|
314
|
+
// if (typeof window === "undefined") {
|
|
315
|
+
// // Running on the server
|
|
316
|
+
// } else {
|
|
317
|
+
// await fetch("/api/login", {
|
|
318
|
+
// method: "post",
|
|
319
|
+
// headers: {
|
|
320
|
+
// "Content-Type": "application/json",
|
|
321
|
+
// },
|
|
322
|
+
// body: JSON.stringify({ session: this.session }),
|
|
323
|
+
// });
|
|
324
|
+
// }
|
|
325
|
+
// } catch (error: any) {
|
|
326
|
+
// throw new Error("Failed to refresh token");
|
|
327
|
+
// }
|
|
328
|
+
// }
|
|
329
|
+
async handleRequest(request) {
|
|
330
|
+
try {
|
|
331
|
+
return await request();
|
|
332
|
+
} catch (error) {
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
async post(path, data) {
|
|
337
|
+
const request = async () => {
|
|
338
|
+
const fullPath = this.baseUrl + path;
|
|
339
|
+
const config = this.getConfig();
|
|
340
|
+
const response = await fetch(fullPath, {
|
|
341
|
+
method: "POST",
|
|
342
|
+
headers: {
|
|
343
|
+
...config.headers,
|
|
344
|
+
"Content-Type": "application/json"
|
|
345
|
+
},
|
|
346
|
+
body: JSON.stringify(data)
|
|
347
|
+
});
|
|
348
|
+
if (!response.ok) {
|
|
349
|
+
const apiErrorData = await response.json();
|
|
350
|
+
throw new ServerApiError(apiErrorData, response.status);
|
|
351
|
+
}
|
|
352
|
+
const responseData = await response.json();
|
|
353
|
+
responseData.isSuccessful = true;
|
|
354
|
+
return responseData;
|
|
355
|
+
};
|
|
356
|
+
try {
|
|
357
|
+
return await this.handleRequest(request);
|
|
358
|
+
} catch (error) {
|
|
359
|
+
return this.handleFetchError(error);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
async getSingle(path, params) {
|
|
363
|
+
const request = async () => {
|
|
364
|
+
const sanitizedParams = params ? Object.fromEntries(
|
|
365
|
+
Object.entries(params).map(([k, v]) => [k, String(v)])
|
|
366
|
+
) : void 0;
|
|
367
|
+
const fullPath = this.buildFullPath(path, sanitizedParams);
|
|
368
|
+
const config = this.getConfig();
|
|
369
|
+
return await this.fetchJsonWithCache(fullPath, config);
|
|
370
|
+
};
|
|
371
|
+
try {
|
|
372
|
+
return await this.handleRequest(request);
|
|
373
|
+
} catch (error) {
|
|
374
|
+
return this.handleFetchError(error);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async get(path, params) {
|
|
378
|
+
const request = async () => {
|
|
379
|
+
const sanitizedParams = params ? Object.fromEntries(
|
|
380
|
+
Object.entries(params).map(([k, v]) => [k, String(v)])
|
|
381
|
+
) : void 0;
|
|
382
|
+
const fullPath = this.buildFullPath(path, sanitizedParams);
|
|
383
|
+
const config = this.getConfig();
|
|
384
|
+
console.log(fullPath);
|
|
385
|
+
return await this.fetchJsonWithCache(fullPath, config);
|
|
386
|
+
};
|
|
387
|
+
try {
|
|
388
|
+
return await this.handleRequest(request);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
return this.handleFetchError(error);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
var ServiceClient_default = ServiceClient;
|
|
395
|
+
|
|
396
|
+
export {
|
|
397
|
+
buttonClasses,
|
|
398
|
+
Button_default,
|
|
399
|
+
ServiceClient_default
|
|
400
|
+
};
|