@boodibox/api-client 0.1.1 → 0.1.2
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/index.js +21 -7
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* Changes:
|
|
6
6
|
* - replyPermission defaults to "PUBLIC" when omitted
|
|
7
7
|
* - explicitly empty or falsey replyPermission values are rejected
|
|
8
|
+
* - submitPost now requires either a non-empty body or at least one media
|
|
8
9
|
* - other behavior unchanged (upload -> poll -> submit)
|
|
9
10
|
*/
|
|
10
11
|
|
|
@@ -186,6 +187,7 @@ function createClient(opts = {}) {
|
|
|
186
187
|
* submitPost
|
|
187
188
|
* replyPermission: if undefined -> defaults to PUBLIC
|
|
188
189
|
* if explicitly provided but empty/falsey -> throws
|
|
190
|
+
* now enforces: post must have non-empty body OR at least one media
|
|
189
191
|
*/
|
|
190
192
|
async function submitPost({ body = '', medias = [], replyPermission = undefined, quotePostID = null, userIP = null }) {
|
|
191
193
|
// Handle replyPermission defaulting and validation
|
|
@@ -199,6 +201,13 @@ function createClient(opts = {}) {
|
|
|
199
201
|
|
|
200
202
|
const qid = quotePostID == null ? null : validateQuotePostID(quotePostID);
|
|
201
203
|
|
|
204
|
+
// Validation: require either a non-empty body or at least one media
|
|
205
|
+
const bodyStr = (body == null ? '' : String(body)).trim();
|
|
206
|
+
const hasMedias = Array.isArray(medias) && medias.length > 0;
|
|
207
|
+
if (!bodyStr && !hasMedias) {
|
|
208
|
+
throw new Error('Post must include a non-empty body or at least one media.');
|
|
209
|
+
}
|
|
210
|
+
|
|
202
211
|
const url = new URL(config.postsPath, config.baseUrl).toString();
|
|
203
212
|
const payload = { body, medias, replyPermission: rp, quotePostID: qid };
|
|
204
213
|
if (userIP) payload.userIP = userIP;
|
|
@@ -222,21 +231,26 @@ function createClient(opts = {}) {
|
|
|
222
231
|
* submitPostWithFiles
|
|
223
232
|
* - If replyPermission undefined -> defaults to PUBLIC
|
|
224
233
|
* - If replyPermission explicitly provided but empty -> throw
|
|
234
|
+
* - Requires either body or files; if files provided they will be uploaded
|
|
225
235
|
*/
|
|
226
236
|
async function submitPostWithFiles({ body = '', files = [], replyPermission = undefined, quotePostID = null, pollOptions = {}, timeoutMs = 120000 }) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
237
|
+
// If no files and empty body -> reject early
|
|
238
|
+
const bodyStr = (body == null ? '' : String(body)).trim();
|
|
239
|
+
if ((!Array.isArray(files) || files.length === 0) && !bodyStr) {
|
|
240
|
+
throw new Error('Post must include a non-empty body or at least one file to upload.');
|
|
230
241
|
}
|
|
231
242
|
|
|
232
|
-
// validate upfront: default or validate
|
|
233
|
-
if (replyPermission
|
|
234
|
-
// allow defaulting later in submitPost, but keep same normalization here
|
|
235
|
-
} else {
|
|
243
|
+
// validate upfront: default or validate replyPermission and quotePostID
|
|
244
|
+
if (replyPermission !== undefined) {
|
|
236
245
|
validateReplyPermission(replyPermission);
|
|
237
246
|
}
|
|
238
247
|
if (quotePostID != null) validateQuotePostID(quotePostID);
|
|
239
248
|
|
|
249
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
250
|
+
// no files to upload, delegate to submitPost which also validates
|
|
251
|
+
return submitPost({ body, medias: [], replyPermission, quotePostID });
|
|
252
|
+
}
|
|
253
|
+
|
|
240
254
|
const uploads = await retryAsync(() => uploadFiles(files), config.maxRetries, 300).catch(err => {
|
|
241
255
|
throw new Error(`Uploading files failed: ${err.message}`);
|
|
242
256
|
});
|