lightspeed-retail-sdk 2.0.11 → 2.0.13

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.
Files changed (3) hide show
  1. package/index.cjs +84 -11
  2. package/index.mjs +84 -11
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -27,28 +27,35 @@ class LightspeedRetailSDK {
27
27
  // handleError function to handle errors
28
28
  handleError(context, err, shouldThrow = true) {
29
29
  // Context includes information about where the error occurred
30
- const detailedMessage = `Error in ${context}: ${err.message}`;
30
+ const errorMessage = err?.message || "Unknown error occurred";
31
+ const detailedMessage = `Error in ${context}: ${errorMessage}`;
31
32
 
32
33
  // Log the error message
33
34
  console.error(detailedMessage);
34
35
 
35
36
  // Log the stack trace if available
36
- if (err.stack) {
37
+ if (err?.stack) {
37
38
  console.error("Stack trace:", err.stack);
38
39
  }
39
40
 
40
41
  // If the error has response data, log it
41
- if (err.response) {
42
+ if (err?.response) {
42
43
  console.error("Error response:", {
43
- status: err.response.status,
44
- headers: err.response.headers,
45
- data: err.response.data,
44
+ status: err.response?.status,
45
+ headers: err.response?.headers,
46
+ data: err.response?.data,
46
47
  });
47
- }
48
-
49
- // Optionally rethrow the error with the detailed message
50
- if (shouldThrow) {
51
- throw new Error(detailedMessage);
48
+ } else if (!err?.response && typeof err === "object") {
49
+ console.error("Non-response error object:", err);
50
+ } else if (typeof err === "string") {
51
+ console.error("Error as string:", err);
52
+ } else {
53
+ console.error("Error of unknown type:", err);
54
+
55
+ // Optionally rethrow the error with the detailed message
56
+ if (shouldThrow) {
57
+ throw new Error(detailedMessage);
58
+ }
52
59
  }
53
60
  }
54
61
 
@@ -957,6 +964,72 @@ class LightspeedRetailSDK {
957
964
  return this.handleError("GET SPECIAL ORDER ERROR", error);
958
965
  }
959
966
  }
967
+
968
+ async getImages(relations) {
969
+ const options = {
970
+ url: `${this.baseUrl}/${this.accountID}/Image.json`,
971
+ method: "GET",
972
+ };
973
+
974
+ if (relations) options.url = options.url + `?load_relations=${relations}`;
975
+
976
+ try {
977
+ const response = await this.getAllData(options);
978
+ return response;
979
+ } catch (error) {
980
+ return this.handleError("GET ITEMS ERROR", error);
981
+ }
982
+ }
983
+
984
+ async postImage(imageFilePath, metadata) {
985
+ if (!imageFilePath)
986
+ return this.handleError("You need to provide an image file path");
987
+ if (!metadata || (!metadata.itemID && !metadata.itemMatrixID)) {
988
+ return this.handleError(
989
+ "You need to provide metadata with either itemID or itemMatrixID"
990
+ );
991
+ }
992
+
993
+ // Import required modules dynamically
994
+ const FormData = (await import("form-data")).default;
995
+ const fs = (await import("fs")).default;
996
+ const path = (await import("path")).default;
997
+
998
+ // Create form data object
999
+ const formData = new FormData();
1000
+
1001
+ // Add the metadata as JSON string to the 'data' field
1002
+ formData.append("data", JSON.stringify(metadata));
1003
+
1004
+ // Get filename from path
1005
+ const filename = path.basename(imageFilePath);
1006
+
1007
+ // Add the image file to the form
1008
+ formData.append("image", fs.createReadStream(imageFilePath), {
1009
+ filename,
1010
+ contentType: this.getContentType(filename),
1011
+ });
1012
+
1013
+ const token = await this.getToken();
1014
+ if (!token) throw new Error("Error Fetching Token");
1015
+
1016
+ const options = {
1017
+ url: `${this.baseUrl}/${this.accountID}/Image.json`,
1018
+ method: "POST",
1019
+ headers: {
1020
+ Authorization: `Bearer ${token}`,
1021
+ ...formData.getHeaders(),
1022
+ },
1023
+ data: formData,
1024
+ };
1025
+
1026
+ try {
1027
+ const response = await axios(options);
1028
+ return response.data;
1029
+ } catch (error) {
1030
+ return this.handleError("POST IMAGE ERROR", error);
1031
+ }
1032
+ }
960
1033
  }
961
1034
 
962
1035
  module.exports = LightspeedRetailSDK;
package/index.mjs CHANGED
@@ -27,28 +27,35 @@ class LightspeedRetailSDK {
27
27
  // handleError function to handle errors
28
28
  handleError(context, err, shouldThrow = true) {
29
29
  // Context includes information about where the error occurred
30
- const detailedMessage = `Error in ${context}: ${err.message}`;
30
+ const errorMessage = err?.message || "Unknown error occurred";
31
+ const detailedMessage = `Error in ${context}: ${errorMessage}`;
31
32
 
32
33
  // Log the error message
33
34
  console.error(detailedMessage);
34
35
 
35
36
  // Log the stack trace if available
36
- if (err.stack) {
37
+ if (err?.stack) {
37
38
  console.error("Stack trace:", err.stack);
38
39
  }
39
40
 
40
41
  // If the error has response data, log it
41
- if (err.response) {
42
+ if (err?.response) {
42
43
  console.error("Error response:", {
43
- status: err.response.status,
44
- headers: err.response.headers,
45
- data: err.response.data,
44
+ status: err.response?.status,
45
+ headers: err.response?.headers,
46
+ data: err.response?.data,
46
47
  });
47
- }
48
-
49
- // Optionally rethrow the error with the detailed message
50
- if (shouldThrow) {
51
- throw new Error(detailedMessage);
48
+ } else if (!err?.response && typeof err === "object") {
49
+ console.error("Non-response error object:", err);
50
+ } else if (typeof err === "string") {
51
+ console.error("Error as string:", err);
52
+ } else {
53
+ console.error("Error of unknown type:", err);
54
+
55
+ // Optionally rethrow the error with the detailed message
56
+ if (shouldThrow) {
57
+ throw new Error(detailedMessage);
58
+ }
52
59
  }
53
60
  }
54
61
 
@@ -957,6 +964,72 @@ class LightspeedRetailSDK {
957
964
  return this.handleError("GET SPECIAL ORDER ERROR", error);
958
965
  }
959
966
  }
967
+
968
+ async getImages(relations) {
969
+ const options = {
970
+ url: `${this.baseUrl}/${this.accountID}/Image.json`,
971
+ method: "GET",
972
+ };
973
+
974
+ if (relations) options.url = options.url + `?load_relations=${relations}`;
975
+
976
+ try {
977
+ const response = await this.getAllData(options);
978
+ return response;
979
+ } catch (error) {
980
+ return this.handleError("GET IMAGES ERROR", error);
981
+ }
982
+ }
983
+
984
+ async postImage(imageFilePath, metadata) {
985
+ if (!imageFilePath)
986
+ return this.handleError("You need to provide an image file path");
987
+ if (!metadata || (!metadata.itemID && !metadata.itemMatrixID)) {
988
+ return this.handleError(
989
+ "You need to provide metadata with either itemID or itemMatrixID"
990
+ );
991
+ }
992
+
993
+ // Import required modules dynamically
994
+ const FormData = (await import("form-data")).default;
995
+ const fs = (await import("fs")).default;
996
+ const path = (await import("path")).default;
997
+
998
+ // Create form data object
999
+ const formData = new FormData();
1000
+
1001
+ // Add the metadata as JSON string to the 'data' field
1002
+ formData.append("data", JSON.stringify(metadata));
1003
+
1004
+ // Get filename from path
1005
+ const filename = path.basename(imageFilePath);
1006
+
1007
+ // Add the image file to the form
1008
+ formData.append("image", fs.createReadStream(imageFilePath), {
1009
+ filename,
1010
+ contentType: this.getContentType(filename),
1011
+ });
1012
+
1013
+ const token = await this.getToken();
1014
+ if (!token) throw new Error("Error Fetching Token");
1015
+
1016
+ const options = {
1017
+ url: `${this.baseUrl}/${this.accountID}/Image.json`,
1018
+ method: "POST",
1019
+ headers: {
1020
+ Authorization: `Bearer ${token}`,
1021
+ ...formData.getHeaders(),
1022
+ },
1023
+ data: formData,
1024
+ };
1025
+
1026
+ try {
1027
+ const response = await axios(options);
1028
+ return response.data;
1029
+ } catch (error) {
1030
+ return this.handleError("POST IMAGE ERROR", error);
1031
+ }
1032
+ }
960
1033
  }
961
1034
 
962
1035
  export default LightspeedRetailSDK;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightspeed-retail-sdk",
3
- "version": "2.0.11",
3
+ "version": "2.0.13",
4
4
  "description": "Another unofficial Lightspeed Retail API SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "index.cjs",
@@ -25,4 +25,4 @@
25
25
  "lightspeed retail",
26
26
  "node"
27
27
  ]
28
- }
28
+ }