zudello-integration-sdk 1.0.35 → 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 +1 -1
- package/src/utils/logger.js +10 -10
- package/src/utils/s3Client.js +59 -0
package/package.json
CHANGED
package/src/utils/logger.js
CHANGED
|
@@ -33,17 +33,17 @@ class Logger {
|
|
|
33
33
|
|
|
34
34
|
if (this.mode === this.productionMode) {
|
|
35
35
|
this.logs.push(logEntry)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Output the log message
|
|
39
|
-
if (content instanceof Error) {
|
|
40
|
-
console.log(`[${status.toUpperCase()}] ${title}:`)
|
|
41
|
-
console.error(content.stack)
|
|
42
|
-
} else if (Array.isArray(content) || (typeof content === 'object' && content !== null)) {
|
|
43
|
-
console.log(`[${status.toUpperCase()}] ${title}:`)
|
|
44
|
-
console.log(JSON.stringify(content, null, 2))
|
|
45
36
|
} else {
|
|
46
|
-
|
|
37
|
+
// Output the log message
|
|
38
|
+
if (content instanceof Error) {
|
|
39
|
+
console.log(`[${status.toUpperCase()}] ${title}:`)
|
|
40
|
+
console.error(content.stack)
|
|
41
|
+
} else if (Array.isArray(content) || (typeof content === 'object' && content !== null)) {
|
|
42
|
+
console.log(`[${status.toUpperCase()}] ${title}:`)
|
|
43
|
+
console.log(JSON.stringify(content, null, 2))
|
|
44
|
+
} else {
|
|
45
|
+
console.log(`[${status.toUpperCase()}] ${title}: ${content}`)
|
|
46
|
+
}
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
package/src/utils/s3Client.js
CHANGED
|
@@ -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
|