@dotdev/harmony-sdk 1.22.1 → 1.23.0

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.
@@ -110,8 +110,14 @@ export class ApiHelper {
110
110
  static async parseError(error) {
111
111
  if (axios.isAxiosError(error)) {
112
112
  if (error?.response) {
113
- const parsedError = await ApiHelper.parseHarmonyErrorResponse(error?.response?.data);
114
- return new RequestError(`API request failed with status ${error.response.status}: {code: ${parsedError?.code}, name: ${parsedError?.name}, message: ${parsedError?.message}}`);
113
+ if (Utils.isValidXml(error?.response?.data)) {
114
+ const parsedError = await ApiHelper.parseHarmonyErrorResponse(error?.response?.data);
115
+ return new RequestError(`API request failed with status ${error.response.status}: {code: ${parsedError?.code}, name: ${parsedError?.name}, message: ${parsedError?.message}}`);
116
+ }
117
+ else {
118
+ logger.error(`API request error`, { error: error });
119
+ return new RequestError(`API request failed but error response is not in Harmony XML format. Please check the logs for more details.`);
120
+ }
115
121
  }
116
122
  else if (error?.request) {
117
123
  return new RequestError("No response received from the server during API call");
@@ -69,6 +69,12 @@ export declare abstract class Utils {
69
69
  * @returns {*} first value in the array
70
70
  */
71
71
  static getFirst(prop: any): string;
72
+ /**
73
+ * Validate whether the provided string is a valid XML.
74
+ * @param xmlString
75
+ * @returns
76
+ */
77
+ static isValidXml(xmlString: string): boolean;
72
78
  }
73
79
  export declare const TEST_HEADERS: {
74
80
  headers: {
@@ -137,6 +137,27 @@ export class Utils {
137
137
  static getFirst(prop) {
138
138
  return (prop ?? [])[0];
139
139
  }
140
+ /**
141
+ * Validate whether the provided string is a valid XML.
142
+ * @param xmlString
143
+ * @returns
144
+ */
145
+ static isValidXml(xmlString) {
146
+ let stack = [];
147
+ const regex = /<([^>]+)>/g;
148
+ let match;
149
+ while ((match = regex.exec(xmlString)) !== null) {
150
+ if (match[1].charAt(0) === "/") {
151
+ if (stack.length === 0 || stack.pop() !== match[1].slice(1)) {
152
+ return false;
153
+ }
154
+ }
155
+ else {
156
+ stack.push(match[1]);
157
+ }
158
+ }
159
+ return stack.length === 0;
160
+ }
140
161
  }
141
162
  export const TEST_HEADERS = {
142
163
  headers: {
@@ -107,4 +107,10 @@ describe("Misc", () => {
107
107
  // Check if Accept-Encoding is set to gzip, deflate, br for compression support (currently enabled for S2)
108
108
  expect(harmonyApi.axiosInstance.defaults.headers["Accept-Encoding"]).toEqual("gzip, deflate, br");
109
109
  });
110
+ test("validateXml() should validate XML correctly", () => {
111
+ expect(Utils.isValidXml("<root><child></child></root>")).toBe(true);
112
+ expect(Utils.isValidXml("<root><child></root>")).toBe(false);
113
+ expect(Utils.isValidXml("<root><child></child>")).toBe(false);
114
+ expect(Utils.isValidXml("<root><child></child></root><root>")).toBe(false);
115
+ });
110
116
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotdev/harmony-sdk",
3
- "version": "1.22.1",
3
+ "version": "1.23.0",
4
4
  "description": "Harmony API SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",