@dotdev/harmony-sdk 1.27.0 → 1.28.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.
@@ -3,7 +3,7 @@ import axiosRetry from "axios-retry";
3
3
  import * as https from "https";
4
4
  import { promisify } from "util";
5
5
  import { parseString } from "xml2js";
6
- import logger from "./helpers/logger";
6
+ import { logger } from "./helpers/logger";
7
7
  import { AuthModule, CarrierModule, DiaryModule, GiftVoucherModule, PointOfSaleModule, StockClassificationType, StockLevelLookupModule, StockLookupModule, } from "./modules";
8
8
  require("tls").DEFAULT_MIN_VERSION = "TLSv1";
9
9
  const parseXml = promisify(parseString);
@@ -2,7 +2,7 @@ import axios from "axios";
2
2
  import { promisify } from "util";
3
3
  import { parseString } from "xml2js";
4
4
  import { RequestError } from "../errors";
5
- import logger from "./logger";
5
+ import { logger } from "./logger";
6
6
  import { Utils } from "./utils";
7
7
  export * from "./utils";
8
8
  const parseXml = promisify(parseString);
@@ -13,43 +13,33 @@ export class ApiHelper {
13
13
  let response;
14
14
  switch (type) {
15
15
  case "GET":
16
- response = await axiosInstance.get(url).catch((error) => {
17
- logger.error(error, `Error while sending request to ${endpoint} endpoint`);
18
- throw error;
19
- });
16
+ response = await axiosInstance.get(url);
20
17
  break;
21
18
  case "POST":
22
19
  default:
23
- response = await axiosInstance
24
- .post(url, requestBody)
25
- .catch((error) => {
26
- logger.error(error, `Error while sending request to ${endpoint} endpoint`);
27
- throw error;
28
- });
20
+ response = await axiosInstance.post(url, requestBody);
29
21
  break;
30
22
  }
31
- const logs = logger.child({
32
- request: requestBody,
33
- response: response?.data,
34
- size: +(response?.headers?.["Content-Length"] || 0),
35
- status: response?.status,
36
- });
37
- logs.debug(`Received Harmony response: `);
23
+ logger.debug({
24
+ request: Utils.removeTabsAndNewLines(requestBody),
25
+ response: {
26
+ data: response?.data,
27
+ headers: response?.headers,
28
+ size: +(response?.headers?.["content-length"] || 0),
29
+ status: response?.status,
30
+ },
31
+ }, `Received Harmony response 🔽`);
38
32
  // Throw an error if the response is not in XML format
39
- if (response?.headers?.["Content-Type"] !== "text/xml" &&
40
- response?.headers?.["Content-Type"] !== "application/xml") {
41
- throw new Error(`Returned response not in XML format. This is likely a gateway error - [${response?.status} - ${response?.statusText}]: ${JSON.stringify(response)}`);
33
+ if (response?.headers["content-type"] !== "text/xml" &&
34
+ response?.headers["content-type"] !== "text/xml;charset=utf-8" &&
35
+ response?.headers["content-type"] !== "application/xml") {
36
+ logger.error(new Error(`Returned response not in XML format. This is likely a gateway error - [${response?.status} - ${response?.statusText}]: ${response.data}`));
42
37
  }
43
38
  // Parse the XML response
44
39
  const result = (await parseXml(response?.data)); // fix type
45
40
  return result["S:Envelope"]["S:Body"][0];
46
41
  }
47
42
  catch (error) {
48
- const logs = logger.child({
49
- request: requestBody,
50
- error: error,
51
- });
52
- logs.error(`Error while when sending request to Harmony`);
53
43
  throw await ApiHelper.parseError(error);
54
44
  }
55
45
  }
@@ -135,9 +125,10 @@ export class ApiHelper {
135
125
  }
136
126
  }
137
127
  else {
138
- logger.error(`Unexpected error occurred during API request`);
139
- throw JSON.stringify(error);
128
+ console.log(JSON.stringify(error));
129
+ logger.error(`=============== Unexpected error occurred during API request ===============`);
140
130
  }
131
+ return new RequestError(`Unexpected error occurred during API request`);
141
132
  }
142
133
  /**
143
134
  * Extract error response from Harmony in XML format and convert it to JSON format for ease of debugging
@@ -1,3 +1 @@
1
- import * as pino from "pino";
2
- declare const logger: pino.Logger<never, boolean>;
3
- export default logger;
1
+ export declare const logger: import("pino").Logger<never, boolean>;
@@ -1,4 +1,10 @@
1
1
  import { createGcpLoggingPinoConfig } from "@google-cloud/pino-logging-gcp-config";
2
- import * as pino from "pino";
3
- const logger = pino.pino(createGcpLoggingPinoConfig());
4
- export default logger;
2
+ import pino from "pino";
3
+ export const logger = pino(createGcpLoggingPinoConfig({
4
+ serviceContext: {
5
+ service: "harmony-sdk",
6
+ },
7
+ }, {
8
+ // set Pino log level to 'debug'
9
+ level: "debug",
10
+ }));
@@ -75,6 +75,7 @@ export declare abstract class Utils {
75
75
  * @returns
76
76
  */
77
77
  static isValidXml(xmlString: string): boolean;
78
+ static removeTabsAndNewLines(str?: string): string;
78
79
  }
79
80
  export declare const TEST_HEADERS: {
80
81
  headers: {
@@ -158,6 +158,11 @@ export class Utils {
158
158
  }
159
159
  return stack.length === 0;
160
160
  }
161
+ static removeTabsAndNewLines(str) {
162
+ if (!str)
163
+ return "";
164
+ return str.replace(/ |\r\n|\n|\r/gm, "");
165
+ }
161
166
  }
162
167
  export const TEST_HEADERS = {
163
168
  headers: {
@@ -6,6 +6,7 @@ export class AuthModule {
6
6
  this.axiosInstance = axiosInstance;
7
7
  }
8
8
  async authenticate(authToken) {
9
+ console.log(`===========================================================================`);
9
10
  // @todo: TBC whether using `xmlbuilder` or template string
10
11
  const authHeader = `
11
12
  <ns2:AuthenticationToken xmlns:ns2="http://auth.ws.fbsaust.com.au" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
@@ -15,6 +16,7 @@ export class AuthModule {
15
16
  <timeout>${authToken.timeout}${authToken.timeoutUnit}</timeout>
16
17
  </ns2:AuthenticationToken>
17
18
  `;
19
+ console.log(`authHeader`, authHeader);
18
20
  const loginBody = '<ns2:Login xmlns:ns2="http://auth.ws.fbsaust.com.au"/>';
19
21
  const soapEnvelope = `
20
22
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
@@ -22,6 +24,7 @@ export class AuthModule {
22
24
  <S:Body>${loginBody}</S:Body>
23
25
  </S:Envelope>
24
26
  `;
27
+ console.log(`soapEnvelope`, soapEnvelope);
25
28
  try {
26
29
  const response = await ApiHelper.sendSoapRequest("/AuthenticationService/AuthenticationService", this.axiosInstance, soapEnvelope);
27
30
  const token = response["ns2:LoginResponse"][0]["token"][0];
@@ -35,6 +38,8 @@ export class AuthModule {
35
38
  throw error;
36
39
  }
37
40
  else {
41
+ console.error(`Auth error`);
42
+ console.error(error);
38
43
  throw new AuthenticationError(`An unexpected error occurred: ${error.message}`);
39
44
  }
40
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotdev/harmony-sdk",
3
- "version": "1.27.0",
3
+ "version": "1.28.0",
4
4
  "description": "Harmony API SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -45,6 +45,7 @@
45
45
  "watch": "tsc -w",
46
46
  "test": "jest",
47
47
  "release": "./release.sh",
48
- "prettier": "prettier --write ."
48
+ "prettier": "prettier --write .",
49
+ "refreshLocal": "pnpm run build && yalc push --sig"
49
50
  }
50
51
  }