@clickminded/coauthor-cli 0.0.33 → 0.0.34
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/dist/client.d.ts +6 -1
- package/dist/client.js +13 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +7 -4
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -36,12 +36,17 @@ export declare class AgentClient {
|
|
|
36
36
|
private readonly token;
|
|
37
37
|
private readonly fetchImpl;
|
|
38
38
|
private readonly options;
|
|
39
|
-
|
|
39
|
+
private readonly sent;
|
|
40
|
+
constructor(options: AgentClientOptions, sent?: {
|
|
41
|
+
count: number;
|
|
42
|
+
});
|
|
40
43
|
/** Same transport and timeouts, different credentials (used to verify a token before saving it). */
|
|
41
44
|
withCredentials(params: {
|
|
42
45
|
baseUrl?: string;
|
|
43
46
|
token: string;
|
|
44
47
|
}): AgentClient;
|
|
48
|
+
/** Requests and downloads started so far, across derived clients. */
|
|
49
|
+
get requestsSent(): number;
|
|
45
50
|
request(options: RequestOptions): Promise<JsonValue>;
|
|
46
51
|
/**
|
|
47
52
|
* Fetches a public file (a rendered MP4 or slide PNG on the CDN) as bytes.
|
package/dist/client.js
CHANGED
|
@@ -47,17 +47,27 @@ export class AgentClient {
|
|
|
47
47
|
token;
|
|
48
48
|
fetchImpl;
|
|
49
49
|
options;
|
|
50
|
-
|
|
50
|
+
// Shared with clients derived by withCredentials, so the CLI can tell a
|
|
51
|
+
// command rejected before anything was sent (a usage error) from one that
|
|
52
|
+
// failed while talking to CoAuthor.
|
|
53
|
+
sent;
|
|
54
|
+
constructor(options, sent = { count: 0 }) {
|
|
51
55
|
this.options = options;
|
|
56
|
+
this.sent = sent;
|
|
52
57
|
this.baseUrl = new URL('api/agent/v1/', normalizeBaseUrl(options.baseUrl));
|
|
53
58
|
this.token = options.token ?? '';
|
|
54
59
|
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
55
60
|
}
|
|
56
61
|
/** Same transport and timeouts, different credentials (used to verify a token before saving it). */
|
|
57
62
|
withCredentials(params) {
|
|
58
|
-
return new AgentClient({ ...this.options, baseUrl: params.baseUrl ?? this.options.baseUrl, token: params.token });
|
|
63
|
+
return new AgentClient({ ...this.options, baseUrl: params.baseUrl ?? this.options.baseUrl, token: params.token }, this.sent);
|
|
64
|
+
}
|
|
65
|
+
/** Requests and downloads started so far, across derived clients. */
|
|
66
|
+
get requestsSent() {
|
|
67
|
+
return this.sent.count;
|
|
59
68
|
}
|
|
60
69
|
async request(options) {
|
|
70
|
+
this.sent.count += 1;
|
|
61
71
|
const method = options.method ?? 'GET';
|
|
62
72
|
const url = new URL(trimLeadingSlash(options.path), this.baseUrl);
|
|
63
73
|
const headers = new Headers({
|
|
@@ -104,6 +114,7 @@ export class AgentClient {
|
|
|
104
114
|
const target = new URL(url);
|
|
105
115
|
if (target.protocol !== 'https:' && target.protocol !== 'http:')
|
|
106
116
|
throw new Error(`Refusing to download a non-http URL: ${url}`);
|
|
117
|
+
this.sent.count += 1;
|
|
107
118
|
const timeoutMs = this.options.timeoutMs ?? requestTimeoutOverrideMs() ?? DEFAULT_DOWNLOAD_TIMEOUT_MS;
|
|
108
119
|
return withTimeout(timeoutMs, `download ${url}`, undefined, async (signal) => {
|
|
109
120
|
const response = await this.fetchImpl(target, { method: 'GET', signal });
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export type CliIo = {
|
|
|
5
5
|
stderr: (text: string) => void;
|
|
6
6
|
};
|
|
7
7
|
export declare function main(argv?: string[], io?: CliIo): Promise<number>;
|
|
8
|
-
export declare function formatCliError(error: unknown, baseUrl: string, argv?: string[]
|
|
8
|
+
export declare function formatCliError(error: unknown, baseUrl: string, argv?: string[], context?: {
|
|
9
|
+
requestsSent?: number;
|
|
10
|
+
}): {
|
|
9
11
|
error: Error;
|
|
10
12
|
result?: JsonValue;
|
|
11
13
|
exitCode: number;
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ const processIo = {
|
|
|
12
12
|
};
|
|
13
13
|
export async function main(argv = process.argv.slice(2), io = processIo) {
|
|
14
14
|
let resolvedBaseUrl = resolveBaseUrlFromArgv(argv);
|
|
15
|
+
let client;
|
|
15
16
|
try {
|
|
16
17
|
const parsed = parseCliArgs(argv);
|
|
17
18
|
if (parsed.globals.version || (parsed.command.length === 1 && parsed.command[0] === 'version')) {
|
|
@@ -27,7 +28,7 @@ export async function main(argv = process.argv.slice(2), io = processIo) {
|
|
|
27
28
|
tokenOverride: parsed.globals.token,
|
|
28
29
|
});
|
|
29
30
|
resolvedBaseUrl = runtimeConfig.baseUrl;
|
|
30
|
-
|
|
31
|
+
client = new AgentClient(runtimeConfig);
|
|
31
32
|
const result = await runCommand(parsed, {
|
|
32
33
|
client,
|
|
33
34
|
baseUrl: runtimeConfig.baseUrl,
|
|
@@ -44,14 +45,14 @@ export async function main(argv = process.argv.slice(2), io = processIo) {
|
|
|
44
45
|
}
|
|
45
46
|
catch (error) {
|
|
46
47
|
const wantsJson = argv.includes('--json');
|
|
47
|
-
const formatted = formatCliError(error, resolvedBaseUrl, argv);
|
|
48
|
+
const formatted = formatCliError(error, resolvedBaseUrl, argv, { requestsSent: client?.requestsSent ?? 0 });
|
|
48
49
|
io.stderr(wantsJson
|
|
49
50
|
? renderError(formatted.result ?? formatted.error, true)
|
|
50
51
|
: renderError(formatted.error, false));
|
|
51
52
|
return formatted.exitCode;
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
|
-
export function formatCliError(error, baseUrl, argv = []) {
|
|
55
|
+
export function formatCliError(error, baseUrl, argv = [], context = {}) {
|
|
55
56
|
if (error instanceof AgentHttpError) {
|
|
56
57
|
// Keep the server's error code and details (e.g. the current draft version
|
|
57
58
|
// on a 409) on every --json error so agents can recover without guessing.
|
|
@@ -110,7 +111,9 @@ export function formatCliError(error, baseUrl, argv = []) {
|
|
|
110
111
|
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
111
112
|
return {
|
|
112
113
|
error: normalized,
|
|
113
|
-
|
|
114
|
+
// Anything rejected before the first request is the command's own fault
|
|
115
|
+
// (bad flag, missing argument, invalid value): a usage error.
|
|
116
|
+
result: { ok: false, error: normalized.message, code: context.requestsSent === 0 || isUsageMessage(normalized.message) ? 'usage_error' : 'cli_error', http_status: null },
|
|
114
117
|
exitCode: 1,
|
|
115
118
|
};
|
|
116
119
|
}
|