zudello-integration-sdk 1.0.36 → 1.0.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zudello-integration-sdk",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "Zudello Integrations SDK",
5
5
  "main": "./src/index.js",
6
6
  "repository": {
@@ -57,6 +57,64 @@ const uploadFile = async (bucketName, key, body, contentType = null) => {
57
57
  return await s3.send(new PutObjectCommand(params))
58
58
  }
59
59
 
60
+ /**
61
+ * Appends items to a JSON array stored in S3. If the file doesn't exist,
62
+ * creates a new array with the provided items.
63
+ *
64
+ * @param {string} bucketName - The name of the S3 bucket
65
+ * @param {string} key - The key of the file to upload
66
+ * @param {Array|Object} newItems - Item or array of items to append
67
+ * @returns {Promise<Object>} - Response from S3
68
+ * @throws {Error} If existing content is not a JSON array or if newItems is invalid
69
+ */
70
+ const appendJsonArray = async (bucketName, key, newItems) => {
71
+ // Ensure newItems is an array
72
+ const itemsToAppend = Array.isArray(newItems) ? newItems : [newItems]
73
+
74
+ let existingArray = []
75
+
76
+ try {
77
+ // Try to get existing content
78
+ const data = await s3.send(new GetObjectCommand({
79
+ Bucket: bucketName,
80
+ Key: key
81
+ }))
82
+
83
+ const existingContent = await data.Body.transformToString()
84
+
85
+ try {
86
+ const parsedContent = JSON.parse(existingContent)
87
+
88
+ // Verify the existing content is an array
89
+ if (!Array.isArray(parsedContent)) {
90
+ throw new Error('Existing S3 object is not a JSON array')
91
+ }
92
+
93
+ existingArray = parsedContent
94
+ } catch (parseError) {
95
+ throw new Error(`Failed to parse existing content as JSON: ${parseError.message}`)
96
+ }
97
+ } catch (error) {
98
+ // If file doesn't exist, we'll start with an empty array
99
+ if (error.name !== 'NoSuchKey') {
100
+ console.warn(`Warning getting existing file: ${error.message}`)
101
+ }
102
+ }
103
+
104
+ // Combine existing array with new items
105
+ const finalArray = [...existingArray, ...itemsToAppend]
106
+
107
+ // Upload the combined array
108
+ const params = {
109
+ Bucket: bucketName,
110
+ Key: key,
111
+ Body: JSON.stringify(finalArray),
112
+ ContentType: 'application/json'
113
+ }
114
+
115
+ return await s3.send(new PutObjectCommand(params))
116
+ }
117
+
60
118
  /**
61
119
  * Lists the files in an S3 bucket under a specific prefix.
62
120
  *
@@ -122,6 +180,7 @@ const deleteFiles = async (bucketName, keys) => {
122
180
  module.exports = {
123
181
  getFileContent,
124
182
  uploadFile,
183
+ appendJsonArray,
125
184
  listFiles,
126
185
  deleteFile,
127
186
  deleteFiles