@nuelink/nuelink-cli 1.4.1 → 1.4.3
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/README.md +47 -41
- package/package.json +31 -30
- package/src/commands.js +194 -142
- package/src/http.js +126 -126
- package/src/postPayload.js +26 -20
- package/src/validation.js +94 -50
- package/test/cli.test.mjs +320 -219
package/src/http.js
CHANGED
|
@@ -2,10 +2,10 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { CliError } from "./errors.js";
|
|
4
4
|
import { printApiResponse } from "./output.js";
|
|
5
|
-
|
|
6
|
-
const require = createRequire(import.meta.url);
|
|
7
|
-
const { version: cliVersion } = require("../package.json");
|
|
8
|
-
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const { version: cliVersion } = require("../package.json");
|
|
8
|
+
|
|
9
9
|
const DEFAULT_API_BASE_URL = "https://app.nuelink.com/api/public/v1";
|
|
10
10
|
|
|
11
11
|
function readIntegerEnvironment(name, fallback, { minimum = 0, maximum = 600000 } = {}) {
|
|
@@ -34,25 +34,25 @@ function getRequestConfiguration() {
|
|
|
34
34
|
retryMaxDelayMs: readIntegerEnvironment("NUELINK_RETRY_MAX_MS", 2000),
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
function pathParam(value, label) {
|
|
39
|
-
const stringValue = String(value ?? "").trim();
|
|
40
|
-
|
|
41
|
-
if (!stringValue) {
|
|
42
|
-
throw new Error(`Missing required ${label}.`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return encodeURIComponent(stringValue);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function brandPath(brandId) {
|
|
49
|
-
return `/brands/${pathParam(brandId, "brand-id")}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function collectionPath(brandId, collectionId) {
|
|
53
|
-
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
54
|
-
}
|
|
55
|
-
|
|
37
|
+
|
|
38
|
+
function pathParam(value, label) {
|
|
39
|
+
const stringValue = String(value ?? "").trim();
|
|
40
|
+
|
|
41
|
+
if (!stringValue) {
|
|
42
|
+
throw new Error(`Missing required ${label}.`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return encodeURIComponent(stringValue);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function brandPath(brandId) {
|
|
49
|
+
return `/brands/${pathParam(brandId, "brand-id")}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function collectionPath(brandId, collectionId) {
|
|
53
|
+
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
56
|
function buildApiUrl(endpoint, apiBaseUrl) {
|
|
57
57
|
try {
|
|
58
58
|
return new URL(endpoint.replace(/^\//, ""), `${apiBaseUrl.replace(/\/?$/, "/")}`);
|
|
@@ -66,91 +66,91 @@ function shouldRetryRequest(method, statusCode, attempt, maxGetRetries) {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
function parseRetryAfterMs(headers, attempt, retryBaseDelayMs, retryMaxDelayMs) {
|
|
69
|
-
const retryAfter = headers.get("retry-after");
|
|
70
|
-
|
|
71
|
-
if (retryAfter) {
|
|
72
|
-
const seconds = Number(retryAfter);
|
|
73
|
-
|
|
74
|
-
if (!Number.isNaN(seconds)) {
|
|
75
|
-
return Math.max(seconds * 1000, 0);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const dateDelay = Date.parse(retryAfter) - Date.now();
|
|
79
|
-
if (!Number.isNaN(dateDelay)) {
|
|
80
|
-
return Math.max(dateDelay, 0);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
69
|
+
const retryAfter = headers.get("retry-after");
|
|
70
|
+
|
|
71
|
+
if (retryAfter) {
|
|
72
|
+
const seconds = Number(retryAfter);
|
|
73
|
+
|
|
74
|
+
if (!Number.isNaN(seconds)) {
|
|
75
|
+
return Math.max(seconds * 1000, 0);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const dateDelay = Date.parse(retryAfter) - Date.now();
|
|
79
|
+
if (!Number.isNaN(dateDelay)) {
|
|
80
|
+
return Math.max(dateDelay, 0);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
84
|
const exponentialDelay = Math.min(retryBaseDelayMs * (2 ** attempt), retryMaxDelayMs);
|
|
85
|
-
return Math.floor(Math.random() * (exponentialDelay + 1));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function sleep(ms) {
|
|
89
|
-
if (ms <= 0) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function printDryRun({ method, url, query, body, multipart, dryRunBody }) {
|
|
97
|
-
console.log(JSON.stringify({
|
|
98
|
-
dryRun: true,
|
|
99
|
-
method,
|
|
100
|
-
url: url.toString(),
|
|
101
|
-
...(query ? { query } : {}),
|
|
102
|
-
...(body !== undefined ? { body } : {}),
|
|
103
|
-
...(multipart ? { multipart: dryRunBody || true } : {}),
|
|
104
|
-
}, null, 2));
|
|
105
|
-
}
|
|
106
|
-
|
|
85
|
+
return Math.floor(Math.random() * (exponentialDelay + 1));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function sleep(ms) {
|
|
89
|
+
if (ms <= 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function printDryRun({ method, url, query, body, multipart, dryRunBody }) {
|
|
97
|
+
console.log(JSON.stringify({
|
|
98
|
+
dryRun: true,
|
|
99
|
+
method,
|
|
100
|
+
url: url.toString(),
|
|
101
|
+
...(query ? { query } : {}),
|
|
102
|
+
...(body !== undefined ? { body } : {}),
|
|
103
|
+
...(multipart ? { multipart: dryRunBody || true } : {}),
|
|
104
|
+
}, null, 2));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
107
|
async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, multipart, outputMode = "json", quiet = false, dryRun = false, dryRunBody }) {
|
|
108
108
|
const configuration = getRequestConfiguration();
|
|
109
109
|
const url = buildApiUrl(endpoint, configuration.apiBaseUrl);
|
|
110
|
-
|
|
111
|
-
if (query) {
|
|
112
|
-
Object.entries(query).forEach(([key, value]) => {
|
|
113
|
-
if (value !== undefined && value !== null && value !== "") {
|
|
114
|
-
url.searchParams.set(key, String(value));
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (dryRun) {
|
|
120
|
-
printDryRun({ method, url, query, body, multipart, dryRunBody });
|
|
121
|
-
return { dryRun: true };
|
|
122
|
-
}
|
|
123
|
-
|
|
110
|
+
|
|
111
|
+
if (query) {
|
|
112
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
113
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
114
|
+
url.searchParams.set(key, String(value));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (dryRun) {
|
|
120
|
+
printDryRun({ method, url, query, body, multipart, dryRunBody });
|
|
121
|
+
return { dryRun: true };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
124
|
if (!apiKey) {
|
|
125
125
|
throw new CliError(
|
|
126
126
|
"AUTH_MISSING",
|
|
127
127
|
"Missing API key. Use --api-key <api_key> for a single command or auth:login to save one.",
|
|
128
128
|
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const headers = {
|
|
132
|
-
Authorization: `Bearer ${apiKey}`,
|
|
133
|
-
Accept: "application/json",
|
|
134
|
-
"User-Agent": `nuelink-cli/${cliVersion}`,
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const init = {
|
|
138
|
-
method,
|
|
139
|
-
headers,
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
if (multipart) {
|
|
143
|
-
init.body = multipart;
|
|
144
|
-
} else if (body !== undefined) {
|
|
145
|
-
headers["Content-Type"] = "application/json";
|
|
146
|
-
init.body = JSON.stringify(body);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
let response;
|
|
151
|
-
|
|
152
|
-
for (let attempt = 0; ; attempt += 1) {
|
|
153
|
-
response = await fetch(url, {
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const headers = {
|
|
132
|
+
Authorization: `Bearer ${apiKey}`,
|
|
133
|
+
Accept: "application/json",
|
|
134
|
+
"User-Agent": `nuelink-cli/${cliVersion}`,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const init = {
|
|
138
|
+
method,
|
|
139
|
+
headers,
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (multipart) {
|
|
143
|
+
init.body = multipart;
|
|
144
|
+
} else if (body !== undefined) {
|
|
145
|
+
headers["Content-Type"] = "application/json";
|
|
146
|
+
init.body = JSON.stringify(body);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
let response;
|
|
151
|
+
|
|
152
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
153
|
+
response = await fetch(url, {
|
|
154
154
|
...init,
|
|
155
155
|
signal: AbortSignal.timeout(configuration.requestTimeoutMs),
|
|
156
156
|
});
|
|
@@ -165,13 +165,13 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
165
165
|
configuration.retryBaseDelayMs,
|
|
166
166
|
configuration.retryMaxDelayMs,
|
|
167
167
|
));
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const text = await response.text();
|
|
171
|
-
let data = {};
|
|
172
|
-
|
|
173
|
-
if (text) {
|
|
174
|
-
try {
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const text = await response.text();
|
|
171
|
+
let data = {};
|
|
172
|
+
|
|
173
|
+
if (text) {
|
|
174
|
+
try {
|
|
175
175
|
data = JSON.parse(text);
|
|
176
176
|
} catch {
|
|
177
177
|
if (response.ok) {
|
|
@@ -180,25 +180,25 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
180
180
|
`Protocol error: expected a JSON response (HTTP ${response.status}).`,
|
|
181
181
|
{ httpStatus: response.status },
|
|
182
182
|
);
|
|
183
|
-
}
|
|
184
|
-
data = { raw: text };
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
183
|
+
}
|
|
184
|
+
data = { raw: text };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
188
|
if (!response.ok) {
|
|
189
189
|
throw new CliError(
|
|
190
190
|
"HTTP_ERROR",
|
|
191
191
|
`Request failed (${response.status} ${response.statusText})`,
|
|
192
192
|
{ httpStatus: response.status, details: data },
|
|
193
193
|
);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (outputMode !== "json" && !quiet) {
|
|
197
|
-
console.log(chalk.green(`OK ${method} ${url.pathname}`));
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
printApiResponse(data, { outputMode });
|
|
201
|
-
return data;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (outputMode !== "json" && !quiet) {
|
|
197
|
+
console.log(chalk.green(`OK ${method} ${url.pathname}`));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
printApiResponse(data, { outputMode });
|
|
201
|
+
return data;
|
|
202
202
|
} catch (error) {
|
|
203
203
|
if (error instanceof CliError) throw error;
|
|
204
204
|
|
|
@@ -212,10 +212,10 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
212
212
|
throw new CliError("NETWORK_ERROR", `Request error: ${error.message}`);
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
|
|
216
|
-
export {
|
|
217
|
-
brandPath,
|
|
218
|
-
collectionPath,
|
|
219
|
-
nuelinkRequest,
|
|
220
|
-
pathParam,
|
|
221
|
-
};
|
|
215
|
+
|
|
216
|
+
export {
|
|
217
|
+
brandPath,
|
|
218
|
+
collectionPath,
|
|
219
|
+
nuelinkRequest,
|
|
220
|
+
pathParam,
|
|
221
|
+
};
|
package/src/postPayload.js
CHANGED
|
@@ -4,9 +4,9 @@ import {
|
|
|
4
4
|
parseOptionalNonNegativeInteger,
|
|
5
5
|
splitCsv,
|
|
6
6
|
validateHttpUrl,
|
|
7
|
-
validatePollOptions,
|
|
8
|
-
validateRequiredString,
|
|
9
|
-
validateScheduledAt,
|
|
7
|
+
validatePollOptions,
|
|
8
|
+
validateRequiredString,
|
|
9
|
+
validateScheduledAt,
|
|
10
10
|
} from "./validation.js";
|
|
11
11
|
|
|
12
12
|
const VALID_PUBLISH_MODES = ["DRAFT", "QUEUE", "SCHEDULE", "IMMEDIATE"];
|
|
@@ -80,16 +80,19 @@ function validateRawPostPayload(payload) {
|
|
|
80
80
|
validateOptionalLength(payload.title, 255, "title");
|
|
81
81
|
validateOptionalLength(payload.caption, 3000, "caption");
|
|
82
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
|
+
}
|
|
83
86
|
if (payload.link !== undefined) {
|
|
84
87
|
validateHttpUrl(payload.link, "link");
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
const poll = payload.poll;
|
|
88
|
-
const caption = !poll
|
|
89
|
-
? validateRequiredString(payload.caption, 3000, "caption")
|
|
90
|
-
: payload.caption === undefined
|
|
91
|
-
? undefined
|
|
92
|
-
: String(payload.caption).trim();
|
|
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();
|
|
93
96
|
if (poll !== undefined) {
|
|
94
97
|
if (!poll || typeof poll !== "object" || Array.isArray(poll)) {
|
|
95
98
|
throw new Error("Invalid poll. Expected a JSON object.");
|
|
@@ -123,15 +126,15 @@ function validateRawPostPayload(payload) {
|
|
|
123
126
|
});
|
|
124
127
|
}
|
|
125
128
|
|
|
126
|
-
return {
|
|
127
|
-
...payload,
|
|
128
|
-
...(payload.caption !== undefined ? { caption } : {}),
|
|
129
|
-
publishMode,
|
|
129
|
+
return {
|
|
130
|
+
...payload,
|
|
131
|
+
...(payload.caption !== undefined ? { caption } : {}),
|
|
132
|
+
publishMode,
|
|
130
133
|
...(payload.scheduledAt !== undefined ? { scheduledAt } : {}),
|
|
131
134
|
};
|
|
132
135
|
}
|
|
133
136
|
|
|
134
|
-
function buildPostPayload(options) {
|
|
137
|
+
function buildPostPayload(options) {
|
|
135
138
|
const media = mergeMedia(options.mediaIds, options.mediaUrls);
|
|
136
139
|
const publishMode = normalizePublishMode(options.publishMode);
|
|
137
140
|
const scheduledAt = validateScheduledAt(options.scheduledAt);
|
|
@@ -256,11 +259,11 @@ function buildPostPayload(options) {
|
|
|
256
259
|
};
|
|
257
260
|
|
|
258
261
|
const hasPoll = Object.keys(poll).length > 0;
|
|
259
|
-
const caption = !hasPoll
|
|
260
|
-
? validateRequiredString(options.caption, 3000, "caption")
|
|
261
|
-
: options.caption === undefined
|
|
262
|
-
? undefined
|
|
263
|
-
: String(options.caption).trim();
|
|
262
|
+
const caption = !hasPoll
|
|
263
|
+
? validateRequiredString(options.caption, 3000, "caption")
|
|
264
|
+
: options.caption === undefined
|
|
265
|
+
? undefined
|
|
266
|
+
: String(options.caption).trim();
|
|
264
267
|
if (hasPoll && (!poll.question || poll.options?.length < 2 || !poll.period)) {
|
|
265
268
|
throw new Error("poll requires question, 2 to 4 options, and period.");
|
|
266
269
|
}
|
|
@@ -270,11 +273,14 @@ function buildPostPayload(options) {
|
|
|
270
273
|
|
|
271
274
|
return {
|
|
272
275
|
...(options.title ? { title: options.title } : {}),
|
|
273
|
-
...(caption ? { caption } : {}),
|
|
276
|
+
...(caption ? { caption } : {}),
|
|
274
277
|
...(options.alt ? { alt: options.alt } : {}),
|
|
275
278
|
...(options.link ? { link: validateHttpUrl(options.link, "link") } : {}),
|
|
276
279
|
publishMode,
|
|
277
280
|
scheduledAt,
|
|
281
|
+
...(parseBooleanStrict(options.autoThreadText, "auto-thread-text") !== undefined
|
|
282
|
+
? { autoThreadText: parseBooleanStrict(options.autoThreadText, "auto-thread-text") }
|
|
283
|
+
: {}),
|
|
278
284
|
...(parseBooleanStrict(options.postAsStory, "post-as-story") !== undefined
|
|
279
285
|
? { postAsStory: parseBooleanStrict(options.postAsStory, "post-as-story") }
|
|
280
286
|
: {}),
|
package/src/validation.js
CHANGED
|
@@ -32,20 +32,29 @@ const VALID_AUTOMATION_SUB_TYPES = [
|
|
|
32
32
|
];
|
|
33
33
|
const VALID_IMPORT_AS_TYPES = ["LINK", "IMAGE", "VIDEO", "CAROUSEL"];
|
|
34
34
|
const VALID_MEDIA_FILTER_TYPES = ["IMAGE", "VIDEO", "GIF", "APPLICATION", "DOCUMENT", "CSV"];
|
|
35
|
+
const VALID_POST_STATUSES = ["PENDING", "DRAFT", "PUBLISHED"];
|
|
36
|
+
const VALID_POST_TYPES = [
|
|
37
|
+
"TEXT", "IMAGE", "VIDEO", "MULTIMEDIA", "SHORT", "DOCUMENT", "THREAD",
|
|
38
|
+
"SLIDES", "REEL", "TIKTOK", "STORY", "POLL", "ENGAGE_COMMENT",
|
|
39
|
+
];
|
|
40
|
+
const VALID_POSTING_TYPES = ["NOW", "SCHEDULE", "QUEUE", "DRAFT"];
|
|
41
|
+
const VALID_POST_SORT_FIELDS = ["id", "title", "post_date", "created_at", "updated_at"];
|
|
42
|
+
const VALID_SORT_ORDERS = ["asc", "desc"];
|
|
35
43
|
const SUPPORTED_MEDIA_TYPES = new Map([
|
|
36
44
|
[".jpg", "image/jpeg"],
|
|
37
45
|
[".jpeg", "image/jpeg"],
|
|
38
46
|
[".png", "image/png"],
|
|
39
|
-
[".
|
|
40
|
-
[".webp", "image/webp"],
|
|
47
|
+
[".bmp", "image/bmp"],
|
|
41
48
|
[".mp4", "video/mp4"],
|
|
42
49
|
[".mov", "video/quicktime"],
|
|
50
|
+
[".pdf", "application/pdf"],
|
|
43
51
|
]);
|
|
44
|
-
const DEFAULT_MAX_MEDIA_SIZE_BYTES = 100 * 1024 * 1024;
|
|
52
|
+
const DEFAULT_MAX_MEDIA_SIZE_BYTES = 100 * 1024 * 1024;
|
|
53
|
+
const MAX_MEDIA_SIZE_BYTES = 100 * 1024 * 1024;
|
|
45
54
|
const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
|
|
46
55
|
const SQL_DATETIME_PATTERN = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
|
47
56
|
|
|
48
|
-
function splitCsv(value) {
|
|
57
|
+
function splitCsv(value) {
|
|
49
58
|
if (!value) {
|
|
50
59
|
return [];
|
|
51
60
|
}
|
|
@@ -54,40 +63,40 @@ function splitCsv(value) {
|
|
|
54
63
|
.split(",")
|
|
55
64
|
.map((item) => item.trim())
|
|
56
65
|
.filter(Boolean);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function validateRequiredString(value, maximum, label) {
|
|
60
|
-
const normalized = String(value ?? "").trim();
|
|
61
|
-
|
|
62
|
-
if (!normalized) {
|
|
63
|
-
throw new Error(`${label} must not be blank.`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (normalized.length > maximum) {
|
|
67
|
-
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return normalized;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function readPositiveIntegerEnvironment(name, fallback) {
|
|
74
|
-
const raw = process.env[name];
|
|
75
|
-
if (raw === undefined || raw === "") {
|
|
76
|
-
return fallback;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const normalized = String(raw).trim();
|
|
80
|
-
if (!/^\d+$/.test(normalized)) {
|
|
81
|
-
throw new Error(`${name} must be a positive integer.`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const parsed = Number(normalized);
|
|
85
|
-
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
86
|
-
throw new Error(`${name} must be a positive integer.`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return parsed;
|
|
90
|
-
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function validateRequiredString(value, maximum, label) {
|
|
69
|
+
const normalized = String(value ?? "").trim();
|
|
70
|
+
|
|
71
|
+
if (!normalized) {
|
|
72
|
+
throw new Error(`${label} must not be blank.`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (normalized.length > maximum) {
|
|
76
|
+
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return normalized;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function readPositiveIntegerEnvironment(name, fallback) {
|
|
83
|
+
const raw = process.env[name];
|
|
84
|
+
if (raw === undefined || raw === "") {
|
|
85
|
+
return fallback;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const normalized = String(raw).trim();
|
|
89
|
+
if (!/^\d+$/.test(normalized)) {
|
|
90
|
+
throw new Error(`${name} must be a positive integer.`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const parsed = Number(normalized);
|
|
94
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
95
|
+
throw new Error(`${name} must be a positive integer.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return parsed;
|
|
99
|
+
}
|
|
91
100
|
|
|
92
101
|
function normalizeEnum(value, label, allowedValues) {
|
|
93
102
|
const normalized = String(value ?? "").trim().toUpperCase();
|
|
@@ -224,6 +233,34 @@ function validateScheduledAt(value) {
|
|
|
224
233
|
return normalized;
|
|
225
234
|
}
|
|
226
235
|
|
|
236
|
+
function validatePostDateTime(value, label) {
|
|
237
|
+
if (value === undefined || value === null || value === "") {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const normalized = String(value).trim();
|
|
242
|
+
if (!SQL_DATETIME_PATTERN.test(normalized)) {
|
|
243
|
+
throw new Error(`Invalid ${label}. Expected YYYY-MM-DD HH:mm:ss.`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const [datePart, timePart] = normalized.split(" ");
|
|
247
|
+
const [year, month, day] = datePart.split("-").map(Number);
|
|
248
|
+
const [hours, minutes, seconds] = timePart.split(":").map(Number);
|
|
249
|
+
const date = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));
|
|
250
|
+
const isValidDate = date.getUTCFullYear() === year
|
|
251
|
+
&& date.getUTCMonth() === month - 1
|
|
252
|
+
&& date.getUTCDate() === day
|
|
253
|
+
&& date.getUTCHours() === hours
|
|
254
|
+
&& date.getUTCMinutes() === minutes
|
|
255
|
+
&& date.getUTCSeconds() === seconds;
|
|
256
|
+
|
|
257
|
+
if (!isValidDate) {
|
|
258
|
+
throw new Error(`Invalid ${label}. Expected a real calendar date and time.`);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return normalized;
|
|
262
|
+
}
|
|
263
|
+
|
|
227
264
|
function validatePollOptions(options, correctOptionIndex) {
|
|
228
265
|
if (options.length === 1) {
|
|
229
266
|
throw new Error("Polls require at least two options.");
|
|
@@ -242,12 +279,13 @@ function parseJsonString(value, label) {
|
|
|
242
279
|
}
|
|
243
280
|
}
|
|
244
281
|
|
|
245
|
-
function validateMediaFile(filePath, fileStat) {
|
|
246
|
-
const
|
|
247
|
-
"NUELINK_MAX_MEDIA_SIZE_BYTES",
|
|
248
|
-
DEFAULT_MAX_MEDIA_SIZE_BYTES,
|
|
249
|
-
);
|
|
250
|
-
|
|
282
|
+
function validateMediaFile(filePath, fileStat) {
|
|
283
|
+
const configuredMaximum = readPositiveIntegerEnvironment(
|
|
284
|
+
"NUELINK_MAX_MEDIA_SIZE_BYTES",
|
|
285
|
+
DEFAULT_MAX_MEDIA_SIZE_BYTES,
|
|
286
|
+
);
|
|
287
|
+
const maximumSize = Math.min(configuredMaximum, MAX_MEDIA_SIZE_BYTES);
|
|
288
|
+
|
|
251
289
|
if (!fileStat.isFile()) {
|
|
252
290
|
throw new Error("Invalid file. Expected a regular file path.");
|
|
253
291
|
}
|
|
@@ -256,8 +294,8 @@ function validateMediaFile(filePath, fileStat) {
|
|
|
256
294
|
throw new Error("Invalid file. File must not be empty.");
|
|
257
295
|
}
|
|
258
296
|
|
|
259
|
-
if (fileStat.size > maximumSize) {
|
|
260
|
-
throw new Error(`Invalid file. File exceeds ${maximumSize} bytes.`);
|
|
297
|
+
if (fileStat.size > maximumSize) {
|
|
298
|
+
throw new Error(`Invalid file. File exceeds ${maximumSize} bytes.`);
|
|
261
299
|
}
|
|
262
300
|
|
|
263
301
|
const extension = path.extname(filePath).toLowerCase();
|
|
@@ -278,6 +316,11 @@ export {
|
|
|
278
316
|
VALID_AUTOMATION_TYPES,
|
|
279
317
|
VALID_IMPORT_AS_TYPES,
|
|
280
318
|
VALID_MEDIA_FILTER_TYPES,
|
|
319
|
+
VALID_POST_STATUSES,
|
|
320
|
+
VALID_POST_TYPES,
|
|
321
|
+
VALID_POSTING_TYPES,
|
|
322
|
+
VALID_POST_SORT_FIELDS,
|
|
323
|
+
VALID_SORT_ORDERS,
|
|
281
324
|
parseBooleanStrict,
|
|
282
325
|
parseIntegerInRange,
|
|
283
326
|
parseJsonString,
|
|
@@ -287,9 +330,10 @@ export {
|
|
|
287
330
|
splitCsv,
|
|
288
331
|
validateHttpUrl,
|
|
289
332
|
validateMediaFile,
|
|
333
|
+
validatePostDateTime,
|
|
290
334
|
validatePollOptions,
|
|
291
335
|
validateScheduledAt,
|
|
292
|
-
validateQueueSlots,
|
|
293
|
-
validateRequiredString,
|
|
294
|
-
normalizeEnum,
|
|
295
|
-
};
|
|
336
|
+
validateQueueSlots,
|
|
337
|
+
validateRequiredString,
|
|
338
|
+
normalizeEnum,
|
|
339
|
+
};
|