@nuelink/nuelink-cli 1.4.0 → 1.4.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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +51 -29
- package/SECURITY.md +70 -0
- package/package.json +22 -4
- package/src/cli.js +37 -13
- package/src/commands.js +109 -67
- package/src/config.js +107 -38
- package/src/errors.js +47 -0
- package/src/http.js +95 -54
- package/src/postPayload.js +187 -175
- package/src/validation.js +53 -14
- package/test/cli.test.mjs +1235 -0
- package/test/mock-fetch.cjs +261 -0
- package/test/mock-fetch.mjs +261 -0
package/src/http.js
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import chalk from "chalk";
|
|
3
|
-
import {
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import { CliError } from "./errors.js";
|
|
4
|
+
import { printApiResponse } from "./output.js";
|
|
4
5
|
|
|
5
6
|
const require = createRequire(import.meta.url);
|
|
6
7
|
const { version: cliVersion } = require("../package.json");
|
|
7
8
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
9
|
+
const DEFAULT_API_BASE_URL = "https://app.nuelink.com/api/public/v1";
|
|
10
|
+
|
|
11
|
+
function readIntegerEnvironment(name, fallback, { minimum = 0, maximum = 600000 } = {}) {
|
|
12
|
+
const raw = process.env[name];
|
|
13
|
+
if (raw === undefined || raw === "") return fallback;
|
|
14
|
+
|
|
15
|
+
const normalized = String(raw).trim();
|
|
16
|
+
if (!/^\d+$/.test(normalized)) {
|
|
17
|
+
throw new CliError("CONFIG_INVALID", `${name} must be an integer from ${minimum} to ${maximum}.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const parsed = Number(normalized);
|
|
21
|
+
if (!Number.isSafeInteger(parsed) || parsed < minimum || parsed > maximum) {
|
|
22
|
+
throw new CliError("CONFIG_INVALID", `${name} must be an integer from ${minimum} to ${maximum}.`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return parsed;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getRequestConfiguration() {
|
|
29
|
+
return {
|
|
30
|
+
apiBaseUrl: process.env.NUELINK_BASE_URL || DEFAULT_API_BASE_URL,
|
|
31
|
+
requestTimeoutMs: readIntegerEnvironment("NUELINK_TIMEOUT_MS", 30000, { minimum: 1 }),
|
|
32
|
+
maxGetRetries: readIntegerEnvironment("NUELINK_GET_RETRIES", 2, { maximum: 10 }),
|
|
33
|
+
retryBaseDelayMs: readIntegerEnvironment("NUELINK_RETRY_BASE_MS", 500),
|
|
34
|
+
retryMaxDelayMs: readIntegerEnvironment("NUELINK_RETRY_MAX_MS", 2000),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
13
37
|
|
|
14
38
|
function pathParam(value, label) {
|
|
15
39
|
const stringValue = String(value ?? "").trim();
|
|
@@ -29,15 +53,19 @@ function collectionPath(brandId, collectionId) {
|
|
|
29
53
|
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
30
54
|
}
|
|
31
55
|
|
|
32
|
-
function buildApiUrl(endpoint) {
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function
|
|
56
|
+
function buildApiUrl(endpoint, apiBaseUrl) {
|
|
57
|
+
try {
|
|
58
|
+
return new URL(endpoint.replace(/^\//, ""), `${apiBaseUrl.replace(/\/?$/, "/")}`);
|
|
59
|
+
} catch {
|
|
60
|
+
throw new CliError("CONFIG_INVALID", "NUELINK_BASE_URL must be a valid absolute URL.");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function shouldRetryRequest(method, statusCode, attempt, maxGetRetries) {
|
|
65
|
+
return method === "GET" && attempt < maxGetRetries && (statusCode === 429 || statusCode >= 500);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function parseRetryAfterMs(headers, attempt, retryBaseDelayMs, retryMaxDelayMs) {
|
|
41
69
|
const retryAfter = headers.get("retry-after");
|
|
42
70
|
|
|
43
71
|
if (retryAfter) {
|
|
@@ -53,7 +81,7 @@ function parseRetryAfterMs(headers, attempt) {
|
|
|
53
81
|
}
|
|
54
82
|
}
|
|
55
83
|
|
|
56
|
-
const exponentialDelay = Math.min(
|
|
84
|
+
const exponentialDelay = Math.min(retryBaseDelayMs * (2 ** attempt), retryMaxDelayMs);
|
|
57
85
|
return Math.floor(Math.random() * (exponentialDelay + 1));
|
|
58
86
|
}
|
|
59
87
|
|
|
@@ -76,8 +104,9 @@ function printDryRun({ method, url, query, body, multipart, dryRunBody }) {
|
|
|
76
104
|
}, null, 2));
|
|
77
105
|
}
|
|
78
106
|
|
|
79
|
-
async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, multipart, outputMode = "json", quiet = false, dryRun = false, dryRunBody }) {
|
|
80
|
-
const
|
|
107
|
+
async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, multipart, outputMode = "json", quiet = false, dryRun = false, dryRunBody }) {
|
|
108
|
+
const configuration = getRequestConfiguration();
|
|
109
|
+
const url = buildApiUrl(endpoint, configuration.apiBaseUrl);
|
|
81
110
|
|
|
82
111
|
if (query) {
|
|
83
112
|
Object.entries(query).forEach(([key, value]) => {
|
|
@@ -92,10 +121,11 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
92
121
|
return { dryRun: true };
|
|
93
122
|
}
|
|
94
123
|
|
|
95
|
-
if (!apiKey) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
124
|
+
if (!apiKey) {
|
|
125
|
+
throw new CliError(
|
|
126
|
+
"AUTH_MISSING",
|
|
127
|
+
"Missing API key. Use --api-key <api_key> for a single command or auth:login to save one.",
|
|
128
|
+
);
|
|
99
129
|
}
|
|
100
130
|
|
|
101
131
|
const headers = {
|
|
@@ -121,15 +151,20 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
121
151
|
|
|
122
152
|
for (let attempt = 0; ; attempt += 1) {
|
|
123
153
|
response = await fetch(url, {
|
|
124
|
-
...init,
|
|
125
|
-
signal: AbortSignal.timeout(
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
if (!shouldRetryRequest(method, response.status, attempt)) {
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
await sleep(parseRetryAfterMs(
|
|
154
|
+
...init,
|
|
155
|
+
signal: AbortSignal.timeout(configuration.requestTimeoutMs),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
if (!shouldRetryRequest(method, response.status, attempt, configuration.maxGetRetries)) {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
await sleep(parseRetryAfterMs(
|
|
163
|
+
response.headers,
|
|
164
|
+
attempt,
|
|
165
|
+
configuration.retryBaseDelayMs,
|
|
166
|
+
configuration.retryMaxDelayMs,
|
|
167
|
+
));
|
|
133
168
|
}
|
|
134
169
|
|
|
135
170
|
const text = await response.text();
|
|
@@ -137,20 +172,25 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
137
172
|
|
|
138
173
|
if (text) {
|
|
139
174
|
try {
|
|
140
|
-
data = JSON.parse(text);
|
|
141
|
-
} catch {
|
|
142
|
-
if (response.ok) {
|
|
143
|
-
throw new
|
|
175
|
+
data = JSON.parse(text);
|
|
176
|
+
} catch {
|
|
177
|
+
if (response.ok) {
|
|
178
|
+
throw new CliError(
|
|
179
|
+
"PROTOCOL_ERROR",
|
|
180
|
+
`Protocol error: expected a JSON response (HTTP ${response.status}).`,
|
|
181
|
+
{ httpStatus: response.status },
|
|
182
|
+
);
|
|
144
183
|
}
|
|
145
184
|
data = { raw: text };
|
|
146
185
|
}
|
|
147
186
|
}
|
|
148
187
|
|
|
149
|
-
if (!response.ok) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
188
|
+
if (!response.ok) {
|
|
189
|
+
throw new CliError(
|
|
190
|
+
"HTTP_ERROR",
|
|
191
|
+
`Request failed (${response.status} ${response.statusText})`,
|
|
192
|
+
{ httpStatus: response.status, details: data },
|
|
193
|
+
);
|
|
154
194
|
}
|
|
155
195
|
|
|
156
196
|
if (outputMode !== "json" && !quiet) {
|
|
@@ -159,18 +199,19 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
159
199
|
|
|
160
200
|
printApiResponse(data, { outputMode });
|
|
161
201
|
return data;
|
|
162
|
-
} catch (error) {
|
|
163
|
-
if (error
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (error instanceof CliError) throw error;
|
|
204
|
+
|
|
205
|
+
if (error?.name === "TimeoutError") {
|
|
206
|
+
throw new CliError(
|
|
207
|
+
"TIMEOUT",
|
|
208
|
+
`Request timed out after ${configuration.requestTimeoutMs}ms.`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
throw new CliError("NETWORK_ERROR", `Request error: ${error.message}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
174
215
|
|
|
175
216
|
export {
|
|
176
217
|
brandPath,
|
package/src/postPayload.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
parseBooleanStrict,
|
|
3
|
-
parseJsonString,
|
|
4
|
-
parseOptionalNonNegativeInteger,
|
|
2
|
+
parseBooleanStrict,
|
|
3
|
+
parseJsonString,
|
|
4
|
+
parseOptionalNonNegativeInteger,
|
|
5
5
|
splitCsv,
|
|
6
6
|
validateHttpUrl,
|
|
7
7
|
validatePollOptions,
|
|
8
|
+
validateRequiredString,
|
|
8
9
|
validateScheduledAt,
|
|
9
10
|
} from "./validation.js";
|
|
10
11
|
|
|
11
|
-
const VALID_PUBLISH_MODES = ["DRAFT", "QUEUE", "SCHEDULE", "IMMEDIATE"];
|
|
12
|
-
const VALID_POLL_PERIODS = ["1d", "3d", "7d"];
|
|
13
|
-
const VALID_TRIAL_REEL_MODES = ["MANUAL", "SS_PERFORMANCE"];
|
|
14
|
-
|
|
15
|
-
function validateOptionalLength(value, maximum, label) {
|
|
16
|
-
if (value !== undefined && value !== null && String(value).length > maximum) {
|
|
17
|
-
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
12
|
+
const VALID_PUBLISH_MODES = ["DRAFT", "QUEUE", "SCHEDULE", "IMMEDIATE"];
|
|
13
|
+
const VALID_POLL_PERIODS = ["1d", "3d", "7d"];
|
|
14
|
+
const VALID_TRIAL_REEL_MODES = ["MANUAL", "SS_PERFORMANCE"];
|
|
15
|
+
|
|
16
|
+
function validateOptionalLength(value, maximum, label) {
|
|
17
|
+
if (value !== undefined && value !== null && String(value).length > maximum) {
|
|
18
|
+
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
20
21
|
|
|
21
22
|
function parseCsv(value) {
|
|
22
23
|
return splitCsv(value);
|
|
@@ -55,7 +56,7 @@ function mergeMedia(idsCsv, urlsCsv) {
|
|
|
55
56
|
return [...idMedia, ...urlMedia];
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
function normalizePublishMode(value) {
|
|
59
|
+
function normalizePublishMode(value) {
|
|
59
60
|
const normalized = String(value ?? "").trim().toUpperCase();
|
|
60
61
|
|
|
61
62
|
if (!VALID_PUBLISH_MODES.includes(normalized)) {
|
|
@@ -63,71 +64,77 @@ function normalizePublishMode(value) {
|
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
return normalized;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function validateRawPostPayload(payload) {
|
|
69
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
70
|
-
throw new Error("Invalid payload. Expected a JSON object.");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const publishMode = normalizePublishMode(payload.publishMode);
|
|
74
|
-
const scheduledAt = validateScheduledAt(payload.scheduledAt);
|
|
75
|
-
if (publishMode === "SCHEDULE" && !scheduledAt) {
|
|
76
|
-
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
validateOptionalLength(payload.title, 255, "title");
|
|
80
|
-
validateOptionalLength(payload.caption, 3000, "caption");
|
|
81
|
-
validateOptionalLength(payload.alt, 255, "alt");
|
|
82
|
-
if (payload.
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (!poll
|
|
97
|
-
throw new Error("poll
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
options.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
throw new Error(`Invalid media item at index ${index}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function validateRawPostPayload(payload) {
|
|
70
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
71
|
+
throw new Error("Invalid payload. Expected a JSON object.");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const publishMode = normalizePublishMode(payload.publishMode);
|
|
75
|
+
const scheduledAt = validateScheduledAt(payload.scheduledAt);
|
|
76
|
+
if (publishMode === "SCHEDULE" && !scheduledAt) {
|
|
77
|
+
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
validateOptionalLength(payload.title, 255, "title");
|
|
81
|
+
validateOptionalLength(payload.caption, 3000, "caption");
|
|
82
|
+
validateOptionalLength(payload.alt, 255, "alt");
|
|
83
|
+
if (payload.autoThreadText !== undefined && payload.autoThreadText !== null && typeof payload.autoThreadText !== "boolean") {
|
|
84
|
+
throw new Error("Invalid autoThreadText. Expected a boolean or null.");
|
|
85
|
+
}
|
|
86
|
+
if (payload.link !== undefined) {
|
|
87
|
+
validateHttpUrl(payload.link, "link");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const poll = payload.poll;
|
|
91
|
+
const caption = !poll
|
|
92
|
+
? validateRequiredString(payload.caption, 3000, "caption")
|
|
93
|
+
: payload.caption === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: String(payload.caption).trim();
|
|
96
|
+
if (poll !== undefined) {
|
|
97
|
+
if (!poll || typeof poll !== "object" || Array.isArray(poll)) {
|
|
98
|
+
throw new Error("Invalid poll. Expected a JSON object.");
|
|
99
|
+
}
|
|
100
|
+
const options = Array.isArray(poll.options) ? poll.options : [];
|
|
101
|
+
validatePollOptions(options, poll.correctOptionIndex);
|
|
102
|
+
if (!poll.question || options.length < 2 || options.length > 4 || !VALID_POLL_PERIODS.includes(poll.period)) {
|
|
103
|
+
throw new Error("poll requires question, 2 to 4 options, and period (1d, 3d, or 7d).");
|
|
104
|
+
}
|
|
105
|
+
validateOptionalLength(poll.question, 280, "poll-question");
|
|
106
|
+
validateOptionalLength(poll.explanation, 512, "poll-explanation");
|
|
107
|
+
options.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (payload.media !== undefined) {
|
|
111
|
+
if (!Array.isArray(payload.media)) {
|
|
112
|
+
throw new Error("Invalid media. Expected an array.");
|
|
113
|
+
}
|
|
114
|
+
payload.media.forEach((item, index) => {
|
|
115
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
116
|
+
throw new Error(`Invalid media item at index ${index}.`);
|
|
117
|
+
}
|
|
118
|
+
const hasId = typeof item.id === "string" && item.id.trim().length > 0;
|
|
119
|
+
const hasUrl = typeof item.url === "string" && item.url.trim().length > 0;
|
|
120
|
+
if (hasId === hasUrl) {
|
|
121
|
+
throw new Error(`Invalid media item at index ${index}. Expected exactly one of id or url.`);
|
|
122
|
+
}
|
|
123
|
+
if (hasUrl) {
|
|
124
|
+
validateHttpUrl(item.url, `media[${index}].url`);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
...payload,
|
|
131
|
+
...(payload.caption !== undefined ? { caption } : {}),
|
|
132
|
+
publishMode,
|
|
133
|
+
...(payload.scheduledAt !== undefined ? { scheduledAt } : {}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function buildPostPayload(options) {
|
|
131
138
|
const media = mergeMedia(options.mediaIds, options.mediaUrls);
|
|
132
139
|
const publishMode = normalizePublishMode(options.publishMode);
|
|
133
140
|
const scheduledAt = validateScheduledAt(options.scheduledAt);
|
|
@@ -136,38 +143,38 @@ function buildPostPayload(options) {
|
|
|
136
143
|
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
137
144
|
}
|
|
138
145
|
|
|
139
|
-
const pollOptions = options.pollOptions ? parseCsv(options.pollOptions) : [];
|
|
140
|
-
const pollCorrectOptionIndex = parseOptionalNonNegativeInteger(options.pollCorrectOptionIndex, "poll-correct-option-index");
|
|
141
|
-
validatePollOptions(pollOptions, pollCorrectOptionIndex);
|
|
142
|
-
validateOptionalLength(options.title, 255, "title");
|
|
143
|
-
validateOptionalLength(options.caption, 3000, "caption");
|
|
144
|
-
validateOptionalLength(options.alt, 255, "alt");
|
|
145
|
-
validateOptionalLength(options.pollQuestion, 280, "poll-question");
|
|
146
|
-
validateOptionalLength(options.pollExplanation, 512, "poll-explanation");
|
|
147
|
-
validateOptionalLength(options.comment, 1000, "comment");
|
|
148
|
-
validateOptionalLength(options.instagramCollab, 1000, "instagram-collab");
|
|
149
|
-
pollOptions.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
150
|
-
|
|
151
|
-
if (options.pollPeriod && !VALID_POLL_PERIODS.includes(options.pollPeriod)) {
|
|
152
|
-
throw new Error(`Invalid poll-period. Expected one of: ${VALID_POLL_PERIODS.join(", ")}.`);
|
|
153
|
-
}
|
|
154
|
-
if (options.instagramTrialReel && !VALID_TRIAL_REEL_MODES.includes(options.instagramTrialReel)) {
|
|
155
|
-
throw new Error(`Invalid instagram-trial-reel. Expected one of: ${VALID_TRIAL_REEL_MODES.join(", ")}.`);
|
|
156
|
-
}
|
|
157
|
-
if (Boolean(options.instagramLocationId) !== Boolean(options.instagramLocationName)) {
|
|
158
|
-
throw new Error("instagram-location-id and instagram-location-name must be provided together.");
|
|
159
|
-
}
|
|
160
|
-
if (Boolean(options.facebookLocationId) !== Boolean(options.facebookLocationName)) {
|
|
161
|
-
throw new Error("facebook-location-id and facebook-location-name must be provided together.");
|
|
162
|
-
}
|
|
146
|
+
const pollOptions = options.pollOptions ? parseCsv(options.pollOptions) : [];
|
|
147
|
+
const pollCorrectOptionIndex = parseOptionalNonNegativeInteger(options.pollCorrectOptionIndex, "poll-correct-option-index");
|
|
148
|
+
validatePollOptions(pollOptions, pollCorrectOptionIndex);
|
|
149
|
+
validateOptionalLength(options.title, 255, "title");
|
|
150
|
+
validateOptionalLength(options.caption, 3000, "caption");
|
|
151
|
+
validateOptionalLength(options.alt, 255, "alt");
|
|
152
|
+
validateOptionalLength(options.pollQuestion, 280, "poll-question");
|
|
153
|
+
validateOptionalLength(options.pollExplanation, 512, "poll-explanation");
|
|
154
|
+
validateOptionalLength(options.comment, 1000, "comment");
|
|
155
|
+
validateOptionalLength(options.instagramCollab, 1000, "instagram-collab");
|
|
156
|
+
pollOptions.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
157
|
+
|
|
158
|
+
if (options.pollPeriod && !VALID_POLL_PERIODS.includes(options.pollPeriod)) {
|
|
159
|
+
throw new Error(`Invalid poll-period. Expected one of: ${VALID_POLL_PERIODS.join(", ")}.`);
|
|
160
|
+
}
|
|
161
|
+
if (options.instagramTrialReel && !VALID_TRIAL_REEL_MODES.includes(options.instagramTrialReel)) {
|
|
162
|
+
throw new Error(`Invalid instagram-trial-reel. Expected one of: ${VALID_TRIAL_REEL_MODES.join(", ")}.`);
|
|
163
|
+
}
|
|
164
|
+
if (Boolean(options.instagramLocationId) !== Boolean(options.instagramLocationName)) {
|
|
165
|
+
throw new Error("instagram-location-id and instagram-location-name must be provided together.");
|
|
166
|
+
}
|
|
167
|
+
if (Boolean(options.facebookLocationId) !== Boolean(options.facebookLocationName)) {
|
|
168
|
+
throw new Error("facebook-location-id and facebook-location-name must be provided together.");
|
|
169
|
+
}
|
|
163
170
|
|
|
164
171
|
const poll = {
|
|
165
172
|
...(options.pollQuestion ? { question: options.pollQuestion } : {}),
|
|
166
173
|
...(pollOptions.length > 0 ? { options: pollOptions } : {}),
|
|
167
174
|
...(options.pollPeriod ? { period: options.pollPeriod } : {}),
|
|
168
|
-
...(pollCorrectOptionIndex !== undefined
|
|
169
|
-
? { correctOptionIndex: pollCorrectOptionIndex }
|
|
170
|
-
: {}),
|
|
175
|
+
...(pollCorrectOptionIndex !== undefined
|
|
176
|
+
? { correctOptionIndex: pollCorrectOptionIndex }
|
|
177
|
+
: {}),
|
|
171
178
|
...(options.pollExplanation ? { explanation: options.pollExplanation } : {}),
|
|
172
179
|
};
|
|
173
180
|
|
|
@@ -188,7 +195,7 @@ function buildPostPayload(options) {
|
|
|
188
195
|
};
|
|
189
196
|
|
|
190
197
|
const facebook = {
|
|
191
|
-
...(options.facebookLocationId || options.facebookLocationName
|
|
198
|
+
...(options.facebookLocationId || options.facebookLocationName
|
|
192
199
|
? {
|
|
193
200
|
location: {
|
|
194
201
|
...(options.facebookLocationId ? { id: options.facebookLocationId } : {}),
|
|
@@ -199,76 +206,81 @@ function buildPostPayload(options) {
|
|
|
199
206
|
};
|
|
200
207
|
|
|
201
208
|
const tiktok = {
|
|
202
|
-
...(parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") !== undefined
|
|
203
|
-
? { sendToInbox: parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") }
|
|
204
|
-
: {}),
|
|
205
|
-
...(parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") !== undefined
|
|
206
|
-
? { autoAddMusic: parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") }
|
|
207
|
-
: {}),
|
|
209
|
+
...(parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") !== undefined
|
|
210
|
+
? { sendToInbox: parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") }
|
|
211
|
+
: {}),
|
|
212
|
+
...(parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") !== undefined
|
|
213
|
+
? { autoAddMusic: parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") }
|
|
214
|
+
: {}),
|
|
208
215
|
};
|
|
209
216
|
|
|
210
217
|
let youtubePlaylists;
|
|
211
|
-
if (options.youtubePlaylists) {
|
|
212
|
-
youtubePlaylists = parseJsonString(options.youtubePlaylists, "youtube-playlists");
|
|
213
|
-
if (!Array.isArray(youtubePlaylists)) {
|
|
214
|
-
throw new Error("Invalid youtube-playlists. Expected a JSON array.");
|
|
215
|
-
}
|
|
216
|
-
youtubePlaylists = youtubePlaylists.map((playlist) => ({
|
|
217
|
-
...playlist,
|
|
218
|
-
channelId: Number(playlist.channelId),
|
|
219
|
-
}));
|
|
220
|
-
if (youtubePlaylists.some((playlist) =>
|
|
221
|
-
!Number.isSafeInteger(playlist.channelId) ||
|
|
222
|
-
playlist.channelId < 1 ||
|
|
223
|
-
!Array.isArray(playlist.playlistIds)
|
|
224
|
-
)) {
|
|
225
|
-
throw new Error("Invalid youtube-playlists. Each item requires a positive integer channelId and playlistIds array.");
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const youtube = {
|
|
230
|
-
...(options.youtubeTags
|
|
231
|
-
? {
|
|
232
|
-
tags: parseCsv(options.youtubeTags).map((tag) => {
|
|
233
|
-
validateOptionalLength(tag, 255, "youtube-tag");
|
|
234
|
-
return tag;
|
|
235
|
-
}),
|
|
236
|
-
}
|
|
237
|
-
: {}),
|
|
218
|
+
if (options.youtubePlaylists) {
|
|
219
|
+
youtubePlaylists = parseJsonString(options.youtubePlaylists, "youtube-playlists");
|
|
220
|
+
if (!Array.isArray(youtubePlaylists)) {
|
|
221
|
+
throw new Error("Invalid youtube-playlists. Expected a JSON array.");
|
|
222
|
+
}
|
|
223
|
+
youtubePlaylists = youtubePlaylists.map((playlist) => ({
|
|
224
|
+
...playlist,
|
|
225
|
+
channelId: Number(playlist.channelId),
|
|
226
|
+
}));
|
|
227
|
+
if (youtubePlaylists.some((playlist) =>
|
|
228
|
+
!Number.isSafeInteger(playlist.channelId) ||
|
|
229
|
+
playlist.channelId < 1 ||
|
|
230
|
+
!Array.isArray(playlist.playlistIds)
|
|
231
|
+
)) {
|
|
232
|
+
throw new Error("Invalid youtube-playlists. Each item requires a positive integer channelId and playlistIds array.");
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const youtube = {
|
|
237
|
+
...(options.youtubeTags
|
|
238
|
+
? {
|
|
239
|
+
tags: parseCsv(options.youtubeTags).map((tag) => {
|
|
240
|
+
validateOptionalLength(tag, 255, "youtube-tag");
|
|
241
|
+
return tag;
|
|
242
|
+
}),
|
|
243
|
+
}
|
|
244
|
+
: {}),
|
|
238
245
|
...(youtubePlaylists ? { playlists: youtubePlaylists } : {}),
|
|
239
|
-
};
|
|
240
|
-
const googlemybusiness = {
|
|
241
|
-
...(parseBooleanStrict(
|
|
242
|
-
options.googlemybusinessUploadToPhotosSection,
|
|
243
|
-
"googlemybusiness-upload-to-photos-section",
|
|
244
|
-
) !== undefined
|
|
245
|
-
? {
|
|
246
|
-
uploadToPhotosSection: parseBooleanStrict(
|
|
247
|
-
options.googlemybusinessUploadToPhotosSection,
|
|
248
|
-
"googlemybusiness-upload-to-photos-section",
|
|
249
|
-
),
|
|
250
|
-
}
|
|
251
|
-
: {}),
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const hasPoll = Object.keys(poll).length > 0;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
246
|
+
};
|
|
247
|
+
const googlemybusiness = {
|
|
248
|
+
...(parseBooleanStrict(
|
|
249
|
+
options.googlemybusinessUploadToPhotosSection,
|
|
250
|
+
"googlemybusiness-upload-to-photos-section",
|
|
251
|
+
) !== undefined
|
|
252
|
+
? {
|
|
253
|
+
uploadToPhotosSection: parseBooleanStrict(
|
|
254
|
+
options.googlemybusinessUploadToPhotosSection,
|
|
255
|
+
"googlemybusiness-upload-to-photos-section",
|
|
256
|
+
),
|
|
257
|
+
}
|
|
258
|
+
: {}),
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const hasPoll = Object.keys(poll).length > 0;
|
|
262
|
+
const caption = !hasPoll
|
|
263
|
+
? validateRequiredString(options.caption, 3000, "caption")
|
|
264
|
+
: options.caption === undefined
|
|
265
|
+
? undefined
|
|
266
|
+
: String(options.caption).trim();
|
|
267
|
+
if (hasPoll && (!poll.question || poll.options?.length < 2 || !poll.period)) {
|
|
268
|
+
throw new Error("poll requires question, 2 to 4 options, and period.");
|
|
269
|
+
}
|
|
270
|
+
if (poll.options?.length > 4) {
|
|
271
|
+
throw new Error("Polls allow at most four options.");
|
|
272
|
+
}
|
|
264
273
|
|
|
265
274
|
return {
|
|
266
275
|
...(options.title ? { title: options.title } : {}),
|
|
267
|
-
...(
|
|
276
|
+
...(caption ? { caption } : {}),
|
|
268
277
|
...(options.alt ? { alt: options.alt } : {}),
|
|
269
278
|
...(options.link ? { link: validateHttpUrl(options.link, "link") } : {}),
|
|
270
|
-
publishMode,
|
|
279
|
+
publishMode,
|
|
271
280
|
scheduledAt,
|
|
281
|
+
...(parseBooleanStrict(options.autoThreadText, "auto-thread-text") !== undefined
|
|
282
|
+
? { autoThreadText: parseBooleanStrict(options.autoThreadText, "auto-thread-text") }
|
|
283
|
+
: {}),
|
|
272
284
|
...(parseBooleanStrict(options.postAsStory, "post-as-story") !== undefined
|
|
273
285
|
? { postAsStory: parseBooleanStrict(options.postAsStory, "post-as-story") }
|
|
274
286
|
: {}),
|
|
@@ -276,22 +288,22 @@ function buildPostPayload(options) {
|
|
|
276
288
|
? {
|
|
277
289
|
comment: {
|
|
278
290
|
comment: options.comment,
|
|
279
|
-
...(parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") !== undefined
|
|
280
|
-
? { delay: parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") }
|
|
291
|
+
...(parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") !== undefined
|
|
292
|
+
? { delay: parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") }
|
|
281
293
|
: {}),
|
|
282
294
|
},
|
|
283
295
|
}
|
|
284
296
|
: {}),
|
|
285
297
|
...(media.length > 0 ? { media } : {}),
|
|
286
298
|
...(Object.keys(poll).length > 0 ? { poll } : {}),
|
|
287
|
-
...(Object.keys(instagram).length > 0 || Object.keys(facebook).length > 0 || Object.keys(tiktok).length > 0 || Object.keys(youtube).length > 0 || Object.keys(googlemybusiness).length > 0
|
|
299
|
+
...(Object.keys(instagram).length > 0 || Object.keys(facebook).length > 0 || Object.keys(tiktok).length > 0 || Object.keys(youtube).length > 0 || Object.keys(googlemybusiness).length > 0
|
|
288
300
|
? {
|
|
289
301
|
platforms: {
|
|
290
302
|
...(Object.keys(instagram).length > 0 ? { instagram } : {}),
|
|
291
303
|
...(Object.keys(facebook).length > 0 ? { facebook } : {}),
|
|
292
304
|
...(Object.keys(tiktok).length > 0 ? { tiktok } : {}),
|
|
293
|
-
...(Object.keys(youtube).length > 0 ? { youtube } : {}),
|
|
294
|
-
...(Object.keys(googlemybusiness).length > 0 ? { googlemybusiness } : {}),
|
|
305
|
+
...(Object.keys(youtube).length > 0 ? { youtube } : {}),
|
|
306
|
+
...(Object.keys(googlemybusiness).length > 0 ? { googlemybusiness } : {}),
|
|
295
307
|
},
|
|
296
308
|
}
|
|
297
309
|
: {}),
|
|
@@ -303,7 +315,7 @@ export {
|
|
|
303
315
|
mergeMedia,
|
|
304
316
|
parseBoolean,
|
|
305
317
|
parseCsv,
|
|
306
|
-
parseNumber,
|
|
307
|
-
validateRawPostPayload,
|
|
308
|
-
VALID_PUBLISH_MODES,
|
|
309
|
-
};
|
|
318
|
+
parseNumber,
|
|
319
|
+
validateRawPostPayload,
|
|
320
|
+
VALID_PUBLISH_MODES,
|
|
321
|
+
};
|