@nuelink/nuelink-cli 1.1.0 → 1.2.0
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 +4 -4
- package/src/cli.js +30 -0
- package/src/commands.js +339 -0
- package/src/config.js +116 -0
- package/src/http.js +103 -0
- package/src/postPayload.js +144 -0
- package/src/main.js +0 -734
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuelink/nuelink-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "The official Nuelink CLI for automating social media workflows from your terminal. Schedule posts, upload media, manage brands, and integrate Nuelink into scripts, CI/CD pipelines, and AI agents.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Nuelink",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"main": "./src/
|
|
8
|
+
"main": "./src/cli.js",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "node --test test/cli.test.mjs",
|
|
11
|
-
"smoke:auth": "node src/
|
|
11
|
+
"smoke:auth": "node src/cli.js auth:status",
|
|
12
12
|
"check:pack": "npm pack --dry-run"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"commander": "^15.0.0"
|
|
29
29
|
},
|
|
30
30
|
"bin": {
|
|
31
|
-
"nuelink-cli": "./src/
|
|
31
|
+
"nuelink-cli": "./src/cli.js"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from "commander";
|
|
4
|
+
import { saveApiKey } from "./config.js";
|
|
5
|
+
import { extractAuthArg, registerCommands } from "./commands.js";
|
|
6
|
+
|
|
7
|
+
const parsedAuth = extractAuthArg(process.argv.slice(2));
|
|
8
|
+
const runtimeApiKey = parsedAuth.apiKey;
|
|
9
|
+
const parseArgv = [process.argv[0], process.argv[1], ...parsedAuth.sanitizedArgs];
|
|
10
|
+
|
|
11
|
+
if (runtimeApiKey) {
|
|
12
|
+
await saveApiKey(runtimeApiKey);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
registerCommands(program, runtimeApiKey);
|
|
16
|
+
|
|
17
|
+
const argvHasCommand = parsedAuth.sanitizedArgs.some((arg) => !arg.startsWith("-"));
|
|
18
|
+
const hasHelpFlag = parsedAuth.sanitizedArgs.includes("--help") || parsedAuth.sanitizedArgs.includes("-h");
|
|
19
|
+
|
|
20
|
+
if (!argvHasCommand && runtimeApiKey && !hasHelpFlag) {
|
|
21
|
+
console.log("API key saved and ready to use.");
|
|
22
|
+
process.exit(0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!argvHasCommand && !runtimeApiKey && !hasHelpFlag) {
|
|
26
|
+
program.outputHelp();
|
|
27
|
+
process.exit(0);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await program.parseAsync(parseArgv);
|
package/src/commands.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
import { clearApiKey, maskToken, readConfig, resolveApiKey, CONFIG_FILE, decryptApiKey } from "./config.js";
|
|
6
|
+
import { brandPath, collectionPath, nuelinkRequest } from "./http.js";
|
|
7
|
+
import { buildPostPayload, parseCsv } from "./postPayload.js";
|
|
8
|
+
|
|
9
|
+
function extractAuthArg(args) {
|
|
10
|
+
const sanitizedArgs = [];
|
|
11
|
+
let apiKey;
|
|
12
|
+
|
|
13
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
14
|
+
const arg = args[index];
|
|
15
|
+
|
|
16
|
+
if (arg === "--auth" || arg === "-a") {
|
|
17
|
+
const next = args[index + 1];
|
|
18
|
+
if (next && !next.startsWith("-")) {
|
|
19
|
+
apiKey = next;
|
|
20
|
+
index += 1;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (arg.startsWith("--auth=")) {
|
|
26
|
+
apiKey = arg.slice("--auth=".length);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
sanitizedArgs.push(arg);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { apiKey, sanitizedArgs };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function withCommandErrorHandling(handler) {
|
|
37
|
+
return async (...args) => {
|
|
38
|
+
try {
|
|
39
|
+
await handler(...args);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error(chalk.red(error.message));
|
|
42
|
+
process.exitCode = 1;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function registerCommands(program, runtimeApiKey) {
|
|
48
|
+
program
|
|
49
|
+
.name("nuelink-cli")
|
|
50
|
+
.version("1.0.0")
|
|
51
|
+
.description("Nuelink CLI for the Public API")
|
|
52
|
+
.option("-a, --auth <apiKey>", "Set and use API key");
|
|
53
|
+
|
|
54
|
+
program
|
|
55
|
+
.command("auth:clear")
|
|
56
|
+
.description("Remove the saved API key")
|
|
57
|
+
.action(withCommandErrorHandling(async () => {
|
|
58
|
+
await clearApiKey();
|
|
59
|
+
console.log(chalk.yellow("Saved API key removed."));
|
|
60
|
+
}));
|
|
61
|
+
|
|
62
|
+
program
|
|
63
|
+
.command("auth:status")
|
|
64
|
+
.description("Show where auth is currently resolved from")
|
|
65
|
+
.action(withCommandErrorHandling(async () => {
|
|
66
|
+
if (runtimeApiKey) {
|
|
67
|
+
console.log(chalk.green(`Auth source: CLI flag (${maskToken(runtimeApiKey)})`));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (process.env.NUELINK_API_KEY) {
|
|
72
|
+
console.log(chalk.green(`Auth source: NUELINK_API_KEY (${maskToken(process.env.NUELINK_API_KEY)})`));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const config = await readConfig();
|
|
77
|
+
const storedApiKey = decryptApiKey(config);
|
|
78
|
+
if (storedApiKey) {
|
|
79
|
+
console.log(chalk.green(`Auth source: saved config (${maskToken(storedApiKey)})`));
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const fileStat = await stat(CONFIG_FILE);
|
|
83
|
+
console.log(chalk.blue(`Config path: ${CONFIG_FILE}`));
|
|
84
|
+
console.log(chalk.blue(`Last updated: ${fileStat.mtime.toISOString()}`));
|
|
85
|
+
} catch {
|
|
86
|
+
// Ignore non-critical stat failure.
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log(chalk.yellow("No API key configured. Set one with --auth <api_key>."));
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
program
|
|
96
|
+
.command("me")
|
|
97
|
+
.description("Get current profile")
|
|
98
|
+
.action(withCommandErrorHandling(async () => {
|
|
99
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
100
|
+
await nuelinkRequest({ apiKey, endpoint: "/me" });
|
|
101
|
+
}));
|
|
102
|
+
|
|
103
|
+
program
|
|
104
|
+
.command("brands")
|
|
105
|
+
.description("List brands")
|
|
106
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
107
|
+
.option("--page <number>", "Page number", "1")
|
|
108
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
109
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
110
|
+
await nuelinkRequest({
|
|
111
|
+
apiKey,
|
|
112
|
+
endpoint: "/brands",
|
|
113
|
+
query: {
|
|
114
|
+
per_page: options.perPage,
|
|
115
|
+
page: options.page,
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}));
|
|
119
|
+
|
|
120
|
+
program
|
|
121
|
+
.command("collections")
|
|
122
|
+
.description("List collections for a brand")
|
|
123
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
124
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
125
|
+
.option("--page <number>", "Page number", "1")
|
|
126
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
127
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
128
|
+
await nuelinkRequest({
|
|
129
|
+
apiKey,
|
|
130
|
+
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
131
|
+
query: {
|
|
132
|
+
per_page: options.perPage,
|
|
133
|
+
page: options.page,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}));
|
|
137
|
+
|
|
138
|
+
program
|
|
139
|
+
.command("collections:create")
|
|
140
|
+
.description("Create a collection")
|
|
141
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
142
|
+
.requiredOption("--title <title>", "Collection title")
|
|
143
|
+
.option("--description <text>", "Collection description", "")
|
|
144
|
+
.option("--max-republish <number>", "Max republish")
|
|
145
|
+
.option("--channels <ids>", "Comma-separated channel IDs")
|
|
146
|
+
.option("--queues <slots>", "Comma-separated queue slots (e.g. Mon 10:10,Mon 12:12)")
|
|
147
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
148
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
149
|
+
await nuelinkRequest({
|
|
150
|
+
apiKey,
|
|
151
|
+
method: "POST",
|
|
152
|
+
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
153
|
+
body: {
|
|
154
|
+
title: options.title,
|
|
155
|
+
description: options.description,
|
|
156
|
+
maxRepublish: options.maxRepublish ? Number(options.maxRepublish) : undefined,
|
|
157
|
+
channels: parseCsv(options.channels).map((id) => Number(id)),
|
|
158
|
+
queues: parseCsv(options.queues),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}));
|
|
162
|
+
|
|
163
|
+
program
|
|
164
|
+
.command("automations")
|
|
165
|
+
.description("List automations for a brand")
|
|
166
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
167
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
168
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
169
|
+
await nuelinkRequest({
|
|
170
|
+
apiKey,
|
|
171
|
+
endpoint: `${brandPath(options.brandId)}/automations`,
|
|
172
|
+
});
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
program
|
|
176
|
+
.command("automations:create")
|
|
177
|
+
.description("Create an automation")
|
|
178
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
179
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
180
|
+
.requiredOption("--feed-url <url>", "Feed URL")
|
|
181
|
+
.requiredOption("--import-as-type <type>", "Import type, e.g. IMAGE")
|
|
182
|
+
.requiredOption("--sub-type <type>", "Sub type, e.g. RSS")
|
|
183
|
+
.requiredOption("--title <title>", "Automation title")
|
|
184
|
+
.option("--type <type>", "Automation type", "FEED")
|
|
185
|
+
.option("--dynamic-title <text>", "Dynamic title", "{{title}}")
|
|
186
|
+
.option("--dynamic-body <text>", "Dynamic body", "{title} {link}")
|
|
187
|
+
.option("--description <text>", "Automation description")
|
|
188
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
189
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
190
|
+
await nuelinkRequest({
|
|
191
|
+
apiKey,
|
|
192
|
+
method: "POST",
|
|
193
|
+
endpoint: `${brandPath(options.brandId)}/automations`,
|
|
194
|
+
body: {
|
|
195
|
+
collectionId: Number(options.collectionId),
|
|
196
|
+
feedUrl: options.feedUrl,
|
|
197
|
+
importAsType: options.importAsType,
|
|
198
|
+
subType: options.subType,
|
|
199
|
+
title: options.title,
|
|
200
|
+
type: options.type,
|
|
201
|
+
dynamicTitle: options.dynamicTitle,
|
|
202
|
+
dynamicBody: options.dynamicBody,
|
|
203
|
+
description: options.description,
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
}));
|
|
207
|
+
|
|
208
|
+
program
|
|
209
|
+
.command("channels")
|
|
210
|
+
.description("List channels for a brand")
|
|
211
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
212
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
213
|
+
.option("--page <number>", "Page number", "1")
|
|
214
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
215
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
216
|
+
await nuelinkRequest({
|
|
217
|
+
apiKey,
|
|
218
|
+
endpoint: `${brandPath(options.brandId)}/channels`,
|
|
219
|
+
query: {
|
|
220
|
+
per_page: options.perPage,
|
|
221
|
+
page: options.page,
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
}));
|
|
225
|
+
|
|
226
|
+
program
|
|
227
|
+
.command("media")
|
|
228
|
+
.description("List media for a brand")
|
|
229
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
230
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
231
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
232
|
+
await nuelinkRequest({
|
|
233
|
+
apiKey,
|
|
234
|
+
endpoint: `${brandPath(options.brandId)}/media`,
|
|
235
|
+
});
|
|
236
|
+
}));
|
|
237
|
+
|
|
238
|
+
program
|
|
239
|
+
.command("media:upload")
|
|
240
|
+
.description("Upload a media file")
|
|
241
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
242
|
+
.requiredOption("--file <path>", "Path to local file")
|
|
243
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
244
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
245
|
+
|
|
246
|
+
const fileBuffer = await readFile(options.file);
|
|
247
|
+
const formData = new FormData();
|
|
248
|
+
formData.append("media", new Blob([fileBuffer]), path.basename(options.file));
|
|
249
|
+
|
|
250
|
+
await nuelinkRequest({
|
|
251
|
+
apiKey,
|
|
252
|
+
method: "POST",
|
|
253
|
+
endpoint: `${brandPath(options.brandId)}/media`,
|
|
254
|
+
multipart: formData,
|
|
255
|
+
});
|
|
256
|
+
}));
|
|
257
|
+
|
|
258
|
+
program
|
|
259
|
+
.command("posts")
|
|
260
|
+
.description("List posts in a collection")
|
|
261
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
262
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
263
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
264
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
265
|
+
await nuelinkRequest({
|
|
266
|
+
apiKey,
|
|
267
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
268
|
+
});
|
|
269
|
+
}));
|
|
270
|
+
|
|
271
|
+
program
|
|
272
|
+
.command("posts:add-json")
|
|
273
|
+
.description("Create a post using a JSON payload file")
|
|
274
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
275
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
276
|
+
.requiredOption("--payload <file>", "Path to JSON payload file")
|
|
277
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
278
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
279
|
+
const payloadText = await readFile(options.payload, "utf8");
|
|
280
|
+
const payload = JSON.parse(payloadText);
|
|
281
|
+
|
|
282
|
+
await nuelinkRequest({
|
|
283
|
+
apiKey,
|
|
284
|
+
method: "POST",
|
|
285
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
286
|
+
body: payload,
|
|
287
|
+
});
|
|
288
|
+
}));
|
|
289
|
+
|
|
290
|
+
program
|
|
291
|
+
.command("posts:create")
|
|
292
|
+
.description("Create a post using CLI options")
|
|
293
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
294
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
295
|
+
.option("--title <text>", "Post title")
|
|
296
|
+
.option("--caption <text>", "Post caption/body")
|
|
297
|
+
.option("--alt <text>", "Alt text")
|
|
298
|
+
.option("--link <url>", "Post link")
|
|
299
|
+
.option("--publish-mode <mode>", "Publish mode (e.g. DRAFT, NOW, SCHEDULE)", "DRAFT")
|
|
300
|
+
.option("--publish-at <value>", "Publish datetime (YYYY-MM-DD HH:mm:ss or ISO) or null")
|
|
301
|
+
.option("--post-as-story <boolean>", "Post as story: true/false")
|
|
302
|
+
.option("--post-type <type>", "Post type override")
|
|
303
|
+
.option("--comment <text>", "First comment")
|
|
304
|
+
.option("--comment-delay <seconds>", "First comment delay seconds")
|
|
305
|
+
.option("--media-ids <ids>", "Comma-separated media IDs")
|
|
306
|
+
.option("--media-urls <urls>", "Comma-separated media URLs")
|
|
307
|
+
.option("--poll-question <text>", "Poll question")
|
|
308
|
+
.option("--poll-options <items>", "Comma-separated poll options")
|
|
309
|
+
.option("--poll-period <value>", "Poll period (e.g. 3d)")
|
|
310
|
+
.option("--poll-correct-option-index <number>", "Poll correct option index")
|
|
311
|
+
.option("--poll-explanation <text>", "Poll explanation")
|
|
312
|
+
.option("--instagram-collab <username>", "Instagram collab username")
|
|
313
|
+
.option("--instagram-location-id <id>", "Instagram location ID")
|
|
314
|
+
.option("--instagram-location-name <name>", "Instagram location name")
|
|
315
|
+
.option("--instagram-trial-reel <mode>", "Instagram trial reel mode")
|
|
316
|
+
.option("--instagram-share-to-feed <boolean>", "Instagram share to feed true/false")
|
|
317
|
+
.option("--facebook-collab <username>", "Facebook collab username")
|
|
318
|
+
.option("--facebook-location-id <id>", "Facebook location ID")
|
|
319
|
+
.option("--facebook-location-name <name>", "Facebook location name")
|
|
320
|
+
.option("--tiktok-send-to-inbox <boolean>", "TikTok send to inbox true/false")
|
|
321
|
+
.option("--youtube-tags <tags>", "Comma-separated YouTube tags")
|
|
322
|
+
.option("--youtube-playlists <json>", "YouTube playlists JSON string")
|
|
323
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
324
|
+
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
325
|
+
const payload = buildPostPayload(options);
|
|
326
|
+
|
|
327
|
+
await nuelinkRequest({
|
|
328
|
+
apiKey,
|
|
329
|
+
method: "POST",
|
|
330
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
331
|
+
body: payload,
|
|
332
|
+
});
|
|
333
|
+
}));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export {
|
|
337
|
+
extractAuthArg,
|
|
338
|
+
registerCommands,
|
|
339
|
+
};
|
package/src/config.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
|
|
5
|
+
|
|
6
|
+
const CONFIG_DIR = path.join(os.homedir(), ".nuelink-cli");
|
|
7
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
|
|
8
|
+
const CONFIG_ENCRYPTION_SALT = "nuelink-cli-config-salt-v1";
|
|
9
|
+
const CONFIG_ENCRYPTION_PASSPHRASE = "nuelink-cli::config::v1::auth";
|
|
10
|
+
|
|
11
|
+
async function readConfig() {
|
|
12
|
+
try {
|
|
13
|
+
const content = await readFile(CONFIG_FILE, "utf8");
|
|
14
|
+
return JSON.parse(content);
|
|
15
|
+
} catch {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getConfigCipherKey() {
|
|
21
|
+
return crypto.scryptSync(CONFIG_ENCRYPTION_PASSPHRASE, CONFIG_ENCRYPTION_SALT, 32);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function encryptApiKey(apiKey) {
|
|
25
|
+
const iv = crypto.randomBytes(12);
|
|
26
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", getConfigCipherKey(), iv);
|
|
27
|
+
const encrypted = Buffer.concat([cipher.update(apiKey, "utf8"), cipher.final()]);
|
|
28
|
+
const authTag = cipher.getAuthTag();
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
version: 1,
|
|
32
|
+
encryptedApiKey: encrypted.toString("base64"),
|
|
33
|
+
iv: iv.toString("base64"),
|
|
34
|
+
authTag: authTag.toString("base64"),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function decryptApiKey(config) {
|
|
39
|
+
if (config?.encryptedApiKey && config?.iv && config?.authTag) {
|
|
40
|
+
const decipher = crypto.createDecipheriv(
|
|
41
|
+
"aes-256-gcm",
|
|
42
|
+
getConfigCipherKey(),
|
|
43
|
+
Buffer.from(config.iv, "base64"),
|
|
44
|
+
);
|
|
45
|
+
decipher.setAuthTag(Buffer.from(config.authTag, "base64"));
|
|
46
|
+
|
|
47
|
+
const decrypted = Buffer.concat([
|
|
48
|
+
decipher.update(Buffer.from(config.encryptedApiKey, "base64")),
|
|
49
|
+
decipher.final(),
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
return decrypted.toString("utf8");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (config?.apiKey) {
|
|
56
|
+
return config.apiKey;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function writeConfig(config) {
|
|
63
|
+
await mkdir(CONFIG_DIR, { recursive: true });
|
|
64
|
+
await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), "utf8");
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
await chmod(CONFIG_FILE, 0o600);
|
|
68
|
+
} catch {
|
|
69
|
+
// Ignore on platforms where chmod permissions are not meaningful.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function saveApiKey(apiKey) {
|
|
74
|
+
await writeConfig(encryptApiKey(apiKey));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function clearApiKey() {
|
|
78
|
+
await writeConfig({});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function resolveApiKey(runtimeApiKey) {
|
|
82
|
+
if (runtimeApiKey) {
|
|
83
|
+
return runtimeApiKey;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (process.env.NUELINK_API_KEY) {
|
|
87
|
+
return process.env.NUELINK_API_KEY;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const config = await readConfig();
|
|
91
|
+
const storedApiKey = decryptApiKey(config);
|
|
92
|
+
|
|
93
|
+
if (storedApiKey && config.apiKey) {
|
|
94
|
+
await saveApiKey(storedApiKey);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return storedApiKey;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function maskToken(token) {
|
|
101
|
+
if (!token || token.length < 8) {
|
|
102
|
+
return "(set)";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return `${token.slice(0, 4)}...${token.slice(-4)}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export {
|
|
109
|
+
CONFIG_FILE,
|
|
110
|
+
clearApiKey,
|
|
111
|
+
decryptApiKey,
|
|
112
|
+
maskToken,
|
|
113
|
+
readConfig,
|
|
114
|
+
resolveApiKey,
|
|
115
|
+
saveApiKey,
|
|
116
|
+
};
|
package/src/http.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
|
|
3
|
+
const API_BASE_URL = "https://nuelink.com/api/public/v1";
|
|
4
|
+
const REQUEST_TIMEOUT_MS = Number(process.env.NUELINK_TIMEOUT_MS || 30000);
|
|
5
|
+
|
|
6
|
+
function pathParam(value, label) {
|
|
7
|
+
const stringValue = String(value ?? "").trim();
|
|
8
|
+
|
|
9
|
+
if (!stringValue) {
|
|
10
|
+
throw new Error(`Missing required ${label}.`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return encodeURIComponent(stringValue);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function brandPath(brandId) {
|
|
17
|
+
return `/brands/${pathParam(brandId, "brand-id")}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function collectionPath(brandId, collectionId) {
|
|
21
|
+
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function nuelinkRequest({ apiKey, method = "GET", endpoint, query, body, multipart }) {
|
|
25
|
+
if (!apiKey) {
|
|
26
|
+
console.error(chalk.red("Missing API key. Use --auth <api_key> to set it."));
|
|
27
|
+
process.exitCode = 1;
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const url = new URL(`${API_BASE_URL}${endpoint}`);
|
|
32
|
+
|
|
33
|
+
if (query) {
|
|
34
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
35
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
36
|
+
url.searchParams.set(key, String(value));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const headers = {
|
|
42
|
+
Authorization: `Bearer ${apiKey}`,
|
|
43
|
+
Accept: "application/json",
|
|
44
|
+
"User-Agent": "nuelink-cli/1.0.0",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const init = {
|
|
48
|
+
method,
|
|
49
|
+
headers,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
if (multipart) {
|
|
53
|
+
init.body = multipart;
|
|
54
|
+
} else if (body !== undefined) {
|
|
55
|
+
headers["Content-Type"] = "application/json";
|
|
56
|
+
init.body = JSON.stringify(body);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const response = await fetch(url, {
|
|
61
|
+
...init,
|
|
62
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
63
|
+
});
|
|
64
|
+
const text = await response.text();
|
|
65
|
+
let data = {};
|
|
66
|
+
|
|
67
|
+
if (text) {
|
|
68
|
+
try {
|
|
69
|
+
data = JSON.parse(text);
|
|
70
|
+
} catch {
|
|
71
|
+
data = { raw: text };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
console.error(chalk.red(`Request failed (${response.status} ${response.statusText})`));
|
|
77
|
+
console.error(chalk.red(JSON.stringify(data, null, 2)));
|
|
78
|
+
process.exitCode = 1;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
console.log(chalk.green(`OK ${method} ${url.pathname}`));
|
|
83
|
+
console.log(JSON.stringify(data, null, 2));
|
|
84
|
+
return data;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error?.name === "TimeoutError") {
|
|
87
|
+
console.error(chalk.red(`Request timed out after ${REQUEST_TIMEOUT_MS}ms.`));
|
|
88
|
+
process.exitCode = 1;
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.error(chalk.red("Request error:"), error.message);
|
|
93
|
+
process.exitCode = 1;
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export {
|
|
99
|
+
brandPath,
|
|
100
|
+
collectionPath,
|
|
101
|
+
nuelinkRequest,
|
|
102
|
+
pathParam,
|
|
103
|
+
};
|