@nuelink/nuelink-cli 1.2.0 → 1.3.1
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 +61 -12
- package/package.json +1 -1
- package/src/cli.js +11 -12
- package/src/commands.js +252 -79
- package/src/config.js +9 -4
- package/src/http.js +87 -14
- package/src/output.js +183 -0
- package/src/postPayload.js +54 -24
- package/src/validation.js +186 -0
package/src/commands.js
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
+
import { openAsBlob } from "node:fs";
|
|
1
2
|
import { readFile, stat } from "node:fs/promises";
|
|
3
|
+
import readline from "node:readline";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import chalk from "chalk";
|
|
4
6
|
|
|
5
|
-
import { clearApiKey, maskToken, readConfig, resolveApiKey, CONFIG_FILE, decryptApiKey } from "./config.js";
|
|
7
|
+
import { clearApiKey, maskToken, readConfig, resolveApiKey, CONFIG_FILE, decryptApiKey, saveApiKey } from "./config.js";
|
|
6
8
|
import { brandPath, collectionPath, nuelinkRequest } from "./http.js";
|
|
7
|
-
import { buildPostPayload, parseCsv } from "./postPayload.js";
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
import { buildPostPayload, parseCsv, VALID_PUBLISH_MODES } from "./postPayload.js";
|
|
10
|
+
import {
|
|
11
|
+
VALID_AUTOMATION_SUB_TYPES,
|
|
12
|
+
VALID_AUTOMATION_TYPES,
|
|
13
|
+
VALID_IMPORT_AS_TYPES,
|
|
14
|
+
normalizeEnum,
|
|
15
|
+
parseJsonString,
|
|
16
|
+
parseOptionalPositiveInteger,
|
|
17
|
+
parsePositiveInteger,
|
|
18
|
+
splitCsv,
|
|
19
|
+
validateHttpUrl,
|
|
20
|
+
validateMediaFile,
|
|
21
|
+
validateQueueSlots,
|
|
22
|
+
} from "./validation.js";
|
|
23
|
+
|
|
24
|
+
function extractApiKeyArg(args) {
|
|
10
25
|
const sanitizedArgs = [];
|
|
11
26
|
let apiKey;
|
|
12
27
|
|
|
13
28
|
for (let index = 0; index < args.length; index += 1) {
|
|
14
29
|
const arg = args[index];
|
|
15
30
|
|
|
16
|
-
if (arg === "--auth" || arg === "-a") {
|
|
31
|
+
if (arg === "--api-key" || arg === "--auth" || arg === "-a") {
|
|
17
32
|
const next = args[index + 1];
|
|
18
33
|
if (next && !next.startsWith("-")) {
|
|
19
34
|
apiKey = next;
|
|
@@ -22,6 +37,11 @@ function extractAuthArg(args) {
|
|
|
22
37
|
}
|
|
23
38
|
}
|
|
24
39
|
|
|
40
|
+
if (arg.startsWith("--api-key=")) {
|
|
41
|
+
apiKey = arg.slice("--api-key=".length);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
25
45
|
if (arg.startsWith("--auth=")) {
|
|
26
46
|
apiKey = arg.slice("--auth=".length);
|
|
27
47
|
continue;
|
|
@@ -33,6 +53,122 @@ function extractAuthArg(args) {
|
|
|
33
53
|
return { apiKey, sanitizedArgs };
|
|
34
54
|
}
|
|
35
55
|
|
|
56
|
+
async function readStdinText() {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
let payload = "";
|
|
59
|
+
|
|
60
|
+
process.stdin.setEncoding("utf8");
|
|
61
|
+
process.stdin.on("data", (chunk) => {
|
|
62
|
+
payload += chunk;
|
|
63
|
+
});
|
|
64
|
+
process.stdin.on("end", () => {
|
|
65
|
+
resolve(payload.trim());
|
|
66
|
+
});
|
|
67
|
+
process.stdin.on("error", reject);
|
|
68
|
+
process.stdin.resume();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function promptHidden(promptText) {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const stdin = process.stdin;
|
|
75
|
+
const stdout = process.stdout;
|
|
76
|
+
const wasRaw = stdin.isTTY ? stdin.isRaw : false;
|
|
77
|
+
let value = "";
|
|
78
|
+
let settled = false;
|
|
79
|
+
|
|
80
|
+
const onError = (error) => {
|
|
81
|
+
cleanup();
|
|
82
|
+
reject(error);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const cleanup = () => {
|
|
86
|
+
if (settled) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
settled = true;
|
|
91
|
+
stdin.removeListener("keypress", onKeypress);
|
|
92
|
+
stdin.removeListener("error", onError);
|
|
93
|
+
|
|
94
|
+
if (stdin.isTTY) {
|
|
95
|
+
stdin.setRawMode(Boolean(wasRaw));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
stdin.pause();
|
|
99
|
+
|
|
100
|
+
stdout.write("\n");
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const onKeypress = (character, key) => {
|
|
104
|
+
if (key?.ctrl && key.name === "c") {
|
|
105
|
+
cleanup();
|
|
106
|
+
reject(new Error("Authentication cancelled."));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (key?.name === "return" || key?.name === "enter") {
|
|
111
|
+
cleanup();
|
|
112
|
+
resolve(value.trim());
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (key?.name === "backspace") {
|
|
117
|
+
value = value.slice(0, -1);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (character) {
|
|
122
|
+
value += character;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
stdout.write(promptText);
|
|
127
|
+
readline.emitKeypressEvents(stdin);
|
|
128
|
+
|
|
129
|
+
if (stdin.isTTY) {
|
|
130
|
+
stdin.setRawMode(true);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
stdin.resume();
|
|
134
|
+
stdin.on("error", onError);
|
|
135
|
+
stdin.on("keypress", onKeypress);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function resolveLoginApiKey(options) {
|
|
140
|
+
if (options.apiKey) {
|
|
141
|
+
return options.apiKey.trim();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (options.stdin || !process.stdin.isTTY) {
|
|
145
|
+
return readStdinText();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return promptHidden("API key: ");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function buildRequestOptions(program) {
|
|
152
|
+
const options = program.opts();
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
dryRun: Boolean(options.dryRun),
|
|
156
|
+
outputMode: options.json ? "json" : "table",
|
|
157
|
+
quiet: Boolean(options.quiet),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function executeRequest(program, runtimeApiKey, request) {
|
|
162
|
+
const requestOptions = buildRequestOptions(program);
|
|
163
|
+
const apiKey = requestOptions.dryRun ? undefined : await resolveApiKey(runtimeApiKey);
|
|
164
|
+
|
|
165
|
+
return nuelinkRequest({
|
|
166
|
+
apiKey,
|
|
167
|
+
...request,
|
|
168
|
+
...requestOptions,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
36
172
|
function withCommandErrorHandling(handler) {
|
|
37
173
|
return async (...args) => {
|
|
38
174
|
try {
|
|
@@ -44,12 +180,33 @@ function withCommandErrorHandling(handler) {
|
|
|
44
180
|
};
|
|
45
181
|
}
|
|
46
182
|
|
|
47
|
-
function registerCommands(program, runtimeApiKey) {
|
|
183
|
+
function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
48
184
|
program
|
|
49
185
|
.name("nuelink-cli")
|
|
50
|
-
.version(
|
|
186
|
+
.version(cliVersion)
|
|
51
187
|
.description("Nuelink CLI for the Public API")
|
|
52
|
-
.option("-a, --
|
|
188
|
+
.option("-a, --api-key <apiKey>", "Use API key for this invocation only")
|
|
189
|
+
.option("--auth <apiKey>", "Deprecated alias for --api-key")
|
|
190
|
+
.option("--dry-run", "Print the validated request without sending it")
|
|
191
|
+
.option("--json", "Output raw JSON instead of tables for list commands")
|
|
192
|
+
.option("--quiet", "Suppress non-essential success banners");
|
|
193
|
+
|
|
194
|
+
program
|
|
195
|
+
.command("auth:login")
|
|
196
|
+
.description("Persist an API key to local config")
|
|
197
|
+
.option("--api-key <apiKey>", "API key to save (prefer stdin or interactive prompt)")
|
|
198
|
+
.option("--stdin", "Read API key from stdin")
|
|
199
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
200
|
+
const apiKey = await resolveLoginApiKey(options);
|
|
201
|
+
|
|
202
|
+
if (!apiKey) {
|
|
203
|
+
throw new Error("Missing API key. Provide --stdin, pipe a key to auth:login, or enter it interactively.");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
process.env.NUELINK_API_KEY = apiKey;
|
|
207
|
+
await saveApiKey(apiKey);
|
|
208
|
+
console.log(chalk.green("API key saved to local config."));
|
|
209
|
+
}));
|
|
53
210
|
|
|
54
211
|
program
|
|
55
212
|
.command("auth:clear")
|
|
@@ -63,13 +220,13 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
63
220
|
.command("auth:status")
|
|
64
221
|
.description("Show where auth is currently resolved from")
|
|
65
222
|
.action(withCommandErrorHandling(async () => {
|
|
66
|
-
if (
|
|
67
|
-
console.log(chalk.green(`Auth source:
|
|
223
|
+
if (process.env.NUELINK_API_KEY) {
|
|
224
|
+
console.log(chalk.green(`Auth source: NUELINK_API_KEY (${maskToken(process.env.NUELINK_API_KEY)})`));
|
|
68
225
|
return;
|
|
69
226
|
}
|
|
70
227
|
|
|
71
|
-
if (
|
|
72
|
-
console.log(chalk.green(`Auth source:
|
|
228
|
+
if (runtimeApiKey) {
|
|
229
|
+
console.log(chalk.green(`Auth source: runtime CLI flag (${maskToken(runtimeApiKey)})`));
|
|
73
230
|
return;
|
|
74
231
|
}
|
|
75
232
|
|
|
@@ -89,15 +246,14 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
89
246
|
return;
|
|
90
247
|
}
|
|
91
248
|
|
|
92
|
-
console.log(chalk.yellow("No API key configured.
|
|
249
|
+
console.log(chalk.yellow("No API key configured. Use auth:login to save one, or pass --api-key for a single command."));
|
|
93
250
|
}));
|
|
94
251
|
|
|
95
252
|
program
|
|
96
253
|
.command("me")
|
|
97
254
|
.description("Get current profile")
|
|
98
255
|
.action(withCommandErrorHandling(async () => {
|
|
99
|
-
|
|
100
|
-
await nuelinkRequest({ apiKey, endpoint: "/me" });
|
|
256
|
+
await executeRequest(program, runtimeApiKey, { endpoint: "/me" });
|
|
101
257
|
}));
|
|
102
258
|
|
|
103
259
|
program
|
|
@@ -106,13 +262,11 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
106
262
|
.option("--per-page <number>", "Items per page", "25")
|
|
107
263
|
.option("--page <number>", "Page number", "1")
|
|
108
264
|
.action(withCommandErrorHandling(async (options) => {
|
|
109
|
-
|
|
110
|
-
await nuelinkRequest({
|
|
111
|
-
apiKey,
|
|
265
|
+
await executeRequest(program, runtimeApiKey, {
|
|
112
266
|
endpoint: "/brands",
|
|
113
267
|
query: {
|
|
114
|
-
per_page: options.perPage,
|
|
115
|
-
page: options.page,
|
|
268
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
269
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
116
270
|
},
|
|
117
271
|
});
|
|
118
272
|
}));
|
|
@@ -124,13 +278,11 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
124
278
|
.option("--per-page <number>", "Items per page", "25")
|
|
125
279
|
.option("--page <number>", "Page number", "1")
|
|
126
280
|
.action(withCommandErrorHandling(async (options) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
apiKey,
|
|
130
|
-
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
281
|
+
await executeRequest(program, runtimeApiKey, {
|
|
282
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
131
283
|
query: {
|
|
132
|
-
per_page: options.perPage,
|
|
133
|
-
page: options.page,
|
|
284
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
285
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
134
286
|
},
|
|
135
287
|
});
|
|
136
288
|
}));
|
|
@@ -145,17 +297,18 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
145
297
|
.option("--channels <ids>", "Comma-separated channel IDs")
|
|
146
298
|
.option("--queues <slots>", "Comma-separated queue slots (e.g. Mon 10:10,Mon 12:12)")
|
|
147
299
|
.action(withCommandErrorHandling(async (options) => {
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
300
|
+
const channels = splitCsv(options.channels).map((id) => parsePositiveInteger(id, "channel-id"));
|
|
301
|
+
const queues = validateQueueSlots(splitCsv(options.queues));
|
|
302
|
+
|
|
303
|
+
await executeRequest(program, runtimeApiKey, {
|
|
151
304
|
method: "POST",
|
|
152
|
-
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
305
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
153
306
|
body: {
|
|
154
307
|
title: options.title,
|
|
155
308
|
description: options.description,
|
|
156
|
-
maxRepublish: options.maxRepublish
|
|
157
|
-
channels
|
|
158
|
-
queues
|
|
309
|
+
maxRepublish: parseOptionalPositiveInteger(options.maxRepublish, "max-republish"),
|
|
310
|
+
channels,
|
|
311
|
+
queues,
|
|
159
312
|
},
|
|
160
313
|
});
|
|
161
314
|
}));
|
|
@@ -164,11 +317,15 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
164
317
|
.command("automations")
|
|
165
318
|
.description("List automations for a brand")
|
|
166
319
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
320
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
321
|
+
.option("--page <number>", "Page number", "1")
|
|
167
322
|
.action(withCommandErrorHandling(async (options) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
323
|
+
await executeRequest(program, runtimeApiKey, {
|
|
324
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
325
|
+
query: {
|
|
326
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
327
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
328
|
+
},
|
|
172
329
|
});
|
|
173
330
|
}));
|
|
174
331
|
|
|
@@ -186,18 +343,16 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
186
343
|
.option("--dynamic-body <text>", "Dynamic body", "{title} {link}")
|
|
187
344
|
.option("--description <text>", "Automation description")
|
|
188
345
|
.action(withCommandErrorHandling(async (options) => {
|
|
189
|
-
|
|
190
|
-
await nuelinkRequest({
|
|
191
|
-
apiKey,
|
|
346
|
+
await executeRequest(program, runtimeApiKey, {
|
|
192
347
|
method: "POST",
|
|
193
|
-
endpoint: `${brandPath(options.brandId)}/automations`,
|
|
348
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
194
349
|
body: {
|
|
195
|
-
collectionId:
|
|
196
|
-
feedUrl: options.feedUrl,
|
|
197
|
-
importAsType: options.importAsType,
|
|
198
|
-
subType: options.subType,
|
|
350
|
+
collectionId: parsePositiveInteger(options.collectionId, "collection-id"),
|
|
351
|
+
feedUrl: validateHttpUrl(options.feedUrl, "feed-url"),
|
|
352
|
+
importAsType: normalizeEnum(options.importAsType, "import-as-type", VALID_IMPORT_AS_TYPES),
|
|
353
|
+
subType: normalizeEnum(options.subType, "sub-type", VALID_AUTOMATION_SUB_TYPES),
|
|
199
354
|
title: options.title,
|
|
200
|
-
type: options.type,
|
|
355
|
+
type: normalizeEnum(options.type, "type", VALID_AUTOMATION_TYPES),
|
|
201
356
|
dynamicTitle: options.dynamicTitle,
|
|
202
357
|
dynamicBody: options.dynamicBody,
|
|
203
358
|
description: options.description,
|
|
@@ -212,13 +367,11 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
212
367
|
.option("--per-page <number>", "Items per page", "25")
|
|
213
368
|
.option("--page <number>", "Page number", "1")
|
|
214
369
|
.action(withCommandErrorHandling(async (options) => {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
apiKey,
|
|
218
|
-
endpoint: `${brandPath(options.brandId)}/channels`,
|
|
370
|
+
await executeRequest(program, runtimeApiKey, {
|
|
371
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/channels`,
|
|
219
372
|
query: {
|
|
220
|
-
per_page: options.perPage,
|
|
221
|
-
page: options.page,
|
|
373
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
374
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
222
375
|
},
|
|
223
376
|
});
|
|
224
377
|
}));
|
|
@@ -227,11 +380,15 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
227
380
|
.command("media")
|
|
228
381
|
.description("List media for a brand")
|
|
229
382
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
383
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
384
|
+
.option("--page <number>", "Page number", "1")
|
|
230
385
|
.action(withCommandErrorHandling(async (options) => {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
386
|
+
await executeRequest(program, runtimeApiKey, {
|
|
387
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/media`,
|
|
388
|
+
query: {
|
|
389
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
390
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
391
|
+
},
|
|
235
392
|
});
|
|
236
393
|
}));
|
|
237
394
|
|
|
@@ -241,17 +398,22 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
241
398
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
242
399
|
.requiredOption("--file <path>", "Path to local file")
|
|
243
400
|
.action(withCommandErrorHandling(async (options) => {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
const
|
|
401
|
+
const brandId = parsePositiveInteger(options.brandId, "brand-id");
|
|
402
|
+
const fileStat = await stat(options.file);
|
|
403
|
+
const { mediaType, size } = validateMediaFile(options.file, fileStat);
|
|
247
404
|
const formData = new FormData();
|
|
248
|
-
|
|
405
|
+
const fileBlob = await openAsBlob(options.file, { type: mediaType });
|
|
406
|
+
formData.append("media", fileBlob, path.basename(options.file));
|
|
249
407
|
|
|
250
|
-
await
|
|
251
|
-
apiKey,
|
|
408
|
+
await executeRequest(program, runtimeApiKey, {
|
|
252
409
|
method: "POST",
|
|
253
|
-
endpoint: `${brandPath(
|
|
410
|
+
endpoint: `${brandPath(brandId)}/media`,
|
|
254
411
|
multipart: formData,
|
|
412
|
+
dryRunBody: {
|
|
413
|
+
file: path.basename(options.file),
|
|
414
|
+
size,
|
|
415
|
+
type: mediaType,
|
|
416
|
+
},
|
|
255
417
|
});
|
|
256
418
|
}));
|
|
257
419
|
|
|
@@ -260,11 +422,18 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
260
422
|
.description("List posts in a collection")
|
|
261
423
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
262
424
|
.requiredOption("--collection-id <id>", "Collection ID")
|
|
425
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
426
|
+
.option("--page <number>", "Page number", "1")
|
|
263
427
|
.action(withCommandErrorHandling(async (options) => {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
428
|
+
await executeRequest(program, runtimeApiKey, {
|
|
429
|
+
endpoint: `${collectionPath(
|
|
430
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
431
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
432
|
+
)}/posts`,
|
|
433
|
+
query: {
|
|
434
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
435
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
436
|
+
},
|
|
268
437
|
});
|
|
269
438
|
}));
|
|
270
439
|
|
|
@@ -275,14 +444,17 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
275
444
|
.requiredOption("--collection-id <id>", "Collection ID")
|
|
276
445
|
.requiredOption("--payload <file>", "Path to JSON payload file")
|
|
277
446
|
.action(withCommandErrorHandling(async (options) => {
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
447
|
+
const payloadText = options.payload === "-"
|
|
448
|
+
? await readStdinText()
|
|
449
|
+
: await readFile(options.payload, "utf8");
|
|
450
|
+
const payload = parseJsonString(payloadText, "payload");
|
|
281
451
|
|
|
282
|
-
await
|
|
283
|
-
apiKey,
|
|
452
|
+
await executeRequest(program, runtimeApiKey, {
|
|
284
453
|
method: "POST",
|
|
285
|
-
endpoint: `${collectionPath(
|
|
454
|
+
endpoint: `${collectionPath(
|
|
455
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
456
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
457
|
+
)}/posts`,
|
|
286
458
|
body: payload,
|
|
287
459
|
});
|
|
288
460
|
}));
|
|
@@ -296,7 +468,7 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
296
468
|
.option("--caption <text>", "Post caption/body")
|
|
297
469
|
.option("--alt <text>", "Alt text")
|
|
298
470
|
.option("--link <url>", "Post link")
|
|
299
|
-
.option("--publish-mode <mode>",
|
|
471
|
+
.option("--publish-mode <mode>", `Publish mode (${VALID_PUBLISH_MODES.join(", ")})`, "DRAFT")
|
|
300
472
|
.option("--publish-at <value>", "Publish datetime (YYYY-MM-DD HH:mm:ss or ISO) or null")
|
|
301
473
|
.option("--post-as-story <boolean>", "Post as story: true/false")
|
|
302
474
|
.option("--post-type <type>", "Post type override")
|
|
@@ -321,19 +493,20 @@ function registerCommands(program, runtimeApiKey) {
|
|
|
321
493
|
.option("--youtube-tags <tags>", "Comma-separated YouTube tags")
|
|
322
494
|
.option("--youtube-playlists <json>", "YouTube playlists JSON string")
|
|
323
495
|
.action(withCommandErrorHandling(async (options) => {
|
|
324
|
-
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
325
496
|
const payload = buildPostPayload(options);
|
|
326
497
|
|
|
327
|
-
await
|
|
328
|
-
apiKey,
|
|
498
|
+
await executeRequest(program, runtimeApiKey, {
|
|
329
499
|
method: "POST",
|
|
330
|
-
endpoint: `${collectionPath(
|
|
500
|
+
endpoint: `${collectionPath(
|
|
501
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
502
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
503
|
+
)}/posts`,
|
|
331
504
|
body: payload,
|
|
332
505
|
});
|
|
333
506
|
}));
|
|
334
507
|
}
|
|
335
508
|
|
|
336
509
|
export {
|
|
337
|
-
|
|
510
|
+
extractApiKeyArg,
|
|
338
511
|
registerCommands,
|
|
339
512
|
};
|
package/src/config.js
CHANGED
|
@@ -79,14 +79,15 @@ async function clearApiKey() {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
async function resolveApiKey(runtimeApiKey) {
|
|
82
|
-
if (runtimeApiKey) {
|
|
83
|
-
return runtimeApiKey;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
82
|
if (process.env.NUELINK_API_KEY) {
|
|
87
83
|
return process.env.NUELINK_API_KEY;
|
|
88
84
|
}
|
|
89
85
|
|
|
86
|
+
if (runtimeApiKey) {
|
|
87
|
+
process.env.NUELINK_API_KEY = runtimeApiKey;
|
|
88
|
+
return runtimeApiKey;
|
|
89
|
+
}
|
|
90
|
+
|
|
90
91
|
const config = await readConfig();
|
|
91
92
|
const storedApiKey = decryptApiKey(config);
|
|
92
93
|
|
|
@@ -94,6 +95,10 @@ async function resolveApiKey(runtimeApiKey) {
|
|
|
94
95
|
await saveApiKey(storedApiKey);
|
|
95
96
|
}
|
|
96
97
|
|
|
98
|
+
if (storedApiKey) {
|
|
99
|
+
process.env.NUELINK_API_KEY = storedApiKey;
|
|
100
|
+
}
|
|
101
|
+
|
|
97
102
|
return storedApiKey;
|
|
98
103
|
}
|
|
99
104
|
|
package/src/http.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import chalk from "chalk";
|
|
3
|
+
import { printApiResponse } from "./output.js";
|
|
2
4
|
|
|
3
|
-
const
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const { version: cliVersion } = require("../package.json");
|
|
7
|
+
|
|
8
|
+
const API_BASE_URL = process.env.NUELINK_BASE_URL || "https://nuelink.com/api/public/v1";
|
|
4
9
|
const REQUEST_TIMEOUT_MS = Number(process.env.NUELINK_TIMEOUT_MS || 30000);
|
|
10
|
+
const MAX_GET_RETRIES = Number(process.env.NUELINK_GET_RETRIES || 2);
|
|
11
|
+
const RETRY_BASE_DELAY_MS = Number(process.env.NUELINK_RETRY_BASE_MS || 500);
|
|
12
|
+
const RETRY_MAX_DELAY_MS = Number(process.env.NUELINK_RETRY_MAX_MS || 2000);
|
|
5
13
|
|
|
6
14
|
function pathParam(value, label) {
|
|
7
15
|
const stringValue = String(value ?? "").trim();
|
|
@@ -21,14 +29,54 @@ function collectionPath(brandId, collectionId) {
|
|
|
21
29
|
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
22
30
|
}
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
function buildApiUrl(endpoint) {
|
|
33
|
+
return new URL(endpoint.replace(/^\//, ""), `${API_BASE_URL.replace(/\/?$/, "/")}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function shouldRetryRequest(method, statusCode, attempt) {
|
|
37
|
+
return method === "GET" && attempt < MAX_GET_RETRIES && (statusCode === 429 || statusCode >= 500);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseRetryAfterMs(headers, attempt) {
|
|
41
|
+
const retryAfter = headers.get("retry-after");
|
|
42
|
+
|
|
43
|
+
if (retryAfter) {
|
|
44
|
+
const seconds = Number(retryAfter);
|
|
45
|
+
|
|
46
|
+
if (!Number.isNaN(seconds)) {
|
|
47
|
+
return Math.min(Math.max(seconds * 1000, 0), RETRY_MAX_DELAY_MS);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const dateDelay = Date.parse(retryAfter) - Date.now();
|
|
51
|
+
if (!Number.isNaN(dateDelay)) {
|
|
52
|
+
return Math.min(Math.max(dateDelay, 0), RETRY_MAX_DELAY_MS);
|
|
53
|
+
}
|
|
29
54
|
}
|
|
30
55
|
|
|
31
|
-
|
|
56
|
+
return Math.min(RETRY_BASE_DELAY_MS * (attempt + 1), RETRY_MAX_DELAY_MS);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function sleep(ms) {
|
|
60
|
+
if (ms <= 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function printDryRun({ method, url, query, body, multipart, dryRunBody }) {
|
|
68
|
+
console.log(JSON.stringify({
|
|
69
|
+
dryRun: true,
|
|
70
|
+
method,
|
|
71
|
+
url: url.toString(),
|
|
72
|
+
...(query ? { query } : {}),
|
|
73
|
+
...(body !== undefined ? { body } : {}),
|
|
74
|
+
...(multipart ? { multipart: dryRunBody || true } : {}),
|
|
75
|
+
}, null, 2));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, multipart, outputMode = "json", quiet = false, dryRun = false, dryRunBody }) {
|
|
79
|
+
const url = buildApiUrl(endpoint);
|
|
32
80
|
|
|
33
81
|
if (query) {
|
|
34
82
|
Object.entries(query).forEach(([key, value]) => {
|
|
@@ -38,10 +86,21 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
38
86
|
});
|
|
39
87
|
}
|
|
40
88
|
|
|
89
|
+
if (dryRun) {
|
|
90
|
+
printDryRun({ method, url, query, body, multipart, dryRunBody });
|
|
91
|
+
return { dryRun: true };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!apiKey) {
|
|
95
|
+
console.error(chalk.red("Missing API key. Use --api-key <api_key> for a single command or auth:login to save one."));
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
41
100
|
const headers = {
|
|
42
101
|
Authorization: `Bearer ${apiKey}`,
|
|
43
102
|
Accept: "application/json",
|
|
44
|
-
"User-Agent":
|
|
103
|
+
"User-Agent": `nuelink-cli/${cliVersion}`,
|
|
45
104
|
};
|
|
46
105
|
|
|
47
106
|
const init = {
|
|
@@ -57,10 +116,21 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
57
116
|
}
|
|
58
117
|
|
|
59
118
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
119
|
+
let response;
|
|
120
|
+
|
|
121
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
122
|
+
response = await fetch(url, {
|
|
123
|
+
...init,
|
|
124
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (!shouldRetryRequest(method, response.status, attempt)) {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
await sleep(parseRetryAfterMs(response.headers, attempt));
|
|
132
|
+
}
|
|
133
|
+
|
|
64
134
|
const text = await response.text();
|
|
65
135
|
let data = {};
|
|
66
136
|
|
|
@@ -79,8 +149,11 @@ async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, m
|
|
|
79
149
|
return null;
|
|
80
150
|
}
|
|
81
151
|
|
|
82
|
-
|
|
83
|
-
|
|
152
|
+
if (outputMode !== "json" && !quiet) {
|
|
153
|
+
console.log(chalk.green(`OK ${method} ${url.pathname}`));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
printApiResponse(data, { outputMode });
|
|
84
157
|
return data;
|
|
85
158
|
} catch (error) {
|
|
86
159
|
if (error?.name === "TimeoutError") {
|